Skip to content

Commit

Permalink
prod(aor24): fixes (#81)
Browse files Browse the repository at this point in the history
* fix: send mapspace id after inventory upgrade

* self destruction

* fix: seeder, mine and defender name sending

* fix: add game table debug print statements

* fix: send next block id after upgrade, add trophies to game table, change history and leaderboard response

* timeout game termination and defender fix ++

* fix: update attacks, defenses won, dummy data, random avatar

* fix: no bot in leaderboard

* fix: rip simulation log

* feat: avatar

* fix: fmt

---------

Co-authored-by: ashw1nr2023 <ashwin19rajagopal@gmail.com>
Co-authored-by: MegaaDev <akshay6.nitt@gmail.com>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent abe5e4d commit 5160d98
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 218 deletions.
92 changes: 36 additions & 56 deletions dummy_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ COPY public.map_layout FROM stdin;

COPY public.building_type FROM stdin;
0 Road 1 1 0 0 0 0
1 Bank 3 3 2147483647 1 0 120
2 Building_2 4 4 120 1 0 140
3 Building_3 5 5 140 1 0 160
4 Building_4 3 3 90 1 0 110
5 Building_5 4 4 110 1 0 130
6 Building_6 5 5 130 1 0 150
7 Building_7 3 3 80 1 0 100
8 Building_8 4 4 100 1 0 120
9 Building_9 5 5 120 1 0 140
10 Building_10 3 3 70 1 0 90
11 Building_11 4 4 90 1 0 110
12 Building_12 5 5 110 1 0 130
13 Building_13 3 3 60 1 0 80
14 Building_14 4 4 80 1 0 100
15 Building_15 5 5 100 1 0 120
1 Bank 3 3 2147483647 1 10 120
2 Building_2 4 4 120 1 10 140
3 Building_3 5 5 140 1 10 160
4 Building_4 3 3 90 1 10 110
5 Building_5 4 4 110 1 10 130
6 Building_6 5 5 130 1 10 150
7 Building_7 3 3 80 1 10 100
8 Building_8 4 4 100 1 10 120
9 Building_9 5 5 120 1 10 140
10 Building_10 3 3 70 1 10 90
11 Building_11 4 4 90 1 10 110
12 Building_12 5 5 110 1 10 130
13 Building_13 3 3 60 1 10 80
14 Building_14 4 4 80 1 10 100
15 Building_15 5 5 100 1 10 120
16 Bank 3 3 2147483647 2 50 140
17 Building_2 4 4 130 2 75 160
18 Building_3 5 5 150 2 100 180
Expand Down Expand Up @@ -112,14 +112,8 @@ COPY public.emp_type FROM stdin;

COPY public.mine_type FROM stdin;
1 5 50 1 10 Mine_1
2 6 60 1 10 Mine_2
3 7 70 1 10 Mine_3
4 6 70 2 120 Mine_1
5 7 80 2 180 Mine_2
6 8 90 2 240 Mine_3
7 7 90 3 -1 Mine_1
8 8 100 3 -1 Mine_2
9 9 110 3 -1 Mine_3
2 6 70 2 120 Mine_1
3 7 90 3 -1 Mine_1
\.

COPY public.attacker_type FROM stdin;
Expand Down Expand Up @@ -193,12 +187,6 @@ COPY public.block_type FROM stdin;
55 \N 1 mine 0
56 \N 2 mine 0
57 \N 3 mine 0
58 \N 4 mine 0
59 \N 5 mine 0
60 \N 6 mine 0
61 \N 7 mine 0
62 \N 8 mine 0
63 \N 9 mine 0
\.

COPY public.available_blocks FROM stdin;
Expand All @@ -222,14 +210,12 @@ COPY public.available_blocks FROM stdin;
47 1 \N \N block 17
48 1 \N \N block 18
55 1 \N \N block 19
56 1 \N \N block 20
57 1 \N \N block 21
\N 1 1 \N attacker 22
\N 1 2 \N attacker 23
\N 1 3 \N attacker 24
\N 1 \N 1 emp 25
\N 1 \N 2 emp 26
\N 1 \N 3 emp 27
\N 1 1 \N attacker 20
\N 1 2 \N attacker 21
\N 1 3 \N attacker 22
\N 1 \N 1 emp 23
\N 1 \N 2 emp 24
\N 1 \N 3 emp 25
\.

COPY public.map_spaces FROM stdin;
Expand Down Expand Up @@ -300,15 +286,15 @@ COPY public.map_spaces FROM stdin;
65 1 25 39 0
66 1 26 39 0
67 1 27 39 0
68 1 28 39 57
68 1 28 39 55
69 1 29 39 0
70 1 30 39 56
70 1 30 39 55
71 1 31 39 0
72 1 32 39 55
73 1 33 39 0
74 1 34 39 57
74 1 34 39 55
75 1 35 39 0
76 1 36 39 56
76 1 36 39 55
77 1 37 39 0
78 1 38 39 55
79 1 39 39 46
Expand Down Expand Up @@ -459,21 +445,15 @@ COPY public.level_constraints FROM stdin;
1 2 46
1 2 47
1 2 48
1 1 49
1 1 50
1 1 51
1 1 52
1 1 53
1 1 54
1 2 55
1 2 56
1 2 57
1 1 58
1 1 59
1 1 60
1 1 61
1 1 62
1 1 63
1 2 49
1 2 50
1 2 51
1 2 52
1 2 53
1 2 54
1 6 55
1 6 56
1 6 57
\.

SELECT pg_catalog.setval('public.user_id_seq', 2, false);
Expand Down
60 changes: 41 additions & 19 deletions src/api/attack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::defense::util::{
};
use super::user::util::fetch_user;
use super::{error, PgPool, RedisPool};
use crate::api::attack::socket::{ResultType, SocketRequest};
use crate::api::attack::socket::{ResultType, SocketRequest, SocketResponse};
use crate::api::util::HistoryboardQuery;
use crate::constants::{GAME_AGE_IN_MINUTES, MAX_BOMBS_PER_ATTACK};
use crate::models::{AttackerType, User};
Expand All @@ -17,6 +17,7 @@ use actix_rt;
use actix_web::error::ErrorBadRequest;
use actix_web::web::{Data, Json};
use actix_web::{web, Error, HttpRequest, HttpResponse, Responder, Result};
use log;
use std::collections::{HashMap, HashSet};
use std::time;

Expand Down Expand Up @@ -55,7 +56,7 @@ async fn init_attack(

//Check if attacker is already in a game
if let Ok(Some(_)) = util::get_game_id_from_redis(attacker_id, &mut redis_conn, true) {
println!("Attacker:{} has an ongoing game", attacker_id);
log::info!("Attacker:{} has an ongoing game", attacker_id);
return Err(ErrorBadRequest("Attacker has an ongoing game"));
}

Expand All @@ -64,7 +65,6 @@ async fn init_attack(
.get()
.map_err(|err| error::handle_error(err.into()))?;

//Generate random opponent id
let random_opponent_id = web::block(move || {
Ok(util::get_random_opponent_id(
attacker_id,
Expand Down Expand Up @@ -150,6 +150,7 @@ async fn init_attack(
game_id,
};

println!("Sent game id {} to frontend", game_id);
Ok(Json(response))
}

Expand All @@ -160,7 +161,6 @@ async fn socket_handler(
body: web::Payload,
) -> Result<HttpResponse, Error> {
println!("error bug fs");

let query_params = req.query_string().split('&').collect::<Vec<&str>>();
let user_token = query_params[0].split('=').collect::<Vec<&str>>()[1];
let attack_token = query_params[1].split('=').collect::<Vec<&str>>()[1];
Expand All @@ -171,6 +171,8 @@ async fn socket_handler(
util::decode_attack_token(attack_token).map_err(|err| error::handle_error(err.into()))?;
let game_id = attack_token_data.game_id;

println!("Received game id {} from frontend", game_id);

let mut conn = pool.get().map_err(|err| error::handle_error(err.into()))?;

if attacker_id != attack_token_data.attacker_id {
Expand Down Expand Up @@ -198,6 +200,7 @@ async fn socket_handler(
return Err(ErrorBadRequest("Defender has an ongoing game"));
}

println!("Checking if there are incomplte games");
if util::check_and_remove_incomplete_game(&attacker_id, &defender_id, &game_id, &mut conn)
.is_err()
{
Expand Down Expand Up @@ -233,7 +236,7 @@ async fn socket_handler(
.map_err(|err| error::handle_error(err.into()))?;

let mut conn = pool.get().map_err(|err| error::handle_error(err.into()))?;
let defenders = web::block(move || {
let defenders: Vec<DefenderDetails> = web::block(move || {
Ok(util::get_defenders(&mut conn, map_id, defender_id)?)
as anyhow::Result<Vec<DefenderDetails>>
})
Expand Down Expand Up @@ -269,7 +272,8 @@ async fn socket_handler(

let (response, session, mut msg_stream) = actix_ws::handle(&req, body)?;

let mut session_clone = session.clone();
let mut session_clone1 = session.clone();
let mut session_clone2 = session.clone();

let mut conn = pool.get().map_err(|err| error::handle_error(err.into()))?;
let attacker_type = web::block(move || {
Expand Down Expand Up @@ -316,6 +320,8 @@ async fn socket_handler(
return Err(ErrorBadRequest("Internal Server Error"));
}

println!("Added game:{} to redis", game_id);

let game_log = GameLog {
g: game_id,
a: attacker_user_details.unwrap(),
Expand Down Expand Up @@ -359,7 +365,7 @@ async fn socket_handler(
while let Some(Ok(msg)) = msg_stream.next().await {
match msg {
Message::Ping(bytes) => {
if session_clone.pong(&bytes).await.is_err() {
if session_clone1.pong(&bytes).await.is_err() {
return;
}
}
Expand All @@ -380,10 +386,10 @@ async fn socket_handler(
// println!("Response Json ---- {}", response_json);
if response.result_type == ResultType::GameOver {
println!("Game over. Terminating the socket...");
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
if (session_clone.clone().close(None).await).is_err() {
if (session_clone1.clone().close(None).await).is_err() {
println!("Error closing the socket connection");
}
if util::terminate_game(
Expand All @@ -397,18 +403,18 @@ async fn socket_handler(
}
} else if response.result_type == ResultType::MinesExploded {
println!("MinesExploded response sent");
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
} else if response.result_type == ResultType::DefendersDamaged {
println!("DefendersDamaged response sent");
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
} else if response.result_type == ResultType::DefendersTriggered
{
println!("DefendersTriggered response sent");
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
} else if response.result_type == ResultType::BuildingsDamaged {
Expand All @@ -421,23 +427,24 @@ async fn socket_handler(
{
println!("Failed to deduct artifacts from building");
}
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
} else if response.result_type == ResultType::PlacedAttacker {
println!("PlacedAttacker response sent");
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
} else if response.result_type == ResultType::Nothing {
// println!("Nothing response sent");
if session_clone.text(response_json).await.is_err() {
if session_clone1.text(response_json).await.is_err() {
return;
}
}
} else {
println!("Error serializing JSON");
if session_clone.text("Error serializing JSON").await.is_err() {
if session_clone1.text("Error serializing JSON").await.is_err()
{
return;
}
}
Expand All @@ -453,7 +460,7 @@ async fn socket_handler(
}
} else {
println!("Error parsing JSON");
if session_clone.text("Error parsing JSON").await.is_err() {
if session_clone1.text("Error parsing JSON").await.is_err() {
return;
}
}
Expand All @@ -479,11 +486,26 @@ async fn socket_handler(
actix_rt::time::sleep(time::Duration::from_secs(1)).await;

if time::Instant::now() - last_activity > timeout_duration {
if (session.close(None).await).is_err() {
println!("Can't close socket connection");
let response_json = serde_json::to_string(&SocketResponse {
frame_number: 0,
result_type: ResultType::GameOver,
is_alive: None,
attacker_health: None,
exploded_mines: None,
defender_damaged: None,
damaged_buildings: None,
total_damage_percentage: None,
is_sync: false,
is_game_over: true,
message: Some("Connection timed out".to_string()),
})
.unwrap();
if session_clone2.text(response_json).await.is_err() {
return;
}

println!("Connection timed out");

break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/api/attack/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct SocketResponse {
// pub triggered_defenders: Option<Vec<DefenderResponse>>,
pub defender_damaged: Option<Vec<DefenderResponse>>,
pub damaged_buildings: Option<Vec<BuildingResponse>>,
pub artifacts_gained_total: Option<i32>,
pub total_damage_percentage: Option<f32>,
pub is_sync: bool,
// pub state: Option<GameStateResponse>,
pub is_game_over: bool,
Expand All @@ -43,6 +43,7 @@ pub enum ActionType {
PlaceBombs,
Idle,
Terminate,
SelfDestruct,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
Expand Down
Loading

0 comments on commit 5160d98

Please sign in to comment.