read teams from a json file

This commit is contained in:
2025-11-20 02:26:38 -08:00
parent 37f6310e91
commit 04e3c1a754
3 changed files with 9 additions and 22 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
/target
*~
scores.json
teams.json

View File

@ -9,6 +9,7 @@ FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libssl3 ca-certificates && apt-get clean
COPY --from=builder /usr/src/app/Neighborhood_Map_Atlas_Neighborhoods.geojson /
COPY --from=builder /usr/src/app/map.html /
COPY --from=builder /usr/src/app/teams.json /
COPY --from=builder /usr/src/app/target/release/mapbattle /usr/local/bin/mapbattle
EXPOSE 8080
CMD ["mapbattle"]

View File

@ -9,8 +9,7 @@ use chrono::NaiveDateTime;
use reqwest::Client;
use xml::reader::EventReader;
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
use crate::data::{Team, Player, TeamLegend};
use std::collections::HashMap;
use crate::data::{Team, TeamLegend};
use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
@ -22,7 +21,7 @@ const DATE_FORMAT: &str = "%Y-%m-%dT%H:%M:%SZ";
#[get("/legend-data")]
async fn legend_data() -> impl Responder {
let teams = read_scores();
let teams = read_teams();
let legends: Vec<TeamLegend> = teams.into_iter()
.map(|t| TeamLegend{name: t.name, color: t.color}).collect();
HttpResponse::Ok()
@ -33,7 +32,7 @@ async fn legend_data() -> impl Responder {
#[get("/geojson")]
async fn geojson_endpoint() -> impl Responder {
let utils = geo_utils::GeoUtils::new();
let teams = read_scores();
let teams = read_teams();
let feature_collection = utils.get_colored_collection_copy(teams);
HttpResponse::Ok()
.content_type("application/json")
@ -73,20 +72,7 @@ async fn update_scores()-> Result<(), Box<dyn Error>> {
.user_agent("MapBattle/0.1")
.build()?;
// Load players from a database?
let mut teams: Vec<Team> = vec![
Team{
name: String::from("potato"),
color: String::from("#df1aeaff"),
scores: HashMap::new(),
players: vec![
Player {
username: String::from("ammaratef45")
}
]
}
];
let mut teams = read_teams();
// Configure time range
let start_date = NaiveDateTime::parse_from_str("2025-11-13T11:55:07Z", DATE_FORMAT)?;
let end_date = NaiveDateTime::parse_from_str("2025-11-13T15:55:07Z", DATE_FORMAT)?;
@ -103,7 +89,7 @@ async fn update_scores()-> Result<(), Box<dyn Error>> {
}
}
let file = File::create("scores.json")?;
let file = File::create("teams.json")?;
let mut writer = BufWriter::new(file);
serde_json::to_writer_pretty(&mut writer, &teams)?;
writer.flush()?;
@ -111,7 +97,7 @@ async fn update_scores()-> Result<(), Box<dyn Error>> {
Ok(())
}
fn read_scores() -> Vec<Team> {
let text: String = fs::read_to_string("scores.json").expect("file should be present");
fn read_teams() -> Vec<Team> {
let text: String = fs::read_to_string("teams.json").expect("file should be present");
serde_json::from_str(&text).unwrap()
}