mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Fix CPU number calculation and display in lots of places
This commit is contained in:
57
rc_core/src/cubes/cpu_count.rs
Normal file
57
rc_core/src/cubes/cpu_count.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
pub struct CpuInfo {
|
||||
pub total: u32,
|
||||
pub cosmetic: u32,
|
||||
}
|
||||
|
||||
pub struct CpuListParser {
|
||||
cpu_values: std::collections::HashMap<u32, u32>,
|
||||
cosmetics: std::collections::HashSet<u32>,
|
||||
}
|
||||
|
||||
impl CpuListParser {
|
||||
pub fn with_cubes<'a, I: std::iter::Iterator<Item=&'a crate::persist::Cube>>(iter: I) -> Self {
|
||||
let mut cpu_values = std::collections::HashMap::new();
|
||||
let mut cosmetics = std::collections::HashSet::new();
|
||||
for item in iter {
|
||||
cpu_values.insert(item.id, item.info.cpu);
|
||||
if item.info.cosmetic {
|
||||
cosmetics.insert(item.id);
|
||||
}
|
||||
}
|
||||
Self {
|
||||
cpu_values,
|
||||
cosmetics
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calculate_cpu(&self, r: &mut dyn std::io::Read) -> CpuInfo {
|
||||
match super::parser::Cube::parse_list(r) {
|
||||
Ok(cubes) => {
|
||||
let mut totals = CpuInfo {
|
||||
total: 0,
|
||||
cosmetic: 0,
|
||||
};
|
||||
for cube in cubes {
|
||||
if let Some(cpu_val) = self.cpu_values.get(&cube.id) {
|
||||
totals.total += *cpu_val;
|
||||
if self.cosmetics.contains(&cube.id) {
|
||||
totals.cosmetic += *cpu_val;
|
||||
}
|
||||
} else {
|
||||
totals.total = 10_404;
|
||||
totals.cosmetic = cube.id;
|
||||
return totals;
|
||||
}
|
||||
}
|
||||
totals
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to parse cube data to count cpu: {}", e);
|
||||
CpuInfo {
|
||||
total: 10_400,
|
||||
cosmetic: 10_400,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,28 @@ pub(self) mod parser;
|
||||
mod weapon_list;
|
||||
pub use weapon_list::WeaponListParser;
|
||||
|
||||
mod cpu_count;
|
||||
pub use cpu_count::CpuListParser;
|
||||
|
||||
pub struct CubeParsers {
|
||||
weapon_list: std::sync::Arc<WeaponListParser>,
|
||||
cpu_counter: std::sync::Arc<CpuListParser>,
|
||||
}
|
||||
|
||||
impl CubeParsers {
|
||||
pub fn new(conf: &crate::ConfigImpl) -> Self {
|
||||
let cubes = <crate::ConfigImpl as crate::ConfigProvider<()>>::cubes(conf);
|
||||
Self {
|
||||
weapon_list: std::sync::Arc::new(WeaponListParser::with_cubes(<crate::ConfigImpl as crate::ConfigProvider<()>>::cubes(conf).values())),
|
||||
weapon_list: std::sync::Arc::new(WeaponListParser::with_cubes(cubes.values())),
|
||||
cpu_counter: std::sync::Arc::new(CpuListParser::with_cubes(cubes.values())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn weapon_order(&self) -> std::sync::Arc<WeaponListParser> {
|
||||
self.weapon_list.clone()
|
||||
}
|
||||
|
||||
pub fn cpu_counter(&self) -> std::sync::Arc<CpuListParser> {
|
||||
self.cpu_counter.clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user