diff --git a/rc_core/src/cubes/cpu_count.rs b/rc_core/src/cubes/cpu_count.rs new file mode 100644 index 0000000..905aafb --- /dev/null +++ b/rc_core/src/cubes/cpu_count.rs @@ -0,0 +1,57 @@ +pub struct CpuInfo { + pub total: u32, + pub cosmetic: u32, +} + +pub struct CpuListParser { + cpu_values: std::collections::HashMap, + cosmetics: std::collections::HashSet, +} + +impl CpuListParser { + pub fn with_cubes<'a, I: std::iter::Iterator>(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, + } + } + } + } +} diff --git a/rc_core/src/cubes/mod.rs b/rc_core/src/cubes/mod.rs index 9205eb1..fddf0bd 100644 --- a/rc_core/src/cubes/mod.rs +++ b/rc_core/src/cubes/mod.rs @@ -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, + cpu_counter: std::sync::Arc, } impl CubeParsers { pub fn new(conf: &crate::ConfigImpl) -> Self { + let cubes = >::cubes(conf); Self { - weapon_list: std::sync::Arc::new(WeaponListParser::with_cubes(>::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 { self.weapon_list.clone() } + + pub fn cpu_counter(&self) -> std::sync::Arc { + self.cpu_counter.clone() + } } diff --git a/rc_core/src/persist/user/account_json.rs b/rc_core/src/persist/user/account_json.rs index 014cac3..9cb64c7 100644 --- a/rc_core/src/persist/user/account_json.rs +++ b/rc_core/src/persist/user/account_json.rs @@ -281,7 +281,7 @@ impl UserData { self.perms.administrator | self.perms.developer } - async fn user_player_data(&self) -> Result { + async fn user_player_data(&self, cpu_counter: &crate::cubes::CpuListParser) -> Result { let current_slot = self.db.garage_selected(self.account.id).await.map_err(|e| { log::error!("Failed to retrieve selected vehicle for user_id {} (user_player_data): {}", self.account.id, e); polariton_server::operations::SimpleOpError::with_message(DATABASE_ERR, format!("Could not retrieve selected garage: {}", e)) @@ -291,7 +291,8 @@ impl UserData { polariton_server::operations::SimpleOpError::with_message(INVALID_ROBOT_ERR, "No selected garage".to_owned()) })?; let user_uuid = self.account.public_id.clone(); - let weapon_order = oj_rc_database::schema::parse_int_csv(¤t_slot.weapon_order).into_iter().map(|x| x as i32).collect::>(); + let weapon_orders = oj_rc_database::schema::parse_int_csv(¤t_slot.weapon_order).into_iter().map(|x| x as i32).collect::>(); + let weapon_ranks = oj_rc_database::schema::parse_int_csv(¤t_slot.weapon_order).into_iter().map(|x| (x as i32, if x == 0 { 0 } else { 1 })).collect(); let user_avatar_aux = self.db.user_aux_by_user_id_and_descriptor(self.account.id, oj_rc_database::schema::user_aux::Descriptor::AvatarId).await.map_err(|e| { log::error!("Failed to retrieve avatar for user_id {} (user_player_data): {}", self.account.id, e); polariton_server::operations::SimpleOpError::with_message(DATABASE_ERR, format!("Could not retrieve avatar: {}", e)) @@ -301,32 +302,36 @@ impl UserData { polariton_server::operations::SimpleOpError::with_message(UNEXPECTED_ERR, "No avatar".to_owned()) })?; let avatar_id: Result = user_avatar_aux.data.parse(); + let cpu_count = if current_slot.total_robot_cpu <= 0 { + cpu_counter.calculate_cpu(&mut std::io::Cursor::new(¤t_slot.robot_data)).total as i32 + } else { + current_slot.total_robot_cpu + }; - // real user MUST be last Ok(crate::data::player_data::PlayerData { - name: user_uuid.clone(), + name: user_uuid, display_name: self.account.display_name.clone(), mastery: current_slot.mastery_level as i32, tier: 1, // FIXME robot_name: current_slot.name, - robot_map: current_slot.robot_data.clone(), + robot_map: current_slot.robot_data, group: None, // no platoon team: 0, has_premium: false, // FIXME robot_uuid: super::i64_as_uuid_str(current_slot.uuid).into(), - cpu: current_slot.total_robot_cpu as i32, + cpu: cpu_count, avatar_id: avatar_id.ok(), - weapon_order: weapon_order.clone(), - colour_map: current_slot.colour_data.clone(), + weapon_order: weapon_orders, + colour_map: current_slot.colour_data, is_ai: false, spawn_effect: current_slot.spawn_animation_id, death_effect: current_slot.death_animation_id, player_rank: 1, // FIXME - weapon_rank: oj_rc_database::schema::parse_int_csv(¤t_slot.weapon_order).into_iter().map(|x| (x as i32, if x == 0 { 0 } else { 1 })).collect(), + weapon_rank: weapon_ranks, }) } - async fn resolve_some_singleplayer_vehicles(&self, factory: &dyn oj_rc_factory::VehicleFactoryAdapter, weapon_order: &crate::cubes::WeaponListParser, singleplayer_config: &crate::persist::config::SingleplayerConfig) -> Result, i16> { + async fn resolve_some_singleplayer_vehicles(&self, factory: &dyn oj_rc_factory::VehicleFactoryAdapter, weapon_order: &crate::cubes::WeaponListParser, singleplayer_config: &crate::persist::config::SingleplayerConfig, cpu_counter: &crate::cubes::CpuListParser) -> Result, i16> { use rand::seq::IndexedRandom; let mut players = Vec::with_capacity((singleplayer_config.max_enemies + singleplayer_config.max_teammates + 1) as usize); let mut next_id = 0; @@ -356,6 +361,11 @@ impl UserData { let weapons_guess = weapon_order.guess_weapons(&mut std::io::Cursor::new(&factory_vehicle.0.cube_data)); let weapons_guess = vec![weapons_guess[0], 0, 0]; let weapon_ranks = weapons_guess.iter().map(|&x| (x, if x == 0 { 0 } else { 1 })).collect(); + let cpu_count = if factory_vehicle.1.cpu == 0 { + cpu_counter.calculate_cpu(&mut std::io::Cursor::new(&factory_vehicle.0.cube_data)).total as i32 + } else { + factory_vehicle.1.cpu as i32 + }; crate::data::player_data::PlayerData { name: username.clone(), display_name: username.clone(), @@ -367,7 +377,7 @@ impl UserData { team: team_num, has_premium: false, robot_uuid: uuid_str, - cpu: 420, + cpu: cpu_count, avatar_id: None, // not serialised weapon_order: weapons_guess, colour_map: factory_vehicle.0.colour_data, @@ -391,6 +401,11 @@ impl UserData { crate::persist::config::VehicleDescriptor::Database { garage } => { match self.db.garage_by_id(*garage).await { Ok(Some(db_vehicle)) => { + let cpu_count = if db_vehicle.total_robot_cpu <= 0 { + cpu_counter.calculate_cpu(&mut std::io::Cursor::new(&db_vehicle.robot_data)).total as i32 + } else { + db_vehicle.total_robot_cpu + }; crate::data::player_data::PlayerData { name: username.clone(), display_name: username.clone(), @@ -402,7 +417,7 @@ impl UserData { team: team_num, has_premium: false, robot_uuid: uuid_str, - cpu: db_vehicle.total_robot_cpu as i32, + cpu: cpu_count, avatar_id: None, // not serialised weapon_order: oj_rc_database::schema::parse_int_csv(&db_vehicle.weapon_order).into_iter().map(|x| x as i32).collect::>(), colour_map: db_vehicle.colour_data, @@ -430,6 +445,7 @@ impl UserData { let weapons_guess = weapon_order.guess_weapons(&mut std::io::Cursor::new(&cube_data)); let weapons_guess = vec![weapons_guess[0], 0, 0]; let weapon_ranks = weapons_guess.iter().map(|&x| (x, if x == 0 { 0 } else { 1 })).collect(); + let cpu_counts = cpu_counter.calculate_cpu(&mut std::io::Cursor::new(&cube_data)); crate::data::player_data::PlayerData { name: username.clone(), display_name: username.clone(), @@ -441,7 +457,7 @@ impl UserData { team: team_num, has_premium: false, robot_uuid: uuid_str, - cpu: 420, // FIXME + cpu: cpu_counts.total as i32, avatar_id: None, // not serialised weapon_order: weapons_guess, colour_map: colour_data.to_owned(), @@ -603,14 +619,17 @@ impl super::User for UserData { } } - async fn save_slot(&self, vehicle: crate::persist::user::VehicleData) -> Result<(), i16> { + async fn save_slot(&self, vehicle: crate::persist::user::VehicleData, cpu_counter: &crate::cubes::CpuListParser) -> Result<(), i16> { self.err_on_banned().await?; + let cpu_counts = cpu_counter.calculate_cpu(&mut std::io::Cursor::new(&vehicle.robot_data)); let entity = oj_rc_database::schema::garage::ActiveModel { weapon_order: oj_rc_database::sea_orm::ActiveValue::Set(oj_rc_database::schema::dump_csv(&vehicle.weapon_order)), robot_data: oj_rc_database::sea_orm::ActiveValue::Set(vehicle.robot_data), colour_data: oj_rc_database::sea_orm::ActiveValue::Set(vehicle.colour_data), crf_id: if let Some(crf_id) = vehicle.crf_id { oj_rc_database::sea_orm::ActiveValue::Set(Some(crf_id)) } else { Default::default() }, name: if let Some(new_name) = vehicle.name { oj_rc_database::sea_orm::ActiveValue::Set(new_name) } else { Default::default() }, + total_robot_cpu: oj_rc_database::sea_orm::ActiveValue::Set(cpu_counts.total as _), + total_cosmetic_cpu: oj_rc_database::sea_orm::ActiveValue::Set(cpu_counts.cosmetic as _), ..Default::default() }; self.save_garage_by_slot(entity, vehicle.slot).await.map_err(|e| { @@ -854,10 +873,10 @@ impl super::User for UserData { super::since_windows_epoch(self.account.creation_time) } - async fn singleplayer_robots(&self, factory: &dyn oj_rc_factory::VehicleFactoryAdapter, weapon_order: &crate::cubes::WeaponListParser, singleplayer_config: &crate::persist::config::SingleplayerConfig) -> Result, i16> { + async fn singleplayer_robots(&self, factory: &dyn oj_rc_factory::VehicleFactoryAdapter, weapon_order: &crate::cubes::WeaponListParser, singleplayer_config: &crate::persist::config::SingleplayerConfig, cpu_counter: &crate::cubes::CpuListParser) -> Result, i16> { //self.err_on_banned().await?; - let mut vehicles = self.resolve_some_singleplayer_vehicles(factory, weapon_order, singleplayer_config).await?; - let user_bot = self.user_player_data().await?; + let mut vehicles = self.resolve_some_singleplayer_vehicles(factory, weapon_order, singleplayer_config, cpu_counter).await?; + let user_bot = self.user_player_data(cpu_counter).await?; // real user MUST be last vehicles.push(user_bot); @@ -1121,8 +1140,8 @@ impl super::LobbyUser for UserData { self.account.id } - async fn player_data(&self) -> Result { - self.user_player_data().await.map_err(|e| { + async fn player_data(&self, cpu_counter: &crate::cubes::CpuListParser) -> Result { + self.user_player_data(cpu_counter).await.map_err(|e| { if let Some(msg) = e.error_msg() { polariton_server::operations::SimpleOpError::with_message(crate::data::error_codes::LobbyReasonCode::from_service_error(e.error_code()) as i16, msg.to_owned()) } else { diff --git a/rc_core/src/persist/user/traits.rs b/rc_core/src/persist/user/traits.rs index 40e4b4a..b0622cd 100644 --- a/rc_core/src/persist/user/traits.rs +++ b/rc_core/src/persist/user/traits.rs @@ -66,7 +66,7 @@ pub trait User: ChatUser + LobbyUser + MultiplayerUser { async fn select_garage(&self, slot: i32) -> Result<(), i16>; async fn all_slots(&self) -> UserSlots; async fn slot_by_id(&self, id: i32) -> Result, i16>; - async fn save_slot(&self, vehicle: VehicleData) -> Result<(), i16>; + async fn save_slot(&self, vehicle: VehicleData, cpu_counter: &crate::cubes::CpuListParser) -> Result<(), i16>; async fn save_slot_order(&self, slots: Vec) -> Result<(), i16>; async fn new_slot(&self, reset_slot: Option) -> Result, i16>; async fn copy_slot(&self, slot: i32, into_slot: Option, append: &str) -> Result<(), i16>; @@ -76,7 +76,7 @@ pub trait User: ChatUser + LobbyUser + MultiplayerUser { async fn get_slot_customisations(&self, uuid: &str) -> Result, i16>; async fn set_slot_name(&self, slot: i32, name: String) -> Result<(), i16>; fn signup_date(&self) -> i64; - async fn singleplayer_robots(&self, factory: &dyn oj_rc_factory::VehicleFactoryAdapter, weapon_order: &crate::cubes::WeaponListParser, singleplayer_config: &crate::persist::config::SingleplayerConfig) -> Result, i16>; + async fn singleplayer_robots(&self, factory: &dyn oj_rc_factory::VehicleFactoryAdapter, weapon_order: &crate::cubes::WeaponListParser, singleplayer_config: &crate::persist::config::SingleplayerConfig, cpu_counter: &crate::cubes::CpuListParser) -> Result, i16>; async fn prepare_factory_upload(&self, vehicle: VehicleUploadData) -> Result; async fn last_seen(&self) -> Result; async fn get_avatar_info(&self) -> Result, i16>; @@ -230,7 +230,7 @@ impl SanctionType { #[async_trait::async_trait] pub trait LobbyUser { fn user_id(&self) -> i32; - async fn player_data(&self) -> Result; + async fn player_data(&self, cpu_counter: &crate::cubes::CpuListParser) -> Result; async fn start_game(&self, game: GameDescriptor, players: Vec) -> Result<(), polariton_server::operations::SimpleOpError>; } diff --git a/rc_lobby_room/src/lobby.rs b/rc_lobby_room/src/lobby.rs index f0bed4a..3744201 100644 --- a/rc_lobby_room/src/lobby.rs +++ b/rc_lobby_room/src/lobby.rs @@ -21,10 +21,11 @@ pub struct QueueHandler { hostname: String, hostport: u16, network_conf: crate::data::network::NetworkConfigData, + cpu_counter: std::sync::Arc, } impl QueueHandler { - pub fn new(conf: &oj_rc_core::ConfigImpl, game_host: &str) -> Self { + pub fn new(conf: &oj_rc_core::ConfigImpl, game_host: &str, cpu_counter: std::sync::Arc,) -> Self { let (domain, port_str) = game_host.split_once(':').expect("Invalid redirect address (must be domain:port)"); Self { users_in_queue: tokio::sync::Mutex::new(HashMap::new()), @@ -33,6 +34,7 @@ impl QueueHandler { hostname: domain.to_owned(), hostport: port_str.parse().expect("Invalid redirect port"), network_conf: crate::data::network::NetworkConfigData::from_conf(oj_rc_core::ConfigProvider::<()>::network_config(conf)), + cpu_counter, } } @@ -109,7 +111,7 @@ impl QueueHandler { let key = QueueKey { map, mode, visibility, auto_heal, }; - match user.player_data().await { + match user.player_data(&self.cpu_counter).await { Ok(player_data) => { let mut new_player = QueueUser { emitter: event_emitter, diff --git a/rc_lobby_room/src/main.rs b/rc_lobby_room/src/main.rs index 794c78b..0cc208c 100644 --- a/rc_lobby_room/src/main.rs +++ b/rc_lobby_room/src/main.rs @@ -30,8 +30,8 @@ async fn main() -> std::io::Result<()> { let config = oj_rc_core::persist::config::ConfigImpl::load(&args.assets).expect("Bad config data"); let users = std::sync::Arc::new(oj_rc_core::persist::user::UserImpl::load(&args.data, &config).await.expect("Bad user data")); - let queue = std::sync::Arc::new(QueueHandler::new(&config, &args.redirect)); let parsers = oj_rc_core::cubes::CubeParsers::new(&config); + let queue = std::sync::Arc::new(QueueHandler::new(&config, &args.redirect, parsers.cpu_counter())); let init_ctx = InitConfig { config, diff --git a/rc_services_room/src/operations/crf_purchase.rs b/rc_services_room/src/operations/crf_purchase.rs index 171c380..78adac5 100644 --- a/rc_services_room/src/operations/crf_purchase.rs +++ b/rc_services_room/src/operations/crf_purchase.rs @@ -9,7 +9,7 @@ const SLOT_PARAM_KEY: u8 = 43; // in; int const FACTORY_ID_PARAM_KEY: u8 = 94; // in; int -async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc, weapon_order: &std::sync::Arc) -> Result { +async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc, weapon_order: &std::sync::Arc, cpu_counter: &std::sync::Arc,) -> Result { let mut params = params.to_dict(); let user_info = user.user()?; let slot = if let Some(Typed::Int(slot)) = params.remove(&SLOT_PARAM_KEY) { @@ -37,7 +37,7 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: weapon_order: weapons, crf_id: Some(factory_id), }; - user_info.save_slot(to_save).await?; + user_info.save_slot(to_save, cpu_counter).await?; } else { log::warn!("Failed to retrieve (for copy-construct) non-existent factory vehicle {}", factory_id); return Err(oj_rc_core::data::error_codes::WebServicesError::DatabaseError as i16); @@ -51,6 +51,7 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: pub struct CrfItemPurchaseProvider { factory: std::sync::Arc, weapon_order: std::sync::Arc, + cpu_counter: std::sync::Arc, } #[async_trait::async_trait] @@ -58,7 +59,7 @@ impl polariton_server::operations::Operation<()> for CrfItemPurchaseProvider { type User = crate::UserTy; async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> { - polariton_server::operations::result_to_op_resp::(do_handling(params, user, &self.factory, &self.weapon_order).await) + polariton_server::operations::result_to_op_resp::(do_handling(params, user, &self.factory, &self.weapon_order, &self.cpu_counter).await) } } @@ -69,9 +70,10 @@ impl polariton_server::operations::OperationCode for CrfItemPurchaseProvider { } -pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc, weapon_order: std::sync::Arc) -> CrfItemPurchaseProvider { +pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc, weapon_order: std::sync::Arc, cpu_counter: std::sync::Arc) -> CrfItemPurchaseProvider { CrfItemPurchaseProvider { factory: factory.to_owned(), weapon_order, + cpu_counter } } diff --git a/rc_services_room/src/operations/machine.rs b/rc_services_room/src/operations/machine.rs index 2c69e1c..549d303 100644 --- a/rc_services_room/src/operations/machine.rs +++ b/rc_services_room/src/operations/machine.rs @@ -59,11 +59,13 @@ const COMPRESSED_COLOUR_DATA_PARAM_KEY: u8 = 33; // byte arr const INVALID_ROBOT_ERR: i16 = 140; -pub(super) fn garage_machine_save_provider() -> MachineSaver { - MachineSaver +pub(super) fn garage_machine_save_provider(cpu_counter: std::sync::Arc) -> MachineSaver { + MachineSaver { + cpu_counter + } } -async fn do_save(params: ParameterTable<()>, user: &crate::UserTy) -> Result { +async fn do_save(params: ParameterTable<()>, user: &crate::UserTy, cpu_counter: &std::sync::Arc) -> Result { log::debug!("machine save params: {:?}", params); let mut params = params.to_dict(); if let Some(Typed::Int(slot_index)) = params.remove(&SLOT_PARAM_KEY) { @@ -80,7 +82,7 @@ async fn do_save(params: ParameterTable<()>, user: &crate::UserTy) -> Result, user: &crate::UserTy) -> Result, +} #[async_trait::async_trait] impl polariton_server::operations::Operation<()> for MachineSaver { type User = crate::UserTy; async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> { - polariton_server::operations::result_to_op_resp::(do_save(params, user).await) + polariton_server::operations::result_to_op_resp::(do_save(params, user, &self.cpu_counter).await) } } diff --git a/rc_services_room/src/operations/mod.rs b/rc_services_room/src/operations/mod.rs index 2e7560f..48b8897 100644 --- a/rc_services_room/src/operations/mod.rs +++ b/rc_services_room/src/operations/mod.rs @@ -185,7 +185,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler .add(building_xp::building_xp_save_provider()) .add(robot_sanction::all_robot_sanctions_provider()) .add(reconnect_game::available_reconnect_provider()) - .add(machine::garage_machine_save_provider()) + .add(machine::garage_machine_save_provider(init_ctx.parsers.cpu_counter())) .add(polariton_server::operations::Ack::<32, _>::default()) // TODO handle SaveMachineColorRequest instead of ignoring it .add(polariton_server::operations::Ack::<45, _>::default()) // TODO handle UpdateThumbnailVersionRequest instead of ignoring it .add(weapon_order::weapon_order_provider(&init_ctx.cubes)) @@ -209,7 +209,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler .add(crf_earnings::robot_shop_user_earnings_provider()) .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_purchase::crf_copy_to_bay_provider(&init_ctx.factory, init_ctx.parsers.weapon_order())) + .add(crf_purchase::crf_copy_to_bay_provider(&init_ctx.factory, init_ctx.parsers.weapon_order(), init_ctx.parsers.cpu_counter())) .add(crf_upload::crf_upload_provider(&init_ctx.factory)) .add(avatar_set_custom::custom_avatar_upload_handler()) .add(avatar_set::avatar_set_provider()) diff --git a/rc_singleplayer_room/src/operations/load_ai_robots.rs b/rc_singleplayer_room/src/operations/load_ai_robots.rs index c41f023..8988d21 100644 --- a/rc_singleplayer_room/src/operations/load_ai_robots.rs +++ b/rc_singleplayer_room/src/operations/load_ai_robots.rs @@ -4,18 +4,19 @@ const CODE: u8 = 1; const PARAM_KEY: u8 = 8; -pub(super) fn tdm_machines_provider(factory: &std::sync::Arc, weapon_order: std::sync::Arc, conf: &oj_rc_core::ConfigImpl) -> AiRobots { +pub(super) fn tdm_machines_provider(factory: &std::sync::Arc, weapon_order: std::sync::Arc, conf: &oj_rc_core::ConfigImpl, cpu_counter: std::sync::Arc) -> AiRobots { AiRobots { factory: factory.to_owned(), weapon_parser: weapon_order, singleplayer_config: >::singleplayer_details(conf), + cpu_parser: cpu_counter, } } -async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &oj_rc_core::factory::Factory, weapon_order: &oj_rc_core::cubes::WeaponListParser, singleplayer_conf: &oj_rc_core::persist::config::SingleplayerConfig) -> Result, i16> { +async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &oj_rc_core::factory::Factory, weapon_order: &oj_rc_core::cubes::WeaponListParser, singleplayer_conf: &oj_rc_core::persist::config::SingleplayerConfig, cpu_counter: &std::sync::Arc) -> Result, i16> { let ulock = user.user()?; let mut params = params.to_dict(); - params.insert(PARAM_KEY, ulock.singleplayer_robots(factory, weapon_order, singleplayer_conf).await?); + params.insert(PARAM_KEY, ulock.singleplayer_robots(factory, weapon_order, singleplayer_conf, cpu_counter).await?); Ok(params.into()) } @@ -23,6 +24,7 @@ pub struct AiRobots { factory: std::sync::Arc, weapon_parser: std::sync::Arc, singleplayer_config: oj_rc_core::persist::config::SingleplayerConfig, + cpu_parser: std::sync::Arc, } #[async_trait::async_trait] @@ -30,7 +32,7 @@ impl polariton_server::operations::Operation<()> for AiRobots { type User = crate::UserTy; async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> { - polariton_server::operations::result_to_op_resp::(do_handling(params, user, self.factory.as_ref(), self.weapon_parser.as_ref(), &self.singleplayer_config).await) + polariton_server::operations::result_to_op_resp::(do_handling(params, user, self.factory.as_ref(), self.weapon_parser.as_ref(), &self.singleplayer_config, &self.cpu_parser).await) } } diff --git a/rc_singleplayer_room/src/operations/mod.rs b/rc_singleplayer_room/src/operations/mod.rs index 2a44f3f..7a3bdb7 100644 --- a/rc_singleplayer_room/src/operations/mod.rs +++ b/rc_singleplayer_room/src/operations/mod.rs @@ -9,7 +9,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler .modify(oj_rc_core::polariton::RcOpModifier) .add(more_auth::MoreLobbyAuth) .add(eac::EacChallengeIgnorer) - .add(load_ai_robots::tdm_machines_provider(&init_ctx.factory, init_ctx.parsers.weapon_order(), &init_ctx.config)) + .add(load_ai_robots::tdm_machines_provider(&init_ctx.factory, init_ctx.parsers.weapon_order(), &init_ctx.config, init_ctx.parsers.cpu_counter())) //.add(polariton_server::operations::Ack::<33, _>::default()) // get user clan info (this is equivalent to not being in a clan) .add(polariton_server::operations::Ack::<2, _>::default()) // Save singleplayer result (parameter-less response) }