config file

This commit is contained in:
catvayor 2024-06-16 09:40:32 +02:00
parent f4bd578ad3
commit 5d9b730bb6
4 changed files with 56 additions and 38 deletions

View file

@ -4,9 +4,9 @@ bonus_timeout = 5000
event_timeout = 100 event_timeout = 100
admin_token = "root" admin_token = "root"
serve_static = true serve_static = true
base_teams = [ teams = [
["team00", "Équipe 00", false], { id = "team00", name = "Équipe 00", vieux = false},
["team01", "Équipe 01", false], { id = "team01", name = "Équipe 01", vieux = false},
["npc0", "PNJ 0", true], { id = "npc0", name = "PNJ 0", vieux = true},
["npc1", "PNJ 1", true], { id = "npc1", name = "PNJ 1", vieux = true},
] ]

View file

@ -9,6 +9,14 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct TeamConfig {
pub id: String,
pub name: String,
pub vieux: bool,
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
pub struct Config { pub struct Config {
@ -16,7 +24,7 @@ pub struct Config {
pub bonus_timeout: u64, pub bonus_timeout: u64,
pub event_timeout: u64, pub event_timeout: u64,
pub admin_token: String, pub admin_token: String,
pub base_teams : Vec<(String, String, bool)>, pub teams: Vec<TeamConfig>,
pub serve_static: bool, pub serve_static: bool,
} }
@ -211,7 +219,11 @@ pub fn admin_view(team: &Tracked) -> AdminTrackedInfo {
} }
} }
pub fn apparent_info(watcher: &Tracked, team: &Tracked, blurred_move : (f32, f32)) -> Option<TrackedInfo> { pub fn apparent_info(
watcher: &Tracked,
team: &Tracked,
blurred_move: (f32, f32),
) -> Option<TrackedInfo> {
if watcher.id == team.id { if watcher.id == team.id {
None None
} else if let Conscrit { } else if let Conscrit {

View file

@ -1,8 +1,8 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::{ use rocket::{
fs::{relative, FileServer},
fairing::AdHoc, fairing::AdHoc,
fs::{relative, FileServer},
response::stream::Event, response::stream::Event,
tokio::{ tokio::{
self, select, self, select,
@ -11,7 +11,7 @@ use rocket::{
}; };
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use std::{ use std::{
collections::{HashMap, VecDeque}, collections::VecDeque,
sync::{Arc, RwLock}, 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 watcher = tracking_lock.get(id).unwrap().read().unwrap();
let mut infos: Vec<TrackedInfo> = Vec::new(); let mut infos: Vec<TrackedInfo> = Vec::new();
for (_, tracked) in tracking_lock.iter() { 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); 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() { for (_, queue) in evt_queues.read().unwrap().iter() {
let queue = &mut queue.write().unwrap(); let queue = &mut queue.write().unwrap();
while let Some(queued_evt) = queue.front() { while let Some(queued_evt) = queue.front() {
@ -67,25 +73,22 @@ fn clean_expired_evt(evt_queues: &TrackingEventQueue, admin_queue: &AdminEventQu
async fn rocket() -> _ { async fn rocket() -> _ {
let rocket = rocket::build(); let rocket = rocket::build();
let config: Config = rocket.figment().extract().unwrap(); let config: Config = rocket.figment().extract().unwrap();
//TODO: read a config file on release let tracking: Tracking = Arc::new(RwLock::new(
let tracking: Tracking = Arc::new(RwLock::new(HashMap::from([ config
.teams
.iter()
.map(|team| {
( (
"team00".to_string(), team.id.clone(),
RwLock::new(build_conscrit("team00".to_string(), "Équipe 0".to_string())), RwLock::new(if team.vieux {
), build_vieux(team.id.clone(), team.name.clone())
( } else {
"team01".to_string(), build_conscrit(team.id.clone(), team.name.clone())
RwLock::new(build_conscrit("team01".to_string(), "Équipe 1".to_string())), }),
), )
( })
"npc0".to_string(), .collect(),
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 evt_queue: TrackingEventQueue = Arc::new(RwLock::new( let evt_queue: TrackingEventQueue = Arc::new(RwLock::new(
tracking tracking
.read() .read()
@ -95,7 +98,7 @@ async fn rocket() -> _ {
.collect(), .collect(),
)); ));
let admin_evt_queue: AdminEventQueue = Arc::new(RwLock::new(VecDeque::new())); 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); println!("Admin token: {}", key);
let mut rocket = rocket let mut rocket = rocket
.attach(Template::fairing()) .attach(Template::fairing())

View file

@ -36,7 +36,10 @@ fn tracked_view(
} }
fn evts_for(id: &str, evt_queues: &TrackingEventQueue, timeout: Duration) -> Vec<Event> { fn evts_for(id: &str, evt_queues: &TrackingEventQueue, timeout: Duration) -> Vec<Event> {
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("/<id>/events")] #[get("/<id>/events")]