fix game restart

This commit is contained in:
sparshg
2024-10-06 00:46:53 +05:30
committed by Sparsh Goenka
parent cd1a156d3e
commit 5fee374ac9
5 changed files with 54 additions and 35 deletions

View File

@@ -10,11 +10,8 @@ export class State {
users = $state(0); users = $state(0);
room = $state(''); room = $state('');
turn = $state(-1); // -1 not my turn, 0 might be, 1 is turn = $state(-1); // -1 not my turn, 0 might be, 1 is
game_over = $state(false);
socket: Socket; socket: Socket;
play_again_phase = $state(false);
constructor() { constructor() {
const url = import.meta.env.DEV ? 'ws://localhost:3000' : 'wss://battleship.icyground-d91964e0.centralindia.azurecontainerapps.io'; const url = import.meta.env.DEV ? 'ws://localhost:3000' : 'wss://battleship.icyground-d91964e0.centralindia.azurecontainerapps.io';
this.socket = io(url, { this.socket = io(url, {
@@ -32,8 +29,12 @@ 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) => {
@@ -73,18 +74,19 @@ export class State {
} }
} }
} }
if (game_over) {
this.game_over = 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.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.game_over = game_over; this.phase = 'gameover';
}
}) })
} }
@@ -102,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

@@ -67,7 +67,7 @@
callback={(i, j) => gameState.attack(i, j)} callback={(i, j) => gameState.attack(i, j)}
/> />
{#if gameState.game_over} {#if gameState.phase === 'gameover'}
<div <div
class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-50 pointer-events-none" class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-50 pointer-events-none"
> >
@@ -78,11 +78,7 @@
</p> </p>
<button <button
class="btn btn-primary mt-4 pointer-events-auto" class="btn btn-primary mt-4 pointer-events-auto"
onclick={() => { onclick={() => gameState.playAgain()}
let room = gameState.room;
leaveRoom();
gameState.joinRoom(room);
}}
> >
Play Again Play Again
</button> </button>

View File

@@ -135,8 +135,7 @@ impl Board {
} }
pub fn is_game_over(&self) -> bool { pub fn is_game_over(&self) -> bool {
self.iter() !self.iter().any(|row| row.iter().any(|&cell| cell == 's'))
.all(|row| row.iter().all(|cell| matches!(cell, 'e' | 'm' | 'h')))
} }
// fn validate_syntax(&self) -> bool { // fn validate_syntax(&self) -> bool {

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,7 +35,7 @@ pub enum Status {
Waiting, Waiting,
P1Turn, P1Turn,
P2Turn, P2Turn,
GameOver 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>> {
@@ -80,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)
@@ -88,26 +90,42 @@ 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()
.filter(|&x| x == sid)
.next()
.is_some()
{
// 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?;

View File

@@ -60,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, "game_over": data.3}), 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();
@@ -118,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 {