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-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-01 22:16:55 +02:00
|
|
|
pub static COLOURS: [&'static str; 9] = [
|
|
|
|
"gray", "blue", "cyan", "green", "yellow", "orange", "red", "pink", "purple",
|
|
|
|
];
|
|
|
|
|
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-01 20:49:50 +02:00
|
|
|
#[derive(Serialize, Eq, PartialEq)]
|
|
|
|
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-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-01 20:49:50 +02:00
|
|
|
#[derive(Serialize, Eq, PartialEq)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum GameState {
|
|
|
|
Finished {
|
|
|
|
name: String,
|
|
|
|
map: String,
|
|
|
|
players: Vec<(String, bool)>,
|
|
|
|
turns: u64,
|
2020-04-01 21:36:35 +02:00
|
|
|
file: String,
|
2020-04-01 20:49:50 +02:00
|
|
|
},
|
|
|
|
Playing {
|
|
|
|
name: String,
|
|
|
|
map: String,
|
|
|
|
players: Vec<PlayerStatus>,
|
|
|
|
connected: usize,
|
|
|
|
total: usize,
|
2020-04-02 09:56:47 +02:00
|
|
|
state: Value,
|
2020-04-01 21:36:35 +02:00
|
|
|
},
|
2020-04-01 20:49:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for GameState {
|
|
|
|
fn partial_cmp(&self, other: &GameState) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for GameState {
|
|
|
|
fn cmp(&self, other: &GameState) -> Ordering {
|
|
|
|
match self {
|
2020-04-01 21:36:35 +02:00
|
|
|
GameState::Finished { name, .. } => match other {
|
|
|
|
GameState::Finished { name: _name, .. } => name.cmp(_name),
|
|
|
|
_ => Ordering::Greater,
|
2020-04-01 20:49:50 +02:00
|
|
|
},
|
2020-04-01 21:36:35 +02:00
|
|
|
GameState::Playing { name, .. } => match other {
|
|
|
|
GameState::Playing { name: _name, .. } => name.cmp(_name),
|
|
|
|
_ => Ordering::Less,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-09 22:57:12 +02:00
|
|
|
/// Games is the game manager wrapper so Rocket can manage it
|
2020-03-28 19:07:55 +01:00
|
|
|
pub struct Games {
|
|
|
|
inner: Arc<Mutex<Vec<(String, u64)>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
self.inner.lock().unwrap().push((name, id));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_games(&self) -> Vec<(String, u64)> {
|
|
|
|
self.inner.lock().unwrap().clone()
|
|
|
|
}
|
|
|
|
}
|