parent
05925f0622
commit
fd3178060a
6 changed files with 55 additions and 44 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "backend"
|
name = "backend"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["ajuvercr <arthur.vercruysse@ugent.be>"]
|
authors = ["ajuvercr <arthur.vercruysse@ugent.be>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
@ -22,4 +22,4 @@ tracing-subscriber = "0.1.5"
|
||||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" }
|
rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "async" }
|
||||||
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", branch = "async", features = ["handlebars_templates", "tera_templates"] }
|
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", branch = "async", features = ["handlebars_templates", "tera_templates"] }
|
||||||
|
|
||||||
rust-ini = "0.15.2"
|
educe = { version = "0.4.2", features = ["Debug", "Default", "Hash", "Clone", "Copy"] }
|
||||||
|
|
|
@ -19,7 +19,8 @@ extern crate tracing_subscriber;
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
extern crate rocket_contrib;
|
extern crate rocket_contrib;
|
||||||
|
|
||||||
extern crate ini;
|
#[macro_use]
|
||||||
|
extern crate educe;
|
||||||
|
|
||||||
use tracing_subscriber::{EnvFilter, FmtSubscriber};
|
use tracing_subscriber::{EnvFilter, FmtSubscriber};
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ use std::convert::TryInto;
|
||||||
use std::fs::{create_dir, File};
|
use std::fs::{create_dir, File};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
mod pw_config;
|
mod pw_config;
|
||||||
mod pw_protocol;
|
mod pw_protocol;
|
||||||
|
@ -213,6 +214,7 @@ impl game::Controller for PlanetWarsGame {
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
"map": self.map,
|
"map": self.map,
|
||||||
"file": self.log_file_loc,
|
"file": self.log_file_loc,
|
||||||
|
"time": SystemTime::now(),
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -220,6 +222,10 @@ impl game::Controller for PlanetWarsGame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_epoch() -> SystemTime {
|
||||||
|
SystemTime::UNIX_EPOCH
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct FinishedState {
|
pub struct FinishedState {
|
||||||
pub winners: Vec<u64>,
|
pub winners: Vec<u64>,
|
||||||
|
@ -227,5 +233,7 @@ pub struct FinishedState {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub file: String,
|
pub file: String,
|
||||||
pub map: String,
|
pub map: String,
|
||||||
|
#[serde(default = "get_epoch")]
|
||||||
|
pub time: SystemTime,
|
||||||
pub players: Vec<(u64, String)>,
|
pub players: Vec<(u64, String)>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::planetwars;
|
use crate::planetwars::{self, FinishedState};
|
||||||
use crate::util::*;
|
use crate::util::*;
|
||||||
|
|
||||||
use rocket::{Route, State};
|
use rocket::{Route, State};
|
||||||
|
@ -13,10 +13,12 @@ use async_std::fs;
|
||||||
use async_std::prelude::StreamExt;
|
use async_std::prelude::StreamExt;
|
||||||
|
|
||||||
use futures::executor::ThreadPool;
|
use futures::executor::ThreadPool;
|
||||||
|
use futures::future::{join_all, FutureExt};
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
/// The type required to build a game.
|
/// The type required to build a game.
|
||||||
/// (json in POST request).
|
/// (json in POST request).
|
||||||
|
@ -174,11 +176,8 @@ async fn get_maps() -> Result<Vec<Map>, String> {
|
||||||
Ok(maps)
|
Ok(maps)
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::planetwars::FinishedState;
|
|
||||||
|
|
||||||
use futures::future::{join_all, FutureExt};
|
|
||||||
pub async fn get_states(
|
pub async fn get_states(
|
||||||
game_ids: &Vec<(String, u64)>,
|
game_ids: &Vec<(String, u64, SystemTime)>,
|
||||||
manager: &game::Manager,
|
manager: &game::Manager,
|
||||||
) -> Result<Vec<GameState>, String> {
|
) -> Result<Vec<GameState>, String> {
|
||||||
let mut states = Vec::new();
|
let mut states = Vec::new();
|
||||||
|
@ -186,17 +185,18 @@ pub async fn get_states(
|
||||||
game_ids
|
game_ids
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.map(|(name, id)| manager.get_state(id).map(move |f| (f, name))),
|
.map(|(name, id, time)| manager.get_state(id).map(move |f| (f, name, time))),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for (gs, name) in gss {
|
for (gs, name, time) in gss {
|
||||||
if let Some(state) = gs {
|
if let Some(state) = gs {
|
||||||
match state {
|
match state {
|
||||||
Ok((state, conns)) => {
|
Ok((state, conns)) => {
|
||||||
let players: Vec<PlayerStatus> =
|
let players: Vec<PlayerStatus> =
|
||||||
conns.iter().cloned().map(|x| x.into()).collect();
|
conns.iter().cloned().map(|x| x.into()).collect();
|
||||||
let connected = players.iter().filter(|x| x.connected).count();
|
let connected = players.iter().filter(|x| x.connected).count();
|
||||||
|
|
||||||
states.push(GameState::Playing {
|
states.push(GameState::Playing {
|
||||||
name: name,
|
name: name,
|
||||||
total: players.len(),
|
total: players.len(),
|
||||||
|
@ -204,6 +204,7 @@ pub async fn get_states(
|
||||||
connected,
|
connected,
|
||||||
map: String::new(),
|
map: String::new(),
|
||||||
state,
|
state,
|
||||||
|
time,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Err(value) => {
|
Err(value) => {
|
||||||
|
|
|
@ -47,7 +47,9 @@ async fn debug_get() -> Result<Template, String> {
|
||||||
/// Routes the visualizer page, rendering the visualizer Template.
|
/// Routes the visualizer page, rendering the visualizer Template.
|
||||||
#[get("/visualizer")]
|
#[get("/visualizer")]
|
||||||
async fn visualizer_get() -> Template {
|
async fn visualizer_get() -> Template {
|
||||||
let game_options = get_played_games().await;
|
let mut game_options: Vec<GameState> = get_played_games().await;
|
||||||
|
game_options.sort();
|
||||||
|
|
||||||
let context = Context::new_with(
|
let context = Context::new_with(
|
||||||
"Visualizer",
|
"Visualizer",
|
||||||
json!({"games": game_options, "colours": COLOURS}),
|
json!({"games": game_options, "colours": COLOURS}),
|
||||||
|
|
|
@ -4,6 +4,7 @@ use serde_json::Value;
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
static NAV: [(&'static str, &'static str); 6] = [
|
static NAV: [(&'static str, &'static str); 6] = [
|
||||||
("/", "Home"),
|
("/", "Home"),
|
||||||
|
@ -20,7 +21,8 @@ pub static COLOURS: [&'static str; 9] = [
|
||||||
|
|
||||||
/// The state of a player, in a running game.
|
/// The state of a player, in a running game.
|
||||||
/// This represents actual players or connection keys.
|
/// This represents actual players or connection keys.
|
||||||
#[derive(Serialize, Eq, PartialEq)]
|
#[derive(Serialize, Educe)]
|
||||||
|
#[educe(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct PlayerStatus {
|
pub struct PlayerStatus {
|
||||||
pub waiting: bool,
|
pub waiting: bool,
|
||||||
pub connected: bool,
|
pub connected: bool,
|
||||||
|
@ -53,48 +55,41 @@ impl From<Connect> for PlayerStatus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn partial_cmp(a: &SystemTime, b: &SystemTime) -> Option<Ordering> {
|
||||||
|
b.partial_cmp(a)
|
||||||
|
}
|
||||||
|
|
||||||
/// The GameState is the state of a game.
|
/// The GameState is the state of a game.
|
||||||
/// Either Finished, so the game is done, not running, and there is a posible visualization.
|
/// 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.
|
/// Or Playing, the game is still being managed by the mozaic framework.
|
||||||
#[derive(Serialize, Eq, PartialEq)]
|
#[derive(Serialize, Educe)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
|
#[educe(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub enum GameState {
|
pub enum GameState {
|
||||||
Finished {
|
#[educe(PartialOrd(rank = 1))]
|
||||||
name: String,
|
|
||||||
map: String,
|
|
||||||
players: Vec<(String, bool)>,
|
|
||||||
turns: u64,
|
|
||||||
file: String,
|
|
||||||
},
|
|
||||||
Playing {
|
Playing {
|
||||||
|
#[educe(PartialOrd(method = "partial_cmp"))]
|
||||||
|
time: SystemTime,
|
||||||
|
|
||||||
name: String,
|
name: String,
|
||||||
map: String,
|
map: String,
|
||||||
players: Vec<PlayerStatus>,
|
players: Vec<PlayerStatus>,
|
||||||
connected: usize,
|
connected: usize,
|
||||||
total: usize,
|
total: usize,
|
||||||
|
#[educe(Ord(ignore), PartialOrd(ignore))]
|
||||||
state: Value,
|
state: Value,
|
||||||
},
|
},
|
||||||
}
|
#[educe(PartialOrd(rank = 2))]
|
||||||
|
Finished {
|
||||||
|
#[educe(PartialOrd(method = "partial_cmp"))]
|
||||||
|
time: SystemTime,
|
||||||
|
name: String,
|
||||||
|
map: String,
|
||||||
|
|
||||||
impl PartialOrd for GameState {
|
players: Vec<(String, bool)>,
|
||||||
fn partial_cmp(&self, other: &GameState) -> Option<Ordering> {
|
turns: u64,
|
||||||
Some(self.cmp(other))
|
file: String,
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for GameState {
|
|
||||||
fn cmp(&self, other: &GameState) -> Ordering {
|
|
||||||
match self {
|
|
||||||
GameState::Finished { name, .. } => match other {
|
|
||||||
GameState::Finished { name: _name, .. } => name.cmp(_name),
|
|
||||||
_ => Ordering::Greater,
|
|
||||||
},
|
},
|
||||||
GameState::Playing { name, .. } => match other {
|
|
||||||
GameState::Playing { name: _name, .. } => name.cmp(_name),
|
|
||||||
_ => Ordering::Less,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FinishedState> for GameState {
|
impl From<FinishedState> for GameState {
|
||||||
|
@ -111,6 +106,7 @@ impl From<FinishedState> for GameState {
|
||||||
name: state.name,
|
name: state.name,
|
||||||
turns: state.turns,
|
turns: state.turns,
|
||||||
file: state.file,
|
file: state.file,
|
||||||
|
time: state.time,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,9 +167,9 @@ impl Context<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Games is the game manager wrapper so Rocket can manage it
|
/// State of current live games
|
||||||
pub struct Games {
|
pub struct Games {
|
||||||
inner: Arc<Mutex<Vec<(String, u64)>>>,
|
inner: Arc<Mutex<Vec<(String, u64, SystemTime)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Games {
|
impl Games {
|
||||||
|
@ -184,10 +180,13 @@ impl Games {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_game(&self, name: String, id: u64) {
|
pub fn add_game(&self, name: String, id: u64) {
|
||||||
self.inner.lock().unwrap().push((name, id));
|
self.inner
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.push((name, id, SystemTime::now()));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_games(&self) -> Vec<(String, u64)> {
|
pub fn get_games(&self) -> Vec<(String, u64, SystemTime)> {
|
||||||
self.inner.lock().unwrap().clone()
|
self.inner.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue