diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-04-17 17:46:05 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-04-17 17:46:05 +0200 |
commit | 3ad0d21e072c7461ea78a9a85111e6e19d3b0632 (patch) | |
tree | 186045fd7b6ecd6c25c1deab504f25297bf6e134 /pkg | |
parent | 95da822f025b544f700729afc676ba0f3c981154 (diff) |
Handle abandon game
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/ui/views/game.go | 12 | ||||
-rw-r--r-- | pkg/ui/views/game_api.go | 8 | ||||
-rw-r--r-- | pkg/ui/views/game_keymap.go | 45 | ||||
-rw-r--r-- | pkg/ui/views/game_moves.go | 9 |
4 files changed, 53 insertions, 21 deletions
diff --git a/pkg/ui/views/game.go b/pkg/ui/views/game.go index d545fcb..7075f8b 100644 --- a/pkg/ui/views/game.go +++ b/pkg/ui/views/game.go @@ -96,7 +96,15 @@ func (m GameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m, cmd = m.handleDatabaseGameMsg(msg) cmds = append(cmds, cmd, m.updateMovesListCmd()) case EndGameMsg: - return m, nil + if msg.abandoned { + if m.peer == "peer-2" { + m.game.Outcome = "1-0" + } else { + m.game.Outcome = "0-1" + } + m, cmd = m.handleDatabaseGameMsg(*m.game) + cmds = append(cmds, cmd) + } case error: m.err = msg } @@ -121,7 +129,7 @@ func (m GameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, m.getMoves(), m.updateMovesListCmd()) if m.chessGame.Outcome() != chess.NoOutcome { - cmds = append(cmds, m.endGame()) + cmds = append(cmds, m.endGame(m.chessGame.Outcome().String())) } } } diff --git a/pkg/ui/views/game_api.go b/pkg/ui/views/game_api.go index 75aa0a1..34ba1f3 100644 --- a/pkg/ui/views/game_api.go +++ b/pkg/ui/views/game_api.go @@ -68,9 +68,11 @@ func (m *GameModel) getGame() tea.Cmd { } } -type EndGameMsg struct{} +type EndGameMsg struct { + abandoned bool +} -func (m *GameModel) endGame() tea.Cmd { +func (m *GameModel) endGame(outcome string) tea.Cmd { return func() tea.Msg { var game database.Game @@ -82,7 +84,7 @@ func (m *GameModel) endGame() tea.Cmd { // Prepare request payload payload, err := json.Marshal(map[string]string{ - "outcome": m.chessGame.Outcome().String(), + "outcome": outcome, }) // Send API request diff --git a/pkg/ui/views/game_keymap.go b/pkg/ui/views/game_keymap.go index f38af02..29881c8 100644 --- a/pkg/ui/views/game_keymap.go +++ b/pkg/ui/views/game_keymap.go @@ -3,6 +3,7 @@ package views import ( "fmt" + "github.com/boozec/rahanna/internal/network" "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -10,45 +11,63 @@ import ( // gameKeyMap defines the key bindings for the game view. type gameKeyMap struct { - GoLogout key.Binding - Quit key.Binding + Abandon key.Binding + Quit key.Binding + Exit key.Binding } // defaultGameKeyMap provides the default key bindings for the game view. var defaultGameKeyMap = gameKeyMap{ - GoLogout: key.NewBinding( - key.WithKeys("alt+Q", "alt+q"), - key.WithHelp("Alt+Q", "Logout"), + Abandon: key.NewBinding( + key.WithKeys("A", "a"), + key.WithHelp(" A", "Abandon"), ), Quit: key.NewBinding( key.WithKeys("Q", "q"), - key.WithHelp(" Q", "Quit"), + key.WithHelp(" Q", "Quit"), + ), + Exit: key.NewBinding( + key.WithKeys("ctrl+c", "ctrl+C"), + key.WithHelp("CTRL+C", "Exit"), ), } func (m GameModel) handleKeyMsg(msg tea.KeyMsg) (GameModel, tea.Cmd) { switch { - case key.Matches(msg, m.keys.GoLogout): - return m, logout(m.width, m.height+1) + case key.Matches(msg, m.keys.Abandon): + var outcome string + if m.peer == "peer-2" { + outcome = "0-1" + } else { + outcome = "1-0" + } + + m.network.Server.Send(network.NetworkID(m.peer), []byte("🏳️")) + return m, m.endGame(outcome) case key.Matches(msg, m.keys.Quit): - return m, tea.Quit + return m, SwitchModelCmd(NewPlayModel(m.width, m.height)) } return m, nil } func (m GameModel) renderNavigationButtons() string { - logoutKey := fmt.Sprintf("%s %s", - altCodeStyle.Render(m.keys.GoLogout.Help().Key), - m.keys.GoLogout.Help().Desc) + abandonKey := fmt.Sprintf("%s %s", + altCodeStyle.Render(m.keys.Abandon.Help().Key), + m.keys.Abandon.Help().Desc) quitKey := fmt.Sprintf("%s %s", altCodeStyle.Render(m.keys.Quit.Help().Key), m.keys.Quit.Help().Desc) + exitKey := fmt.Sprintf("%s %s", + altCodeStyle.Render(m.keys.Exit.Help().Key), + m.keys.Exit.Help().Desc) + return lipgloss.JoinVertical( lipgloss.Left, - logoutKey, + abandonKey, quitKey, + exitKey, ) } diff --git a/pkg/ui/views/game_moves.go b/pkg/ui/views/game_moves.go index 2fa7c9e..4ce3796 100644 --- a/pkg/ui/views/game_moves.go +++ b/pkg/ui/views/game_moves.go @@ -25,12 +25,15 @@ func (i item) FilterValue() string { return i.title } func (m *GameModel) getMoves() tea.Cmd { m.network.Server.OnReceiveFn = func(msg network.Message) { - moveStr := string(msg.Payload) - m.incomingMoves <- moveStr + payload := string(msg.Payload) + m.incomingMoves <- payload } return func() tea.Msg { move := <-m.incomingMoves + if move == "🏳️" { + return EndGameMsg{abandoned: true} + } return ChessMoveMsg(move) } } @@ -75,7 +78,7 @@ func (m GameModel) handleChessMoveMsg(msg ChessMoveMsg) (GameModel, tea.Cmd) { } if m.chessGame.Outcome() != chess.NoOutcome { - cmds = append(cmds, m.endGame()) + cmds = append(cmds, m.endGame(m.chessGame.Outcome().String())) } return m, tea.Batch(cmds...) |