Skip to content

Commit e9bfe5d

Browse files
shubham-1806bhoopesh369
authored andcommitted
wip:pvp
1 parent c0dae18 commit e9bfe5d

14 files changed

+445
-309
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ serde = { version = "1.0", features = ["derive"] }
1818
serde_json = "1.0"
1919
crossbeam-channel = "0.5.9"
2020
fs_extra = "1.3.0"
21+
walkdir = "2.3.2"

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM ubuntu
2+
ENTRYPOINT ["bash"]

src/game_dir.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ pub struct GameDir {
77

88
impl GameDir {
99
pub fn new(game_id: &str) -> Option<Self> {
10-
std::fs::create_dir(format!("/tmp/{game_id}")).ok()?;
10+
let err = std::fs::create_dir(format!("/tmp/{game_id}"));
11+
if let Err(e) = err {
12+
info!("Failed to create dir /tmp/{game_id}: {e}");
13+
return None;
14+
}
1115
info!("Created dir /tmp/{game_id}");
1216
Some(GameDir {
1317
full_path: format!("/tmp/{game_id}"),
1418
})
1519
}
20+
21+
pub fn create_sub_dir(&self, sub_dir: &str) -> Option<()> {
22+
std::fs::create_dir_all(format!("{}/{}", self.full_path, sub_dir)).ok()
23+
}
24+
1625
pub fn get_path(&self) -> &str {
1726
&self.full_path
1827
}

src/handlers.rs

+68-35
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
request::{GameRequest, Language, NormalGameRequest, PlayerCode, PvPGameRequest},
1616
response::GameStatus,
1717
runner::{cpp, java, py, simulator, GameType, Runnable},
18-
utils::{copy_files, send_initial_input, send_initial_pvp_input},
18+
utils::{copy_files, send_initial_input, send_initial_pvp_input}, create_final_pvp_response,
1919
};
2020

2121
pub trait Handler {
@@ -48,6 +48,7 @@ fn handle_event(
4848
match entry {
4949
EpollEntryType::StdErr(_) => unreachable!(),
5050
EpollEntryType::Process(mut p) => {
51+
info!("For process {}", p.get_process().id());
5152
let exit_status = p.wait()?;
5253
println!("died: {}", p.get_process().id());
5354

@@ -68,7 +69,6 @@ fn handle_event(
6869
EpollEntryType::StdErr(_) => unreachable!(),
6970
}
7071
});
71-
println!("exit code after sim: {}", exit_status);
7272
return Err(match exit_status.code() {
7373
// 137 => Stands for container killing itself (by SIGKILL)
7474
// that will be due to contraint provided
@@ -96,23 +96,23 @@ fn get_runner(
9696
player_code: &PlayerCode,
9797
game_id: &String,
9898
game_dir_handle: &GameDir,
99-
file_name: &String,
99+
player_dir: &String,
100100
) -> Box<dyn Runnable> {
101101
match player_code.language {
102102
Language::CPP => Box::new(cpp::Runner::new(
103103
game_dir_handle.get_path().to_string(),
104104
game_id.to_string(),
105-
file_name.to_owned(),
105+
player_dir.to_owned(),
106106
)),
107107
Language::PYTHON => Box::new(py::Runner::new(
108108
game_dir_handle.get_path().to_string(),
109109
game_id.to_string(),
110-
file_name.to_owned(),
110+
player_dir.to_owned(),
111111
)),
112112
Language::JAVA => Box::new(java::Runner::new(
113113
game_dir_handle.get_path().to_string(),
114114
game_id.to_string(),
115-
file_name.to_owned(),
115+
player_dir.to_owned(),
116116
)),
117117
}
118118
}
@@ -133,15 +133,14 @@ impl Handler for NormalGameRequest {
133133
}
134134

135135
let game_dir_handle = game_dir_handle.unwrap();
136-
let game_type_dir = "normal_game".to_string();
137-
let file_name = "run".to_string();
136+
let player_dir = "player".to_string();
138137

139138
if let Some(resp) = copy_files(
140139
&self.game_id,
141140
&self.player_code,
142141
&game_dir_handle,
143-
&game_type_dir,
144-
&file_name,
142+
&player_dir,
143+
&GameType::NormalGame,
145144
) {
146145
return resp;
147146
}
@@ -163,7 +162,7 @@ impl Handler for NormalGameRequest {
163162
&self.player_code,
164163
&self.game_id,
165164
&game_dir_handle,
166-
&file_name,
165+
&player_dir,
167166
);
168167

169168
let initialize = || -> Result<_, SimulatorError> {
@@ -225,8 +224,14 @@ impl Handler for NormalGameRequest {
225224
let process2 = outputs.remove(0);
226225

227226
let (player_process_out, sim_process_out) = match process1.process_type() {
228-
ProcessType::Runner => (process1.output(), process2.output()),
227+
ProcessType::Runner => (process1.output(), process2.output()),
229228
ProcessType::Simulator => (process2.output(), process1.output()),
229+
_ => {
230+
return create_error_response(
231+
self.game_id.to_owned(),
232+
SimulatorError::UnidentifiedError("Failed to map outputs".to_owned()),
233+
);
234+
}
230235
};
231236

232237
info!("Successfully executed for game {}", self.game_id);
@@ -249,10 +254,8 @@ impl Handler for PvPGameRequest {
249254
"Starting pvp game execution for {} with languages player1: {:?} and player2: {:?}",
250255
self.game_id, self.player1.language, self.player2.language
251256
);
252-
info!("Initial Parameters.coins: {}", self.parameters.no_of_coins);
253257
let game_dir_handle = GameDir::new(&self.game_id);
254258

255-
info!("Game dir handle: {:?}", game_dir_handle);
256259

257260
if game_dir_handle.is_none() {
258261
return create_error_response(
@@ -262,16 +265,18 @@ impl Handler for PvPGameRequest {
262265
}
263266

264267
let game_dir_handle = game_dir_handle.unwrap();
265-
let player1_game_type_dir = "pvp_game/player_1".to_string();
266-
let player2_game_type_dir = "pvp_game/player_2".to_string();
267-
let player_file_name = "run".to_string();
268+
let player1_dir = "pvp_game/player_1";
269+
let player2_dir = "pvp_game/player_2";
270+
271+
game_dir_handle.create_sub_dir(player1_dir);
272+
game_dir_handle.create_sub_dir(player2_dir);
268273

269274
if let Some(resp) = copy_files(
270275
&self.game_id,
271276
&self.player1,
272277
&game_dir_handle,
273-
&player1_game_type_dir,
274-
&player_file_name,
278+
&player1_dir.to_string(),
279+
&GameType::PvPGame,
275280
) {
276281
return resp;
277282
}
@@ -280,8 +285,8 @@ impl Handler for PvPGameRequest {
280285
&self.game_id,
281286
&self.player2,
282287
&game_dir_handle,
283-
&player2_game_type_dir,
284-
&player_file_name,
288+
&player2_dir.to_string(),
289+
&GameType::PvPGame,
285290
) {
286291
return resp;
287292
}
@@ -314,18 +319,18 @@ impl Handler for PvPGameRequest {
314319
&self.player1,
315320
&self.game_id,
316321
&game_dir_handle,
317-
&player_file_name,
322+
&player1_dir.to_string(),
318323
);
319324
let runner2 = get_runner(
320325
&self.player2,
321326
&self.game_id,
322327
&game_dir_handle,
323-
&player_file_name,
328+
&player2_dir.to_string(),
324329
);
325330
info!("Got runners");
326331
let initialize = || -> Result<_, SimulatorError> {
327332
let mut player1_process = runner1.run(p1_r, p1_w, GameType::PvPGame)?;
328-
let mut player2_process = runner2.run2(p2_r, p2_w, GameType::PvPGame)?;
333+
let mut player2_process = runner2.run(p2_r, p2_w, GameType::PvPGame)?;
329334
let simulator = simulator::Simulator::new(self.game_id.to_string());
330335

331336
let mut sim_process = simulator.run_pvp(
@@ -351,12 +356,12 @@ impl Handler for PvPGameRequest {
351356

352357
let sim_stderr = sim_process.stderr.take().unwrap();
353358

354-
let player1_process = Process::new(player1_process, ProcessType::Runner);
355-
let player2_process = Process::new(player2_process, ProcessType::Runner);
359+
let player1_process = Process::new(player1_process, ProcessType::RunnerPlayer1);
360+
let player2_process = Process::new(player2_process, ProcessType::RunnerPlayer2);
356361
let sim_process = Process::new(sim_process, ProcessType::Simulator);
357362

358-
let player1_output = ProcessOutput::new(player1_stderr, ProcessType::Runner);
359-
let player2_output = ProcessOutput::new(player2_stderr, ProcessType::Runner);
363+
let player1_output = ProcessOutput::new(player1_stderr, ProcessType::RunnerPlayer1);
364+
let player2_output = ProcessOutput::new(player2_stderr, ProcessType::RunnerPlayer2);
360365
let sim_output = ProcessOutput::new(sim_stderr, ProcessType::Simulator);
361366

362367
let player1 = EpollEntryType::Process(player1_process);
@@ -397,6 +402,8 @@ impl Handler for PvPGameRequest {
397402
Err(err) => return create_error_response(self.game_id.to_owned(), err),
398403
};
399404

405+
info!("Crossed first possib");
406+
400407
let mut outputs: Vec<ProcessOutput> = vec![];
401408

402409
while !event_handler.is_empty() {
@@ -409,19 +416,45 @@ impl Handler for PvPGameRequest {
409416
}
410417
}
411418

419+
info!("Crossed second possib");
420+
412421
let process1 = outputs.remove(0);
413422
let process2 = outputs.remove(0);
414-
415-
let (player_process_out, sim_process_out) = match process1.process_type() {
416-
ProcessType::Runner => (process1.output(), process2.output()),
417-
ProcessType::Simulator => (process2.output(), process1.output()),
423+
let process3 = outputs.remove(0);
424+
425+
let (player1_process_out, player2_process_out, sim_process_out) = match (process1.process_type(), process2.process_type(), process3.process_type()) {
426+
(ProcessType::RunnerPlayer1, ProcessType::RunnerPlayer2, ProcessType::Simulator) => {
427+
(process1.output(), process2.output(), process3.output())
428+
},
429+
(ProcessType::RunnerPlayer2, ProcessType::RunnerPlayer1, ProcessType::Simulator) => {
430+
(process2.output(), process1.output(), process3.output())
431+
},
432+
(ProcessType::RunnerPlayer1, ProcessType::Simulator, ProcessType::RunnerPlayer2) => {
433+
(process1.output(), process3.output(), process2.output())
434+
},
435+
(ProcessType::RunnerPlayer2, ProcessType::Simulator, ProcessType::RunnerPlayer1) => {
436+
(process2.output(), process3.output(), process1.output())
437+
},
438+
(ProcessType::Simulator, ProcessType::RunnerPlayer1, ProcessType::RunnerPlayer2) => {
439+
(process3.output(), process1.output(), process2.output())
440+
},
441+
(ProcessType::Simulator, ProcessType::RunnerPlayer2, ProcessType::RunnerPlayer1) => {
442+
(process3.output(), process2.output(), process1.output())
443+
},
444+
_ => {
445+
return create_error_response(
446+
self.game_id.to_owned(),
447+
SimulatorError::UnidentifiedError("Failed to map outputs".to_owned()),
448+
);
449+
}
418450
};
419-
451+
420452
info!("Successfully executed for game {}", self.game_id);
421-
create_final_response(
453+
create_final_pvp_response(
422454
self.parameters,
423455
self.game_id,
424-
player_process_out,
456+
player1_process_out,
457+
player2_process_out,
425458
sim_process_out,
426459
)
427460
}

0 commit comments

Comments
 (0)