diff --git a/app/src/lib/state.svelte.ts b/app/src/lib/state.svelte.ts
index 4e85bea..dc0d48e 100644
--- a/app/src/lib/state.svelte.ts
+++ b/app/src/lib/state.svelte.ts
@@ -1,6 +1,6 @@
import { io, Socket } from "socket.io-client";
-export type Phase = 'placement' | 'waiting' | 'selfturn' | 'otherturn';
+export type Phase = 'placement' | 'waiting' | 'selfturn' | 'otherturn' | 'gameover';
export type CellType = 'e' | 's' | 'h' | 'm'; // empty, ship, hit, miss
export class State {
@@ -10,8 +10,11 @@ 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, {
@@ -37,7 +40,7 @@ export class State {
this.turn = (id == this.socket.id) ? 1 : -1;
this.phase = this.turn ? 'selfturn' : 'otherturn';
});
- this.socket.on('attacked', ({ by, at, hit, sunk }) => {
+ this.socket.on('attacked', ({ by, at, hit, sunk, game_over }) => {
const [i, j]: [number, number] = at;
const board = by == this.socket.id ? this.opponentBoard : this.playerBoard;
if (by == this.socket.id) {
@@ -70,13 +73,18 @@ export class State {
}
}
}
+
+ this.game_over = game_over;
+
});
- this.socket.on('restore', ({ turn, player, opponent }: { turn: boolean, player: string[], opponent: string[] }) => {
+ this.socket.on('restore', ({ turn, player, opponent, game_over }: { turn: boolean, player: string[], opponent: string[], game_over: 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;
})
}
diff --git a/app/src/routes/+page.svelte b/app/src/routes/+page.svelte
index fad9909..c873a02 100644
--- a/app/src/routes/+page.svelte
+++ b/app/src/routes/+page.svelte
@@ -43,10 +43,7 @@
{gameState.users}
-
+
{/if}
@@ -69,13 +66,37 @@
board={gameState.opponentBoard}
callback={(i, j) => gameState.attack(i, j)}
/>
+
+ {#if gameState.game_over}
+
+
+
Game Over
+
+ {gameState.turn >= 0 ? 'You win!' : 'You lose!'}
+
+
+
+
+
+ {/if}
{#if gameState.hasNotStarted()}
gameState.createRoom()}
joinRoom={(code) => gameState.joinRoom(code)}
- leaveRoom={leaveRoom}
+ {leaveRoom}
/>
{/if}
diff --git a/migrations/0001_schema.sql b/migrations/0001_schema.sql
index ebb0ec4..a7cd0d8 100644
--- a/migrations/0001_schema.sql
+++ b/migrations/0001_schema.sql
@@ -1,5 +1,5 @@
-- DROP OWNED BY CURRENT_USER CASCADE;
-CREATE TYPE STAT AS ENUM ('waiting', 'p1turn', 'p2turn');
+CREATE TYPE STAT AS ENUM ('waiting', 'p1turn', 'p2turn', 'gameover');
CREATE TABLE IF NOT EXISTS players (
id CHAR(16) PRIMARY KEY,
diff --git a/src/board.rs b/src/board.rs
index 0cb83e8..770dac6 100644
--- a/src/board.rs
+++ b/src/board.rs
@@ -134,6 +134,11 @@ impl Board {
self
}
+ pub fn is_game_over(&self) -> bool {
+ self.iter()
+ .all(|row| row.iter().all(|cell| matches!(cell, 'e' | 'm' | 'h')))
+ }
+
// fn validate_syntax(&self) -> bool {
// self
// .iter()
diff --git a/src/game.rs b/src/game.rs
index ce99ce9..5c033b2 100644
--- a/src/game.rs
+++ b/src/game.rs
@@ -33,6 +33,7 @@ pub enum Status {
Waiting,
P1Turn,
P2Turn,
+ GameOver
}
pub async fn room_if_player_exists(sid: &str, pool: &sqlx::PgPool) -> Result