17 Commits
v1.1.1 ... main

Author SHA1 Message Date
8a6fb7ccdb Uses prebuilt server image
Some checks failed
Check / frontend (push) Has been cancelled
Check / backend (push) Has been cancelled
Simplifies deployment by using a prebuilt Docker image for the server.

Removes build configuration and related port mappings from the compose file, relying on the image configuration instead.

Also adds a compose template file with the original configuration.
2025-07-16 23:40:37 +02:00
sparshg
bc4bf2c696 add sqlx checks 2024-10-06 00:56:55 +05:30
sparshg
2b19134d88 v1.2.0 2024-10-06 00:55:40 +05:30
sparshg
2bb5efd9bb fmt, clippy 2024-10-06 00:53:46 +05:30
sparshg
5fee374ac9 fix game restart 2024-10-06 00:53:46 +05:30
Khadeer
cd1a156d3e feat: end screen 2024-10-06 00:53:46 +05:30
sparshg
1d61f8fc32 v1.1.2 2024-10-03 02:28:15 +05:30
sparshg
9e86d40ca8 remove cors layer 2024-10-03 02:27:42 +05:30
arinak1017
cec5d58937 Fixed spelling - "containerized" 2024-10-03 02:09:42 +05:30
sparshg
14e0b47596 leave button styling 2024-10-03 01:16:02 +05:30
Yael Arturo Chavoya Andalón
a7ae0ea4ff refactor(join): change leave button styles to match 2024-10-03 01:16:02 +05:30
Yael Arturo Chavoya Andalón
3db83c03fd refactor(join): shorten prompt text 2024-10-03 01:16:02 +05:30
Yael Arturo Chavoya Andalón
0b5f513520 feat(join): add leave room button when there is no opponent yet 2024-10-03 01:16:02 +05:30
Yael Arturo Chavoya Andalón
e285fa4801 feat(join): hide join button if already on a room 2024-10-03 01:16:02 +05:30
Sidharth-Singh10
db4a58c3e6 Default ALLOWED_ORIGINS to localhost:5173 with trace logging 2024-09-28 23:25:33 +05:30
Sidharth-Singh10
a9ef92721f Updating CORS to be more restrictive 2024-09-28 23:25:33 +05:30
Sparsh Goenka
8a1b9bf603 Update README.md 2024-09-28 13:20:57 +05:30
15 changed files with 197 additions and 70 deletions

View File

@@ -12,7 +12,8 @@
"Enum": [ "Enum": [
"waiting", "waiting",
"p1turn", "p1turn",
"p2turn" "p2turn",
"gameover"
] ]
} }
} }

View File

@@ -23,7 +23,8 @@
"Enum": [ "Enum": [
"waiting", "waiting",
"p1turn", "p1turn",
"p2turn" "p2turn",
"gameover"
] ]
} }
} }

View File

@@ -13,7 +13,8 @@
"Enum": [ "Enum": [
"waiting", "waiting",
"p1turn", "p1turn",
"p2turn" "p2turn",
"gameover"
] ]
} }
} }

2
Cargo.lock generated
View File

@@ -154,7 +154,7 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]] [[package]]
name = "battleship" name = "battleship"
version = "1.1.1" version = "1.2.0"
dependencies = [ dependencies = [
"axum", "axum",
"dotenv", "dotenv",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "battleship" name = "battleship"
version = "1.1.1" version = "1.2.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@@ -12,7 +12,13 @@ The client is built using SvelteKit (static site) and the server uses Axum frame
The client can be started using `npm run dev` inside `app` directory. The client can be started using `npm run dev` inside `app` directory.
The server and the database services are containarized. Just run `docker compose up` to start the server and database services if you are working on the frontend. The server and the database services are containerized. Just run `docker compose up` to start the server and database services if you are working on the frontend.
Make sure to make a `.env` file with these parameters:
```
DATABASE_PASSWORD=db_password
DATABASE_NAME=db_name
DATABASE_URL=postgres://postgres:db_password@localhost:5432/db_name
```
If you are working on the server, you can run `cargo watch -i app -x run` to automatically restart the server when the source code changes, and `docker compose up -d db` to start the database service in the background. If you are working on the server, you can run `cargo watch -i app -x run` to automatically restart the server when the source code changes, and `docker compose up -d db` to start the database service in the background.

View File

@@ -7,11 +7,13 @@
class: className = '', class: className = '',
roomCode, roomCode,
createRoom, createRoom,
joinRoom joinRoom,
leaveRoom
}: { }: {
roomCode: string; roomCode: string;
createRoom: () => void; createRoom: () => void;
joinRoom: (code: string) => void; joinRoom: (code: string) => void;
leaveRoom: () => void;
class: string; class: string;
} = $props(); } = $props();
</script> </script>
@@ -21,6 +23,7 @@
> >
<div class="space-y-4 max-w-[70%]"> <div class="space-y-4 max-w-[70%]">
{#if roomCode} {#if roomCode}
<div class="text-center text-lg text-primary-content">Share this room code</div>
<div class="space-x-2 flex flex-row justify-center items-center"> <div class="space-x-2 flex flex-row justify-center items-center">
<div <div
class="text-3xl font-bold tracking-widest text-secondary-content font-mono bg-secondary py-3 rounded-full px-12" class="text-3xl font-bold tracking-widest text-secondary-content font-mono bg-secondary py-3 rounded-full px-12"
@@ -41,20 +44,31 @@
</button> </button>
{/if} {/if}
<div class="text-center text-lg text-primary-content">OR</div> <div class="text-center text-lg text-primary-content">OR</div>
<div class="space-y-2"> {#if !roomCode}
<input <div class="space-y-2">
type="text" <input
placeholder="Enter code" type="text"
maxlength="4" placeholder="Enter code"
bind:value={joinCode} maxlength="4"
class="input input-bordered input-primary uppercase tracking-widest placeholder-primary text-neutral text-center font-bold text-xl lg:text-3xl w-full glass" bind:value={joinCode}
/> class="input input-bordered input-primary uppercase tracking-widest placeholder-primary text-neutral text-center font-bold text-xl lg:text-3xl w-full glass"
<button />
onclick={() => joinRoom(joinCode)} <button
class="w-full btn btn-outline btn-neutral text-neutral hover:border-neutral hover:bg-transparent text-xl" onclick={() => joinRoom(joinCode)}
> class="w-full btn btn-outline btn-neutral text-neutral hover:border-neutral hover:bg-transparent text-xl"
Join Room >
</button> Join Room
</div> </button>
</div>
{:else}
<div class="space-x-2 flex flex-row justify-center items-center">
<button
class="w-full btn btn-outline btn-neutral text-neutral hover:border-neutral hover:bg-transparent text-xl"
onclick={leaveRoom}
>
Leave room
</button>
</div>
{/if}
</div> </div>
</div> </div>

View File

@@ -1,6 +1,6 @@
import { io, Socket } from "socket.io-client"; 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 type CellType = 'e' | 's' | 'h' | 'm'; // empty, ship, hit, miss
export class State { export class State {
@@ -29,15 +29,19 @@ export class State {
this.room = room; this.room = room;
this.users = users; this.users = users;
}); });
this.socket.on('upload', (_, callback) => { this.socket.on('upload', (_, callback) => {
if (this.phase == 'gameover') {
this.playerBoard.randomize();
this.opponentBoard = new Board(true);
this.phase = 'waiting';
}
callback(this.playerBoard.board); callback(this.playerBoard.board);
}); });
this.socket.on('turnover', (id) => { this.socket.on('turnover', (id) => {
this.turn = (id == this.socket.id) ? 1 : -1; this.turn = (id == this.socket.id) ? 1 : -1;
this.phase = this.turn ? 'selfturn' : 'otherturn'; 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 [i, j]: [number, number] = at;
const board = by == this.socket.id ? this.opponentBoard : this.playerBoard; const board = by == this.socket.id ? this.opponentBoard : this.playerBoard;
if (by == this.socket.id) { if (by == this.socket.id) {
@@ -70,13 +74,19 @@ export class State {
} }
} }
} }
if (game_over) {
this.phase = 'gameover';
}
}); });
this.socket.on('restore', ({ turn, player, opponent }: { turn: boolean, player: string[], opponent: string[] }) => { this.socket.on('restore', ({ turn, player, opponent, gameover }: { turn: boolean, player: string[], opponent: string[], gameover: boolean }) => {
this.turn = turn ? 1 : -1; this.turn = turn ? 1 : -1;
this.phase = this.turn ? 'selfturn' : 'otherturn'; this.phase = this.turn ? 'selfturn' : 'otherturn';
this.playerBoard.board = player.map((s) => s.split('').map(c => c as CellType)); 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.opponentBoard.board = opponent.map((s) => s.split('').map(c => c as CellType));
if (gameover) {
this.phase = 'gameover';
}
}) })
} }
@@ -94,13 +104,17 @@ export class State {
joinRoom(code: string) { joinRoom(code: string) {
code = code.toUpperCase(); 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); this.socket.emit('join', code);
} }
hasNotStarted() { hasNotStarted() {
return this.phase == 'placement' || this.phase == 'waiting'; return this.phase == 'placement' || this.phase == 'waiting';
} }
playAgain() {
this.joinRoom(this.room);
}
} }

View File

@@ -6,6 +6,11 @@
import { Users } from 'lucide-svelte'; import { Users } from 'lucide-svelte';
let gameState = new State(); let gameState = new State();
function leaveRoom() {
gameState.socket.emit('leave');
gameState = new State();
}
</script> </script>
<div class="min-h-screen bg-base-300 py-8 px-4 sm:px-6 lg:px-8"> <div class="min-h-screen bg-base-300 py-8 px-4 sm:px-6 lg:px-8">
@@ -38,13 +43,7 @@
<div class="font-mono font-bold">{gameState.users}</div> <div class="font-mono font-bold">{gameState.users}</div>
<Users /> <Users />
</div> </div>
<button <button class="btn btn-error text-xl" onclick={leaveRoom}>Leave</button>
class="btn btn-error text-xl"
onclick={() => {
gameState.socket.emit('leave');
gameState = new State();
}}>Leave</button
>
</div> </div>
{/if} {/if}
</div> </div>
@@ -67,12 +66,33 @@
board={gameState.opponentBoard} board={gameState.opponentBoard}
callback={(i, j) => gameState.attack(i, j)} callback={(i, j) => gameState.attack(i, j)}
/> />
{#if gameState.phase === 'gameover'}
<div
class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-50 pointer-events-none"
>
<div class="p-6 bg-base-300 rounded-xl text-center">
<h3 class="text-2xl font-semibold">Game Over</h3>
<p class="text-lg">
{gameState.turn >= 0 ? 'You win!' : 'You lose!'}
</p>
<button
class="btn btn-primary mt-4 pointer-events-auto"
onclick={() => gameState.playAgain()}
>
Play Again
</button>
<button class="btn btn-secondary mt-4 ml-4 pointer-events-auto" onclick={leaveRoom}>Leave</button>
</div>
</div>
{/if}
{#if gameState.hasNotStarted()} {#if gameState.hasNotStarted()}
<Join <Join
class="absolute top-[24px] left-[15px] w-[calc(100%-15px)] h-[calc(100%-24px)]" class="absolute top-[24px] left-[15px] w-[calc(100%-15px)] h-[calc(100%-24px)]"
roomCode={gameState.room} roomCode={gameState.room}
createRoom={() => gameState.createRoom()} createRoom={() => gameState.createRoom()}
joinRoom={(code) => gameState.joinRoom(code)} joinRoom={(code) => gameState.joinRoom(code)}
{leaveRoom}
/> />
{/if} {/if}
</div> </div>

View File

@@ -1,12 +1,10 @@
services: services:
server: server:
build: image: docker.registry.computerliebe.org/battleship
context: .
target: final
environment: environment:
DATABASE_URL: postgres://postgres:${DATABASE_PASSWORD}@db:5432/${DATABASE_NAME} DATABASE_URL: postgres://postgres:${DATABASE_PASSWORD}@db:5432/${DATABASE_NAME}
ports: # ports:
- 3000:3000 # - 3000:3000
depends_on: depends_on:
db: db:
condition: service_healthy condition: service_healthy
@@ -20,10 +18,10 @@ services:
environment: environment:
POSTGRES_DB: ${DATABASE_NAME} POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD} POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
ports: # ports:
- 5432:5432 # - 5432:5432
expose: # expose:
- 5432 # - 5432
healthcheck: healthcheck:
test: [ "CMD", "pg_isready" ] test: [ "CMD", "pg_isready" ]
interval: 10s interval: 10s

34
compose.yaml.template Normal file
View File

@@ -0,0 +1,34 @@
services:
server:
build:
context: .
target: final
environment:
DATABASE_URL: postgres://postgres:${DATABASE_PASSWORD}@db:5432/${DATABASE_NAME}
ports:
- 3000:3000
depends_on:
db:
condition: service_healthy
db:
image: postgres
restart: always
user: postgres
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
ports:
- 5432:5432
expose:
- 5432
healthcheck:
test: [ "CMD", "pg_isready" ]
interval: 10s
timeout: 5s
retries: 5
volumes:
db-data:

View File

@@ -1,5 +1,5 @@
-- DROP OWNED BY CURRENT_USER CASCADE; -- 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 ( CREATE TABLE IF NOT EXISTS players (
id CHAR(16) PRIMARY KEY, id CHAR(16) PRIMARY KEY,

View File

@@ -134,6 +134,10 @@ impl Board {
self self
} }
pub fn is_game_over(&self) -> bool {
!self.iter().any(|row| row.iter().any(|&cell| cell == 's'))
}
// fn validate_syntax(&self) -> bool { // fn validate_syntax(&self) -> bool {
// self // self
// .iter() // .iter()

View File

@@ -15,6 +15,8 @@ pub enum Error {
RoomFull(Option<String>), RoomFull(Option<String>),
#[error("Room not full")] #[error("Room not full")]
RoomNotFull, RoomNotFull,
#[error("GameOver room joined")]
GameOverRoom,
#[error("Already in room")] #[error("Already in room")]
AlreadyInRoom, AlreadyInRoom,
#[error("Not in room")] #[error("Not in room")]
@@ -33,6 +35,7 @@ pub enum Status {
Waiting, Waiting,
P1Turn, P1Turn,
P2Turn, P2Turn,
GameOver,
} }
pub async fn room_if_player_exists(sid: &str, pool: &sqlx::PgPool) -> Result<Option<String>> { pub async fn room_if_player_exists(sid: &str, pool: &sqlx::PgPool) -> Result<Option<String>> {
@@ -79,7 +82,7 @@ pub async fn add_room(sid: Sid, pool: &sqlx::PgPool) -> Result<String> {
pub async fn join_room(sid: Sid, code: String, pool: &sqlx::PgPool) -> Result<()> { pub async fn join_room(sid: Sid, code: String, pool: &sqlx::PgPool) -> Result<()> {
let code = code.to_uppercase(); let code = code.to_uppercase();
let room = sqlx::query!( let room = sqlx::query!(
r#"SELECT player1_id, player2_id FROM rooms WHERE code = $1"#, r#"SELECT player1_id, player2_id, stat AS "stat: Status" FROM rooms WHERE code = $1"#,
code code
) )
.fetch_one(pool) .fetch_one(pool)
@@ -87,26 +90,40 @@ pub async fn join_room(sid: Sid, code: String, pool: &sqlx::PgPool) -> Result<()
let sid = sid.as_str(); let sid = sid.as_str();
// if player is already in room
if [room.player1_id.as_ref(), room.player2_id.as_ref()]
.into_iter()
.flatten()
.any(|x| x == sid)
{
// if game was over, set status to waiting and return
if room.stat == Status::GameOver {
sqlx::query!(
r"UPDATE rooms SET stat = $1 WHERE code = $2",
Status::Waiting as Status,
code
)
.execute(pool)
.await?;
return Ok(());
}
return Err(Error::AlreadyInRoom);
}
if room.stat == Status::GameOver {
return Err(Error::GameOverRoom);
}
if let (Some(p1), Some(p2)) = (room.player1_id.as_ref(), room.player2_id.as_ref()) { if let (Some(p1), Some(p2)) = (room.player1_id.as_ref(), room.player2_id.as_ref()) {
if in_delete_sid(p1, pool).await? { if in_delete_sid(p1, pool).await? {
update_sid(p1, sid, pool).await?; // update_sid(p1, sid, pool).await?;
return Err(Error::RoomFull(Some(p1.to_string()))); return Err(Error::RoomFull(Some(p1.to_string())));
} else if in_delete_sid(p2, pool).await? { } else if in_delete_sid(p2, pool).await? {
update_sid(p2, sid, pool).await?; // update_sid(p2, sid, pool).await?;
return Err(Error::RoomFull(Some(p2.to_string()))); return Err(Error::RoomFull(Some(p2.to_string())));
} }
return Err(Error::RoomFull(None)); return Err(Error::RoomFull(None));
} }
if let Some(id) = room.player1_id.as_ref() {
if id == sid {
return Err(Error::AlreadyInRoom);
}
}
if let Some(id) = room.player2_id.as_ref() {
if id == sid {
return Err(Error::AlreadyInRoom);
}
}
delete_sid(sid, pool).await?; delete_sid(sid, pool).await?;
let mut txn = pool.begin().await?; let mut txn = pool.begin().await?;
@@ -158,7 +175,7 @@ pub async fn get_game_state(
sid: &str, sid: &str,
room: &str, room: &str,
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
) -> Result<(bool, Vec<String>, Vec<String>)> { ) -> Result<(bool, Vec<String>, Vec<String>, bool)> {
let room_details = sqlx::query!( let room_details = sqlx::query!(
r#"SELECT player1_id, player2_id, stat AS "stat: Status" FROM rooms WHERE code = $1"#, r#"SELECT player1_id, player2_id, stat AS "stat: Status" FROM rooms WHERE code = $1"#,
room room
@@ -188,7 +205,6 @@ pub async fn get_game_state(
.board .board
.unwrap() .unwrap()
.into(); .into();
let player_board: Vec<String> = player_board.mark_redundant().into();
let opponent_board: Board = sqlx::query!( let opponent_board: Board = sqlx::query!(
r#"SELECT board FROM players WHERE id = $1 AND room_code = $2"#, r#"SELECT board FROM players WHERE id = $1 AND room_code = $2"#,
@@ -200,6 +216,11 @@ pub async fn get_game_state(
.board .board
.unwrap() .unwrap()
.into(); .into();
let game_over = player_board.is_game_over() || opponent_board.is_game_over();
let player_board: Vec<String> = player_board.mark_redundant().into();
let opponent_board: Vec<String> = opponent_board.mark_redundant().into(); let opponent_board: Vec<String> = opponent_board.mark_redundant().into();
let opponent_board: Vec<String> = opponent_board let opponent_board: Vec<String> = opponent_board
.into_iter() .into_iter()
@@ -210,7 +231,7 @@ pub async fn get_game_state(
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok((turn, player_board, opponent_board)) Ok((turn, player_board, opponent_board, game_over))
} }
pub async fn start(sid: Sid, code: String, pool: &sqlx::PgPool) -> Result<()> { pub async fn start(sid: Sid, code: String, pool: &sqlx::PgPool) -> Result<()> {
@@ -247,7 +268,7 @@ pub async fn attack(
sid: Sid, sid: Sid,
(i, j): (usize, usize), (i, j): (usize, usize),
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
) -> Result<(bool, Option<[(usize, usize); 2]>)> { ) -> Result<(bool, Option<[(usize, usize); 2]>, bool)> {
let player = sqlx::query!(r"SELECT room_code FROM players WHERE id = $1", sid.as_str()) let player = sqlx::query!(r"SELECT room_code FROM players WHERE id = $1", sid.as_str())
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;
@@ -302,9 +323,23 @@ pub async fn attack(
.execute(&mut *txn) .execute(&mut *txn)
.await?; .await?;
} }
let game_over = board.is_game_over();
if game_over {
sqlx::query!(
r#"UPDATE rooms SET stat = $1 WHERE code = $2"#,
Status::GameOver as Status,
player.room_code
)
.execute(&mut *txn)
.await?;
}
txn.commit().await?; txn.commit().await?;
Ok((hit, if hit { board.has_sunk((i, j)) } else { None })) Ok((
hit,
if hit { board.has_sunk((i, j)) } else { None },
game_over,
))
} }
pub async fn update_sid(oldsid: &str, newsid: &str, pool: &sqlx::PgPool) -> Result<()> { pub async fn update_sid(oldsid: &str, newsid: &str, pool: &sqlx::PgPool) -> Result<()> {

View File

@@ -1,5 +1,6 @@
mod board; mod board;
mod game; mod game;
use axum::Router; use axum::Router;
use board::Board; use board::Board;
use dotenv::dotenv; use dotenv::dotenv;
@@ -16,7 +17,6 @@ use socketioxide::{
}; };
use sqlx::PgPool; use sqlx::PgPool;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tower_http::cors::CorsLayer;
use tracing_subscriber::FmtSubscriber; use tracing_subscriber::FmtSubscriber;
#[tokio::main] #[tokio::main]
@@ -34,9 +34,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (layer, io) = SocketIo::builder().with_state(pool).build_layer(); let (layer, io) = SocketIo::builder().with_state(pool).build_layer();
io.ns("/", on_connect); io.ns("/", on_connect);
let app = Router::new()
.layer(layer) let app = Router::new().layer(layer);
.layer(CorsLayer::very_permissive());
let listener = TcpListener::bind("0.0.0.0:3000").await?; let listener = TcpListener::bind("0.0.0.0:3000").await?;
println!("listening on {}", listener.local_addr()?); println!("listening on {}", listener.local_addr()?);
@@ -61,7 +60,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
socket socket
.emit( .emit(
"restore", "restore",
serde_json::json!({"turn": data.0, "player": data.1, "opponent": data.2}), serde_json::json!({"turn": data.0, "player": data.1, "opponent": data.2, "gameover": data.3}),
) )
.unwrap(); .unwrap();
socket.join(room.clone()).unwrap(); socket.join(room.clone()).unwrap();
@@ -119,7 +118,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
socket socket
.emit( .emit(
"restore", "restore",
serde_json::json!({"turn": data.0, "player": data.1, "opponent": data.2}), serde_json::json!({"turn": data.0, "player": data.1, "opponent": data.2, "gameover": data.3}),
) )
.unwrap(); .unwrap();
} else { } else {
@@ -171,7 +170,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
socket.on( socket.on(
"attack", "attack",
|socket: SocketRef, Data::<[usize; 2]>([i, j]), pool: State<PgPool>| async move { |socket: SocketRef, Data::<[usize; 2]>([i, j]), pool: State<PgPool>| async move {
let (hit, sunk) = match attack(socket.id, (i, j), &pool).await { let (hit, sunk, game_over) = match attack(socket.id, (i, j), &pool).await {
Ok(res) => res, Ok(res) => res,
Err(e) => { Err(e) => {
tracing::error!("{:?}", e); tracing::error!("{:?}", e);
@@ -183,7 +182,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
.within(socket.rooms().unwrap().first().unwrap().clone()) .within(socket.rooms().unwrap().first().unwrap().clone())
.emit( .emit(
"attacked", "attacked",
serde_json::json!({"by": socket.id.as_str(), "at": [i, j], "hit": hit, "sunk": sunk}), serde_json::json!({"by": socket.id.as_str(), "at": [i, j], "hit": hit, "sunk": sunk, "game_over": game_over}),
) )
.unwrap(); .unwrap();
}, },