Something went wrong. Try again.
a collection of games playable over ssh!
Something went wrong. Try again.
games model.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192package main
import ( "fmt"
twenty48 "github.com/CelestialCrafter/games/apps/2048" "github.com/CelestialCrafter/games/apps/saves" "github.com/CelestialCrafter/games/apps/tictactoe" "github.com/CelestialCrafter/games/common" "github.com/CelestialCrafter/games/saveManager" "github.com/CelestialCrafter/games/selector" "github.com/CelestialCrafter/games/styles" "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/log")
type sessionState int
const ( selectorView sessionState = iota gameView)
type KeyMap struct { common.ArrowsKeyMap Select key.Binding Quit key.Binding}
type MainModel struct { state sessionState saveManager tea.Model selector tea.Model app tea.Model keys KeyMap err *common.ErrorMsg userId string selected int width int height int}
func NewModel(userId string) MainModel { return MainModel{ state: selectorView, saveManager: saveManager.NewModel(userId), selector: selector.NewModel(), app: nil, userId: userId, keys: KeyMap{ ArrowsKeyMap: common.NewArrowsKeyMap(), Select: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")), Quit: key.NewBinding(key.WithKeys("ctrl+c"), key.WithHelp("ctrl+c", "quit")), }, }}
func (m MainModel) NewGame(id uint) tea.Model { switch id { case common.Twenty48.ID: return twenty48.NewModel() case common.TicTacToe.ID: return tictactoe.NewModel() case common.Saves.ID: return saves.NewModel(m.userId) }
return EmptyModel{}}
func (m MainModel) Init() tea.Cmd { return tea.Batch(m.saveManager.Init(), m.selector.Init())}
func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd
switch msg := msg.(type) { case tea.KeyMsg: switch { case key.Matches(msg, m.keys.Quit): return m, tea.Quit case m.err != nil && key.Matches(msg, m.keys.Left): m.selected = max(m.selected-1, 0) case m.err != nil && key.Matches(msg, m.keys.Right): // 2nd value is the amount of option buttons (0 based) m.selected = min(m.selected+1, 1) case m.err != nil && key.Matches(msg, m.keys.Select): if m.selected == 1 { return m, tea.Quit }
action := m.err.Action m.err = nil return m, action } case common.BackMsg: m.state = selectorView m.app = nil case selector.PlayMsg: m.state = gameView m.app = m.NewGame(msg.ID)
return m, tea.Sequence( m.app.Init(), func() tea.Msg { return saveManager.TryLoad{ ID: msg.ID, } }, func() tea.Msg { return tea.WindowSizeMsg{ Width: m.width, Height: m.height, } }, ) case common.ErrorMsg: if msg.Err != nil { log.Error("game sent error message", "error", msg.Err) m.err = &msg } case tea.WindowSizeMsg: m.width = msg.Width m.height = msg.Height }
m.saveManager, cmd = m.saveManager.Update(msg) if cmd != nil { return m, cmd }
switch m.state { case selectorView: m.selector, cmd = m.selector.Update(msg) case gameView: m.app, cmd = m.app.Update(msg) }
return m, cmd}
func (m MainModel) getButtonStyle(option int) lipgloss.Style { if option == m.selected { return styles.ButtonSelected }
return styles.Button}
func (m MainModel) View() string { var s string switch m.state { case selectorView: s = m.selector.View() case gameView: s = m.app.View() }
// error handling if m.err != nil { err := lipgloss.NewStyle().Width(50).Align(lipgloss.Center).Render(fmt.Sprint(m.err.Err))
var actionText string if m.err.ActionText == "" { actionText = "Continue" } else { actionText = m.err.ActionText }
actionButton := m.getButtonStyle(0).Render(actionText) quitButton := m.getButtonStyle(1).Render("Quit")
var buttons string if m.err.Fatal { buttons = quitButton } else { buttons = lipgloss.JoinHorizontal(lipgloss.Top, actionButton, quitButton) }
ui := lipgloss.JoinVertical(lipgloss.Center, err, buttons) s = lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, styles.DialogBox.Render(ui), ) }
return s}