Skip to content

Commit 7646c55

Browse files
committed
fix: parameters for pvp
1 parent 187a291 commit 7646c55

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33

44
use error::SimulatorError;
55
use log::error;
6-
use response::{GameResult, GameStatusEnum, GameResultPvP};
6+
use response::{GameResult, GameResultPvP, GameStatusEnum};
77
pub mod error;
88
pub mod fifo;
99
pub mod game_dir;

src/request.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct Attacker {
1313
pub speed: u32,
1414
pub price: u32,
1515
pub is_aerial: u32,
16-
pub weight: u32
16+
pub weight: u32,
1717
}
1818

1919
#[derive(Deserialize, Debug, PartialEq)]
@@ -38,8 +38,8 @@ pub struct GameParameters {
3838
pub struct PvPGameParameters {
3939
pub attackers: Vec<Attacker>,
4040
pub defenders: Vec<Defender>,
41-
pub coins_per_turn: u32,
4241
pub no_of_turns: u32,
42+
pub no_of_coins: u32, // no of coins per turn
4343
}
4444

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

122122
// TODO: Test the pvp desearialization
123-
use super::{Attacker, Defender, GameParameters, NormalGameRequest, PvPGameRequest,PvPGameParameters};
123+
use super::{
124+
Attacker, Defender, GameParameters, NormalGameRequest, PvPGameParameters, PvPGameRequest,
125+
};
124126
#[test]
125127
pub fn deserealization_test() {
126128
// An example request that we might get from backend for a normal game
@@ -138,7 +140,7 @@ mod tests {
138140
speed: 3,
139141
price: 1,
140142
is_aerial: 0,
141-
weight: 1
143+
weight: 1,
142144
},
143145
Attacker {
144146
id: 2,
@@ -148,7 +150,7 @@ mod tests {
148150
speed: 3,
149151
price: 1,
150152
is_aerial: 1,
151-
weight: 2
153+
weight: 2,
152154
},
153155
],
154156
defenders: vec![
@@ -183,7 +185,7 @@ mod tests {
183185
assert_eq!(deserealized_example_request, expected_deserealized_struct);
184186

185187
// An example request that we might get from backend for a pvp game
186-
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"}}"#;
188+
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"}}"#;
187189

188190
let expected_deserealized_struct = PvPGameRequest {
189191
game_id: "0fa0f12d-d472-42d5-94b4-011e0c916023".to_owned(),
@@ -197,7 +199,7 @@ mod tests {
197199
speed: 3,
198200
price: 1,
199201
is_aerial: 0,
200-
weight: 1
202+
weight: 1,
201203
},
202204
Attacker {
203205
id: 2,
@@ -207,7 +209,7 @@ mod tests {
207209
speed: 3,
208210
price: 1,
209211
is_aerial: 1,
210-
weight: 2
212+
weight: 2,
211213
},
212214
],
213215
defenders: vec![
@@ -229,7 +231,7 @@ mod tests {
229231
},
230232
],
231233
no_of_turns: 500,
232-
coins_per_turn: 10
234+
no_of_coins: 10,
233235
},
234236
player1: PlayerCode {
235237
language: super::Language::PYTHON,

src/utils.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use fs_extra::dir::CopyOptions;
99
use crate::{
1010
create_error_response, error,
1111
game_dir::GameDir,
12-
request::{Language, NormalGameRequest, PlayerCode, PvPGameRequest, Attacker, Defender},
12+
request::{Attacker, Defender, Language, NormalGameRequest, PlayerCode, PvPGameRequest},
1313
response::{self, GameStatus},
1414
runner::GameType,
1515
};
@@ -35,8 +35,8 @@ pub fn copy_dir_all(
3535

3636
pub fn send_troops<'a>(
3737
mut writer: BufWriter<&'a File>,
38-
attackers : &Vec<Attacker>,
39-
defenders : &Vec<Defender>,
38+
attackers: &Vec<Attacker>,
39+
defenders: &Vec<Defender>,
4040
) -> BufWriter<&'a File> {
4141
writer
4242
.write_all(format!("{}\n", attackers.len()).as_bytes())
@@ -85,31 +85,40 @@ pub fn send_initial_pvp_input(fifos: Vec<&File>, pvp_request: &PvPGameRequest) {
8585
for fifo in fifos {
8686
let mut writer = BufWriter::new(fifo);
8787
writer
88-
.write_all(
89-
format!(
90-
"{} {}\n",
91-
pvp_request.parameters.no_of_turns, pvp_request.parameters.coins_per_turn
88+
.write_all(
89+
format!(
90+
"{} {}\n",
91+
pvp_request.parameters.no_of_turns, pvp_request.parameters.no_of_coins
92+
)
93+
.as_bytes(),
9294
)
93-
.as_bytes(),
94-
)
95-
.unwrap();
96-
let _ = send_troops(writer, &pvp_request.parameters.attackers, &pvp_request.parameters.defenders);
95+
.unwrap();
96+
let _ = send_troops(
97+
writer,
98+
&pvp_request.parameters.attackers,
99+
&pvp_request.parameters.defenders,
100+
);
97101
}
98102
}
99103

100104
pub fn send_initial_input(fifos: Vec<&File>, normal_game_request: &NormalGameRequest) {
101105
for fifo in fifos {
102106
let mut writer = BufWriter::new(fifo);
103107
writer
104-
.write_all(
105-
format!(
106-
"{} {}\n",
107-
normal_game_request.parameters.no_of_turns, normal_game_request.parameters.no_of_coins
108+
.write_all(
109+
format!(
110+
"{} {}\n",
111+
normal_game_request.parameters.no_of_turns,
112+
normal_game_request.parameters.no_of_coins
113+
)
114+
.as_bytes(),
108115
)
109-
.as_bytes(),
110-
)
111-
.unwrap();
112-
let mut writer = send_troops(writer, &normal_game_request.parameters.attackers, &normal_game_request.parameters.defenders);
116+
.unwrap();
117+
let mut writer = send_troops(
118+
writer,
119+
&normal_game_request.parameters.attackers,
120+
&normal_game_request.parameters.defenders,
121+
);
113122
writer
114123
.write_all(
115124
format!(

0 commit comments

Comments
 (0)