Skip to content

Commit c0dae18

Browse files
committed
feat: hardcoded pvp
1 parent d7a74e4 commit c0dae18

22 files changed

+1185
-474
lines changed

.cargo/config.example.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ JAVA_COMPILER_IMAGE="ghcr.io/delta/codecharacter-java-compiler:latest"
66
JAVA_RUNNER_IMAGE="ghcr.io/delta/codecharacter-java-runner:latest"
77
PYTHON_RUNNER_IMAGE="ghcr.io/delta/codecharacter-python-runner:latest"
88

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-
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=30_000
2015
MAP_SIZE="64"
16+
17+
RABBIT_MQ_HOST=amqp://guest:guest@localhost
18+
NORMAL_GAME_REQUEST_QUEUE=gameRequestQueue
19+
PVP_GAME_REQUEST_QUEUE=gamePvpRequestQueue
20+
GAME_RESPONSE_QUEUE=gameStatusUpdateQueue

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ Cargo.lock
44
driver.log
55
codecharacter-driver-2022
66
config.toml
7+
.vscode
8+
*.txt

driver_clean.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rm -rf /tmp/20f70cb3-1c90-4118-b916-3e78f1422e3e/
2+
docker stop 20f70cb3-1c90-4118-b916-3e78f1422e3e_player2_cpp_runner
3+
docker stop 20f70cb3-1c90-4118-b916-3e78f1422e3e_player1_cpp_runner
4+
cargo run

run

2.41 MB
Binary file not shown.

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub enum SimulatorError {
66
FifoCreationError(String),
77
EpollError(String),
88
TimeOutError(String),
9+
RabbitMqError(String),
910
}
1011

1112
#[derive(Debug)]

src/fifo.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fs::{remove_file, File, OpenOptions};
22
use std::os::unix::prelude::{AsRawFd, OpenOptionsExt};
33

44
use crate::error::SimulatorError;
5-
use nix::fcntl::{self, FcntlArg, OFlag};
5+
use nix::fcntl::{self, FcntlArg, FdFlag, OFlag};
66
use nix::libc::O_NONBLOCK;
77
use nix::sys::stat;
88
use nix::unistd::mkfifo;
@@ -22,13 +22,25 @@ impl Fifo {
2222
Err(e) => Err(SimulatorError::FifoCreationError(format!("{e}"))),
2323
}
2424
}
25+
2526
fn make_blocking(fd: i32) -> Result<(), SimulatorError> {
2627
let mut flags = OFlag::from_bits_truncate(fcntl::fcntl(fd, FcntlArg::F_GETFL).unwrap());
2728
flags.remove(OFlag::O_NONBLOCK);
2829
fcntl::fcntl(fd, FcntlArg::F_SETFL(flags))
2930
.map_err(|e| SimulatorError::FifoCreationError(format!("{e}")))?;
3031
Ok(())
3132
}
33+
34+
fn remove_close(fd: i32) -> Result<(), SimulatorError> {
35+
let mut flags = FdFlag::from_bits_truncate(fcntl::fcntl(fd, FcntlArg::F_GETFD).unwrap());
36+
37+
flags.remove(FdFlag::FD_CLOEXEC);
38+
fcntl::fcntl(fd, FcntlArg::F_SETFD(flags))
39+
.map_err(|e| SimulatorError::FifoCreationError(format!("{e}")))?;
40+
41+
Ok(())
42+
}
43+
3244
fn setup_pipe(f: &str) -> Result<(File, File), SimulatorError> {
3345
Fifo::open_fifo(f)?;
3446
let stdin = OpenOptions::new()
@@ -41,9 +53,13 @@ impl Fifo {
4153
.open(f)
4254
.map_err(|e| SimulatorError::FifoCreationError(format!("{e}")))?;
4355
let stdin_fd = stdin.as_raw_fd();
56+
let stdout_fd = stdout.as_raw_fd();
4457
Fifo::make_blocking(stdin_fd)?;
58+
Fifo::remove_close(stdin_fd)?;
59+
Fifo::remove_close(stdout_fd)?;
4560
Ok((stdin, stdout))
4661
}
62+
4763
pub fn new(filename: String) -> Result<Self, SimulatorError> {
4864
let (fin, fout) = Fifo::setup_pipe(&filename)?;
4965
Ok(Self {
@@ -52,6 +68,7 @@ impl Fifo {
5268
_out: Some(fout),
5369
})
5470
}
71+
5572
pub fn get_ends(&mut self) -> Option<(File, File)> {
5673
match (self._in.take(), self._out.take()) {
5774
(Some(_in), Some(_out)) => Some((_in, _out)),
@@ -91,7 +108,7 @@ mod fifo_tests {
91108
assert_eq!(s1, s2);
92109
assert_eq!(string, "Hello World".to_owned());
93110

94-
println!("{string}");
111+
log::info!("{string}");
95112
}
96113
#[test]
97114
fn added_data_to_fifo_before_running_cmd_is_saved() {

src/game_dir.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
use log::info;
2+
3+
#[derive(Debug)]
14
pub struct GameDir {
25
full_path: String,
36
}
47

58
impl GameDir {
69
pub fn new(game_id: &str) -> Option<Self> {
710
std::fs::create_dir(format!("/tmp/{game_id}")).ok()?;
11+
info!("Created dir /tmp/{game_id}");
812
Some(GameDir {
913
full_path: format!("/tmp/{game_id}"),
1014
})

0 commit comments

Comments
 (0)