add message_actor and sync_scores methods

This commit is contained in:
2026-05-04 07:36:27 -07:00
parent 343d20dd4e
commit 8b6b4cd4f5
2 changed files with 26 additions and 23 deletions

View File

@ -2,6 +2,6 @@ let
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/25.11.tar.gz") {};
in pkgs.mkShell {
packages = with pkgs; [
openssl pkg-config sqlite
openssl pkg-config sqlite rust-analyzer
];
}

View File

@ -21,7 +21,6 @@ use db::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use reqwest::Client;
use std::error::Error;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
use tera::Context;
@ -32,7 +31,7 @@ use xml::reader::EventReader;
const DATE_FORMAT: &str = "%Y-%m-%dT%H:%M:%S";
#[derive(Message)]
#[derive(Message, Debug, Clone)]
#[rtype(result = "Responses")]
enum Messages {
SynScores,
@ -157,7 +156,7 @@ async fn game_page(game_code: web::Path<String>) -> impl Responder {
#[get("/admin")]
async fn admin() -> impl Responder {
let mut context = Context::new();
let context = Context::new();
let body = Tera::one_off(include_str!("templates/admin.tera"), &context, false)
.expect("Failed to render template");
HttpResponse::Ok().body(body)
@ -189,23 +188,7 @@ async fn main() -> std::io::Result<()> {
games: state.clone(),
}
.start();
let sync_result = actor_addr.send(Messages::SynScores).await;
match sync_result {
Ok(_) => println!("Initial score syncing was successful"),
Err(_) => println!("Initial score syncing failed"),
}
let addr_clone = actor_addr.clone();
let mut interval = time::interval(Duration::from_secs(60 * 5));
let _maintainance = tokio::task::spawn(async move {
loop {
interval.tick().await;
let update_result = addr_clone.send(Messages::UpdateScores).await;
match update_result {
Ok(_) => println!("A successful score update was performed"),
Err(err) => println!("Failed to update scores: {err}"),
}
}
});
sync_scores(actor_addr.clone()).await;
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone()))
@ -214,14 +197,34 @@ async fn main() -> std::io::Result<()> {
.service(game_page)
.service(geojson_endpoint)
.service(legend_data)
//.service(admin)
//.service(list_of_games)
.service(admin)
.service(list_of_games)
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}
async fn sync_scores(addr: Addr<GamesActor>) {
message_actor(Messages::SynScores, addr.clone()).await;
let mut interval = time::interval(Duration::from_secs(60 * 5));
let _maintainance = tokio::task::spawn(async move {
loop {
interval.tick().await;
message_actor(Messages::UpdateScores, addr.clone()).await;
}
});
}
async fn message_actor(message: Messages, addr: Addr<GamesActor>) {
println!("processing message: {message:?}");
let sync_result = addr.send(message.clone()).await;
match sync_result {
Ok(_) => println!("Processing of {message:?} was successful"),
Err(_) => println!("Processing of {message:?} failed"),
}
}
async fn update_scores(games: Vec<Game>) -> Result<Vec<Game>, Box<dyn Error>> {
let client = Client::builder().user_agent("MapBattle/0.1").build()?;