Popup with claimed territory info!
This commit is contained in:
13
src/data.rs
13
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<Team>,
|
||||
pub start_time: String,
|
||||
pub end_time: String,
|
||||
pub territories: Vec<Territory>,
|
||||
pub territories: HashMap<String, Territory>, // 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<Team>, 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<Team>,
|
||||
pub claiming_score: i32,
|
||||
pub territory_feature: Feature,
|
||||
}
|
||||
|
||||
impl Team {
|
||||
|
||||
@ -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<String> {
|
||||
let mut territory_names: Vec<String> = 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);
|
||||
|
||||
27
src/main.rs
27
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<Addr<GamesActor>>,
|
||||
) -> 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<String>,
|
||||
@ -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()
|
||||
|
||||
@ -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 += "<br>Team: " + feature.properties.claiming_team;
|
||||
popup.innerHTML += "<br>Score: " + feature.properties.claiming_score;
|
||||
layer.bindPopup(popup);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user