fix game restart
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -67,7 +67,7 @@
|
||||
callback={(i, j) => gameState.attack(i, j)}
|
||||
/>
|
||||
|
||||
{#if gameState.game_over}
|
||||
{#if gameState.phase === 'gameover'}
|
||||
<div
|
||||
class="absolute inset-0 flex items-center justify-center bg-black bg-opacity-50 pointer-events-none"
|
||||
>
|
||||
@@ -78,11 +78,7 @@
|
||||
</p>
|
||||
<button
|
||||
class="btn btn-primary mt-4 pointer-events-auto"
|
||||
onclick={() => {
|
||||
let room = gameState.room;
|
||||
leaveRoom();
|
||||
gameState.joinRoom(room);
|
||||
}}
|
||||
onclick={() => gameState.playAgain()}
|
||||
>
|
||||
Play Again
|
||||
</button>
|
||||
|
@@ -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 {
|
||||
|
46
src/game.rs
46
src/game.rs
@@ -15,6 +15,8 @@ pub enum Error {
|
||||
RoomFull(Option<String>),
|
||||
#[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<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<()> {
|
||||
let code = code.to_uppercase();
|
||||
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
|
||||
)
|
||||
.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();
|
||||
|
||||
// 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 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())));
|
||||
} 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(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?;
|
||||
let mut txn = pool.begin().await?;
|
||||
|
||||
|
@@ -60,7 +60,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
|
||||
socket
|
||||
.emit(
|
||||
"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();
|
||||
socket.join(room.clone()).unwrap();
|
||||
@@ -118,7 +118,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
|
||||
socket
|
||||
.emit(
|
||||
"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();
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user