Skip to content

Commit

Permalink
Battle contract: Health fix for Event::RoundAction (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
MedovTimur authored Oct 28, 2024
1 parent b5eb3bd commit 0ab20a6
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions contracts/battle/app/src/services/game/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub fn automatic_move(
};
pair.action = None;
let current_round = pair.round;
if let Some(battle_result) = round_result {
let (player_1_health, player_2_health) = if let Some(battle_result) = round_result {
match battle_result {
BattleResult::PlayerWin(winner) => {
let loser = pair.get_opponent(&winner);
Expand All @@ -493,6 +493,12 @@ pub fn automatic_move(
.participants
.get_mut(&winner)
.expect("The player must exist");

let healths = if player_1.owner == winner {
(player_winner.player_settings.health, 0)
} else {
(0, player_winner.player_settings.health)
};
player_winner.player_settings.health = storage.config.health;
player_winner.reflect_reload = 0;
player_winner.ultimate_reload = 0;
Expand All @@ -502,6 +508,7 @@ pub fn automatic_move(
battle.players_to_pairs.remove(&winner);
battle.players_to_pairs.remove(&loser);
battle.check_end_game();
healths
}
BattleResult::Draw(id_1, id_2) => {
let player_1 = battle
Expand All @@ -523,6 +530,7 @@ pub fn automatic_move(
battle.players_to_pairs.remove(&id_1);
battle.players_to_pairs.remove(&id_2);
battle.check_draw_end_game();
(0, 0)
}
}
} else {
Expand All @@ -534,16 +542,16 @@ pub fn automatic_move(
pair.round,
storage.config.time_for_move_in_blocks,
);
}
(
player_1.player_settings.health,
player_2.player_settings.health,
)
};

return Ok(Event::RoundAction {
round: current_round,
player_1: (
opponent_info.0,
opponent_info.1,
player_1.player_settings.health,
),
player_2: (player_id, Move::Attack, player_2.player_settings.health),
player_1: (opponent_info.0, opponent_info.1, player_1_health),
player_2: (player_id, Move::Attack, player_2_health),
});
} else {
pair.action = Some((player_id, Move::Attack));
Expand Down Expand Up @@ -638,7 +646,7 @@ pub fn make_move(storage: &mut Storage, warrior_move: Move) -> Result<Event, Bat
};
pair.action = None;
let current_round = pair.round;
if let Some(battle_result) = round_result {
let (player_1_health, player_2_health) = if let Some(battle_result) = round_result {
match battle_result {
BattleResult::PlayerWin(winner) => {
let loser = pair.get_opponent(&winner);
Expand All @@ -651,6 +659,11 @@ pub fn make_move(storage: &mut Storage, warrior_move: Move) -> Result<Event, Bat
.participants
.get_mut(&winner)
.expect("The player must exist");
let healths = if player_1.owner == winner {
(player_winner.player_settings.health, 0)
} else {
(0, player_winner.player_settings.health)
};
player_winner.player_settings.health = storage.config.health;
player_winner.reflect_reload = 0;
player_winner.ultimate_reload = 0;
Expand All @@ -659,6 +672,7 @@ pub fn make_move(storage: &mut Storage, warrior_move: Move) -> Result<Event, Bat
battle.players_to_pairs.remove(&winner);
battle.players_to_pairs.remove(&loser);
battle.check_end_game();
healths
}
BattleResult::Draw(id_1, id_2) => {
let player_1 = battle
Expand All @@ -680,20 +694,21 @@ pub fn make_move(storage: &mut Storage, warrior_move: Move) -> Result<Event, Bat
battle.players_to_pairs.remove(&id_1);
battle.players_to_pairs.remove(&id_2);
battle.check_draw_end_game();
(0, 0)
}
}
} else {
pair.round += 1;
pair.round_start_time = exec::block_timestamp();
}
(
player_1.player_settings.health,
player_2.player_settings.health,
)
};
Ok(Event::RoundAction {
round: current_round,
player_1: (
opponent_info.0,
opponent_info.1,
player_1.player_settings.health,
),
player_2: (player, warrior_move, player_2.player_settings.health),
player_1: (opponent_info.0, opponent_info.1, player_1_health),
player_2: (player, warrior_move, player_2_health),
})
} else {
pair.action = Some((player, warrior_move));
Expand Down

0 comments on commit 0ab20a6

Please sign in to comment.