This commit is contained in:
sparshg
2024-09-23 22:43:01 +05:30
parent 64f2c27bfc
commit 027b875144
19 changed files with 392 additions and 12 deletions

View File

@@ -64,7 +64,7 @@ async fn generate_code(pool: &sqlx::PgPool) -> Result<String> {
pub async fn add_room(sid: Sid, pool: &sqlx::PgPool) -> Result<String> {
delete_sid(sid.as_str(), pool).await?;
let code = generate_code(&pool).await?;
let code = generate_code(pool).await?;
sqlx::query!(
r"WITH new_user AS (INSERT INTO players (id, room_code) VALUES ($1, $2) RETURNING id) INSERT INTO rooms (player1_id, code) SELECT $1, $2 FROM new_user",
@@ -88,11 +88,11 @@ pub async fn join_room(sid: Sid, code: String, pool: &sqlx::PgPool) -> Result<()
let sid = sid.as_str();
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?;
if in_delete_sid(p1, 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?;
} else if in_delete_sid(p2, pool).await? {
update_sid(p2, sid, pool).await?;
return Err(Error::RoomFull(Some(p2.to_string())));
}
return Err(Error::RoomFull(None));
@@ -205,7 +205,6 @@ pub async fn get_game_state(
.into_iter()
.map(|row| {
row.chars()
.into_iter()
.map(|x| if x == 's' { 'e' } else { x })
.collect()
})

View File

@@ -8,7 +8,6 @@ use game::{
add_board, add_room, attack, delete_sid, get_game_state, get_room, join_room,
room_if_player_exists, start, to_delete_sid, update_sid, Error, ROOM_CODE_LENGTH,
};
use rand::Rng;
use serde::Deserialize;
use socketioxide::{
@@ -58,8 +57,8 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
if let Some(sid) = auth.session {
update_sid(&sid, socket.id.as_str(), &pool).await.unwrap();
let sid = socket.id.as_str();
if let Some(room) = room_if_player_exists(&sid, &pool).await.unwrap() {
let data = get_game_state(&sid, &room, &pool).await.unwrap();
if let Some(room) = room_if_player_exists(sid, &pool).await.unwrap() {
let data = get_game_state(sid, &room, &pool).await.unwrap();
socket
.emit(
"restore",
@@ -116,7 +115,7 @@ async fn on_connect(socket: SocketRef, Data(auth): Data<AuthPayload>, pool: Stat
if let Err(e) = &room_error {
if let Error::RoomFull(Some(player)) = &e {
tracing::warn!("{:?}", e);
update_sid(&player, socket.id.as_str(), &pool).await.unwrap();
update_sid(player, socket.id.as_str(), &pool).await.unwrap();
let data = get_game_state(socket.id.as_str(), &room, &pool).await.unwrap();
socket
.emit(
@@ -220,9 +219,9 @@ async fn leave_and_inform(socket: &SocketRef, pool: &PgPool, delete: bool) {
emit_update_room(socket, &room.to_string(), ops.sockets().unwrap().len());
let sid = socket.id.as_str();
if let Err(e) = if delete {
delete_sid(sid, &pool).await
delete_sid(sid, pool).await
} else {
to_delete_sid(sid, &pool).await
to_delete_sid(sid, pool).await
} {
tracing::error!("{:?}", e);
}