Compare commits
2 Commits
d3abaf61a7
...
32883b2a1b
Author | SHA1 | Date | |
---|---|---|---|
32883b2a1b | |||
c26da8869c |
65
Cargo.lock
generated
65
Cargo.lock
generated
@ -188,6 +188,12 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
|
||||
|
||||
[[package]]
|
||||
name = "bitvec"
|
||||
version = "1.0.1"
|
||||
@ -739,6 +745,16 @@ version = "0.2.169"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.25"
|
||||
@ -830,6 +846,29 @@ version = "11.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
@ -959,6 +998,15 @@ dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
@ -1015,6 +1063,12 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.217"
|
||||
@ -1080,6 +1134,15 @@ dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.9"
|
||||
@ -1174,7 +1237,9 @@ dependencies = [
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"parking_lot",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys 0.52.0",
|
||||
|
@ -17,7 +17,7 @@ nursery = "warn"
|
||||
|
||||
[dependencies]
|
||||
# server
|
||||
tokio = { version = "1.43", features = ["net", "macros", "rt-multi-thread"] }
|
||||
tokio = { version = "1.43", features = ["full"] }
|
||||
axum = { version = "0.8", features = ["http2", "multipart", "ws"] }
|
||||
serde = { version = "1.0", features = ["derive", "rc"] }
|
||||
|
||||
|
@ -82,46 +82,60 @@ async fn get_move(request: Json<Request>) -> response::Json<Response> {
|
||||
}
|
||||
info!("valid actions: {:?}", actions);
|
||||
|
||||
let mut action_data = [(0, 0); 4];
|
||||
let mut total_simulations = 0;
|
||||
let base_turns = board.turn();
|
||||
while start.elapsed() < Duration::from_millis(250) {
|
||||
let mut board = board.clone();
|
||||
let action = *actions.choose(&mut thread_rng()).unwrap_or(&Direction::Up);
|
||||
board.next_turn(&[(id, action)]);
|
||||
let turns = board.simulate_random(|board| {
|
||||
if board.num_snakes() == 0
|
||||
|| board.turn() > base_turns + u32::from(request.you.length) * 3
|
||||
{
|
||||
Some(board.turn())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let base_turns = board.turn();
|
||||
let end_condition: &dyn Fn(&Board) -> Option<u32> = match &*request.game.ruleset.name {
|
||||
"solo" => &|board: &Board| {
|
||||
if board.num_snakes() == 0
|
||||
|| board.turn() > base_turns + u32::from(request.you.length) * 3
|
||||
{
|
||||
Some(board.turn())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
_ => &|board: &Board| {
|
||||
if !board.alive(id) || board.turn() > base_turns + u32::from(request.you.length) * 3
|
||||
{
|
||||
Some(board.turn())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let mut action_data = [(0, 0); 4];
|
||||
let mut total_simulations = 0;
|
||||
while start.elapsed() < Duration::from_millis(250) {
|
||||
let mut board = board.clone();
|
||||
let action = *actions.choose(&mut thread_rng()).unwrap_or(&Direction::Up);
|
||||
board.next_turn(&[(id, action)]);
|
||||
let turns = board.simulate_random(end_condition);
|
||||
let action_data = &mut action_data[usize::from(action)];
|
||||
action_data.0 += turns - base_turns;
|
||||
action_data.1 += 1;
|
||||
total_simulations += 1;
|
||||
}
|
||||
debug!("action data: {action_data:?}");
|
||||
|
||||
let action = actions.into_iter().max_by_key(|action| {
|
||||
let action_data = action_data[usize::from(*action)];
|
||||
(action_data.0 / action_data.1, action_data.0 % action_data.1)
|
||||
});
|
||||
let action_data = &mut action_data[usize::from(action)];
|
||||
action_data.0 += turns - base_turns;
|
||||
action_data.1 += 1;
|
||||
total_simulations += 1;
|
||||
}
|
||||
debug!("action data: {action_data:?}");
|
||||
|
||||
let action = actions.into_iter().max_by_key(|action| {
|
||||
let action_data = action_data[usize::from(*action)];
|
||||
(action_data.0 / action_data.1, action_data.0 % action_data.1)
|
||||
});
|
||||
|
||||
if let Some(action) = action {
|
||||
let action_data = action_data[usize::from(action)];
|
||||
let avg_turns = action_data.0 / action_data.1;
|
||||
info!("found action {action:?} after {total_simulations} simulations with an average of {avg_turns} turns.");
|
||||
} else {
|
||||
warn!("unable to find a valid action");
|
||||
}
|
||||
info!("chose {action:?}");
|
||||
response::Json(Response {
|
||||
direction: action.unwrap_or(Direction::Up),
|
||||
shout: None,
|
||||
})
|
||||
if let Some(action) = action {
|
||||
let action_data = action_data[usize::from(action)];
|
||||
let avg_turns = action_data.0 / action_data.1;
|
||||
info!("found action {action:?} after {total_simulations} simulations with an average of {avg_turns} turns.");
|
||||
} else {
|
||||
warn!("unable to find a valid action");
|
||||
}
|
||||
info!("chose {action:?}");
|
||||
response::Json(Response {
|
||||
direction: action.unwrap_or(Direction::Up),
|
||||
shout: None,
|
||||
})
|
||||
}).await.unwrap()
|
||||
}
|
||||
|
||||
async fn end(request: Json<Request>) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user