From 5d9b730bb6addff8eeb75fe7de8817bf8b4de1e9 Mon Sep 17 00:00:00 2001 From: catvayor Date: Sun, 16 Jun 2024 09:40:32 +0200 Subject: [PATCH] config file --- Rocket.toml | 10 +++++----- src/global.rs | 26 ++++++++++++++++++------- src/main.rs | 53 +++++++++++++++++++++++++++------------------------ src/track.rs | 5 ++++- 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/Rocket.toml b/Rocket.toml index 6c67aa7..8fb93dc 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -4,9 +4,9 @@ bonus_timeout = 5000 event_timeout = 100 admin_token = "root" serve_static = true -base_teams = [ - ["team00", "Équipe 00", false], - ["team01", "Équipe 01", false], - ["npc0", "PNJ 0", true], - ["npc1", "PNJ 1", true], +teams = [ + { id = "team00", name = "Équipe 00", vieux = false}, + { id = "team01", name = "Équipe 01", vieux = false}, + { id = "npc0", name = "PNJ 0", vieux = true}, + { id = "npc1", name = "PNJ 1", vieux = true}, ] diff --git a/src/global.rs b/src/global.rs index 1c496c9..a075300 100644 --- a/src/global.rs +++ b/src/global.rs @@ -9,15 +9,23 @@ use std::{ sync::{Arc, RwLock}, }; +#[derive(Deserialize)] +#[serde(crate = "rocket::serde")] +pub struct TeamConfig { + pub id: String, + pub name: String, + pub vieux: bool, +} + #[derive(Deserialize)] #[serde(crate = "rocket::serde")] pub struct Config { - pub blurred_move : (f32, f32), - pub bonus_timeout : u64, - pub event_timeout : u64, - pub admin_token : String, - pub base_teams : Vec<(String, String, bool)>, - pub serve_static : bool, + pub blurred_move: (f32, f32), + pub bonus_timeout: u64, + pub event_timeout: u64, + pub admin_token: String, + pub teams: Vec, + pub serve_static: bool, } #[derive(Serialize, Deserialize, Clone)] @@ -211,7 +219,11 @@ pub fn admin_view(team: &Tracked) -> AdminTrackedInfo { } } -pub fn apparent_info(watcher: &Tracked, team: &Tracked, blurred_move : (f32, f32)) -> Option { +pub fn apparent_info( + watcher: &Tracked, + team: &Tracked, + blurred_move: (f32, f32), +) -> Option { if watcher.id == team.id { None } else if let Conscrit { diff --git a/src/main.rs b/src/main.rs index a00d431..d942867 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ #[macro_use] extern crate rocket; use rocket::{ - fs::{relative, FileServer}, fairing::AdHoc, + fs::{relative, FileServer}, response::stream::Event, tokio::{ self, select, @@ -11,7 +11,7 @@ use rocket::{ }; use rocket_dyn_templates::Template; use std::{ - collections::{HashMap, VecDeque}, + collections::VecDeque, sync::{Arc, RwLock}, }; @@ -31,7 +31,9 @@ fn send_coords(tracking: &Tracking, evt_queue: &TrackingEventQueue, config: &Con let watcher = tracking_lock.get(id).unwrap().read().unwrap(); let mut infos: Vec = Vec::new(); for (_, tracked) in tracking_lock.iter() { - if let Some(info) = apparent_info(&watcher, &tracked.read().unwrap(), config.blurred_move) { + if let Some(info) = + apparent_info(&watcher, &tracked.read().unwrap(), config.blurred_move) + { infos.push(info); } } @@ -42,7 +44,11 @@ fn send_coords(tracking: &Tracking, evt_queue: &TrackingEventQueue, config: &Con } } -fn clean_expired_evt(evt_queues: &TrackingEventQueue, admin_queue: &AdminEventQueue, config: &Config) { +fn clean_expired_evt( + evt_queues: &TrackingEventQueue, + admin_queue: &AdminEventQueue, + config: &Config, +) { for (_, queue) in evt_queues.read().unwrap().iter() { let queue = &mut queue.write().unwrap(); while let Some(queued_evt) = queue.front() { @@ -66,26 +72,23 @@ fn clean_expired_evt(evt_queues: &TrackingEventQueue, admin_queue: &AdminEventQu #[launch] async fn rocket() -> _ { let rocket = rocket::build(); - let config : Config = rocket.figment().extract().unwrap(); - //TODO: read a config file on release - let tracking: Tracking = Arc::new(RwLock::new(HashMap::from([ - ( - "team00".to_string(), - RwLock::new(build_conscrit("team00".to_string(), "Équipe 0".to_string())), - ), - ( - "team01".to_string(), - RwLock::new(build_conscrit("team01".to_string(), "Équipe 1".to_string())), - ), - ( - "npc0".to_string(), - RwLock::new(build_vieux("npc0".to_string(), "PNJ 0".to_string())), - ), - ( - "npc1".to_string(), - RwLock::new(build_vieux("npc1".to_string(), "PNJ 1".to_string())), - ), - ]))); + let config: Config = rocket.figment().extract().unwrap(); + let tracking: Tracking = Arc::new(RwLock::new( + config + .teams + .iter() + .map(|team| { + ( + team.id.clone(), + RwLock::new(if team.vieux { + build_vieux(team.id.clone(), team.name.clone()) + } else { + build_conscrit(team.id.clone(), team.name.clone()) + }), + ) + }) + .collect(), + )); let evt_queue: TrackingEventQueue = Arc::new(RwLock::new( tracking .read() @@ -95,7 +98,7 @@ async fn rocket() -> _ { .collect(), )); let admin_evt_queue: AdminEventQueue = Arc::new(RwLock::new(VecDeque::new())); - let key: AdminKey = "root".to_string(); //TODO : random on release + let key: AdminKey = config.admin_token.clone(); println!("Admin token: {}", key); let mut rocket = rocket .attach(Template::fairing()) diff --git a/src/track.rs b/src/track.rs index e32fe07..f0d7283 100644 --- a/src/track.rs +++ b/src/track.rs @@ -36,7 +36,10 @@ fn tracked_view( } fn evts_for(id: &str, evt_queues: &TrackingEventQueue, timeout: Duration) -> Vec { - evts_to_send(evt_queues.read().unwrap().get(&id.to_string()).unwrap(), timeout) + evts_to_send( + evt_queues.read().unwrap().get(&id.to_string()).unwrap(), + timeout, + ) } #[get("//events")]