planet-wars/backend/src/util.rs

193 lines
4.8 KiB
Rust
Raw Normal View History

2020-04-09 20:57:12 +00:00
use crate::planetwars::FinishedState;
use mozaic::util::request::Connect;
use serde_json::Value;
use std::cmp::Ordering;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
2020-03-25 21:14:26 +00:00
2020-04-06 18:22:10 +00:00
static NAV: [(&'static str, &'static str); 6] = [
2020-04-01 19:36:35 +00:00
("/", "Home"),
("/mapbuilder", "Map Builder"),
2020-04-06 12:30:29 +00:00
("/lobby", "Lobby"),
2020-04-01 19:36:35 +00:00
("/visualizer", "Visualizer"),
("/debug", "Debug Station"),
2020-04-06 18:22:10 +00:00
("/info", "Info"),
2020-04-01 19:36:35 +00:00
];
2020-03-27 09:31:56 +00:00
pub static COLOURS: [&'static str; 10] = [
"#808080", "#FF8000", "#0080ff", "#FF6693", "#3fcb55", "#cbc33f", "#cf40e9", "#FF3F0D", "#1beef0", "#0DC5FF"
2020-04-01 20:16:55 +00:00
];
2020-04-09 20:57:12 +00:00
/// The state of a player, in a running game.
/// This represents actual players or connection keys.
#[derive(Serialize, Educe)]
#[educe(PartialEq, Eq, PartialOrd, Ord)]
2020-04-01 18:49:50 +00:00
pub struct PlayerStatus {
2020-04-09 20:57:12 +00:00
pub waiting: bool,
pub connected: bool,
pub reconnecting: bool,
pub value: String,
2020-04-01 18:49:50 +00:00
}
impl From<Connect> for PlayerStatus {
fn from(value: Connect) -> Self {
match value {
2020-04-01 19:36:35 +00:00
Connect::Connected(_, name) => PlayerStatus {
waiting: false,
connected: true,
reconnecting: false,
value: name,
},
Connect::Reconnecting(_, name) => PlayerStatus {
waiting: false,
connected: true,
reconnecting: true,
value: name,
},
Connect::Waiting(_, key) => PlayerStatus {
waiting: true,
connected: false,
reconnecting: false,
value: format!("Key: {}", key),
},
2020-04-01 18:49:50 +00:00
_ => panic!("No playerstatus possible from Connect::Request"),
}
}
}
fn partial_cmp(a: &SystemTime, b: &SystemTime) -> Option<Ordering> {
b.partial_cmp(a)
}
2020-04-09 20:57:12 +00:00
/// The GameState is the state of a game.
/// Either Finished, so the game is done, not running, and there is a posible visualization.
/// Or Playing, the game is still being managed by the mozaic framework.
#[derive(Serialize, Educe)]
2020-04-01 18:49:50 +00:00
#[serde(tag = "type")]
#[educe(PartialEq, Eq, PartialOrd, Ord)]
2020-04-01 18:49:50 +00:00
pub enum GameState {
#[educe(PartialOrd(rank = 1))]
2020-04-01 18:49:50 +00:00
Playing {
#[educe(PartialOrd(method = "partial_cmp"))]
time: SystemTime,
2020-04-01 18:49:50 +00:00
name: String,
map: String,
players: Vec<PlayerStatus>,
connected: usize,
total: usize,
#[educe(Ord(ignore), PartialOrd(ignore))]
2020-04-02 07:56:47 +00:00
state: Value,
2020-04-01 19:36:35 +00:00
},
#[educe(PartialOrd(rank = 2))]
Finished {
#[educe(PartialOrd(method = "partial_cmp"))]
time: SystemTime,
name: String,
map: String,
2020-04-01 18:49:50 +00:00
players: Vec<(String, bool)>,
turns: u64,
file: String,
},
2020-04-01 19:36:35 +00:00
}
impl From<FinishedState> for GameState {
2020-04-01 20:16:55 +00:00
fn from(mut state: FinishedState) -> Self {
state.players.sort_by_key(|x| x.0);
2020-04-01 19:36:35 +00:00
GameState::Finished {
players: state
2020-04-09 20:57:12 +00:00
.players
.iter()
.map(|(id, name)| (name.clone(), state.winners.contains(&id)))
.collect(),
2020-04-02 07:56:47 +00:00
map: state.map,
2020-04-01 19:36:35 +00:00
name: state.name,
turns: state.turns,
file: state.file,
time: state.time,
2020-04-01 18:49:50 +00:00
}
}
2020-03-28 18:07:55 +00:00
}
2020-04-09 20:57:12 +00:00
/// Link struct, holding all necessary information
2020-03-27 09:31:56 +00:00
#[derive(Serialize)]
struct Link {
name: String,
href: String,
active: bool,
}
2020-04-09 20:57:12 +00:00
impl Link {
fn build_nav(active: &str) -> Vec<Link> {
NAV.iter()
.map(|(href, name)| Link {
name: name.to_string(),
href: href.to_string(),
active: *name == active,
})
.collect()
}
2020-03-28 18:07:55 +00:00
}
2020-04-09 20:57:12 +00:00
/// Context used as template context.
/// This way you know to add nav bar support etc.
/// This T can be anything that is serializable, like json!({}) macro.
2020-03-25 21:14:26 +00:00
#[derive(Serialize)]
2020-03-28 18:07:55 +00:00
pub struct Context<T> {
2020-03-25 21:14:26 +00:00
pub name: String,
2020-03-27 09:31:56 +00:00
nav: Vec<Link>,
2020-03-27 16:32:18 +00:00
#[serde(flatten)]
2020-04-01 19:36:35 +00:00
pub t: Option<T>,
2020-03-25 21:14:26 +00:00
}
2020-03-28 18:07:55 +00:00
impl<T> Context<T> {
pub fn new_with(active: &str, t: T) -> Self {
2020-04-09 20:57:12 +00:00
let nav = Link::build_nav(active);
2020-03-27 09:31:56 +00:00
Context {
2020-04-01 19:36:35 +00:00
nav,
name: String::from(""),
t: Some(t),
2020-03-27 16:32:18 +00:00
}
}
2020-03-28 18:07:55 +00:00
}
2020-03-27 16:32:18 +00:00
2020-03-28 18:07:55 +00:00
impl Context<()> {
2020-03-27 16:32:18 +00:00
pub fn new(active: &str) -> Self {
2020-04-09 20:57:12 +00:00
let nav = Link::build_nav(active);
2020-03-27 16:32:18 +00:00
Context {
2020-04-01 19:36:35 +00:00
nav,
name: String::from(""),
t: None,
2020-03-27 09:31:56 +00:00
}
}
}
/// State of current live games
2020-03-28 18:07:55 +00:00
pub struct Games {
inner: Arc<Mutex<Vec<(String, u64, SystemTime)>>>,
2020-03-28 18:07:55 +00:00
}
impl Games {
pub fn new() -> Self {
Self {
2020-04-01 19:36:35 +00:00
inner: Arc::new(Mutex::new(Vec::new())),
2020-03-28 18:07:55 +00:00
}
}
pub fn add_game(&self, name: String, id: u64) {
self.inner
.lock()
.unwrap()
.push((name, id, SystemTime::now()));
2020-03-28 18:07:55 +00:00
}
pub fn get_games(&self) -> Vec<(String, u64, SystemTime)> {
2020-03-28 18:07:55 +00:00
self.inner.lock().unwrap().clone()
}
}