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
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},
]

View file

@ -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<TeamConfig>,
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<TrackedInfo> {
pub fn apparent_info(
watcher: &Tracked,
team: &Tracked,
blurred_move: (f32, f32),
) -> Option<TrackedInfo> {
if watcher.id == team.id {
None
} else if let Conscrit {

View file

@ -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<TrackedInfo> = 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([
let config: Config = rocket.figment().extract().unwrap();
let tracking: Tracking = Arc::new(RwLock::new(
config
.teams
.iter()
.map(|team| {
(
"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())),
),
])));
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())

View file

@ -36,7 +36,10 @@ fn tracked_view(
}
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")]