make map visible, all of the time

This commit is contained in:
ajuvercr 2020-04-02 09:56:47 +02:00
parent eb8f4d514f
commit 463a40c3fb
4 changed files with 19 additions and 7 deletions

View file

@ -7,6 +7,7 @@ use std::collections::HashMap;
use std::convert::TryInto; 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;
mod pw_config; mod pw_config;
mod pw_protocol; mod pw_protocol;
@ -47,7 +48,7 @@ impl PlanetWarsGame {
log_file: file, log_file: file,
turns: 0, turns: 0,
name: name.to_string(), name: name.to_string(),
map: map.to_string(), map: PathBuf::from(map).file_stem().and_then(|x| x.to_str()).unwrap().to_string(),
} }
} }
@ -194,6 +195,12 @@ impl game::Controller for PlanetWarsGame {
updates updates
} }
fn state(&mut self) -> Value {
json!({
"map": self.map,
})
}
fn is_done(&mut self) -> Option<Value> { fn is_done(&mut self) -> Option<Value> {
if self.state.is_finished() { if self.state.is_finished() {
Some(json!({ Some(json!({

View file

@ -1,5 +1,6 @@
use serde::{Deserialize}; use serde::{Deserialize};
use serde_json::Value;
use rocket::{Route, State}; use rocket::{Route, State};
use rocket::response::NamedFile; use rocket::response::NamedFile;
@ -107,6 +108,7 @@ struct GameReq {
#[derive(Serialize)] #[derive(Serialize)]
struct GameRes { struct GameRes {
players: Vec<u64>, players: Vec<u64>,
state: Value,
} }
use mozaic::util::request::Connect; use mozaic::util::request::Connect;
@ -117,13 +119,13 @@ async fn game_post(game_req: Json<GameReq>, tp: State<'_, ThreadPool>, gm: State
state.add_game(game_req.name.clone(), game_id); state.add_game(game_req.name.clone(), game_id);
match gm.get_state(game_id).await { match gm.get_state(game_id).await {
Some(Ok(conns)) => { Some(Ok((state, conns))) => {
let players: Vec<u64> = conns.iter().map(|conn| match conn { let players: Vec<u64> = conns.iter().map(|conn| match conn {
Connect::Waiting(_, key) => *key, Connect::Waiting(_, key) => *key,
_ => 0, _ => 0,
}).collect(); }).collect();
Ok(Json(GameRes { players })) Ok(Json(GameRes { players, state }))
}, },
Some(Err(v)) => { Some(Err(v)) => {
Err(serde_json::to_string(&v).unwrap()) Err(serde_json::to_string(&v).unwrap())

View file

@ -52,6 +52,8 @@ impl From<Connect> for PlayerStatus {
} }
} }
use serde_json::Value;
#[derive(Serialize, Eq, PartialEq)] #[derive(Serialize, Eq, PartialEq)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum GameState { pub enum GameState {
@ -68,6 +70,7 @@ pub enum GameState {
players: Vec<PlayerStatus>, players: Vec<PlayerStatus>,
connected: usize, connected: usize,
total: usize, total: usize,
state: Value,
}, },
} }
@ -94,7 +97,6 @@ impl Ord for GameState {
} }
} }
use std::path::PathBuf;
impl From<FinishedState> for GameState { impl From<FinishedState> for GameState {
fn from(mut state: FinishedState) -> Self { fn from(mut state: FinishedState) -> Self {
state.players.sort_by_key(|x| x.0); state.players.sort_by_key(|x| x.0);
@ -105,7 +107,7 @@ impl From<FinishedState> for GameState {
.iter() .iter()
.map(|(id, name)| (name.clone(), state.winners.contains(&id))) .map(|(id, name)| (name.clone(), state.winners.contains(&id)))
.collect(), .collect(),
map: PathBuf::from(state.map).file_stem().and_then(|x| x.to_str()).unwrap().to_string(), map: state.map,
name: state.name, name: state.name,
turns: state.turns, turns: state.turns,
file: state.file, file: state.file,
@ -238,7 +240,7 @@ pub async fn get_states(
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((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();
@ -248,6 +250,7 @@ pub async fn get_states(
players, players,
connected, connected,
map: String::new(), map: String::new(),
state,
}); });
} }
Err(value) => { Err(value) => {

View file

@ -3,7 +3,7 @@
<input type="checkbox" name="collapse" id="handle_{{loop.index}}"> <input type="checkbox" name="collapse" id="handle_{{loop.index}}">
<h2 class="handle"> <h2 class="handle">
<label for="handle_{{loop.index}}"> <label for="handle_{{loop.index}}">
<span>{{state.name}} ({{state.map}})</span> <span>{{state.name}} ({% if state.state %}{{state.state.map}}{% else %}{{state.map}}{% endif %})</span>
<span style="float: right">{% if state.type == "Playing" %}{{ state.connected }}/{{state.total}}{% endif %}</span> <span style="float: right">{% if state.type == "Playing" %}{{ state.connected }}/{{state.total}}{% endif %}</span>
</label> </label>
</h2> </h2>