Draft: feat login
This commit is contained in:
13
plays/base.go
Normal file
13
plays/base.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package plays
|
||||
|
||||
import "github.com/go-resty/resty/v2"
|
||||
|
||||
type Base struct {
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewBase(client *resty.Client) *Base {
|
||||
return &Base{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
93
plays/landing.go
Normal file
93
plays/landing.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
var (
|
||||
landingChoices = []string{"Login", "Register"}
|
||||
)
|
||||
|
||||
type Landing struct {
|
||||
*Base
|
||||
|
||||
choice string
|
||||
cursor int
|
||||
}
|
||||
|
||||
func NewLanding(base *Base) *Landing {
|
||||
m := Landing{
|
||||
Base: base,
|
||||
choice: "",
|
||||
cursor: 0,
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Landing) Init() tea.Cmd {
|
||||
return tea.ClearScreen
|
||||
}
|
||||
|
||||
func (m *Landing) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+l":
|
||||
return m, tea.ClearScreen
|
||||
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
|
||||
case "enter":
|
||||
m.choice = landingChoices[m.cursor]
|
||||
return m, tea.Quit
|
||||
|
||||
case "tab", "shift+tab", "up", "down":
|
||||
s := msg.String()
|
||||
|
||||
if s == "up" || s == "shift+tab" {
|
||||
m.cursor--
|
||||
} else {
|
||||
m.cursor++
|
||||
}
|
||||
|
||||
if m.cursor >= len(landingChoices) {
|
||||
m.cursor = 0
|
||||
} else if m.cursor < 0 {
|
||||
m.cursor = len(landingChoices) - 1
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Landing) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
for i := 0; i < len(landingChoices); i++ {
|
||||
if m.cursor == i {
|
||||
fmt.Fprintf(&b, "(•) %s\n", landingChoices[i])
|
||||
} else {
|
||||
fmt.Fprintf(&b, "( ) %s\n", landingChoices[i])
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Landing) Next(queue *[]*tea.Program) error {
|
||||
switch m.choice {
|
||||
case "Login":
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLogin(m.Base)))
|
||||
case "Register":
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewRegister(m.Base)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
92
plays/lobby.go
Normal file
92
plays/lobby.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/models"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
lobbyChoices = []string{""}
|
||||
)
|
||||
|
||||
type Lobby struct {
|
||||
*Base
|
||||
|
||||
choice string
|
||||
cursor int
|
||||
|
||||
updateCh chan struct{}
|
||||
users []models.UserStatus
|
||||
rooms []models.Room
|
||||
}
|
||||
|
||||
func NewLobby(base *Base) *Lobby {
|
||||
m := Lobby{
|
||||
Base: base,
|
||||
choice: "",
|
||||
cursor: 0,
|
||||
}
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Lobby) Init() tea.Cmd {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-m.updateCh:
|
||||
return
|
||||
default:
|
||||
_, err := m.Base.client.R().
|
||||
SetResult(&m.users).
|
||||
ForceContentType("application/json").
|
||||
Get("/api/lobby/users")
|
||||
if err != nil {
|
||||
zap.L().
|
||||
Error("failed to get lobby users",
|
||||
zap.Error(err))
|
||||
}
|
||||
_, err = m.Base.client.R().
|
||||
SetResult(&m.rooms).
|
||||
ForceContentType("application/json").
|
||||
Get("/api/lobby/rooms")
|
||||
if err != nil {
|
||||
zap.L().
|
||||
Error("failed to get lobby rooms",
|
||||
zap.Error(err))
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return tea.Batch(tea.ClearScreen)
|
||||
}
|
||||
|
||||
func (m *Lobby) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+l":
|
||||
return m, tea.ClearScreen
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Interrupt
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Lobby) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString("nmsl")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Lobby) Next(queue *[]*tea.Program) error {
|
||||
return nil
|
||||
}
|
||||
137
plays/login.go
Normal file
137
plays/login.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Login struct {
|
||||
*Base
|
||||
|
||||
focusIndex int
|
||||
inputs []textinput.Model
|
||||
}
|
||||
|
||||
func NewLogin(base *Base) *Login {
|
||||
m := Login{
|
||||
Base: base,
|
||||
|
||||
focusIndex: 0,
|
||||
inputs: make([]textinput.Model, 2),
|
||||
}
|
||||
|
||||
for i := range m.inputs {
|
||||
t := textinput.New()
|
||||
t.CharLimit = 32
|
||||
t.Width = 20
|
||||
|
||||
switch i {
|
||||
case 0:
|
||||
t.Placeholder = "Username"
|
||||
t.Focus()
|
||||
case 1:
|
||||
t.Placeholder = "Password"
|
||||
t.EchoMode = textinput.EchoPassword
|
||||
t.EchoCharacter = '•'
|
||||
}
|
||||
|
||||
m.inputs[i] = t
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Login) Init() tea.Cmd {
|
||||
return tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
}
|
||||
|
||||
func (m *Login) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+l":
|
||||
return m, tea.ClearScreen
|
||||
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Interrupt
|
||||
|
||||
case "tab", "shift+tab", "enter", "up", "down":
|
||||
s := msg.String()
|
||||
|
||||
if s == "enter" && m.focusIndex == len(m.inputs)-1 {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
if s == "up" || s == "shift+tab" {
|
||||
m.focusIndex--
|
||||
} else {
|
||||
m.focusIndex++
|
||||
}
|
||||
|
||||
if m.focusIndex >= len(m.inputs) {
|
||||
m.focusIndex = 0
|
||||
} else if m.focusIndex < 0 {
|
||||
m.focusIndex = len(m.inputs) - 1
|
||||
}
|
||||
|
||||
cmds := make([]tea.Cmd, len(m.inputs))
|
||||
for i := 0; i < len(m.inputs); i++ {
|
||||
if i == m.focusIndex {
|
||||
cmds[i] = m.inputs[i].Focus()
|
||||
continue
|
||||
}
|
||||
m.inputs[i].Blur()
|
||||
}
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := m.updateInputs(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *Login) updateInputs(msg tea.Msg) tea.Cmd {
|
||||
cmds := make([]tea.Cmd, len(m.inputs))
|
||||
|
||||
for i := range m.inputs {
|
||||
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
|
||||
}
|
||||
|
||||
return tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (m *Login) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
fmt.Fprintf(&b, "User login:\n")
|
||||
for i := range m.inputs {
|
||||
b.WriteString(m.inputs[i].View())
|
||||
if i < len(m.inputs) {
|
||||
b.WriteRune('\n')
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Login) Next(queue *[]*tea.Program) error {
|
||||
resp, _ := m.Base.client.R().
|
||||
SetBasicAuth(m.inputs[0].Value(), m.inputs[1].Value()).
|
||||
Get("/auth/login")
|
||||
|
||||
if resp.StatusCode() == http.StatusOK {
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLobby(m.Base)))
|
||||
} else {
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewRedirect("Login failed")))
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLanding(m.Base)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
9
plays/next.go
Normal file
9
plays/next.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package plays
|
||||
|
||||
import tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
type Next interface {
|
||||
Next(
|
||||
queue *[]*tea.Program,
|
||||
) error
|
||||
}
|
||||
59
plays/redirect.go
Normal file
59
plays/redirect.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type tickMsg time.Time
|
||||
|
||||
type Redirect struct {
|
||||
Message string
|
||||
|
||||
secLeft int
|
||||
}
|
||||
|
||||
func NewRedirect(msg string) *Redirect {
|
||||
return &Redirect{
|
||||
Message: msg,
|
||||
|
||||
secLeft: 3,
|
||||
}
|
||||
}
|
||||
|
||||
func tick() tea.Cmd {
|
||||
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
|
||||
return tickMsg(t)
|
||||
})
|
||||
}
|
||||
|
||||
func (m *Redirect) Init() tea.Cmd {
|
||||
return tick()
|
||||
}
|
||||
|
||||
func (m *Redirect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "q", "ctrl+c", "enter":
|
||||
return m, tea.Quit
|
||||
}
|
||||
case tickMsg:
|
||||
m.secLeft--
|
||||
if m.secLeft <= 0 {
|
||||
return m, tea.Quit
|
||||
}
|
||||
return m, tick()
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Redirect) View() string {
|
||||
return fmt.Sprintf("%s\n\nExit in %d seconds...", m.Message, m.secLeft)
|
||||
}
|
||||
|
||||
func (m *Redirect) Next(queue *[]*tea.Program) error {
|
||||
return nil
|
||||
}
|
||||
143
plays/register.go
Normal file
143
plays/register.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/models"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Register struct {
|
||||
*Base
|
||||
|
||||
focusIndex int
|
||||
inputs []textinput.Model
|
||||
}
|
||||
|
||||
func NewRegister(base *Base) *Register {
|
||||
m := Register{
|
||||
Base: base,
|
||||
focusIndex: 0,
|
||||
inputs: make([]textinput.Model, 2),
|
||||
}
|
||||
|
||||
for i := range m.inputs {
|
||||
t := textinput.New()
|
||||
t.CharLimit = 32
|
||||
t.Width = 20
|
||||
|
||||
switch i {
|
||||
case 0:
|
||||
t.Placeholder = "Username"
|
||||
t.Focus()
|
||||
case 1:
|
||||
t.Placeholder = "Password"
|
||||
t.EchoMode = textinput.EchoPassword
|
||||
t.EchoCharacter = '•'
|
||||
}
|
||||
|
||||
m.inputs[i] = t
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Register) Init() tea.Cmd {
|
||||
return tea.Batch(tea.ClearScreen, textinput.Blink)
|
||||
}
|
||||
|
||||
func (m *Register) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+l":
|
||||
return m, tea.ClearScreen
|
||||
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
|
||||
case "tab", "shift+tab", "enter", "up", "down":
|
||||
s := msg.String()
|
||||
|
||||
if s == "enter" && m.focusIndex == len(m.inputs)-1 {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
if s == "up" || s == "shift+tab" {
|
||||
m.focusIndex--
|
||||
} else {
|
||||
m.focusIndex++
|
||||
}
|
||||
|
||||
if m.focusIndex >= len(m.inputs) {
|
||||
m.focusIndex = 0
|
||||
} else if m.focusIndex < 0 {
|
||||
m.focusIndex = len(m.inputs) - 1
|
||||
}
|
||||
|
||||
cmds := make([]tea.Cmd, len(m.inputs))
|
||||
for i := 0; i < len(m.inputs); i++ {
|
||||
if i == m.focusIndex {
|
||||
cmds[i] = m.inputs[i].Focus()
|
||||
continue
|
||||
}
|
||||
m.inputs[i].Blur()
|
||||
}
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
}
|
||||
|
||||
cmd := m.updateInputs(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *Register) updateInputs(msg tea.Msg) tea.Cmd {
|
||||
cmds := make([]tea.Cmd, len(m.inputs))
|
||||
|
||||
for i := range m.inputs {
|
||||
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
|
||||
}
|
||||
|
||||
return tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (m *Register) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
fmt.Fprintf(&b, "User register:\n")
|
||||
for i := range m.inputs {
|
||||
b.WriteString(m.inputs[i].View())
|
||||
if i < len(m.inputs) {
|
||||
b.WriteRune('\n')
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Register) Next(queue *[]*tea.Program) error {
|
||||
resp, err := m.Base.client.R().
|
||||
SetBody(models.User{
|
||||
Username: m.inputs[0].Value(),
|
||||
Password: m.inputs[1].Value(),
|
||||
}).
|
||||
Post("/auth/register")
|
||||
|
||||
switch resp.StatusCode() {
|
||||
case http.StatusOK:
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLogin(m.Base)))
|
||||
case http.StatusBadRequest:
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewRedirect("Username already exist")))
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewRegister(m.Base)))
|
||||
case http.StatusInternalServerError:
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user