Draft: big refactor
This commit is contained in:
77
plays/game.go
Normal file
77
plays/game.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package plays
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/game/types"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
*Base
|
||||
state types.WordleState
|
||||
input string
|
||||
}
|
||||
|
||||
func NewGame(base *Base) *Game {
|
||||
m := Game{
|
||||
Base: base,
|
||||
state: types.NewWordleState(),
|
||||
input: "",
|
||||
}
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *Game) Init() tea.Cmd {
|
||||
return tea.Batch(tea.ClearScreen)
|
||||
}
|
||||
|
||||
func (m *Game) 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.Type {
|
||||
case tea.KeyBackspace:
|
||||
if len(m.input) > 0 {
|
||||
m.input = m.input[:len(m.input)-1]
|
||||
}
|
||||
case tea.KeyEnter:
|
||||
if len(m.input) == types.MaxLength {
|
||||
return m, tea.Quit
|
||||
}
|
||||
case tea.KeyRunes:
|
||||
if len(m.input) < types.MaxLength {
|
||||
m.input = m.input + string(msg.Runes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Game) View() string {
|
||||
var b strings.Builder
|
||||
|
||||
for _, s := range m.history {
|
||||
b.WriteString(s.View())
|
||||
b.WriteRune('\n')
|
||||
}
|
||||
|
||||
b.WriteString(m.input)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Game) Next(queue *[]*tea.Program) error {
|
||||
if len(m.input) == types.MaxLength {
|
||||
resp, err := m.Base.client.R().
|
||||
Post("/api/guess")
|
||||
|
||||
*queue = append(*queue,
|
||||
tea.NewProgram(NewGame(m.Base)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -25,7 +25,7 @@ type Lobby struct {
|
||||
|
||||
updateCh chan struct{}
|
||||
users []models.UserStatus
|
||||
rooms []models.Room
|
||||
rooms []types.Room
|
||||
}
|
||||
|
||||
func NewLobby(base *Base) *Lobby {
|
||||
@@ -58,7 +58,8 @@ func updateLobbyInfo(m *Lobby) error {
|
||||
}
|
||||
m.users = users
|
||||
|
||||
var rooms []models.Room
|
||||
var rooms []types.Room
|
||||
// TODO: scan rooms
|
||||
_, err = m.Base.client.R().
|
||||
SetResult(&rooms).
|
||||
ForceContentType("application/json").
|
||||
|
||||
Reference in New Issue
Block a user