mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Guess weapon order for factory downloads (since they have no weapon order) #6
This commit is contained in:
20
rc_core/src/cubes/mod.rs
Normal file
20
rc_core/src/cubes/mod.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
pub(self) mod parser;
|
||||
|
||||
mod weapon_list;
|
||||
pub use weapon_list::WeaponListParser;
|
||||
|
||||
pub struct CubeParsers {
|
||||
weapon_list: std::sync::Arc<WeaponListParser>,
|
||||
}
|
||||
|
||||
impl CubeParsers {
|
||||
pub fn new(conf: &crate::ConfigImpl) -> Self {
|
||||
Self {
|
||||
weapon_list: std::sync::Arc::new(WeaponListParser::with_cubes(<crate::ConfigImpl as crate::ConfigProvider<()>>::cubes(conf).values())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn weapon_order(&self) -> std::sync::Arc<WeaponListParser> {
|
||||
self.weapon_list.clone()
|
||||
}
|
||||
}
|
||||
70
rc_core/src/cubes/parser.rs
Normal file
70
rc_core/src/cubes/parser.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub struct Cube {
|
||||
pub id: u32,
|
||||
pub x: u8,
|
||||
pub y: u8,
|
||||
pub z: u8,
|
||||
pub orientation: u8,
|
||||
}
|
||||
|
||||
impl Cube {
|
||||
pub fn parse(r: &mut dyn std::io::Read) -> std::io::Result<Self> {
|
||||
let mut buf = [0u8; 4];
|
||||
r.read_exact(&mut buf)?;
|
||||
let id = u32::from_le_bytes(buf);
|
||||
let mut buf = [0u8; 4];
|
||||
r.read_exact(&mut buf)?;
|
||||
Ok(Self {
|
||||
id,
|
||||
x: buf[0],
|
||||
y: buf[1],
|
||||
z: buf[2],
|
||||
orientation: buf[3],
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_list(r: &mut dyn std::io::Read) -> std::io::Result<Vec<Self>> {
|
||||
let mut buf = [0u8; 4];
|
||||
r.read_exact(&mut buf)?;
|
||||
let count = u32::from_le_bytes(buf);
|
||||
let mut cubes = Vec::with_capacity(count as _);
|
||||
for _ in 0..count {
|
||||
let cube = Self::parse(r)?;
|
||||
cubes.push(cube);
|
||||
}
|
||||
Ok(cubes)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Colour {
|
||||
pub colour: u8,
|
||||
pub x: u8,
|
||||
pub y: u8,
|
||||
pub z: u8,
|
||||
}
|
||||
|
||||
impl Colour {
|
||||
pub fn parse(r: &mut dyn std::io::Read) -> std::io::Result<Self> {
|
||||
let mut buf = [0u8; 4];
|
||||
r.read_exact(&mut buf)?;
|
||||
Ok(Self {
|
||||
colour: buf[0],
|
||||
x: buf[1],
|
||||
y: buf[2],
|
||||
z: buf[3],
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_list(r: &mut dyn std::io::Read) -> std::io::Result<Vec<Self>> {
|
||||
let mut buf = [0u8; 4];
|
||||
r.read_exact(&mut buf)?;
|
||||
let count = u32::from_le_bytes(buf);
|
||||
let mut cubes = Vec::with_capacity(count as _);
|
||||
for _ in 0..count {
|
||||
let cube = Self::parse(r)?;
|
||||
cubes.push(cube);
|
||||
}
|
||||
Ok(cubes)
|
||||
}
|
||||
}
|
||||
52
rc_core/src/cubes/weapon_list.rs
Normal file
52
rc_core/src/cubes/weapon_list.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
const MAX_WEAPON_SLOTS: usize = 3;
|
||||
|
||||
struct WeaponInfo {
|
||||
category: crate::data::weapon_list::ItemCategory,
|
||||
tier: crate::data::cube_list::ItemTier,
|
||||
}
|
||||
|
||||
impl WeaponInfo {
|
||||
fn weapon_order_key(&self) -> i32 {
|
||||
self.category.but_bigger() + (self.tier as i32)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WeaponListParser {
|
||||
weapons: std::collections::HashMap<u32, WeaponInfo>,
|
||||
}
|
||||
|
||||
impl WeaponListParser {
|
||||
pub fn with_cubes<'a, I: std::iter::Iterator<Item=&'a crate::persist::Cube>>(iter: I) -> Self {
|
||||
let mut weapons = std::collections::HashMap::new();
|
||||
for item in iter {
|
||||
if let crate::persist::ItemType::Weapon = item.info.type_ {
|
||||
weapons.insert(item.id, WeaponInfo {
|
||||
category: item.info.category.into(),
|
||||
tier: item.info.size.into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Self {
|
||||
weapons,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn guess_weapons(&self, r: &mut dyn std::io::Read) -> Vec<i32> {
|
||||
match super::parser::Cube::parse_list(r) {
|
||||
Ok(cubes) => {
|
||||
let mut keys = std::collections::HashSet::new();
|
||||
for cube in cubes {
|
||||
if let Some(weapon) = self.weapons.get(&cube.id) {
|
||||
keys.insert(weapon.weapon_order_key());
|
||||
if keys.len() == MAX_WEAPON_SLOTS { break; }
|
||||
}
|
||||
}
|
||||
keys.into_iter().collect::<Vec<i32>>()
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to parse cube data to guess weapon order: {}", e);
|
||||
Vec::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,3 +11,5 @@ pub use persist::config::{ConfigImpl, ConfigProvider};
|
||||
pub mod polariton;
|
||||
|
||||
pub mod factory;
|
||||
|
||||
pub mod cubes;
|
||||
|
||||
@@ -277,4 +277,8 @@ impl <C: Clone> super::ConfigProvider<C> for CubeConfig {
|
||||
async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>> {
|
||||
crate::factory::Factory::from_config(&self.factory).await
|
||||
}
|
||||
|
||||
fn cubes(&self) -> &'_ std::collections::HashMap<String, crate::persist::Cube> {
|
||||
&self.cubes
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ pub trait ConfigProvider<C: Clone> {
|
||||
fn server_config(&self) -> ServerConfig;
|
||||
fn garage_upgrades(&self) -> GarageUpgrades;
|
||||
async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>>;
|
||||
fn cubes(&self) -> &'_ std::collections::HashMap<String, crate::persist::Cube>;
|
||||
}
|
||||
|
||||
pub struct CompleteCampaignProvider {
|
||||
|
||||
@@ -2,7 +2,7 @@ pub mod config;
|
||||
pub mod user;
|
||||
|
||||
mod cube_data;
|
||||
pub use cube_data::{Cube, ItemTier, ItemCategory};
|
||||
pub use cube_data::{Cube, ItemTier, ItemCategory, ItemType};
|
||||
//pub use cube_data::{VisibilityMode, ItemType};
|
||||
|
||||
mod garage;
|
||||
|
||||
Reference in New Issue
Block a user