diff --git a/app/src/lib/state.svelte.ts b/app/src/lib/state.svelte.ts
index dc0d48e..8f20abd 100644
--- a/app/src/lib/state.svelte.ts
+++ b/app/src/lib/state.svelte.ts
@@ -10,11 +10,8 @@ export class State {
users = $state(0);
room = $state('');
turn = $state(-1); // -1 not my turn, 0 might be, 1 is
- game_over = $state(false);
socket: Socket;
- play_again_phase = $state(false);
-
constructor() {
const url = import.meta.env.DEV ? 'ws://localhost:3000' : 'wss://battleship.icyground-d91964e0.centralindia.azurecontainerapps.io';
this.socket = io(url, {
@@ -32,8 +29,12 @@ export class State {
this.room = room;
this.users = users;
});
-
this.socket.on('upload', (_, callback) => {
+ if (this.phase == 'gameover') {
+ this.playerBoard.randomize();
+ this.opponentBoard = new Board(true);
+ this.phase = 'waiting';
+ }
callback(this.playerBoard.board);
});
this.socket.on('turnover', (id) => {
@@ -73,18 +74,19 @@ export class State {
}
}
}
-
- this.game_over = game_over;
-
+ if (game_over) {
+ this.phase = 'gameover';
+ }
});
- this.socket.on('restore', ({ turn, player, opponent, game_over }: { turn: boolean, player: string[], opponent: string[], game_over: boolean }) => {
+ this.socket.on('restore', ({ turn, player, opponent, gameover }: { turn: boolean, player: string[], opponent: string[], gameover: boolean }) => {
this.turn = turn ? 1 : -1;
this.phase = this.turn ? 'selfturn' : 'otherturn';
this.playerBoard.board = player.map((s) => s.split('').map(c => c as CellType));
this.opponentBoard.board = opponent.map((s) => s.split('').map(c => c as CellType));
-
- this.game_over = game_over;
+ if (gameover) {
+ this.phase = 'gameover';
+ }
})
}
@@ -102,13 +104,17 @@ export class State {
joinRoom(code: string) {
code = code.toUpperCase();
- if (code.length != 4 || code == this.room) return;
+ if (code.length != 4 || code == this.room && this.phase !== 'gameover') return;
this.socket.emit('join', code);
}
hasNotStarted() {
return this.phase == 'placement' || this.phase == 'waiting';
}
+
+ playAgain() {
+ this.joinRoom(this.room);
+ }
}
diff --git a/app/src/routes/+page.svelte b/app/src/routes/+page.svelte
index c873a02..db51067 100644
--- a/app/src/routes/+page.svelte
+++ b/app/src/routes/+page.svelte
@@ -67,7 +67,7 @@
callback={(i, j) => gameState.attack(i, j)}
/>
- {#if gameState.game_over}
+ {#if gameState.phase === 'gameover'}
@@ -78,11 +78,7 @@
diff --git a/src/board.rs b/src/board.rs
index 770dac6..8da27f0 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -135,8 +135,7 @@ impl Board {
}
pub fn is_game_over(&self) -> bool {
- self.iter()
- .all(|row| row.iter().all(|cell| matches!(cell, 'e' | 'm' | 'h')))
+ !self.iter().any(|row| row.iter().any(|&cell| cell == 's'))
}
// fn validate_syntax(&self) -> bool {
diff --git a/src/game.rs b/src/game.rs
index 5c033b2..e934682 100644
--- a/src/game.rs
+++ b/src/game.rs
@@ -15,6 +15,8 @@ pub enum Error {
RoomFull(Option
),
#[error("Room not full")]
RoomNotFull,
+ #[error("GameOver room joined")]
+ GameOverRoom,
#[error("Already in room")]
AlreadyInRoom,
#[error("Not in room")]
@@ -33,7 +35,7 @@ pub enum Status {
Waiting,
P1Turn,
P2Turn,
- GameOver
+ GameOver,
}
pub async fn room_if_player_exists(sid: &str, pool: &sqlx::PgPool) -> Result