aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'planetwars-server/src/modules')
-rw-r--r--planetwars-server/src/modules/client_api.rs11
-rw-r--r--planetwars-server/src/modules/matches.rs22
-rw-r--r--planetwars-server/src/modules/ranking.rs22
-rw-r--r--planetwars-server/src/modules/registry.rs4
4 files changed, 48 insertions, 11 deletions
diff --git a/planetwars-server/src/modules/client_api.rs b/planetwars-server/src/modules/client_api.rs
index 7026671..3402964 100644
--- a/planetwars-server/src/modules/client_api.rs
+++ b/planetwars-server/src/modules/client_api.rs
@@ -111,14 +111,23 @@ impl pb::client_api_service_server::ClientApiService for ClientApiServer {
db::bots::find_bot_with_version_by_name(&match_request.opponent_name, &conn)
.map_err(|_| Status::not_found("opponent not found"))?;
+ let map_name = match match_request.map_name.as_str() {
+ "" => "hex",
+ name => name,
+ };
+ let map = db::maps::find_map_by_name(map_name, &conn)
+ .map_err(|_| Status::not_found("map not found"))?;
+
let player_key = gen_alphanumeric(32);
let remote_bot_spec = Box::new(RemoteBotSpec {
player_key: player_key.clone(),
router: self.router.clone(),
});
- let run_match = RunMatch::from_players(
+ let run_match = RunMatch::new(
self.runner_config.clone(),
+ false,
+ map,
vec![
MatchPlayer::BotSpec {
spec: remote_bot_spec,
diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs
index 4f538ed..ecc7976 100644
--- a/planetwars-server/src/modules/matches.rs
+++ b/planetwars-server/src/modules/matches.rs
@@ -7,6 +7,7 @@ use tokio::task::JoinHandle;
use crate::{
db::{
self,
+ maps::Map,
matches::{MatchData, MatchResult},
},
util::gen_alphanumeric,
@@ -17,6 +18,11 @@ pub struct RunMatch {
log_file_name: String,
players: Vec<MatchPlayer>,
config: Arc<GlobalConfig>,
+ is_public: bool,
+ // Map is mandatory for now.
+ // It would be nice to allow "anonymous" (eg. randomly generated) maps
+ // in the future, too.
+ map: Map,
}
pub enum MatchPlayer {
@@ -30,19 +36,27 @@ pub enum MatchPlayer {
}
impl RunMatch {
- pub fn from_players(config: Arc<GlobalConfig>, players: Vec<MatchPlayer>) -> Self {
+ // TODO: create a MatchParams struct
+ pub fn new(
+ config: Arc<GlobalConfig>,
+ is_public: bool,
+ map: Map,
+ players: Vec<MatchPlayer>,
+ ) -> Self {
let log_file_name = format!("{}.log", gen_alphanumeric(16));
RunMatch {
config,
log_file_name,
players,
+ is_public,
+ map,
}
}
fn into_runner_config(self) -> runner::MatchConfig {
runner::MatchConfig {
- map_path: PathBuf::from(&self.config.maps_directory).join("hex.json"),
- map_name: "hex".to_string(),
+ map_path: PathBuf::from(&self.config.maps_directory).join(self.map.file_path),
+ map_name: self.map.name,
log_path: PathBuf::from(&self.config.match_logs_directory).join(&self.log_file_name),
players: self
.players
@@ -80,6 +94,8 @@ impl RunMatch {
let new_match_data = db::matches::NewMatch {
state: db::matches::MatchState::Playing,
log_path: &self.log_file_name,
+ is_public: self.is_public,
+ map_id: Some(self.map.id),
};
let new_match_players = self
.players
diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs
index cb699fe..90c4a56 100644
--- a/planetwars-server/src/modules/ranking.rs
+++ b/planetwars-server/src/modules/ranking.rs
@@ -1,4 +1,5 @@
use crate::db::bots::BotVersion;
+use crate::db::maps::Map;
use crate::{db::bots::Bot, DbPool, GlobalConfig};
use crate::db;
@@ -25,22 +26,31 @@ pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) {
.expect("could not get database connection");
loop {
interval.tick().await;
- let bots = db::bots::all_active_bots_with_version(&db_conn).unwrap();
+ let bots = db::bots::all_active_bots_with_version(&db_conn).expect("could not load bots");
if bots.len() < 2 {
// not enough bots to play a match
continue;
}
- let selected_bots: Vec<(Bot, BotVersion)> = {
- let mut rng = &mut rand::thread_rng();
- bots.choose_multiple(&mut rng, 2).cloned().collect()
+
+ let selected_bots: Vec<(Bot, BotVersion)> = bots
+ .choose_multiple(&mut rand::thread_rng(), 2)
+ .cloned()
+ .collect();
+
+ let maps = db::maps::list_maps(&db_conn).expect("could not load map");
+ let map = match maps.choose(&mut rand::thread_rng()).cloned() {
+ None => continue, // no maps available
+ Some(map) => map,
};
- play_ranking_match(config.clone(), selected_bots, db_pool.clone()).await;
+
+ play_ranking_match(config.clone(), map, selected_bots, db_pool.clone()).await;
recalculate_ratings(&db_conn).expect("could not recalculate ratings");
}
}
async fn play_ranking_match(
config: Arc<GlobalConfig>,
+ map: Map,
selected_bots: Vec<(Bot, BotVersion)>,
db_pool: DbPool,
) {
@@ -53,7 +63,7 @@ async fn play_ranking_match(
players.push(player);
}
- let (_, handle) = RunMatch::from_players(config, players)
+ let (_, handle) = RunMatch::new(config, true, map, players)
.run(db_pool.clone())
.await
.expect("failed to run match");
diff --git a/planetwars-server/src/modules/registry.rs b/planetwars-server/src/modules/registry.rs
index 297404a..4a79d59 100644
--- a/planetwars-server/src/modules/registry.rs
+++ b/planetwars-server/src/modules/registry.rs
@@ -300,8 +300,10 @@ async fn put_upload(
while let Some(Ok(chunk)) = stream.next().await {
file.write_all(&chunk).await.unwrap();
}
- file.flush().await.unwrap();
let range_end = last_byte_pos(&file).await.unwrap();
+ // Close the file to ensure all data has been flushed to the kernel.
+ // If we don't do this, calculating the checksum can fail.
+ std::mem::drop(file);
let expected_digest = params.digest.strip_prefix("sha256:").unwrap();
let digest = file_sha256_digest(&upload_path).unwrap();