fuck partials
This commit is contained in:
parent
86ffa9726c
commit
b6fa959f1e
3 changed files with 134 additions and 76 deletions
|
@ -72,10 +72,10 @@ async fn debug_get() -> Result<Template, String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/visualizer")]
|
#[get("/visualizer")]
|
||||||
async fn visualizer_get() -> Result<Template, String> {
|
async fn visualizer_get() -> Template {
|
||||||
let game_options = get_games().await?;
|
let game_options = get_games().await;
|
||||||
let context = Context::new_with("Visualizer", ContextT::Games(game_options));
|
let context = Context::new_with("Visualizer", ContextT::Games(game_options));
|
||||||
Ok(Template::render("visualizer", &context))
|
Template::render("visualizer", &context)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/maps/<file>")]
|
#[get("/maps/<file>")]
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
use async_std::prelude::*;
|
|
||||||
use async_std::fs;
|
use async_std::fs;
|
||||||
|
use async_std::prelude::*;
|
||||||
|
|
||||||
static NAV: [(&'static str, &'static str); 5] = [("/", "Home"), ("/lobby", "Lobby"), ("/mapbuilder", "Map Builder"), ("/visualizer", "Visualizer"), ("/debug", "Debug Station")];
|
static NAV: [(&'static str, &'static str); 5] = [
|
||||||
|
("/", "Home"),
|
||||||
|
("/lobby", "Lobby"),
|
||||||
|
("/mapbuilder", "Map Builder"),
|
||||||
|
("/visualizer", "Visualizer"),
|
||||||
|
("/debug", "Debug Station"),
|
||||||
|
];
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
|
@ -19,9 +25,24 @@ pub struct PlayerStatus {
|
||||||
impl From<Connect> for PlayerStatus {
|
impl From<Connect> for PlayerStatus {
|
||||||
fn from(value: Connect) -> Self {
|
fn from(value: Connect) -> Self {
|
||||||
match value {
|
match value {
|
||||||
Connect::Connected(_, name) => PlayerStatus { waiting: false, connected: true, reconnecting: false, value: name },
|
Connect::Connected(_, name) => PlayerStatus {
|
||||||
Connect::Reconnecting(_, name) => PlayerStatus { waiting: false, connected: true, reconnecting: true, value: name },
|
waiting: false,
|
||||||
Connect::Waiting(_, key) => PlayerStatus { waiting: true, connected: false, reconnecting: false, value: format!("Key: {}", key) },
|
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),
|
||||||
|
},
|
||||||
_ => panic!("No playerstatus possible from Connect::Request"),
|
_ => panic!("No playerstatus possible from Connect::Request"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +56,7 @@ pub enum GameState {
|
||||||
map: String,
|
map: String,
|
||||||
players: Vec<(String, bool)>,
|
players: Vec<(String, bool)>,
|
||||||
turns: u64,
|
turns: u64,
|
||||||
|
file: String,
|
||||||
},
|
},
|
||||||
Playing {
|
Playing {
|
||||||
name: String,
|
name: String,
|
||||||
|
@ -42,7 +64,7 @@ pub enum GameState {
|
||||||
players: Vec<PlayerStatus>,
|
players: Vec<PlayerStatus>,
|
||||||
connected: usize,
|
connected: usize,
|
||||||
total: usize,
|
total: usize,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
@ -56,19 +78,31 @@ impl PartialOrd for GameState {
|
||||||
impl Ord for GameState {
|
impl Ord for GameState {
|
||||||
fn cmp(&self, other: &GameState) -> Ordering {
|
fn cmp(&self, other: &GameState) -> Ordering {
|
||||||
match self {
|
match self {
|
||||||
GameState::Finished { name, .. } => {
|
GameState::Finished { name, .. } => match other {
|
||||||
match other {
|
|
||||||
GameState::Finished { name: _name, .. } => name.cmp(_name),
|
GameState::Finished { name: _name, .. } => name.cmp(_name),
|
||||||
_ => Ordering::Greater,
|
_ => Ordering::Greater,
|
||||||
}
|
|
||||||
},
|
},
|
||||||
GameState::Playing { name, .. } => {
|
GameState::Playing { name, .. } => match other {
|
||||||
match other {
|
|
||||||
GameState::Playing { name: _name, .. } => name.cmp(_name),
|
GameState::Playing { name: _name, .. } => name.cmp(_name),
|
||||||
_ => Ordering::Less,
|
_ => Ordering::Less,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<FinishedState> for GameState {
|
||||||
|
fn from(state: FinishedState) -> Self {
|
||||||
|
GameState::Finished {
|
||||||
|
map: String::new(),
|
||||||
|
players: state
|
||||||
|
.players
|
||||||
|
.iter()
|
||||||
|
.map(|(id, name)| (name.clone(), state.winners.contains(&id)))
|
||||||
|
.collect(),
|
||||||
|
name: state.name,
|
||||||
|
turns: state.turns,
|
||||||
|
file: state.file,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +118,7 @@ impl GameOption {
|
||||||
Self {
|
Self {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
location: location.to_string(),
|
location: location.to_string(),
|
||||||
turns
|
turns,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +139,7 @@ pub struct Lobby {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub enum ContextT {
|
pub enum ContextT {
|
||||||
Games(Vec<GameOption>),
|
Games(Vec<GameState>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
|
@ -114,106 +148,123 @@ pub struct Context<T> {
|
||||||
nav: Vec<Link>,
|
nav: Vec<Link>,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub t: Option<T>
|
pub t: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Context<T> {
|
impl<T> Context<T> {
|
||||||
pub fn new_with(active: &str, t: T) -> Self {
|
pub fn new_with(active: &str, t: T) -> Self {
|
||||||
let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect();
|
let nav = NAV
|
||||||
|
.iter()
|
||||||
|
.map(|(href, name)| Link {
|
||||||
|
name: name.to_string(),
|
||||||
|
href: href.to_string(),
|
||||||
|
active: *name == active,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
Context {
|
Context {
|
||||||
nav, name: String::from(""), t: Some(t)
|
nav,
|
||||||
|
name: String::from(""),
|
||||||
|
t: Some(t),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context<()> {
|
impl Context<()> {
|
||||||
pub fn new(active: &str) -> Self {
|
pub fn new(active: &str) -> Self {
|
||||||
let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect();
|
let nav = NAV
|
||||||
|
.iter()
|
||||||
|
.map(|(href, name)| Link {
|
||||||
|
name: name.to_string(),
|
||||||
|
href: href.to_string(),
|
||||||
|
active: *name == active,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
Context {
|
Context {
|
||||||
nav, name: String::from(""), t: None,
|
nav,
|
||||||
|
name: String::from(""),
|
||||||
|
t: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_maps() -> Result<Vec<Map>, String> {
|
pub async fn get_maps() -> Result<Vec<Map>, String> {
|
||||||
let mut maps = Vec::new();
|
let mut maps = Vec::new();
|
||||||
let mut entries = fs::read_dir("maps").await.map_err(|_| "IO error".to_string())?;
|
let mut entries = fs::read_dir("maps")
|
||||||
|
.await
|
||||||
|
.map_err(|_| "IO error".to_string())?;
|
||||||
while let Some(file) = entries.next().await {
|
while let Some(file) = entries.next().await {
|
||||||
let file = file.map_err(|_| "IO error".to_string())?.path();
|
let file = file.map_err(|_| "IO error".to_string())?.path();
|
||||||
if let Some(stem) = file.file_stem().and_then(|x| x.to_str()) {
|
if let Some(stem) = file.file_stem().and_then(|x| x.to_str()) {
|
||||||
maps.push(Map { name: stem.to_string(), url: file.to_str().unwrap().to_string() });
|
maps.push(Map {
|
||||||
|
name: stem.to_string(),
|
||||||
|
url: file.to_str().unwrap().to_string(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(maps)
|
Ok(maps)
|
||||||
}
|
}
|
||||||
|
|
||||||
use ini::Ini;
|
use async_std::io::BufReader;
|
||||||
pub async fn get_games() -> Result<Vec<GameOption>, String> {
|
use futures::stream::StreamExt;
|
||||||
let mut games = Vec::new();
|
pub async fn get_games() -> Vec<GameState> {
|
||||||
|
match fs::File::open("games.ini").await {
|
||||||
let content = match fs::read_to_string("games.ini").await {
|
Ok(file) => {
|
||||||
Ok(v) => v,
|
let mut file = BufReader::new(file);
|
||||||
Err(_) => {
|
file.lines().filter_map(move |maybe| async {
|
||||||
fs::File::create("games.ini").await.map_err(|_| "IO Error".to_string())?;
|
maybe
|
||||||
String::new()
|
.ok()
|
||||||
|
.and_then(|line| serde_json::from_str::<FinishedState>(&line).ok())
|
||||||
|
}).map(|state| state.into()).collect().await
|
||||||
}
|
}
|
||||||
};
|
Err(_) => Vec::new(),
|
||||||
|
|
||||||
let i = Ini::load_from_str(&content).map_err(|_| "Corrupt ini file".to_string())?;
|
|
||||||
|
|
||||||
for (sec, prop) in i.iter() {
|
|
||||||
if let Some(sec) = sec {
|
|
||||||
let name = match prop.get("name") { None => continue, Some(v) => v};
|
|
||||||
let turns = match prop.get("turns").and_then(|v| v.parse().ok()) { None => continue, Some(v) => v};
|
|
||||||
games.push(GameOption::new(name, sec, turns));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(games)
|
|
||||||
}
|
|
||||||
|
|
||||||
use crate::planetwars::FinishedState;
|
use crate::planetwars::FinishedState;
|
||||||
|
|
||||||
|
use futures::future::{join_all, FutureExt};
|
||||||
use mozaic::modules::game;
|
use mozaic::modules::game;
|
||||||
use mozaic::util::request::Connect;
|
use mozaic::util::request::Connect;
|
||||||
use futures::future::{join_all, FutureExt};
|
pub async fn get_states(
|
||||||
pub async fn get_states(game_ids: &Vec<(String, u64)>, manager: &game::Manager) -> Result<Vec<GameState>, String> {
|
game_ids: &Vec<(String, u64)>,
|
||||||
|
manager: &game::Manager,
|
||||||
|
) -> Result<Vec<GameState>, String> {
|
||||||
let mut states = Vec::new();
|
let mut states = Vec::new();
|
||||||
let gss = join_all(game_ids.iter().cloned().map(|(name, id)| manager.get_state(id).map(move |f| (f, name)))).await;
|
let gss = join_all(
|
||||||
|
game_ids
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.map(|(name, id)| manager.get_state(id).map(move |f| (f, name))),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
for (gs, name) in gss {
|
for (gs, name) in gss {
|
||||||
if let Some(state) = gs {
|
if let Some(state) = gs {
|
||||||
match state {
|
match state {
|
||||||
Ok(conns) => {
|
Ok(conns) => {
|
||||||
let players: Vec<PlayerStatus> = conns.iter().cloned().map(|x| x.into()).collect();
|
let players: Vec<PlayerStatus> =
|
||||||
|
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(
|
states.push(GameState::Playing {
|
||||||
GameState::Playing { name: name, total: players.len(), players, connected, map: String::new(), }
|
name: name,
|
||||||
);
|
total: players.len(),
|
||||||
},
|
players,
|
||||||
|
connected,
|
||||||
|
map: String::new(),
|
||||||
|
});
|
||||||
|
}
|
||||||
Err(value) => {
|
Err(value) => {
|
||||||
let state: FinishedState = serde_json::from_value(value).expect("Shit failed");
|
let state: FinishedState = serde_json::from_value(value).expect("Shit failed");
|
||||||
states.push(
|
states.push(state.into());
|
||||||
GameState::Finished {
|
|
||||||
map: String::new(),
|
|
||||||
players: state.players.iter().map(|(id, name)| (name.clone(), state.winners.contains(&id))).collect(),
|
|
||||||
name: state.name,
|
|
||||||
turns: state.turns,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
states.sort();
|
states.sort();
|
||||||
println!(
|
|
||||||
"{}", serde_json::to_string_pretty(&states).unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(states)
|
Ok(states)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,7 +277,7 @@ pub struct Games {
|
||||||
impl Games {
|
impl Games {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: Arc::new(Mutex::new(Vec::new()))
|
inner: Arc::new(Mutex::new(Vec::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<link rel="stylesheet" type="text/css" href="/style/visualizer.css">
|
<link rel="stylesheet" type="text/css" href="/style/visualizer.css">
|
||||||
|
<link rel="stylesheet" type="text/css" href="/style/state.css">
|
||||||
|
|
||||||
<input type="file" id="fileselect" style="display: none">
|
<input type="file" id="fileselect" style="display: none">
|
||||||
<div id=wrapper>
|
<div id=wrapper>
|
||||||
|
@ -26,10 +27,16 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="options">
|
<div class="options">
|
||||||
{% for game in games %}
|
{% for state in games %}
|
||||||
<div class="option" onclick="visualizer.handle('/games/{{ game.location}}', '{{ game.name }}')">
|
<div class="option" onclick="visualizer.handle('/games/{{ state.file }}', '{{ state.name }}')">
|
||||||
<p>{{game.name}}</p>
|
<p>{{state.name}} ({{ state.map }}) <span style="float: right;">{{state.turns}} turns</span></p>
|
||||||
<p>Turns: {{game.turns}}</p>
|
<div class="content">
|
||||||
|
<div class="players">
|
||||||
|
{% for player in state.players %}
|
||||||
|
<p class="{% if player[1] %}winner{% endif %}">{{ player[0] }}</p>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue