From 498a78085474277191999fdcac1138e559e07fe4 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Sat, 28 Mar 2020 19:07:55 +0100 Subject: [PATCH] fancy shit yo --- backend/.gitignore | 3 + backend/games.ini | 111 ---------------------------- backend/src/main.rs | 2 + backend/src/routes.rs | 14 ++-- backend/src/util.rs | 88 ++++++++++++++++++++-- backend/static/script/mapbuilder.js | 12 ++- backend/static/script/maps.js | 2 +- backend/static/style/style.css | 16 +++- backend/templates/lobby.html.tera | 17 ++++- 9 files changed, 133 insertions(+), 132 deletions(-) delete mode 100644 backend/games.ini diff --git a/backend/.gitignore b/backend/.gitignore index f889b68..7700136 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,6 @@ target/ Cargo.lock games/ +maps/ +trace.log +games.ini diff --git a/backend/games.ini b/backend/games.ini deleted file mode 100644 index 814de4e..0000000 --- a/backend/games.ini +++ /dev/null @@ -1,111 +0,0 @@ -path=static/games/ - -[Alisha Lara.json] -name=Alisha Lara -turns=143 -players= "SimpleBot 2 (check if python is correct before executing)" "mybot2" - -[Andre Head.json] -name=Andre Head -turns=1229 -players= "mybot2" "SimpleBot 2 (check if python is correct before executing)" "mybot" - -[Angela Sims.json] -name=Angela Sims -turns=221 -players= "SimpleBot 2 (check if python is correct before executing)" "mybot2" - -[Buford Humphrey.json] -name=Buford Humphrey -turns=174 -players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" - -[Catalina Grant.json] -name=Catalina Grant -turns=202 -players= "mybot" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" - -[Cesar Jordan.json] -name=Cesar Jordan -turns=214 -players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" - -[Chandra Garrett.json] -name=Chandra Garrett -turns=202 -players= "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" - -[Cory Mooney.json] -name=Cory Mooney -turns=45 -players= "SimpleBot 2 (check if python is correct before executing)" "SimpleBot 1 (check if python is correct before executing)" - -[Deana Bishop.json] -name=Deana Bishop -turns=302 -players= "mybot" "mybot2" - -[game2.json] -name=game2 -turns=110 -players="bot_auto_player_full_rand" "adversary_auto_player_link_rand" - -[Georgina Wiley.json] -name=Georgina Wiley -turns=302 -players= "mybot2" "mybot" - -[hungergames.json] -name=hungergames -turns=119 -players= - -[Jeannie Swanson.json] -name=Jeannie Swanson -turns=251 -players= "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" "mybot2" "mybot" - -[Joanne Blair.json] -name=Joanne Blair -turns=202 -players= "mybot2" "mybot" "SimpleBot 1 (check if python is correct before executing)" "SimpleBot 2 (check if python is correct before executing)" - -[large.json] -name=large -turns=1166 -players= - -[Lester Berg.json] -name=Lester Berg -turns=1132 -players= "mybot" "mybot2" "SimpleBot 2 (check if python is correct before executing)" - -[Lindsey Farrell.json] -name=Lindsey Farrell -turns=461 -players= "jens" "SimpleBot 2 (check if python is correct before executing)" "dinkske" "SimpleBot 1 (check if python is correct before executing)" - -[Pansy Le.json] -name=Pansy Le -turns=417 -players= "Subutai" "SimpleBot 1 (check if python is correct before executing)" "SammyBot" - -[pietmap.json] -name=pietmap -turns=2788 -players= - -[Randell Wolfe.json] -name=Randell Wolfe -turns=300 -players= "mybot" "SimpleBot 2 (check if python is correct before executing)" - -[spiral2.json] -name=spiral2 -turns=537 -players= - -[standard_game.json] -name=standard_game -turns=82 -players="bot_auto_player_full_rand" "adversary_auto_player_orfirst_rand" diff --git a/backend/src/main.rs b/backend/src/main.rs index 4d85271..c2aed37 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -35,6 +35,7 @@ use mozaic::modules::*; mod planetwars; mod routes; mod util; +use util::Games; use rocket_contrib::templates::{Template, Engines}; use rocket_contrib::templates::tera::{self, Value}; @@ -90,6 +91,7 @@ async fn main() { rocket::ignite() .manage(gm) .manage(pool) + .manage(Games::new()) .attach(tera) .mount("/", routes) .launch() diff --git a/backend/src/routes.rs b/backend/src/routes.rs index 0a4be80..be1011a 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -52,9 +52,10 @@ async fn map_post(map_req: Json) -> Result { } #[get("/lobby")] -async fn maps_get() -> Result { +async fn lobby_get(gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result { let maps = get_maps().await?; - let context = Context::new_with("Lobby", ContextT::Maps(maps)); + let games = get_states(&state.get_games(), &gm).await?; + let context = Context::new_with("Lobby", Lobby { games, maps }); Ok(Template::render("lobby", &context)) } @@ -89,14 +90,15 @@ struct GameReq { } #[post("/lobby", data="")] -async fn game_post(game_req: Json, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>) -> Result { +async fn game_post(game_req: Json, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result { let game = build_builder(tp.clone(), game_req.nop, game_req.max_turns, &game_req.map, &game_req.name); - let game_id = gm.start_game(game).await; - Ok(format!("{:?}", gm.get_state(game_id.unwrap()).await)) + let game_id = gm.start_game(game).await.unwrap(); + state.add_game(game_req.name.clone(), game_id); + Ok(format!("{:?}", gm.get_state(game_id).await)) } pub fn fuel(routes: &mut Vec) { - routes.extend(routes![files, index, map_post, map_get, maps_get, builder_get, visualizer_get, game_post]); + routes.extend(routes![files, index, map_post, map_get, lobby_get, builder_get, visualizer_get, game_post]); } diff --git a/backend/src/util.rs b/backend/src/util.rs index d360d4c..c21e921 100644 --- a/backend/src/util.rs +++ b/backend/src/util.rs @@ -9,6 +9,15 @@ pub struct Map { url: String, } +#[derive(Serialize)] +pub struct GameState { + name: String, + finished: bool, + turns: Option, + players: Vec, +} + +/// Visualiser game option #[derive(Serialize)] pub struct GameOption { name: String, @@ -32,36 +41,43 @@ struct Link { active: bool, } +#[derive(Serialize)] +pub struct Lobby { + pub games: Vec, + pub maps: Vec, +} + #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub enum ContextT { - Maps(Vec), Games(Vec), } #[derive(Serialize)] -pub struct Context { +pub struct Context { pub name: String, nav: Vec, #[serde(flatten)] - pub inner: Option, + pub t: Option } -impl Context { - pub fn new_with(active: &str, inner: ContextT) -> Self { +impl Context { + 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(); Context { - nav, name: String::from(""), inner: Some(inner) + nav, name: String::from(""), t: Some(t) } } +} +impl Context<()> { 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(); Context { - nav, name: String::from(""), inner: None, + nav, name: String::from(""), t: None, } } } @@ -97,3 +113,61 @@ pub async fn get_games() -> Result, String> { Ok(games) } + + +use mozaic::modules::game; +use mozaic::util::request::Connect; +use futures::future::{join_all, FutureExt}; +pub async fn get_states(game_ids: &Vec<(String, u64)>, manager: &game::Manager) -> Result, String> { + 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; + + for (gs, name) in gss { + if let Some(state) = gs { + 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(), + finished: false, + } + ) + } else { + states.push( + GameState { + name, + turns: None, + players: Vec::new(), + finished: true, + } + ) + } + } + + Ok(states) +} + +use std::sync::{Arc, Mutex}; + +pub struct Games { + inner: Arc>>, +} + +impl Games { + pub fn new() -> Self { + Self { + inner: Arc::new(Mutex::new(Vec::new())) + } + } + + pub fn add_game(&self, name: String, id: u64) { + self.inner.lock().unwrap().push((name, id)); + } + + pub fn get_games(&self) -> Vec<(String, u64)> { + self.inner.lock().unwrap().clone() + } +} diff --git a/backend/static/script/mapbuilder.js b/backend/static/script/mapbuilder.js index 978369a..07c99d3 100644 --- a/backend/static/script/mapbuilder.js +++ b/backend/static/script/mapbuilder.js @@ -34,13 +34,17 @@ } // const real_j = j % 2 == 1 ? j + 0.5 : j; // const real_j = j % 2 == 1 ? j + 0.5 : j; - planets.push({ + const planet = { "name": parameters[i][j]["name"], "ship_count": parseInt(parameters[i][j]["shipCount"]), - "owner": parameters[i][j]["colour"], "x": r_j, "y": r_i - }); + }; + + if (parameters[i][j]["colour"] !== 0) { + planet["owner"] = parameters[i][j]["colour"]; + } + planets.push(planet); } } } @@ -208,4 +212,4 @@ } init(); -} \ No newline at end of file +} diff --git a/backend/static/script/maps.js b/backend/static/script/maps.js index 1ab0459..7bf6df9 100644 --- a/backend/static/script/maps.js +++ b/backend/static/script/maps.js @@ -29,7 +29,7 @@ async function start_game() { xhr.onreadystatechange = async function() { console.log(this); - console.log(await this.text()); + // console.log(await this.text()); // TODO: make response visible }; diff --git a/backend/static/style/style.css b/backend/static/style/style.css index 96e47c2..c43d159 100644 --- a/backend/static/style/style.css +++ b/backend/static/style/style.css @@ -17,7 +17,9 @@ body { .lobby { flex-grow: 1; - background-color: red; + display: flex; + flex-direction: column; + align-self: flex-start; } .input_container { @@ -101,4 +103,14 @@ svg { top: 0; width: 100%; height: 100%; -} \ No newline at end of file +} + +.game_state { + background-color: #333; + display: flex; + max-width: 80%; + width: 500px; + margin: 20px auto; + justify-content: space-between; + padding: 20px; +} diff --git a/backend/templates/lobby.html.tera b/backend/templates/lobby.html.tera index d6ca46d..c18aa5f 100644 --- a/backend/templates/lobby.html.tera +++ b/backend/templates/lobby.html.tera @@ -2,7 +2,22 @@ {% 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