diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-04-18 16:09:32 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-04-18 16:09:32 +0200 |
commit | 7c5a6176b27b6b0c0c3ef8a4aedbdec871391a80 (patch) | |
tree | 5ebfc9b402b8aa941ab9617b5f3842071edafedf /pkg/ui/views | |
parent | 0b4fb251efdfd2fba7cd6154f5f59bd61ede15f1 (diff) |
Add type on messages between peers
Diffstat (limited to 'pkg/ui/views')
-rw-r--r-- | pkg/ui/views/game.go | 6 | ||||
-rw-r--r-- | pkg/ui/views/game_keymap.go | 2 | ||||
-rw-r--r-- | pkg/ui/views/game_moves.go | 12 |
3 files changed, 12 insertions, 8 deletions
diff --git a/pkg/ui/views/game.go b/pkg/ui/views/game.go index 4e8cc87..3742101 100644 --- a/pkg/ui/views/game.go +++ b/pkg/ui/views/game.go @@ -28,7 +28,7 @@ type GameModel struct { game *database.Game network *multiplayer.GameNetwork chessGame *chess.Game - incomingMoves chan string + incomingMoves chan multiplayer.GameMove turn int availableMovesList list.Model } @@ -56,7 +56,7 @@ func NewGameModel(width, height int, currentGameID int, network *multiplayer.Gam currentGameID: currentGameID, network: network, chessGame: chess.NewGame(chess.UseNotation(chess.UCINotation{})), - incomingMoves: make(chan string), + incomingMoves: make(chan multiplayer.GameMove), turn: 0, availableMovesList: moveList, } @@ -122,7 +122,7 @@ func (m GameModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.err = err } else { m.turn++ - m.network.Send([]byte(moveStr)) + m.network.Send([]byte("new-move"), []byte(moveStr)) m.err = nil } cmds = append(cmds, m.getMoves(), m.updateMovesListCmd()) diff --git a/pkg/ui/views/game_keymap.go b/pkg/ui/views/game_keymap.go index 00b062f..283a69c 100644 --- a/pkg/ui/views/game_keymap.go +++ b/pkg/ui/views/game_keymap.go @@ -44,7 +44,7 @@ func (m GameModel) handleKeyMsg(msg tea.KeyMsg) (GameModel, tea.Cmd) { outcome = string(chess.WhiteWon) } - m.network.Send([]byte("🏳️")) + m.network.Send([]byte("abandon"), []byte("🏳️")) return m, m.endGame(outcome) } case key.Matches(msg, m.keys.Quit): diff --git a/pkg/ui/views/game_moves.go b/pkg/ui/views/game_moves.go index daf24e6..ea1fa02 100644 --- a/pkg/ui/views/game_moves.go +++ b/pkg/ui/views/game_moves.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/boozec/rahanna/pkg/p2p" + "github.com/boozec/rahanna/pkg/ui/multiplayer" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" "github.com/notnil/chess" @@ -25,16 +26,19 @@ func (i item) FilterValue() string { return i.title } func (m *GameModel) getMoves() tea.Cmd { m.network.AddReceiveFunction(func(msg p2p.Message) { - payload := string(msg.Payload) - m.incomingMoves <- payload + gm := multiplayer.GameMove{ + Type: msg.Type, + Payload: msg.Payload, + } + m.incomingMoves <- gm }) return func() tea.Msg { move := <-m.incomingMoves - if move == "🏳️" { + if multiplayer.MoveType(string(move.Type)) == multiplayer.AbandonGameMessage { return EndGameMsg{abandoned: true} } - return ChessMoveMsg(move) + return ChessMoveMsg(string(move.Payload)) } } |