Skip to content

Commit 65b9e35

Browse files
committed
fix: self attack game table row creation
1 parent 2c2ae4d commit 65b9e35

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/api/attack/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn init_attack(
4646
is_self: web::Query<HashMap<String, bool>>,
4747
) -> Result<impl Responder> {
4848
let attacker_id = user.0;
49-
49+
let is_self_attack = *is_self.get("is_self").unwrap_or(&false);
5050
log::info!("Attacker:{} is trying to initiate an attack", attacker_id);
5151
let mut conn = pool.get().map_err(|err| error::handle_error(err.into()))?;
5252
if let Ok(check) = util::can_attack_happen(&mut conn, attacker_id, true) {
@@ -73,7 +73,7 @@ async fn init_attack(
7373
.map_err(|err| error::handle_error(err.into()))?;
7474

7575
let opponent_id: i32;
76-
if *is_self.get("is_self").unwrap_or(&false) {
76+
if is_self_attack {
7777
opponent_id = attacker_id;
7878
} else {
7979
let random_opponent_id = web::block(move || {
@@ -142,10 +142,10 @@ async fn init_attack(
142142

143143
log::info!("User details fetched for Opponent:{}", opponent_id);
144144

145-
//Create game
145+
//Create a new game
146146
let mut conn = pool.get().map_err(|err| error::handle_error(err.into()))?;
147147
let game_id = web::block(move || {
148-
Ok(util::add_game(attacker_id, opponent_id, map_id, &mut conn)?) as anyhow::Result<i32>
148+
Ok(util::add_game(attacker_id, opponent_id, map_id, &mut conn, is_self_attack)?) as anyhow::Result<i32>
149149
})
150150
.await?
151151
.map_err(|err| error::handle_error(err.into()))?;
@@ -244,7 +244,7 @@ async fn socket_handler(
244244
} else {
245245
if attacker_id != defender_id {
246246
log::info!("Attacker:{} is trying to attack someone else", attacker_id);
247-
return Err(ErrorBadRequest("Can't attack yourself"));
247+
return Err(ErrorBadRequest("Can't attack someone else"));
248248
}
249249
}
250250

src/api/attack/util.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub fn add_game(
143143
defender_id: i32,
144144
map_layout_id: i32,
145145
conn: &mut PgConnection,
146+
is_self_attack: bool,
146147
) -> Result<i32> {
147148
use crate::schema::game;
148149

@@ -160,17 +161,20 @@ pub fn add_game(
160161
is_game_over: &false,
161162
date: &chrono::Local::now().date_naive(),
162163
};
163-
164-
let inserted_game: Game = diesel::insert_into(game::table)
164+
let inserted_game: Game;
165+
if !is_self_attack {
166+
inserted_game = diesel::insert_into(game::table)
165167
.values(&new_game)
166168
.get_result(conn)
167169
.map_err(|err| DieselError {
168170
table: "game",
169171
function: function!(),
170172
error: err,
171173
})?;
172-
173-
Ok(inserted_game.id)
174+
Ok(inserted_game.id)
175+
} else {
176+
Ok(-1)
177+
}
174178
}
175179

176180
pub fn fetch_attack_history(

0 commit comments

Comments
 (0)