Feat: login done
This commit is contained in:
51
plays/base.go
Normal file
51
plays/base.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/types"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
refreshTick = 100 * time.Millisecond
|
||||
refetchTick = 1 * time.Second
|
||||
)
|
||||
|
||||
func Tick(d time.Duration) tea.Cmd {
|
||||
return tea.Tick(d, func(t time.Time) tea.Msg {
|
||||
return types.TickMsg(t)
|
||||
})
|
||||
}
|
||||
|
||||
type Base struct {
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewBase(client *resty.Client) *Base {
|
||||
return &Base{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Base) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Base) 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":
|
||||
return m, tea.Interrupt
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Base) View() string {
|
||||
return ""
|
||||
}
|
||||
92
plays/landing.go
Normal file
92
plays/landing.go
Normal file
@@ -0,0 +1,92 @@
|
||||
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
|
||||
}
|
||||
155
plays/lobby.go
Normal file
155
plays/lobby.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/models"
|
||||
"gitea.konchin.com/ytshih/inp2025/game/tracing"
|
||||
"gitea.konchin.com/ytshih/inp2025/game/types"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
lobbyChoices = []string{"No-op", "Logout"}
|
||||
)
|
||||
|
||||
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,
|
||||
updateCh: make(chan struct{}, 1),
|
||||
}
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func updateLobbyInfo(m *Lobby) error {
|
||||
for {
|
||||
select {
|
||||
case <-m.updateCh:
|
||||
return nil
|
||||
default:
|
||||
var users []models.UserStatus
|
||||
resp, err := m.Base.client.R().
|
||||
SetResult(&users).
|
||||
ForceContentType("application/json").
|
||||
Get("/api/lobby/users")
|
||||
if resp.StatusCode() != http.StatusOK {
|
||||
tracing.Logger.
|
||||
Error("failed to get lobby users",
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
m.users = users
|
||||
|
||||
var rooms []models.Room
|
||||
_, err = m.Base.client.R().
|
||||
SetResult(&rooms).
|
||||
ForceContentType("application/json").
|
||||
Get("/api/lobby/rooms")
|
||||
if err != nil {
|
||||
tracing.Logger.
|
||||
Error("failed to get lobby rooms",
|
||||
zap.Error(err))
|
||||
return err
|
||||
}
|
||||
m.rooms = rooms
|
||||
|
||||
time.Sleep(refetchTick)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Lobby) Init() tea.Cmd {
|
||||
go updateLobbyInfo(m)
|
||||
return tea.Sequence(tea.ClearScreen, Tick(refreshTick))
|
||||
}
|
||||
|
||||
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
|
||||
case "enter":
|
||||
m.choice = lobbyChoices[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(lobbyChoices) {
|
||||
m.cursor = 0
|
||||
} else if m.cursor < 0 {
|
||||
m.cursor = len(lobbyChoices) - 1
|
||||
}
|
||||
}
|
||||
case types.TickMsg:
|
||||
return m, Tick(refreshTick)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Lobby) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
fmt.Fprintf(&b, "Game lobby\n")
|
||||
|
||||
fmt.Fprintf(&b, "User Status (%d):\n", len(m.users))
|
||||
for _, user := range m.users {
|
||||
b.WriteString("- " + user.View() + "\n")
|
||||
}
|
||||
|
||||
fmt.Fprintf(&b, "Room Status (%d):\n", len(m.rooms))
|
||||
for _, room := range m.rooms {
|
||||
b.WriteString("- " + room.View() + "\n")
|
||||
}
|
||||
|
||||
b.WriteString("==========\n")
|
||||
|
||||
for i := 0; i < len(lobbyChoices); i++ {
|
||||
if m.cursor == i {
|
||||
fmt.Fprintf(&b, "(•) %s\n", lobbyChoices[i])
|
||||
} else {
|
||||
fmt.Fprintf(&b, "( ) %s\n", lobbyChoices[i])
|
||||
}
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Lobby) Next(queue *[]*tea.Program) error {
|
||||
m.updateCh <- struct{}{}
|
||||
switch m.choice {
|
||||
case "No-op":
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLobby(m.Base)))
|
||||
case "Logout":
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewLogout(m.Base)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
138
plays/login.go
Normal file
138
plays/login.go
Normal file
@@ -0,0 +1,138 @@
|
||||
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) {
|
||||
if _, cmd := m.Base.Update(msg); cmd != nil {
|
||||
return m, cmd
|
||||
}
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
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 {
|
||||
username := m.inputs[0].Value()
|
||||
password := m.inputs[1].Value()
|
||||
resp, _ := m.Base.client.R().
|
||||
SetBasicAuth(username, password).
|
||||
Get("/auth/login")
|
||||
|
||||
if resp.StatusCode() == http.StatusOK {
|
||||
m.Base.client.
|
||||
SetBasicAuth(username, password)
|
||||
*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
|
||||
}
|
||||
34
plays/logout.go
Normal file
34
plays/logout.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package plays
|
||||
|
||||
import tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
type Logout struct{ *Base }
|
||||
|
||||
func NewLogout(base *Base) *Logout {
|
||||
return &Logout{Base: base}
|
||||
}
|
||||
|
||||
func (m *Logout) Init() tea.Cmd {
|
||||
return tea.ClearScreen
|
||||
}
|
||||
|
||||
func (m *Logout) Update(tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
func (m *Logout) View() string {
|
||||
return "Logout"
|
||||
}
|
||||
|
||||
func (m *Logout) Next(queue *[]*tea.Program) error {
|
||||
_, _ = m.Base.client.R().
|
||||
Post("/auth/logout")
|
||||
|
||||
// cleanup
|
||||
m.Base.client.
|
||||
SetBasicAuth("", "")
|
||||
|
||||
*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
|
||||
}
|
||||
52
plays/redirect.go
Normal file
52
plays/redirect.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/types"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Redirect struct {
|
||||
Message string
|
||||
|
||||
secLeft int
|
||||
}
|
||||
|
||||
func NewRedirect(msg string) *Redirect {
|
||||
return &Redirect{
|
||||
Message: msg,
|
||||
|
||||
secLeft: 3,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Redirect) Init() tea.Cmd {
|
||||
return tea.Sequence(tea.ClearScreen, Tick(time.Second))
|
||||
}
|
||||
|
||||
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 types.TickMsg:
|
||||
m.secLeft--
|
||||
if m.secLeft <= 0 {
|
||||
return m, tea.Quit
|
||||
}
|
||||
return m, Tick(time.Second)
|
||||
}
|
||||
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
|
||||
}
|
||||
140
plays/register.go
Normal file
140
plays/register.go
Normal file
@@ -0,0 +1,140 @@
|
||||
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) {
|
||||
if _, cmd := m.Base.Update(msg); cmd != nil {
|
||||
return m, cmd
|
||||
}
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
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