1
0
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:
NG (Graham)
2025-05-18 20:33:29 -04:00
parent 49781d35b2
commit fd75dba80e
10 changed files with 164 additions and 6 deletions

20
rc_core/src/cubes/mod.rs Normal file
View 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()
}
}

View 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)
}
}

View 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()
}
}
}
}

View File

@@ -11,3 +11,5 @@ pub use persist::config::{ConfigImpl, ConfigProvider};
pub mod polariton; pub mod polariton;
pub mod factory; pub mod factory;
pub mod cubes;

View File

@@ -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>> { async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>> {
crate::factory::Factory::from_config(&self.factory).await crate::factory::Factory::from_config(&self.factory).await
} }
fn cubes(&self) -> &'_ std::collections::HashMap<String, crate::persist::Cube> {
&self.cubes
}
} }

View File

@@ -22,6 +22,7 @@ pub trait ConfigProvider<C: Clone> {
fn server_config(&self) -> ServerConfig; fn server_config(&self) -> ServerConfig;
fn garage_upgrades(&self) -> GarageUpgrades; fn garage_upgrades(&self) -> GarageUpgrades;
async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>>; 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 { pub struct CompleteCampaignProvider {

View File

@@ -2,7 +2,7 @@ pub mod config;
pub mod user; pub mod user;
mod cube_data; mod cube_data;
pub use cube_data::{Cube, ItemTier, ItemCategory}; pub use cube_data::{Cube, ItemTier, ItemCategory, ItemType};
//pub use cube_data::{VisibilityMode, ItemType}; //pub use cube_data::{VisibilityMode, ItemType};
mod garage; mod garage;

View File

@@ -17,6 +17,7 @@ pub struct InitConfig {
pub cubes: rc_core::persist::config::ConfigImpl, pub cubes: rc_core::persist::config::ConfigImpl,
pub users: std::sync::Arc<rc_core::persist::user::UserImpl>, pub users: std::sync::Arc<rc_core::persist::user::UserImpl>,
pub factory: std::sync::Arc<rc_core::factory::Factory>, pub factory: std::sync::Arc<rc_core::factory::Factory>,
pub parsers: rc_core::cubes::CubeParsers,
} }
#[tokio::main] #[tokio::main]
@@ -28,10 +29,12 @@ async fn main() -> std::io::Result<()> {
let cubes = rc_core::persist::config::ConfigImpl::load(&args.assets).expect("Bad config data"); let cubes = rc_core::persist::config::ConfigImpl::load(&args.assets).expect("Bad config data");
let users = std::sync::Arc::new(rc_core::persist::user::UserImpl::load(&args.data, &cubes).await.expect("Bad user data")); let users = std::sync::Arc::new(rc_core::persist::user::UserImpl::load(&args.data, &cubes).await.expect("Bad user data"));
let factory = std::sync::Arc::new(<rc_core::persist::config::ConfigImpl as rc_core::ConfigProvider<()>>::factory::<'_, '_>(&cubes).await.expect("Bad vehicle factory (CRF) config")); let factory = std::sync::Arc::new(<rc_core::persist::config::ConfigImpl as rc_core::ConfigProvider<()>>::factory::<'_, '_>(&cubes).await.expect("Bad vehicle factory (CRF) config"));
let parsers = rc_core::cubes::CubeParsers::new(&cubes);
let init_ctx = std::sync::Arc::new(InitConfig { let init_ctx = std::sync::Arc::new(InitConfig {
cubes, cubes,
users, users,
factory, factory,
parsers,
}); });
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(&init_ctx), polariton_server::events::EventsHandler::new())); let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(&init_ctx), polariton_server::events::EventsHandler::new()));

View File

@@ -9,7 +9,7 @@ const SLOT_PARAM_KEY: u8 = 43; // in; int
const FACTORY_ID_PARAM_KEY: u8 = 94; // in; int const FACTORY_ID_PARAM_KEY: u8 = 94; // in; int
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc<rc_core::factory::Factory>) -> Result<ParameterTable, i16> { async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc<rc_core::factory::Factory>, weapon_order: &std::sync::Arc<rc_core::cubes::WeaponListParser>) -> Result<ParameterTable, i16> {
let mut params = params.to_dict(); let mut params = params.to_dict();
let user_info = user.user()?; let user_info = user.user()?;
let slot = if let Some(Typed::Int(slot)) = params.remove(&SLOT_PARAM_KEY) { let slot = if let Some(Typed::Int(slot)) = params.remove(&SLOT_PARAM_KEY) {
@@ -25,12 +25,16 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory:
rc_core::data::error_codes::WebServicesError::DatabaseError as i16 rc_core::data::error_codes::WebServicesError::DatabaseError as i16
})?; })?;
if let Some((vehicle_to_copy, vehicle_meta)) = vehicle { if let Some((vehicle_to_copy, vehicle_meta)) = vehicle {
// parse cube data for weapon order
let mut cursor = std::io::Cursor::new(&vehicle_to_copy.cube_data);
let weapons = weapon_order.guess_weapons(&mut cursor);
// save to database
let to_save = rc_core::persist::user::VehicleData { let to_save = rc_core::persist::user::VehicleData {
name: Some(vehicle_meta.name), name: Some(vehicle_meta.name),
slot, slot,
robot_data: vehicle_to_copy.cube_data, robot_data: vehicle_to_copy.cube_data,
colour_data: vehicle_to_copy.colour_data, colour_data: vehicle_to_copy.colour_data,
weapon_order: Vec::default(), // FIXME calculate this somehow? Or maybe get the factory adapter to calculate this weapon_order: weapons,
crf_id: Some(factory_id), crf_id: Some(factory_id),
}; };
user_info.save_slot(to_save).await?; user_info.save_slot(to_save).await?;
@@ -46,6 +50,7 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory:
pub struct CrfItemPurchaseProvider { pub struct CrfItemPurchaseProvider {
factory: std::sync::Arc<rc_core::factory::Factory>, factory: std::sync::Arc<rc_core::factory::Factory>,
weapon_order: std::sync::Arc<rc_core::cubes::WeaponListParser>,
} }
#[async_trait::async_trait] #[async_trait::async_trait]
@@ -53,7 +58,7 @@ impl polariton_server::operations::Operation<()> for CrfItemPurchaseProvider {
type User = crate::UserTy; type User = crate::UserTy;
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> { async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, &self.factory).await) polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, &self.factory, &self.weapon_order).await)
} }
} }
@@ -64,8 +69,9 @@ impl polariton_server::operations::OperationCode for CrfItemPurchaseProvider {
} }
pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc<rc_core::factory::Factory>) -> CrfItemPurchaseProvider { pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc<rc_core::factory::Factory>, weapon_order: std::sync::Arc<rc_core::cubes::WeaponListParser>) -> CrfItemPurchaseProvider {
CrfItemPurchaseProvider { CrfItemPurchaseProvider {
factory: factory.to_owned(), factory: factory.to_owned(),
weapon_order,
} }
} }

View File

@@ -201,5 +201,5 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler<crate::UserTy>
.add(crf_earnings::robot_shop_user_earnings_provider()) .add(crf_earnings::robot_shop_user_earnings_provider())
.add(crf_list_query::crf_item_list_query_provider(&init_ctx.factory)) .add(crf_list_query::crf_item_list_query_provider(&init_ctx.factory))
.add(crf_vehicle_data::crf_item_data_provider(&init_ctx.factory)) .add(crf_vehicle_data::crf_item_data_provider(&init_ctx.factory))
.add(crf_purchase::crf_copy_to_bay_provider(&init_ctx.factory)) .add(crf_purchase::crf_copy_to_bay_provider(&init_ctx.factory, init_ctx.parsers.weapon_order()))
} }