This commit is contained in:
41666 2022-11-20 16:25:17 -05:00
parent aff10840fd
commit 85e8729261
5 changed files with 74 additions and 7 deletions

View file

@ -9,4 +9,6 @@ edition = "2021"
salvo = { version = "0.37.4", features = ["cors"] }
tokio = { version = "1.22.0", features = ["macros"] }
serde_json = "1.0.88"
serde = "1.0.147"
serde = "1.0.147"
redis = "0.22.1"
once_cell = "1.16.0"

View file

@ -1,3 +1,7 @@
use core::time;
use std::{ops::Sub, time::SystemTime};
use once_cell::sync::Lazy;
use salvo::cors::Cors;
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
@ -29,6 +33,11 @@ struct MultipleWorldPopulation {
worlds: Vec<WorldPopulation>,
}
pub static REDIS_CLIENT: Lazy<redis::Client> = Lazy::new(|| {
redis::Client::open(std::env::var("REDIS_ADDR").unwrap_or("redis://localhost:6379".to_string()))
.unwrap()
});
#[handler]
async fn info(req: &mut Request, res: &mut Response) {
let headers: IncomingHeaders = req.parse_headers().unwrap();
@ -78,14 +87,27 @@ async fn get_world_multi(req: &mut Request, res: &mut Response) {
}
async fn get_world_pop(world_id: String) -> WorldPopulation {
let mut con = REDIS_CLIENT.get_connection().unwrap();
let filter_timestamp = SystemTime::now()
.sub(time::Duration::from_secs(60 * 15))
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
let (tr, vs, nc): (u32, u32, u32) = redis::pipe()
.zcount(format!("{}/{}", world_id, 1), filter_timestamp, "+inf")
.zcount(format!("{}/{}", world_id, 2), filter_timestamp, "+inf")
.zcount(format!("{}/{}", world_id, 3), filter_timestamp, "+inf")
.query(&mut con)
.unwrap();
let total = tr + vs + nc;
let response = WorldPopulation {
world_id: world_id.parse().unwrap(),
total: 0,
factions: Factions {
tr: 0,
nc: 0,
vs: 0,
},
total,
factions: Factions { tr, nc, vs },
};
response