2020-04-09 22:57:12 +02:00
|
|
|
use crate::planetwars::FinishedState;
|
|
|
|
use mozaic::util::request::Connect;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2020-04-10 13:50:52 +02:00
|
|
|
use std::time::SystemTime;
|
2020-03-25 22:14:26 +01:00
|
|
|
|
2020-04-06 20:22:10 +02:00
|
|
|
static NAV: [(&'static str, &'static str); 6] = [
|
2020-04-01 21:36:35 +02:00
|
|
|
("/", "Home"),
|
|
|
|
("/mapbuilder", "Map Builder"),
|
2020-04-06 14:30:29 +02:00
|
|
|
("/lobby", "Lobby"),
|
2020-04-01 21:36:35 +02:00
|
|
|
("/visualizer", "Visualizer"),
|
|
|
|
("/debug", "Debug Station"),
|
2020-04-06 20:22:10 +02:00
|
|
|
("/info", "Info"),
|
2020-04-01 21:36:35 +02:00
|
|
|
];
|
2020-03-27 10:31:56 +01:00
|
|
|
|
2020-04-10 13:51:19 +02:00
|
|
|
pub static COLOURS: [&'static str; 10] = [
|
|
|
|
"#808080", "#FF8000", "#0080ff", "#FF6693", "#3fcb55", "#cbc33f", "#cf40e9", "#FF3F0D", "#1beef0", "#0DC5FF"
|
2020-04-01 22:16:55 +02:00
|
|
|
];
|
|
|
|
|
2020-04-09 22:57:12 +02:00
|
|
|
/// The state of a player, in a running game.
|
|
|
|
/// This represents actual players or connection keys.
|
2020-04-10 13:50:52 +02:00
|
|
|
#[derive(Serialize, Educe)]
|
|
|
|
#[educe(PartialEq, Eq, PartialOrd, Ord)]
|
2020-04-01 20:49:50 +02:00
|
|
|
pub struct PlayerStatus {
|
2020-04-09 22:57:12 +02:00
|
|
|
pub waiting: bool,
|
|
|
|
pub connected: bool,
|
|
|
|
pub reconnecting: bool,
|
|
|
|
pub value: String,
|
2020-04-01 20:49:50 +02:00
|
|
|
}
|
|
|
|
impl From<Connect> for PlayerStatus {
|
|
|
|
fn from(value: Connect) -> Self {
|
|
|
|
match value {
|
2020-04-01 21:36:35 +02: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 20:49:50 +02:00
|
|
|
_ => panic!("No playerstatus possible from Connect::Request"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:50:52 +02:00
|
|
|
fn partial_cmp(a: &SystemTime, b: &SystemTime) -> Option<Ordering> {
|
|
|
|
b.partial_cmp(a)
|
|
|
|
}
|
|
|
|
|
2020-04-09 22:57:12 +02: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.
|
2020-04-10 13:50:52 +02:00
|
|
|
#[derive(Serialize, Educe)]
|
2020-04-01 20:49:50 +02:00
|
|
|
#[serde(tag = "type")]
|
2020-04-10 13:50:52 +02:00
|
|
|
#[educe(PartialEq, Eq, PartialOrd, Ord)]
|
2020-04-01 20:49:50 +02:00
|
|
|
pub enum GameState {
|
2020-04-10 13:50:52 +02:00
|
|
|
#[educe(PartialOrd(rank = 1))]
|
2020-04-01 20:49:50 +02:00
|
|
|
Playing {
|
2020-04-10 13:50:52 +02:00
|
|
|
#[educe(PartialOrd(method = "partial_cmp"))]
|
|
|
|
time: SystemTime,
|
|
|
|
|
2020-04-01 20:49:50 +02:00
|
|
|
name: String,
|
|
|
|
map: String,
|
|
|
|
players: Vec<PlayerStatus>,
|
|
|
|
connected: usize,
|
|
|
|
total: usize,
|
2020-04-10 13:50:52 +02:00
|
|
|
#[educe(Ord(ignore), PartialOrd(ignore))]
|
2020-04-02 09:56:47 +02:00
|
|
|
state: Value,
|
2020-04-01 21:36:35 +02:00
|
|
|
},
|
2020-04-10 13:50:52 +02:00
|
|
|
#[educe(PartialOrd(rank = 2))]
|
|
|
|
Finished {
|
|
|
|
#[educe(PartialOrd(method = "partial_cmp"))]
|
|
|
|
time: SystemTime,
|
|
|
|
name: String,
|
|
|
|
map: String,
|
2020-04-01 20:49:50 +02:00
|
|
|
|
2020-04-10 13:50:52 +02:00
|
|
|
players: Vec<(String, bool)>,
|
|
|
|
turns: u64,
|
|
|
|
file: String,
|
|
|
|
},
|
2020-04-01 21:36:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FinishedState> for GameState {
|
2020-04-01 22:16:55 +02:00
|
|
|
fn from(mut state: FinishedState) -> Self {
|
|
|
|
state.players.sort_by_key(|x| x.0);
|
|
|
|
|
2020-04-01 21:36:35 +02:00
|
|
|
GameState::Finished {
|
|
|
|
players: state
|
2020-04-09 22:57:12 +02:00
|
|
|
.players
|
|
|
|
.iter()
|
|
|
|
.map(|(id, name)| (name.clone(), state.winners.contains(&id)))
|
|
|
|
.collect(),
|
2020-04-02 09:56:47 +02:00
|
|
|
map: state.map,
|
2020-04-01 21:36:35 +02:00
|
|
|
name: state.name,
|
|
|
|
turns: state.turns,
|
|
|
|
file: state.file,
|
2020-04-10 13:50:52 +02:00
|
|
|
time: state.time,
|
2020-04-01 20:49:50 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-28 19:07:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:57:12 +02:00
|
|
|
/// Link struct, holding all necessary information
|
2020-03-27 10:31:56 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Link {
|
|
|
|
name: String,
|
|
|
|
href: String,
|
|
|
|
active: bool,
|
|
|
|
}
|
|
|
|
|
2020-04-09 22:57:12 +02: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 19:07:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:57:12 +02: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 22:14:26 +01:00
|
|
|
#[derive(Serialize)]
|
2020-03-28 19:07:55 +01:00
|
|
|
pub struct Context<T> {
|
2020-03-25 22:14:26 +01:00
|
|
|
pub name: String,
|
2020-03-27 10:31:56 +01:00
|
|
|
nav: Vec<Link>,
|
2020-03-27 17:32:18 +01:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
2020-04-01 21:36:35 +02:00
|
|
|
pub t: Option<T>,
|
2020-03-25 22:14:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 19:07:55 +01:00
|
|
|
impl<T> Context<T> {
|
|
|
|
pub fn new_with(active: &str, t: T) -> Self {
|
2020-04-09 22:57:12 +02:00
|
|
|
let nav = Link::build_nav(active);
|
2020-03-27 10:31:56 +01:00
|
|
|
|
|
|
|
Context {
|
2020-04-01 21:36:35 +02:00
|
|
|
nav,
|
|
|
|
name: String::from(""),
|
|
|
|
t: Some(t),
|
2020-03-27 17:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-28 19:07:55 +01:00
|
|
|
}
|
2020-03-27 17:32:18 +01:00
|
|
|
|
2020-03-28 19:07:55 +01:00
|
|
|
impl Context<()> {
|
2020-03-27 17:32:18 +01:00
|
|
|
pub fn new(active: &str) -> Self {
|
2020-04-09 22:57:12 +02:00
|
|
|
let nav = Link::build_nav(active);
|
2020-03-27 17:32:18 +01:00
|
|
|
|
|
|
|
Context {
|
2020-04-01 21:36:35 +02:00
|
|
|
nav,
|
|
|
|
name: String::from(""),
|
|
|
|
t: None,
|
2020-03-27 10:31:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:50:52 +02:00
|
|
|
/// State of current live games
|
2020-03-28 19:07:55 +01:00
|
|
|
pub struct Games {
|
2020-04-10 13:50:52 +02:00
|
|
|
inner: Arc<Mutex<Vec<(String, u64, SystemTime)>>>,
|
2020-03-28 19:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Games {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2020-04-01 21:36:35 +02:00
|
|
|
inner: Arc::new(Mutex::new(Vec::new())),
|
2020-03-28 19:07:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_game(&self, name: String, id: u64) {
|
2020-04-10 13:50:52 +02:00
|
|
|
self.inner
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push((name, id, SystemTime::now()));
|
2020-03-28 19:07:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 13:50:52 +02:00
|
|
|
pub fn get_games(&self) -> Vec<(String, u64, SystemTime)> {
|
2020-03-28 19:07:55 +01:00
|
|
|
self.inner.lock().unwrap().clone()
|
|
|
|
}
|
|
|
|
}
|