Skip to content

Commit

Permalink
fix: negaitive artifact bug (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsKhadeer authored Feb 24, 2024
1 parent 26d2cdb commit 444119b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
32 changes: 22 additions & 10 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, SocketResponse};
use crate::api::attack::socket::{BuildingResponse, 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 Down Expand Up @@ -357,6 +357,8 @@ async fn socket_handler(
return Err(ErrorBadRequest("Internal Server Error"));
}

let mut damaged_buildings: Vec<BuildingResponse> = Vec::new();

let game_log = GameLog {
g: game_id,
a: attacker_user_details.unwrap(),
Expand Down Expand Up @@ -455,6 +457,7 @@ async fn socket_handler(
if util::terminate_game(
game_logs,
&mut conn,
&damaged_buildings,
&mut redis_conn,
)
.is_err()
Expand All @@ -475,14 +478,16 @@ async fn socket_handler(
return;
}
} else if response.result_type == ResultType::BuildingsDamaged {
if util::deduct_artifacts_from_building(
response.damaged_buildings.unwrap(),
&mut conn,
)
.is_err()
{
log::info!("Failed to deduct artifacts from building for game:{} and attacker:{} and opponent:{}", game_id, attacker_id, defender_id);
}
damaged_buildings
.extend(response.damaged_buildings.unwrap());
// if util::deduct_artifacts_from_building(
// response.damaged_buildings.unwrap(),
// &mut conn,
// )
// .is_err()
// {
// log::info!("Failed to deduct artifacts from building for game:{} and attacker:{} and opponent:{}", game_id, attacker_id, defender_id);
// }
if session_clone1.text(response_json).await.is_err() {
return;
}
Expand Down Expand Up @@ -526,7 +531,14 @@ async fn socket_handler(
}
}
Message::Close(_s) => {
if util::terminate_game(game_logs, &mut conn, &mut redis_conn).is_err() {
if util::terminate_game(
game_logs,
&mut conn,
&damaged_buildings,
&mut redis_conn,
)
.is_err()
{
log::info!("Error terminating the game 2 for game:{} and attacker:{} and opponent:{}", game_id, attacker_id, defender_id);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/api/attack/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct DefenderResponse {
pub damage: i32,
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct BuildingResponse {
pub id: i32,
pub position: Coords,
Expand Down
10 changes: 9 additions & 1 deletion src/api/attack/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ pub fn update_buidling_artifacts(
pub fn terminate_game(
game_log: &mut GameLog,
conn: &mut PgConnection,
damaged_buildings: &[BuildingResponse],
redis_conn: &mut RedisConn,
) -> Result<()> {
use crate::schema::{artifact, game};
Expand All @@ -702,7 +703,6 @@ pub fn terminate_game(
let bombs_used = game_log.r.b;
let artifacts_collected = game_log.r.a;
let game_id = game_log.g;

log::info!(
"Terminating game for game:{} and attacker:{} and opponent:{}",
game_id,
Expand Down Expand Up @@ -786,6 +786,14 @@ pub fn terminate_game(
error: err,
})?;

if deduct_artifacts_from_building(damaged_buildings.to_vec(), conn).is_err() {
log::info!(
"Failed to deduct artifacts from building for game:{} and attacker:{} and opponent:{}",
game_id,
attacker_id,
defender_id
);
}
diesel::update(user::table.find(&game_log.d.id))
.set((
user::artifacts.eq(user::artifacts - artifacts_collected),
Expand Down

0 comments on commit 444119b

Please sign in to comment.