diff --git a/src/data.rs b/src/data.rs index d9436c1..a00add8 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,6 @@ use crate::geo_utils::GeoUtils; use serde::{Deserialize, Serialize}; +use geojson::{Feature}; use std::collections::HashMap; #[derive(Debug, Clone, Deserialize, Serialize)] @@ -27,11 +28,16 @@ pub struct Game { impl Game { fn build_territories(&mut self, geoutils: &GeoUtils) { let territory_names = geoutils.get_all_territories(); - for name in territory_names { + 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: name, + territory_name: String::from(name), claiming_team: None, claiming_score: 0, + territory_feature:feat, }; self.territories.push(t); } @@ -73,6 +79,7 @@ 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 0f4b990..b83b81d 100644 --- a/src/geo_utils.rs +++ b/src/geo_utils.rs @@ -1,9 +1,9 @@ -use crate::Team; 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, @@ -42,58 +42,28 @@ impl GeoUtils { self.feature_collection.clone() } - pub fn get_colored_collection_copy(self, teams: Vec) -> FeatureCollection { + pub fn get_colored_collection_copy(self, game: &Game) -> FeatureCollection { let mut copy = self.feature_collection.clone(); for feature in &mut copy.features { let name = feature.property("S_HOOD").unwrap_or_default(); let name = name.as_str().unwrap_or("unknown"); - let mut max_team: (Option, i32) = (None, 0); - for team in &teams { - let score = team.scores.get(name).unwrap_or(&0); - if *score > max_team.1 { - max_team = (Some(team.color.clone()), *score); - } else if *score > 0 && *score == max_team.1 { - // Tie doesn't count - max_team = (None, 0); - } - } - match max_team.0 { - None => feature.set_property("color", "#888888ff"), - Some(c) => { - feature.set_property("color", c); + // 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) => { + feature.set_property("color", team.color.clone()); + feature.set_property("claiming_team", team.name.clone()); + } + None => { + feature.set_property("color", "#888888ff"); } } + feature.set_property("claiming_score", this_territory.claiming_score); } copy } - // pub fn get_number_neighborhoods(self, teams : Vec) -> HashMap { - // // new dicts of {team_name: number_claimed_neighborhoods} - // let team_to_score = HashMap::new(); - // let mut copy = self.feature_collection.clone(); - // for feature in &mut copy.features { - // let name = feature.property("S_HOOD").unwrap_or_default(); - // let name = name.as_str().unwrap_or("unknown"); - // let mut max_team: (Option, i32) = (None, 0); - // for team in &teams { - // let score = team.scores.get(name).unwrap_or(&0); - // if *score > max_team.1 { - // max_team = (Some(team.clone()), *score); - // } else if *score > 0 && *score == max_team.1 { - // // Tie doesn't count - // max_team = (None, 0); - // } - // } - // match max_team.0 { - // None => println!("No team has claimed this neighborhood: {name}"), - // Some(c) => { - // team_to_score.entry(c).and_modify(|val| *val+=1).or_insert(1); - // } - // } - // } - // team_to_score - // } - pub fn get_territory_for(self, point: geo_types::Point) -> String { let feature_collection: FeatureCollection = self.get_feature_collection_copy(); for feature in feature_collection { diff --git a/src/main.rs b/src/main.rs index 6f88495..f2f930e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -139,7 +139,6 @@ async fn get_territories( if let Ok(Responses::GamesResult(games)) = games_actor.send(Messages::GetGames).await && let Some(game) = games.iter().find(|g| g.code == *game_code) { - println!("{0:?}", game); return HttpResponse::Ok() .content_type("application/json") .body(serde_json::to_string(&game.territories).unwrap()); @@ -183,9 +182,8 @@ async fn geojson_endpoint( if let Ok(Responses::GamesResult(games)) = games_actor.send(Messages::GetGames).await && let Some(game) = games.iter().find(|g| g.code == *game_code) { - let teams = &game.teams; let utils = geo_utils::GeoUtils::new(); - let feature_collection = utils.get_colored_collection_copy(teams.to_vec()); + let feature_collection = utils.get_colored_collection_copy(&game); return HttpResponse::Ok() .content_type("application/json") .body(serde_json::to_string(&feature_collection).unwrap()); diff --git a/src/templates/map.html.tera b/src/templates/map.html.tera index 31b3983..13d033c 100644 --- a/src/templates/map.html.tera +++ b/src/templates/map.html.tera @@ -36,8 +36,13 @@ function onEachFeature(feature, layer) { // bind a popup to each geojson element // does this feature have a property named popupContent? + if (feature.properties) { - layer.bindPopup(feature.properties.S_HOOD); + 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; + layer.bindPopup(popup); } } @@ -48,7 +53,9 @@ fetch('/geojson/{{game_code}}') .then(res => res.json()) - .then(data => L.geoJSON(data, {style: style, onEachFeature : onEachFeature}).addTo(map)); + .then(data => { + L.geoJSON(data, {style: style, onEachFeature : onEachFeature}).addTo(map) + }); // // const endpoint = "/legend-data/{{game_code}}"; diff --git a/src/templates/map.tera b/src/templates/map.tera deleted file mode 100644 index 92c5a94..0000000 --- a/src/templates/map.tera +++ /dev/null @@ -1,79 +0,0 @@ - - - - {{ game_code }} - - - - - -
-
    - - - \ No newline at end of file