simplify entire world zcount to one call

This commit is contained in:
41666 2022-11-26 14:37:13 -05:00
parent 318ffde050
commit a74ae46873
3 changed files with 29 additions and 23 deletions

View file

@ -10,35 +10,37 @@ pub struct Query;
#[graphql_object(context = Context)]
impl Query {
fn world(id: ID) -> FieldResult<World> {
Ok(World { id })
fn world(id: String) -> FieldResult<World> {
Ok(World {
world_id: id.clone(),
})
}
fn allWorlds() -> FieldResult<Vec<World>> {
Ok(vec![
World {
id: ID::from("1".to_string()),
world_id: "1".to_string(),
},
World {
id: ID::from("10".to_string()),
world_id: "10".to_string(),
},
World {
id: ID::from("13".to_string()),
world_id: "13".to_string(),
},
World {
id: ID::from("17".to_string()),
world_id: "17".to_string(),
},
World {
id: ID::from("19".to_string()),
world_id: "19".to_string(),
},
World {
id: ID::from("40".to_string()),
world_id: "40".to_string(),
},
World {
id: ID::from("1000".to_string()),
world_id: "1000".to_string(),
},
World {
id: ID::from("2000".to_string()),
world_id: "2000".to_string(),
},
])
}
@ -57,7 +59,7 @@ impl Query {
};
Ok(World {
id: ID::from(id.to_string()),
world_id: id.to_string(),
})
}

View file

@ -22,21 +22,24 @@ static WORLD_ID_TO_NAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
#[derive(Clone, Debug)]
pub struct World {
pub id: juniper::ID,
pub world_id: String,
}
#[graphql_object(context = super::Context)]
impl World {
pub fn id(&self) -> juniper::ID {
juniper::ID::from(self.world_id.clone())
}
pub fn name(&self) -> String {
WORLD_ID_TO_NAME
.get(&self.id.to_string().as_str())
.get(&self.world_id.to_string().as_str())
.unwrap_or(&"Unknown")
.to_string()
}
pub async fn population(&self, context: &mut super::Context) -> i32 {
let mut con = (*context).con.get().await.unwrap();
let id = self.id.to_string();
let id = self.world_id.to_string();
let filter_timestamp = SystemTime::now()
.sub(Duration::from_secs(60 * 15))
@ -44,33 +47,32 @@ impl World {
.unwrap()
.as_secs();
let (vs, nc, tr, ns): (i32, i32, i32, i32) = pipe()
.zcount(format!("wp:{}/{}", id, 1), filter_timestamp, "+inf")
.zcount(format!("wp:{}/{}", id, 2), filter_timestamp, "+inf")
.zcount(format!("wp:{}/{}", id, 3), filter_timestamp, "+inf")
.zcount(format!("wp:{}/{}", id, 4), filter_timestamp, "+inf")
let pop: u32 = cmd("ZCOUNT")
.arg(format!("wp:{}", id))
.arg(filter_timestamp)
.arg("+inf")
.query_async(&mut con)
.await
.unwrap();
tr + vs + nc + ns
pop as i32
}
pub async fn faction_population(&self) -> FactionPopulation {
FactionPopulation {
world_id: self.id.clone(),
world_id: juniper::ID::from(self.world_id.clone()),
}
}
pub async fn vehicles(&self) -> Vehicles {
Vehicles {
world_id: self.id.clone(),
world_id: juniper::ID::from(self.world_id.clone()),
}
}
pub async fn classes(&self) -> Classes {
Classes {
world_id: self.id.clone(),
world_id: juniper::ID::from(self.world_id.clone()),
}
}
}

View file

@ -74,6 +74,8 @@ async fn track_pop(pop_event: PopEvent) {
} = pop_event;
let key = format!("wp:{}/{}", world_id, team_id);
let _: () = con.zadd(key, character_id.clone(), timestamp).unwrap();
let key = format!("wp:{}", world_id);
let _: () = con.zadd(key, character_id, timestamp).unwrap();
}