Skip to content

Commit 15dd041

Browse files
authored
feat: aerial attackers and fix env (#6)
* feat: aerial attackers and fix env * fix: workflow * fix: player code mapping
1 parent f668188 commit 15dd041

File tree

13 files changed

+126
-79
lines changed

13 files changed

+126
-79
lines changed

.cargo/config.example.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[env]
2+
SIMULATOR_IMAGE="ghcr.io/delta/codecharacter-simulator:latest"
3+
CPP_COMPILER_IMAGE="ghcr.io/delta/codecharacter-cpp-compiler:latest"
4+
CPP_RUNNER_IMAGE="ghcr.io/delta/codecharacter-cpp-runner:latest"
5+
JAVA_COMPILER_IMAGE="ghcr.io/delta/codecharacter-java-compiler:latest"
6+
JAVA_RUNNER_IMAGE="ghcr.io/delta/codecharacter-java-runner:latest"
7+
PYTHON_RUNNER_IMAGE="ghcr.io/delta/codecharacter-python-runner:latest"
8+
9+
MAX_LOG_SIZE="200000"
10+
COMPILATION_TIME_LIMIT="5"
11+
RUNTIME_TIME_LIMIT="10"
12+
COMPILATION_MEMORY_LIMIT="300m"
13+
RUNTIME_MEMORY_LIMIT="100m"
14+
EPOLL_WAIT_TIMEOUT="30000"
15+
16+
RABBITMQ_HOST="amqp://guest:guest@localhost"
17+
REQUEST_QUEUE="gameRequestQueue"
18+
RESPONSE_QUEUE="gameStatusUpdateQueue"
19+
20+
MAP_SIZE="64"

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
toolchain: nightly
2424
override: true
2525
- name: Copy config
26-
run: cp config.example.toml config.toml
26+
run: cp .cargo/config.example.toml .cargo/config.toml
2727
- name: Build
2828
run: cargo build --verbose
2929
- name: Run tests

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
3. Create config files
3030
```
31-
cp config.example.toml config.toml
31+
cp .cargo/config.example.toml .cargo/config.toml
3232
```
3333
4. Run
3434
```

config.example.toml

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/main.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::sync::Arc;
2-
use std::env;
1+
use std::{env, sync::Arc};
32

43
use cc_driver::{
54
create_error_response, create_executing_response,
@@ -13,7 +12,7 @@ use cc_driver::{
1312
},
1413
request::{GameRequest, Language},
1514
response::GameStatus,
16-
runner::{cpp, java, py, simulator, Runnable}
15+
runner::{cpp, java, py, simulator, Runnable},
1716
};
1817
use log::{info, LevelFilter};
1918
use log4rs::{
@@ -29,7 +28,10 @@ use nix::sys::epoll::EpollFlags;
2928
fn handle_event(
3029
epoll_handle: &mut EpollGeneric<EpollEntryType>,
3130
) -> Result<Vec<Option<ProcessOutput>>, SimulatorError> {
32-
let events = epoll_handle.poll(env::var("EPOLL_WAIT_TIMEOUT").unwrap().parse().unwrap(), epoll_handle.get_registered_fds().len())?;
31+
let events = epoll_handle.poll(
32+
env::var("EPOLL_WAIT_TIMEOUT").unwrap().parse().unwrap(),
33+
epoll_handle.get_registered_fds().len(),
34+
)?;
3335
let mut res = vec![];
3436
for e in events {
3537
match epoll_handle.process_event(e)? {
@@ -265,9 +267,9 @@ fn main() {
265267
let _handle = log4rs::init_config(config).unwrap();
266268

267269
let res = consumer(
268-
"amqp://guest:guest@localhost".to_owned(),
269-
"gameRequestQueue".to_owned(),
270-
"gameStatusUpdateQueue".to_owned(),
270+
env::var("RABBITMQ_HOST").unwrap(),
271+
env::var("REQUEST_QUEUE").unwrap(),
272+
env::var("RESPONSE_QUEUE").unwrap(),
271273
worker_fn,
272274
);
273275

src/poll/epoll_entry.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use nix::sys::epoll::EpollFlags;
22

33
use crate::error::EpollError;
44

5+
use std::env;
56
use std::io::Read;
67
use std::os::fd::AsRawFd;
78
use std::os::linux::process::ChildExt;
89
use std::process::ChildStderr;
9-
use std::env;
1010

1111
use crate::error::SimulatorError;
1212

@@ -90,10 +90,7 @@ impl ProcessOutput {
9090

9191
match self.process_type {
9292
ProcessType::Runner => {
93-
let limit: usize = env::var("MAX_LOG_SIZE")
94-
.unwrap()
95-
.parse()
96-
.unwrap();
93+
let limit: usize = env::var("MAX_LOG_SIZE").unwrap().parse().unwrap();
9794
let stderr = &mut self.stderr;
9895

9996
let _ = stderr

src/request.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct Attacker {
1010
pub attack_power: u32,
1111
pub speed: u32,
1212
pub price: u32,
13+
pub is_aerial: u32,
1314
}
1415

1516
#[derive(Deserialize, Debug, PartialEq)]
@@ -19,6 +20,7 @@ pub struct Defender {
1920
pub range: u32,
2021
pub attack_power: u32,
2122
pub price: u32,
23+
pub is_aerial: u32,
2224
}
2325

2426
#[derive(Deserialize, Debug, PartialEq)]
@@ -62,7 +64,7 @@ mod tests {
6264
#[test]
6365
pub fn deserealization_test() {
6466
// An example request that we might get from backend
65-
let example_request = r#"{"game_id":"0fa0f12d-d472-42d5-94b4-011e0c916023","parameters":{"attackers":[{"id":1,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1},{"id":2,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1}],"defenders":[{"id":1,"hp":10,"range":4,"attack_power":5,"price":1},{"id":2,"hp":10,"range":6,"attack_power":5,"price":1}],"no_of_turns":500,"no_of_coins":1000},"source_code":"print(x)","language":"PYTHON","map":"[[1,0],[0,2]]"}"#;
67+
let example_request = 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},{"id":2,"hp":10,"range":3,"attack_power":3,"speed":3,"price":1,"is_aerial":1}],"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":1000},"source_code":"print(x)","language":"PYTHON","map":"[[1,0],[0,2]]"}"#;
6668

6769
let expected_deserealized_struct = GameRequest {
6870
game_id: "0fa0f12d-d472-42d5-94b4-011e0c916023".to_owned(),
@@ -75,6 +77,7 @@ mod tests {
7577
attack_power: 3,
7678
speed: 3,
7779
price: 1,
80+
is_aerial: 0,
7881
},
7982
Attacker {
8083
id: 2,
@@ -83,6 +86,7 @@ mod tests {
8386
attack_power: 3,
8487
speed: 3,
8588
price: 1,
89+
is_aerial: 1,
8690
},
8791
],
8892
defenders: vec![
@@ -92,13 +96,15 @@ mod tests {
9296
range: 4,
9397
attack_power: 5,
9498
price: 1,
99+
is_aerial: 1,
95100
},
96101
Defender {
97102
id: 2,
98103
hp: 10,
99104
range: 6,
100105
attack_power: 5,
101106
price: 1,
107+
is_aerial: 1,
102108
},
103109
],
104110
no_of_turns: 500,

src/runner/cpp.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::{
2+
env,
23
fs::File,
34
os::linux::process::CommandExt,
45
process::{Child, Command, Stdio},
5-
env,
66
};
77

8-
use crate::{
9-
error::SimulatorError
10-
};
8+
use crate::error::SimulatorError;
119

1210
use super::Runnable;
1311

@@ -31,18 +29,23 @@ impl Runnable for Runner {
3129
.args([
3230
"run",
3331
&format!("--memory={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
34-
&format!("--memory-swap={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
32+
&format!(
33+
"--memory-swap={}",
34+
env::var("COMPILATION_MEMORY_LIMIT").unwrap()
35+
),
3536
"--cpus=2",
3637
"--ulimit",
37-
&format!("cpu={}:{}", env::var("COMPILATION_TIME_LIMIT").unwrap(), env::var("COMPILATION_TIME_LIMIT").unwrap()),
38+
&format!(
39+
"cpu={}:{}",
40+
env::var("COMPILATION_TIME_LIMIT").unwrap(),
41+
env::var("COMPILATION_TIME_LIMIT").unwrap()
42+
),
3843
"--rm",
3944
"--name",
4045
&format!("{}_cpp_compiler", self.game_id),
4146
"-v",
42-
format!("{}/run.cpp:/player_code/run.cpp", self.current_dir.as_str()).as_str(),
43-
"-v",
44-
format!("{}/run:/player_code/run", self.current_dir.as_str()).as_str(),
45-
"ghcr.io/delta/codecharacter-cpp-compiler:latest",
47+
format!("{}/:/player_code/", self.current_dir.as_str()).as_str(),
48+
&env::var("CPP_COMPILER_IMAGE").unwrap(),
4649
])
4750
.current_dir(&self.current_dir)
4851
.stdout(Stdio::null())
@@ -69,17 +72,24 @@ impl Runnable for Runner {
6972
.args([
7073
"run",
7174
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
72-
&format!("--memory-swap={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
75+
&format!(
76+
"--memory-swap={}",
77+
env::var("RUNTIME_MEMORY_LIMIT").unwrap()
78+
),
7379
"--cpus=1",
7480
"--ulimit",
75-
&format!("cpu={}:{}", env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
81+
&format!(
82+
"cpu={}:{}",
83+
env::var("RUNTIME_TIME_LIMIT").unwrap(),
84+
env::var("RUNTIME_TIME_LIMIT").unwrap()
85+
),
7686
"--rm",
7787
"--name",
7888
&format!("{}_cpp_runner", self.game_id),
7989
"-i",
8090
"-v",
8191
format!("{}/run:/player_code", self.current_dir.as_str()).as_str(),
82-
"ghcr.io/delta/codecharacter-cpp-runner:latest",
92+
&env::var("CPP_RUNNER_IMAGE").unwrap(),
8393
])
8494
.current_dir(&self.current_dir)
8595
.create_pidfd(true)

src/runner/java.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::{
2+
env,
23
fs::File,
34
os::linux::process::CommandExt,
45
process::{Child, Command, Stdio},
5-
env
66
};
77

8-
use crate::{
9-
error::SimulatorError
10-
};
8+
use crate::error::SimulatorError;
119

1210
use super::Runnable;
1311

@@ -31,22 +29,23 @@ impl Runnable for Runner {
3129
.args([
3230
"run",
3331
&format!("--memory={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
34-
&format!("--memory-swap={}", env::var("COMPILATION_MEMORY_LIMIT").unwrap()),
32+
&format!(
33+
"--memory-swap={}",
34+
env::var("COMPILATION_MEMORY_LIMIT").unwrap()
35+
),
3536
"--cpus=1.5",
3637
"--ulimit",
37-
&format!("cpu={}:{}", env::var("COMPILATION_TIME_LIMIT").unwrap(), env::var("COMPILATION_TIME_LIMIT").unwrap()),
38+
&format!(
39+
"cpu={}:{}",
40+
env::var("COMPILATION_TIME_LIMIT").unwrap(),
41+
env::var("COMPILATION_TIME_LIMIT").unwrap()
42+
),
3843
"--rm",
3944
"--name",
4045
&format!("{}_java_compiler", self.game_id),
4146
"-v",
42-
format!(
43-
"{}/Run.java:/player_code/Run.java",
44-
self.current_dir.as_str()
45-
)
46-
.as_str(),
47-
"-v",
48-
format!("{}/run.jar:/player_code/run.jar", self.current_dir.as_str()).as_str(),
49-
"ghcr.io/delta/codecharacter-java-compiler:latest",
47+
format!("{}/:/player_code/", self.current_dir.as_str()).as_str(),
48+
&env::var("JAVA_COMPILER_IMAGE").unwrap(),
5049
])
5150
.current_dir(&self.current_dir)
5251
.stdout(Stdio::null())
@@ -73,17 +72,24 @@ impl Runnable for Runner {
7372
.args([
7473
"run",
7574
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
76-
&format!("--memory-swap={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
75+
&format!(
76+
"--memory-swap={}",
77+
env::var("RUNTIME_MEMORY_LIMIT").unwrap()
78+
),
7779
"--cpus=1",
7880
"--ulimit",
79-
&format!("cpu={}:{}", env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
81+
&format!(
82+
"cpu={}:{}",
83+
env::var("RUNTIME_TIME_LIMIT").unwrap(),
84+
env::var("RUNTIME_TIME_LIMIT").unwrap()
85+
),
8086
"--rm",
8187
"--name",
8288
&format!("{}_java_runner", self.game_id),
8389
"-i",
8490
"-v",
8591
format!("{}/run.jar:/run.jar", self.current_dir.as_str()).as_str(),
86-
"ghcr.io/delta/codecharacter-java-runner:latest",
92+
&env::var("JAVA_RUNNER_IMAGE").unwrap(),
8793
])
8894
.create_pidfd(true)
8995
.current_dir(&self.current_dir)

0 commit comments

Comments
 (0)