Skip to content

Commit

Permalink
fix: parameters for pvp
Browse files Browse the repository at this point in the history
  • Loading branch information
bhoopesh369 committed Feb 4, 2024
1 parent 187a291 commit 7646c55
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;

use error::SimulatorError;
use log::error;
use response::{GameResult, GameStatusEnum, GameResultPvP};
use response::{GameResult, GameResultPvP, GameStatusEnum};
pub mod error;
pub mod fifo;
pub mod game_dir;
Expand Down
20 changes: 11 additions & 9 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Attacker {
pub speed: u32,
pub price: u32,
pub is_aerial: u32,
pub weight: u32
pub weight: u32,
}

#[derive(Deserialize, Debug, PartialEq)]
Expand All @@ -38,8 +38,8 @@ pub struct GameParameters {
pub struct PvPGameParameters {
pub attackers: Vec<Attacker>,
pub defenders: Vec<Defender>,
pub coins_per_turn: u32,
pub no_of_turns: u32,
pub no_of_coins: u32, // no of coins per turn
}

#[derive(Deserialize, Debug, PartialEq)]
Expand Down Expand Up @@ -120,7 +120,9 @@ mod tests {
use crate::request::PlayerCode;

// TODO: Test the pvp desearialization
use super::{Attacker, Defender, GameParameters, NormalGameRequest, PvPGameRequest,PvPGameParameters};
use super::{
Attacker, Defender, GameParameters, NormalGameRequest, PvPGameParameters, PvPGameRequest,
};
#[test]
pub fn deserealization_test() {
// An example request that we might get from backend for a normal game
Expand All @@ -138,7 +140,7 @@ mod tests {
speed: 3,
price: 1,
is_aerial: 0,
weight: 1
weight: 1,
},
Attacker {
id: 2,
Expand All @@ -148,7 +150,7 @@ mod tests {
speed: 3,
price: 1,
is_aerial: 1,
weight: 2
weight: 2,
},
],
defenders: vec![
Expand Down Expand Up @@ -183,7 +185,7 @@ mod tests {
assert_eq!(deserealized_example_request, expected_deserealized_struct);

// An example request that we might get from backend for a pvp game
let example_request_pvp_game = r#"{"game_id":"0fa0f12d-d472-42d5-94b4-011e0c916023","parameters":{"attackers":[{"id":1,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1,"is_aerial":0,"weight":1},{"id":2,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1,"is_aerial":1,"weight":2}],"defenders":[{"id":1,"hp":10,"range":4,"attack_power":5,"price":1,"is_aerial":1},{"id":2,"hp":10,"range":6,"attack_power":5,"price":1,"is_aerial":1}],"coins_per_turn":10,"no_of_turns":500,"no_of_coins":1000},"player1":{"source_code":"print(x)","language":"PYTHON"},"player2":{"source_code":"print(x)","language":"PYTHON"}}"#;
let example_request_pvp_game = r#"{"game_id":"0fa0f12d-d472-42d5-94b4-011e0c916023","parameters":{"attackers":[{"id":1,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1,"is_aerial":0,"weight":1},{"id":2,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1,"is_aerial":1,"weight":2}],"defenders":[{"id":1,"hp":10,"range":4,"attack_power":5,"price":1,"is_aerial":1},{"id":2,"hp":10,"range":6,"attack_power":5,"price":1,"is_aerial":1}],"no_of_turns":500,"no_of_coins":10},"player1":{"source_code":"print(x)","language":"PYTHON"},"player2":{"source_code":"print(x)","language":"PYTHON"}}"#;

let expected_deserealized_struct = PvPGameRequest {
game_id: "0fa0f12d-d472-42d5-94b4-011e0c916023".to_owned(),
Expand All @@ -197,7 +199,7 @@ mod tests {
speed: 3,
price: 1,
is_aerial: 0,
weight: 1
weight: 1,
},
Attacker {
id: 2,
Expand All @@ -207,7 +209,7 @@ mod tests {
speed: 3,
price: 1,
is_aerial: 1,
weight: 2
weight: 2,
},
],
defenders: vec![
Expand All @@ -229,7 +231,7 @@ mod tests {
},
],
no_of_turns: 500,
coins_per_turn: 10
no_of_coins: 10,
},
player1: PlayerCode {
language: super::Language::PYTHON,
Expand Down
47 changes: 28 additions & 19 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use fs_extra::dir::CopyOptions;
use crate::{
create_error_response, error,
game_dir::GameDir,
request::{Language, NormalGameRequest, PlayerCode, PvPGameRequest, Attacker, Defender},
request::{Attacker, Defender, Language, NormalGameRequest, PlayerCode, PvPGameRequest},
response::{self, GameStatus},
runner::GameType,
};
Expand All @@ -35,8 +35,8 @@ pub fn copy_dir_all(

pub fn send_troops<'a>(
mut writer: BufWriter<&'a File>,
attackers : &Vec<Attacker>,
defenders : &Vec<Defender>,
attackers: &Vec<Attacker>,
defenders: &Vec<Defender>,
) -> BufWriter<&'a File> {
writer
.write_all(format!("{}\n", attackers.len()).as_bytes())
Expand Down Expand Up @@ -85,31 +85,40 @@ pub fn send_initial_pvp_input(fifos: Vec<&File>, pvp_request: &PvPGameRequest) {
for fifo in fifos {
let mut writer = BufWriter::new(fifo);
writer
.write_all(
format!(
"{} {}\n",
pvp_request.parameters.no_of_turns, pvp_request.parameters.coins_per_turn
.write_all(
format!(
"{} {}\n",
pvp_request.parameters.no_of_turns, pvp_request.parameters.no_of_coins
)
.as_bytes(),
)
.as_bytes(),
)
.unwrap();
let _ = send_troops(writer, &pvp_request.parameters.attackers, &pvp_request.parameters.defenders);
.unwrap();
let _ = send_troops(
writer,
&pvp_request.parameters.attackers,
&pvp_request.parameters.defenders,
);
}
}

pub fn send_initial_input(fifos: Vec<&File>, normal_game_request: &NormalGameRequest) {
for fifo in fifos {
let mut writer = BufWriter::new(fifo);
writer
.write_all(
format!(
"{} {}\n",
normal_game_request.parameters.no_of_turns, normal_game_request.parameters.no_of_coins
.write_all(
format!(
"{} {}\n",
normal_game_request.parameters.no_of_turns,
normal_game_request.parameters.no_of_coins
)
.as_bytes(),
)
.as_bytes(),
)
.unwrap();
let mut writer = send_troops(writer, &normal_game_request.parameters.attackers, &normal_game_request.parameters.defenders);
.unwrap();
let mut writer = send_troops(
writer,
&normal_game_request.parameters.attackers,
&normal_game_request.parameters.defenders,
);
writer
.write_all(
format!(
Expand Down

0 comments on commit 7646c55

Please sign in to comment.