summaryrefslogtreecommitdiff
path: root/pkg/ui/views/game_restore.go
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-04-24 13:53:54 +0200
committerSanto Cariotti <santo@dcariotti.me>2025-04-24 13:53:54 +0200
commitb35829071ab0d0f3479021eac151b90e49a2fca5 (patch)
tree0e2af88ed2732e2211ffbf5689488e010174783a /pkg/ui/views/game_restore.go
parent4ae96a216eb50ccec7712fa9ed0d4dc8d9950f68 (diff)
Play co-op 2 vs 2
Diffstat (limited to 'pkg/ui/views/game_restore.go')
-rw-r--r--pkg/ui/views/game_restore.go39
1 files changed, 29 insertions, 10 deletions
diff --git a/pkg/ui/views/game_restore.go b/pkg/ui/views/game_restore.go
index 4ce8184..95f8844 100644
--- a/pkg/ui/views/game_restore.go
+++ b/pkg/ui/views/game_restore.go
@@ -5,25 +5,45 @@ import (
"strings"
"time"
+ "github.com/boozec/rahanna/pkg/p2p"
tea "github.com/charmbracelet/bubbletea"
)
// Catch for `RestoreGameMessage` message from multiplayer
-type SendRestoreMsg struct{}
+type SendRestoreMsg p2p.NetworkID
// Catch for `RestoreAckGameMessage` message from multiplayer
type RestoreMoves string
// For `RestoreGameMessage` from multiplayer it fixes the peer with the new
// address and sends back an ACK to the peer' sender
-func (m GameModel) handleSendRestoreMsg() tea.Cmd {
+func (m GameModel) handleSendRestoreMsg(source p2p.NetworkID) tea.Cmd {
_ = m.getGame()()
- if m.network.Me() == m.playerPeer(1) {
- remote := m.game.IP2
- m.network.AddPeer(m.playerPeer(2), remote)
- } else {
- remote := m.game.IP1
- m.network.AddPeer(m.playerPeer(1), remote)
+
+ peers := map[int]string{
+ 1: m.game.IP1,
+ 2: m.game.IP2,
+ 3: m.game.IP3,
+ 4: m.game.IP4,
+ }
+
+ myPlayerNum := -1
+ switch m.network.Me() {
+ case m.playerPeer(1):
+ myPlayerNum = 1
+ case m.playerPeer(2):
+ myPlayerNum = 2
+ case m.playerPeer(3):
+ myPlayerNum = 3
+ case m.playerPeer(4):
+ myPlayerNum = 4
+ }
+
+ // Add all peers to every other peer
+ for playerNum, ip := range peers {
+ if playerNum != myPlayerNum && ip != "" {
+ m.network.AddPeer(m.playerPeer(playerNum), ip)
+ }
}
// FIXME: add a loading modal
@@ -35,7 +55,7 @@ func (m GameModel) handleSendRestoreMsg() tea.Cmd {
payload += fmt.Sprintf("%s\n", move.String())
}
- m.err = m.network.Send([]byte("restore-ack"), []byte(payload))
+ m.err = m.network.Send(source, []byte("restore-ack"), []byte(payload))
return nil
}
@@ -47,7 +67,6 @@ func (m *GameModel) handleRestoreMoves(msg RestoreMoves) tea.Cmd {
m.chessGame.MoveStr(move)
}
- m.turn = len(moves) - 1
cmds := []tea.Cmd{m.getMoves(), m.updateMovesListCmd()}
return tea.Batch(cmds...)
}