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::fs::{create_dir, File};
use std::io::Write;
use std::path::PathBuf;
mod pw_config;
mod pw_protocol;
@ -47,7 +48,7 @@ impl PlanetWarsGame {
log_file: file,
turns: 0,
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
}
fn state(&mut self) -> Value {
json!({
"map": self.map,
})
}
fn is_done(&mut self) -> Option<Value> {
if self.state.is_finished() {
Some(json!({

View file

@ -1,5 +1,6 @@
use serde::{Deserialize};
use serde_json::Value;
use rocket::{Route, State};
use rocket::response::NamedFile;
@ -107,6 +108,7 @@ struct GameReq {
#[derive(Serialize)]
struct GameRes {
players: Vec<u64>,
state: Value,
}
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);
match gm.get_state(game_id).await {
Some(Ok(conns)) => {
Some(Ok((state, conns))) => {
let players: Vec<u64> = conns.iter().map(|conn| match conn {
Connect::Waiting(_, key) => *key,
_ => 0,
}).collect();
Ok(Json(GameRes { players }))
Ok(Json(GameRes { players, state }))
},
Some(Err(v)) => {
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)]
#[serde(tag = "type")]
pub enum GameState {
@ -68,6 +70,7 @@ pub enum GameState {
players: Vec<PlayerStatus>,
connected: usize,
total: usize,
state: Value,
},
}
@ -94,7 +97,6 @@ impl Ord for GameState {
}
}
use std::path::PathBuf;
impl From<FinishedState> for GameState {
fn from(mut state: FinishedState) -> Self {
state.players.sort_by_key(|x| x.0);
@ -105,7 +107,7 @@ impl From<FinishedState> for GameState {
.iter()
.map(|(id, name)| (name.clone(), state.winners.contains(&id)))
.collect(),
map: PathBuf::from(state.map).file_stem().and_then(|x| x.to_str()).unwrap().to_string(),
map: state.map,
name: state.name,
turns: state.turns,
file: state.file,
@ -238,7 +240,7 @@ pub async fn get_states(
for (gs, name) in gss {
if let Some(state) = gs {
match state {
Ok(conns) => {
Ok((state, conns)) => {
let players: Vec<PlayerStatus> =
conns.iter().cloned().map(|x| x.into()).collect();
let connected = players.iter().filter(|x| x.connected).count();
@ -248,6 +250,7 @@ pub async fn get_states(
players,
connected,
map: String::new(),
state,
});
}
Err(value) => {

View file

@ -3,7 +3,7 @@
<input type="checkbox" name="collapse" id="handle_{{loop.index}}">
<h2 class="handle">
<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>
</label>
</h2>