diff --git a/src/data.rs b/src/data.rs index a00add8..73923e1 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,5 @@ use crate::geo_utils::GeoUtils; use serde::{Deserialize, Serialize}; -use geojson::{Feature}; use std::collections::HashMap; #[derive(Debug, Clone, Deserialize, Serialize)] @@ -22,24 +21,21 @@ pub struct Game { pub teams: Vec, pub start_time: String, pub end_time: String, - pub territories: Vec, + pub territories: HashMap, // territory name/id to struct } impl Game { fn build_territories(&mut self, geoutils: &GeoUtils) { - let territory_names = geoutils.get_all_territories(); let territory_json = geoutils.get_feature_collection_copy(); for feature in &territory_json.features { - let feat: Feature = feature.clone(); let name = feature.property("S_HOOD").unwrap_or_default(); let name = name.as_str().unwrap_or("unknown"); let t: Territory = Territory { territory_name: String::from(name), claiming_team: None, claiming_score: 0, - territory_feature:feat, }; - self.territories.push(t); + self.territories.insert(name.to_string(), t); } } @@ -50,14 +46,14 @@ impl Game { teams: teams, start_time: start_time, end_time: end_time, - territories: vec![], + territories: HashMap::new(), }; game.build_territories(&geoutils); game } pub fn update_territories(&mut self) { - for territory in &mut self.territories { + for (_, territory) in &mut self.territories { let mut max_team: (Option, i32) = (None, 0); for team in &self.teams { let score = team.scores.get(&territory.territory_name).unwrap_or(&0); @@ -79,7 +75,6 @@ pub struct Territory { pub territory_name: String, pub claiming_team: Option, pub claiming_score: i32, - pub territory_feature: Feature, } impl Team { diff --git a/src/geo_utils.rs b/src/geo_utils.rs index 4e513f9..8668831 100644 --- a/src/geo_utils.rs +++ b/src/geo_utils.rs @@ -1,9 +1,9 @@ +use crate::data::{Game, Territory}; use geo as geo_types; use geo::algorithm::contains::Contains; use geojson::{FeatureCollection, GeoJson, Geometry, Value}; use std::convert::TryFrom; use std::fs; -use crate::data::{Game, Territory}; pub struct GeoUtils { feature_collection: FeatureCollection, @@ -29,16 +29,6 @@ impl GeoUtils { FeatureCollection::try_from(geojson).unwrap() } - pub fn get_all_territories(&self) -> Vec { - let mut territory_names: Vec = vec![]; - for feature in self.feature_collection.clone() { - let name = feature.property("S_HOOD").unwrap_or_default(); - let name = name.as_str().unwrap_or("unknown"); - territory_names.push(String::from(name)); - } - territory_names - } - pub fn get_feature_collection_copy(&self) -> FeatureCollection { self.feature_collection.clone() } @@ -48,16 +38,18 @@ impl GeoUtils { for feature in &mut copy.features { let name = feature.property("S_HOOD").unwrap_or_default(); let name = name.as_str().unwrap_or("unknown"); - // TODO: Make the territories field of Game struct to be a hashmap - let this_territory : Territory = game.territories.clone().iter().find(|t| t.territory_name == *name) - .expect("somehow the territory wasn't found").clone(); - match this_territory.claiming_team { - Some(team) => { + let this_territory: Territory = game + .territories + .get(name) + .expect("Whaaa no territory found!") + .clone(); + match this_territory.claiming_team.clone() { + Some(team) => { feature.set_property("color", team.color.clone()); feature.set_property("claiming_team", team.name.clone()); } - None => { - feature.set_property("color", "#888888ff"); + None => { + feature.set_property("color", "#888888ff"); } } feature.set_property("claiming_score", this_territory.claiming_score); diff --git a/src/main.rs b/src/main.rs index 38d1f6f..ad3e1cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,32 +148,6 @@ async fn get_territories( .body("{}") } -// Get the Territory info for a specific territory name? -#[get("/territories/{game_code}/{name}")] -async fn get_territory( - path: web::Path<(String, String)>, - games_actor: web::Data>, -) -> impl Responder { - let (game_code, name) = path.into_inner(); - if let Ok(Responses::GamesResult(games)) = games_actor.send(Messages::GetGames).await - && let Some(game) = games.iter().find(|g| g.code == *game_code) { - let territories = &game.territories; - for t in territories { - println!("Search term {name} vs. {0}", t.territory_name); - if t.territory_name == *name { - println!("{0:?}", t); - return HttpResponse::Ok() - .content_type("application/json") - .body(serde_json::to_string(&t).unwrap()); - } - } - } - HttpResponse::InternalServerError() - .content_type("application/json") - .body("{}") -} - - #[get("/geojson/{game_code}")] async fn geojson_endpoint( game_code: web::Path, @@ -258,7 +232,6 @@ async fn main() -> std::io::Result<()> { .service(admin_query) .service(list_of_games) .service(get_territories) - .service(get_territory) }) .bind(("0.0.0.0", 8080))? .run() diff --git a/src/templates/map.html.tera b/src/templates/map.html.tera index 13d033c..82bdaf9 100644 --- a/src/templates/map.html.tera +++ b/src/templates/map.html.tera @@ -38,10 +38,10 @@ // does this feature have a property named popupContent? if (feature.properties) { - const popup = document.createElement("p"); - popup.textContent += feature.properties.S_HOOD; - popup.textContent += "\nTeam: " + feature.properties.claiming_team; - popup.textContent += "\nScore: " + feature.properties.claiming_score; + var popup = document.createElement("div"); + popup.innerHTML += feature.properties.S_HOOD; + popup.innerHTML += "
Team: " + feature.properties.claiming_team; + popup.innerHTML += "
Score: " + feature.properties.claiming_score; layer.bindPopup(popup); } }