78 lines
1.3 KiB
Go
78 lines
1.3 KiB
Go
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
|
|
}
|