Draft: big refactor

This commit is contained in:
2025-09-16 16:03:27 +08:00
parent f527230f1e
commit 7ee2163a95
33 changed files with 338 additions and 155 deletions

77
plays/game.go Normal file
View 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
}