summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/ui/views/game.go12
-rw-r--r--pkg/ui/views/game_api.go8
-rw-r--r--pkg/ui/views/game_keymap.go45
-rw-r--r--pkg/ui/views/game_moves.go9
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...)