From 7c2896ddb7494446d7ecdba6efbd24e011b2b054 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Sat, 28 Mar 2020 20:55:39 +0100 Subject: [PATCH] sort games + render partial --- backend/src/routes.rs | 11 ++++++++++- backend/src/util.rs | 14 ++++++++++---- backend/static/script/maps.js | 23 ++++++++++++++++++----- backend/templates/lobby.html.tera | 16 +--------------- backend/templates/state_partial.html.tera | 14 ++++++++++++++ 5 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 backend/templates/state_partial.html.tera diff --git a/backend/src/routes.rs b/backend/src/routes.rs index be1011a..575c0cb 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -81,6 +81,15 @@ async fn map_get(file: String) -> Result { Ok(Template::render("map_partial", &serde_json::from_str::(&content).unwrap())) } +#[get("/partial/state")] +async fn state_get(gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result { + let games = get_states(&state.get_games(), &gm).await?; + let context = Context::new_with("Lobby", Lobby { games, maps: Vec::new() }); + + Ok(Template::render("state_partial", &context)) +} + + #[derive(Deserialize, Debug)] struct GameReq { nop: u64, @@ -98,7 +107,7 @@ async fn game_post(game_req: Json, tp: State<'_, ThreadPool>, gm: State } pub fn fuel(routes: &mut Vec) { - routes.extend(routes![files, index, map_post, map_get, lobby_get, builder_get, visualizer_get, game_post]); + routes.extend(routes![files, index, map_post, map_get, lobby_get, builder_get, visualizer_get, game_post, state_get]); } diff --git a/backend/src/util.rs b/backend/src/util.rs index b3fe487..94b5a0f 100644 --- a/backend/src/util.rs +++ b/backend/src/util.rs @@ -130,14 +130,18 @@ pub async fn get_states(game_ids: &Vec<(String, u64)>, manager: &game::Manager) for (gs, name) in gss { if let Some(state) = gs { + let mut players: Vec = state.iter().map(|conn| match conn { + Connect::Waiting(_, key) => format!("Waiting {}", key), + _ => String::from("Some connected player") + }).collect(); + + players.sort(); + states.push( GameState { name, turns: None, - players: state.iter().map(|conn| match conn { - Connect::Waiting(_, key) => format!("Waiting {}", key), - _ => String::from("Some connected player"), - }).collect(), + players: players, finished: false, } ) @@ -153,6 +157,8 @@ pub async fn get_states(game_ids: &Vec<(String, u64)>, manager: &game::Manager) } } + states.sort_by_key(|a| a.name.clone()); + Ok(states) } diff --git a/backend/static/script/maps.js b/backend/static/script/maps.js index 7bf6df9..ee173da 100644 --- a/backend/static/script/maps.js +++ b/backend/static/script/maps.js @@ -1,5 +1,5 @@ const ids = {}; -["map_holder", "name", "turns", "nop"].forEach(id => ids[id] = document.getElementById(id)); +["map_holder", "name", "turns", "nop", "lobby"].forEach(id => ids[id] = document.getElementById(id)); var last_map; var last_url; @@ -15,6 +15,11 @@ async function handle_map_click(url, event) { ids["map_holder"].innerHTML = await c.text(); } +async function refresh_state() { + const c = await fetch("/partial/state"); + ids["lobby"].innerHTML = await c.text(); +} + async function start_game() { const obj = { "nop": parseInt(ids["nop"].value), @@ -23,17 +28,25 @@ async function start_game() { "max_turns": parseInt(ids["turns"].value), }; - console.log(obj); - const xhr = new XMLHttpRequest(); xhr.onreadystatechange = async function() { console.log(this); - // console.log(await this.text()); - // TODO: make response visible }; xhr.open("POST", "/lobby"); xhr.send(JSON.stringify(obj)); + + setTimeout( + () => refresh_state(), + 200 + ); } + +window.onload = () => refresh_state(); + +setInterval( + () => refresh_state(), + 1000 +); diff --git a/backend/templates/lobby.html.tera b/backend/templates/lobby.html.tera index c18aa5f..45ca723 100644 --- a/backend/templates/lobby.html.tera +++ b/backend/templates/lobby.html.tera @@ -2,21 +2,7 @@ {% block content %}
-
- {% for state in games %} -
-
-

{{state.name}}

- {% if state.finished %}

Finished

{% endif %} - {% if state.turns %}

Turns: {{ state.turns }}

{% endif %} -
-
- {% for player in state.players %} -

{{ player }}

- {% endfor %} -
-
- {% endfor %} +

Start new game

diff --git a/backend/templates/state_partial.html.tera b/backend/templates/state_partial.html.tera new file mode 100644 index 0000000..339ea2a --- /dev/null +++ b/backend/templates/state_partial.html.tera @@ -0,0 +1,14 @@ +{% for state in games %} +
+
+

{{state.name}}

+ {% if state.finished %}

Finished

{% endif %} + {% if state.turns %}

Turns: {{ state.turns }}

{% endif %} +
+
+ {% for player in state.players %} +

{{ player }}

+ {% endfor %} +
+
+{% endfor %}