summaryrefslogtreecommitdiff
path: root/pkg/ui/views/game_restore.go
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-04-18 21:25:32 +0200
committerSanto Cariotti <santo@dcariotti.me>2025-04-18 21:25:32 +0200
commit42d68aa99d59339dbdf928a54c28242635728daa (patch)
tree98dadfd64a0fc05d1fb6f6ddbc9a3e8963fbf1dd /pkg/ui/views/game_restore.go
parent7c5a6176b27b6b0c0c3ef8a4aedbdec871391a80 (diff)
Restore a game
Diffstat (limited to 'pkg/ui/views/game_restore.go')
-rw-r--r--pkg/ui/views/game_restore.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/ui/views/game_restore.go b/pkg/ui/views/game_restore.go
new file mode 100644
index 0000000..b2e647c
--- /dev/null
+++ b/pkg/ui/views/game_restore.go
@@ -0,0 +1,54 @@
+package views
+
+import (
+ "fmt"
+ "strings"
+ "time"
+
+ tea "github.com/charmbracelet/bubbletea"
+)
+
+// Catch for `RestoreGameMessage` message from multiplayer
+type SendRestoreMsg struct{}
+
+// 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 {
+ if m.network.Me() == m.playerPeer(1) {
+ _ = m.getGame()()
+ remote := m.game.IP2
+ m.network.AddPeer(m.playerPeer(2), remote)
+ } else {
+ _ = m.getGame()()
+ remote := m.game.IP1
+ m.network.AddPeer(m.playerPeer(1), remote)
+ }
+
+ // FIXME: add a loading modal
+ time.Sleep(2 * time.Second)
+
+ payload := ""
+
+ for _, move := range m.chessGame.Moves() {
+ payload += fmt.Sprintf("%s\n", move.String())
+ }
+
+ m.err = m.network.Send([]byte("restore-ack"), []byte(payload))
+
+ return nil
+}
+
+// Restores the moves for `m.chessGame`
+func (m *GameModel) handleRestoreMoves(msg RestoreMoves) tea.Cmd {
+ moves := strings.Split(string(msg), "\n")
+ for _, move := range moves {
+ m.chessGame.MoveStr(move)
+ }
+
+ m.turn = len(moves) - 1
+ cmds := []tea.Cmd{m.getMoves(), m.updateMovesListCmd()}
+ return tea.Batch(cmds...)
+}