Draft: feat login
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user