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;
|
mod weapon_list;
|
||||||
pub use weapon_list::WeaponListParser;
|
pub use weapon_list::WeaponListParser;
|
||||||
|
|
||||||
|
mod cpu_count;
|
||||||
|
pub use cpu_count::CpuListParser;
|
||||||
|
|
||||||
pub struct CubeParsers {
|
pub struct CubeParsers {
|
||||||
weapon_list: std::sync::Arc<WeaponListParser>,
|
weapon_list: std::sync::Arc<WeaponListParser>,
|
||||||
|
cpu_counter: std::sync::Arc<CpuListParser>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CubeParsers {
|
impl CubeParsers {
|
||||||
pub fn new(conf: &crate::ConfigImpl) -> Self {
|
pub fn new(conf: &crate::ConfigImpl) -> Self {
|
||||||
|
let cubes = <crate::ConfigImpl as crate::ConfigProvider<()>>::cubes(conf);
|
||||||
Self {
|
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> {
|
pub fn weapon_order(&self) -> std::sync::Arc<WeaponListParser> {
|
||||||
self.weapon_list.clone()
|
self.weapon_list.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn cpu_counter(&self) -> std::sync::Arc<CpuListParser> {
|
||||||
|
self.cpu_counter.clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -281,7 +281,7 @@ impl UserData {
|
|||||||
self.perms.administrator | self.perms.developer
|
self.perms.administrator | self.perms.developer
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn user_player_data(&self) -> Result<crate::data::player_data::PlayerData, polariton_server::operations::SimpleOpError> {
|
async fn user_player_data(&self, cpu_counter: &crate::cubes::CpuListParser) -> Result<crate::data::player_data::PlayerData, polariton_server::operations::SimpleOpError> {
|
||||||
let current_slot = self.db.garage_selected(self.account.id).await.map_err(|e| {
|
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);
|
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))
|
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())
|
polariton_server::operations::SimpleOpError::with_message(INVALID_ROBOT_ERR, "No selected garage".to_owned())
|
||||||
})?;
|
})?;
|
||||||
let user_uuid = self.account.public_id.clone();
|
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::<Vec<_>>();
|
let weapon_orders = oj_rc_database::schema::parse_int_csv(¤t_slot.weapon_order).into_iter().map(|x| x as i32).collect::<Vec<_>>();
|
||||||
|
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| {
|
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);
|
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))
|
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())
|
polariton_server::operations::SimpleOpError::with_message(UNEXPECTED_ERR, "No avatar".to_owned())
|
||||||
})?;
|
})?;
|
||||||
let avatar_id: Result<i32, _> = user_avatar_aux.data.parse();
|
let avatar_id: Result<i32, _> = 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 {
|
Ok(crate::data::player_data::PlayerData {
|
||||||
name: user_uuid.clone(),
|
name: user_uuid,
|
||||||
display_name: self.account.display_name.clone(),
|
display_name: self.account.display_name.clone(),
|
||||||
mastery: current_slot.mastery_level as i32,
|
mastery: current_slot.mastery_level as i32,
|
||||||
tier: 1, // FIXME
|
tier: 1, // FIXME
|
||||||
robot_name: current_slot.name,
|
robot_name: current_slot.name,
|
||||||
robot_map: current_slot.robot_data.clone(),
|
robot_map: current_slot.robot_data,
|
||||||
group: None, // no platoon
|
group: None, // no platoon
|
||||||
team: 0,
|
team: 0,
|
||||||
has_premium: false, // FIXME
|
has_premium: false, // FIXME
|
||||||
robot_uuid: super::i64_as_uuid_str(current_slot.uuid).into(),
|
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(),
|
avatar_id: avatar_id.ok(),
|
||||||
weapon_order: weapon_order.clone(),
|
weapon_order: weapon_orders,
|
||||||
colour_map: current_slot.colour_data.clone(),
|
colour_map: current_slot.colour_data,
|
||||||
is_ai: false,
|
is_ai: false,
|
||||||
spawn_effect: current_slot.spawn_animation_id,
|
spawn_effect: current_slot.spawn_animation_id,
|
||||||
death_effect: current_slot.death_animation_id,
|
death_effect: current_slot.death_animation_id,
|
||||||
player_rank: 1, // FIXME
|
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<Vec<crate::data::player_data::PlayerData>, 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<Vec<crate::data::player_data::PlayerData>, i16> {
|
||||||
use rand::seq::IndexedRandom;
|
use rand::seq::IndexedRandom;
|
||||||
let mut players = Vec::with_capacity((singleplayer_config.max_enemies + singleplayer_config.max_teammates + 1) as usize);
|
let mut players = Vec::with_capacity((singleplayer_config.max_enemies + singleplayer_config.max_teammates + 1) as usize);
|
||||||
let mut next_id = 0;
|
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 = weapon_order.guess_weapons(&mut std::io::Cursor::new(&factory_vehicle.0.cube_data));
|
||||||
let weapons_guess = vec![weapons_guess[0], 0, 0];
|
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 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 {
|
crate::data::player_data::PlayerData {
|
||||||
name: username.clone(),
|
name: username.clone(),
|
||||||
display_name: username.clone(),
|
display_name: username.clone(),
|
||||||
@@ -367,7 +377,7 @@ impl UserData {
|
|||||||
team: team_num,
|
team: team_num,
|
||||||
has_premium: false,
|
has_premium: false,
|
||||||
robot_uuid: uuid_str,
|
robot_uuid: uuid_str,
|
||||||
cpu: 420,
|
cpu: cpu_count,
|
||||||
avatar_id: None, // not serialised
|
avatar_id: None, // not serialised
|
||||||
weapon_order: weapons_guess,
|
weapon_order: weapons_guess,
|
||||||
colour_map: factory_vehicle.0.colour_data,
|
colour_map: factory_vehicle.0.colour_data,
|
||||||
@@ -391,6 +401,11 @@ impl UserData {
|
|||||||
crate::persist::config::VehicleDescriptor::Database { garage } => {
|
crate::persist::config::VehicleDescriptor::Database { garage } => {
|
||||||
match self.db.garage_by_id(*garage).await {
|
match self.db.garage_by_id(*garage).await {
|
||||||
Ok(Some(db_vehicle)) => {
|
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 {
|
crate::data::player_data::PlayerData {
|
||||||
name: username.clone(),
|
name: username.clone(),
|
||||||
display_name: username.clone(),
|
display_name: username.clone(),
|
||||||
@@ -402,7 +417,7 @@ impl UserData {
|
|||||||
team: team_num,
|
team: team_num,
|
||||||
has_premium: false,
|
has_premium: false,
|
||||||
robot_uuid: uuid_str,
|
robot_uuid: uuid_str,
|
||||||
cpu: db_vehicle.total_robot_cpu as i32,
|
cpu: cpu_count,
|
||||||
avatar_id: None, // not serialised
|
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::<Vec<_>>(),
|
weapon_order: oj_rc_database::schema::parse_int_csv(&db_vehicle.weapon_order).into_iter().map(|x| x as i32).collect::<Vec<_>>(),
|
||||||
colour_map: db_vehicle.colour_data,
|
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 = weapon_order.guess_weapons(&mut std::io::Cursor::new(&cube_data));
|
||||||
let weapons_guess = vec![weapons_guess[0], 0, 0];
|
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 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 {
|
crate::data::player_data::PlayerData {
|
||||||
name: username.clone(),
|
name: username.clone(),
|
||||||
display_name: username.clone(),
|
display_name: username.clone(),
|
||||||
@@ -441,7 +457,7 @@ impl UserData {
|
|||||||
team: team_num,
|
team: team_num,
|
||||||
has_premium: false,
|
has_premium: false,
|
||||||
robot_uuid: uuid_str,
|
robot_uuid: uuid_str,
|
||||||
cpu: 420, // FIXME
|
cpu: cpu_counts.total as i32,
|
||||||
avatar_id: None, // not serialised
|
avatar_id: None, // not serialised
|
||||||
weapon_order: weapons_guess,
|
weapon_order: weapons_guess,
|
||||||
colour_map: colour_data.to_owned(),
|
colour_map: colour_data.to_owned(),
|
||||||
@@ -603,14 +619,17 @@ impl <C: Clone> super::User<C> 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?;
|
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 {
|
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)),
|
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),
|
robot_data: oj_rc_database::sea_orm::ActiveValue::Set(vehicle.robot_data),
|
||||||
colour_data: oj_rc_database::sea_orm::ActiveValue::Set(vehicle.colour_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() },
|
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() },
|
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()
|
..Default::default()
|
||||||
};
|
};
|
||||||
self.save_garage_by_slot(entity, vehicle.slot).await.map_err(|e| {
|
self.save_garage_by_slot(entity, vehicle.slot).await.map_err(|e| {
|
||||||
@@ -854,10 +873,10 @@ impl <C: Clone> super::User<C> for UserData {
|
|||||||
super::since_windows_epoch(self.account.creation_time)
|
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<polariton::operation::Typed<C>, 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<polariton::operation::Typed<C>, i16> {
|
||||||
//self.err_on_banned().await?;
|
//self.err_on_banned().await?;
|
||||||
let mut vehicles = self.resolve_some_singleplayer_vehicles(factory, weapon_order, singleplayer_config).await?;
|
let mut vehicles = self.resolve_some_singleplayer_vehicles(factory, weapon_order, singleplayer_config, cpu_counter).await?;
|
||||||
let user_bot = self.user_player_data().await?;
|
let user_bot = self.user_player_data(cpu_counter).await?;
|
||||||
|
|
||||||
// real user MUST be last
|
// real user MUST be last
|
||||||
vehicles.push(user_bot);
|
vehicles.push(user_bot);
|
||||||
@@ -1121,8 +1140,8 @@ impl super::LobbyUser for UserData {
|
|||||||
self.account.id
|
self.account.id
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn player_data(&self) -> Result<crate::data::player_data::PlayerData, polariton_server::operations::SimpleOpError> {
|
async fn player_data(&self, cpu_counter: &crate::cubes::CpuListParser) -> Result<crate::data::player_data::PlayerData, polariton_server::operations::SimpleOpError> {
|
||||||
self.user_player_data().await.map_err(|e| {
|
self.user_player_data(cpu_counter).await.map_err(|e| {
|
||||||
if let Some(msg) = e.error_msg() {
|
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())
|
polariton_server::operations::SimpleOpError::with_message(crate::data::error_codes::LobbyReasonCode::from_service_error(e.error_code()) as i16, msg.to_owned())
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ pub trait User<C>: ChatUser + LobbyUser + MultiplayerUser {
|
|||||||
async fn select_garage(&self, slot: i32) -> Result<(), i16>;
|
async fn select_garage(&self, slot: i32) -> Result<(), i16>;
|
||||||
async fn all_slots(&self) -> UserSlots<C>;
|
async fn all_slots(&self) -> UserSlots<C>;
|
||||||
async fn slot_by_id(&self, id: i32) -> Result<UserSlotData<C>, i16>;
|
async fn slot_by_id(&self, id: i32) -> Result<UserSlotData<C>, 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<i32>) -> Result<(), i16>;
|
async fn save_slot_order(&self, slots: Vec<i32>) -> Result<(), i16>;
|
||||||
async fn new_slot(&self, reset_slot: Option<i32>) -> Result<NewSlotData<C>, i16>;
|
async fn new_slot(&self, reset_slot: Option<i32>) -> Result<NewSlotData<C>, i16>;
|
||||||
async fn copy_slot(&self, slot: i32, into_slot: Option<i32>, append: &str) -> Result<(), i16>;
|
async fn copy_slot(&self, slot: i32, into_slot: Option<i32>, append: &str) -> Result<(), i16>;
|
||||||
@@ -76,7 +76,7 @@ pub trait User<C>: ChatUser + LobbyUser + MultiplayerUser {
|
|||||||
async fn get_slot_customisations(&self, uuid: &str) -> Result<GetCustomisationData<C>, i16>;
|
async fn get_slot_customisations(&self, uuid: &str) -> Result<GetCustomisationData<C>, i16>;
|
||||||
async fn set_slot_name(&self, slot: i32, name: String) -> Result<(), i16>;
|
async fn set_slot_name(&self, slot: i32, name: String) -> Result<(), i16>;
|
||||||
fn signup_date(&self) -> i64;
|
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<polariton::operation::Typed<C>, 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<polariton::operation::Typed<C>, i16>;
|
||||||
async fn prepare_factory_upload(&self, vehicle: VehicleUploadData) -> Result<oj_rc_factory::VehicleUploadInfo, i16>;
|
async fn prepare_factory_upload(&self, vehicle: VehicleUploadData) -> Result<oj_rc_factory::VehicleUploadInfo, i16>;
|
||||||
async fn last_seen(&self) -> Result<u64, i16>;
|
async fn last_seen(&self) -> Result<u64, i16>;
|
||||||
async fn get_avatar_info(&self) -> Result<GetAvatarInfo<C>, i16>;
|
async fn get_avatar_info(&self) -> Result<GetAvatarInfo<C>, i16>;
|
||||||
@@ -230,7 +230,7 @@ impl SanctionType {
|
|||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait LobbyUser {
|
pub trait LobbyUser {
|
||||||
fn user_id(&self) -> i32;
|
fn user_id(&self) -> i32;
|
||||||
async fn player_data(&self) -> Result<crate::data::player_data::PlayerData, polariton_server::operations::SimpleOpError>;
|
async fn player_data(&self, cpu_counter: &crate::cubes::CpuListParser) -> Result<crate::data::player_data::PlayerData, polariton_server::operations::SimpleOpError>;
|
||||||
async fn start_game(&self, game: GameDescriptor, players: Vec<PlayerLobbyDescriptor>) -> Result<(), polariton_server::operations::SimpleOpError>;
|
async fn start_game(&self, game: GameDescriptor, players: Vec<PlayerLobbyDescriptor>) -> Result<(), polariton_server::operations::SimpleOpError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,11 @@ pub struct QueueHandler {
|
|||||||
hostname: String,
|
hostname: String,
|
||||||
hostport: u16,
|
hostport: u16,
|
||||||
network_conf: crate::data::network::NetworkConfigData,
|
network_conf: crate::data::network::NetworkConfigData,
|
||||||
|
cpu_counter: std::sync::Arc<oj_rc_core::cubes::CpuListParser>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl QueueHandler {
|
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<oj_rc_core::cubes::CpuListParser>,) -> Self {
|
||||||
let (domain, port_str) = game_host.split_once(':').expect("Invalid redirect address (must be domain:port)");
|
let (domain, port_str) = game_host.split_once(':').expect("Invalid redirect address (must be domain:port)");
|
||||||
Self {
|
Self {
|
||||||
users_in_queue: tokio::sync::Mutex::new(HashMap::new()),
|
users_in_queue: tokio::sync::Mutex::new(HashMap::new()),
|
||||||
@@ -33,6 +34,7 @@ impl QueueHandler {
|
|||||||
hostname: domain.to_owned(),
|
hostname: domain.to_owned(),
|
||||||
hostport: port_str.parse().expect("Invalid redirect port"),
|
hostport: port_str.parse().expect("Invalid redirect port"),
|
||||||
network_conf: crate::data::network::NetworkConfigData::from_conf(oj_rc_core::ConfigProvider::<()>::network_config(conf)),
|
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 {
|
let key = QueueKey {
|
||||||
map, mode, visibility, auto_heal,
|
map, mode, visibility, auto_heal,
|
||||||
};
|
};
|
||||||
match user.player_data().await {
|
match user.player_data(&self.cpu_counter).await {
|
||||||
Ok(player_data) => {
|
Ok(player_data) => {
|
||||||
let mut new_player = QueueUser {
|
let mut new_player = QueueUser {
|
||||||
emitter: event_emitter,
|
emitter: event_emitter,
|
||||||
|
|||||||
@@ -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 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 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 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 {
|
let init_ctx = InitConfig {
|
||||||
config,
|
config,
|
||||||
|
|||||||
@@ -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<oj_rc_core::factory::Factory>, weapon_order: &std::sync::Arc<oj_rc_core::cubes::WeaponListParser>) -> Result<ParameterTable, i16> {
|
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc<oj_rc_core::factory::Factory>, weapon_order: &std::sync::Arc<oj_rc_core::cubes::WeaponListParser>, cpu_counter: &std::sync::Arc<oj_rc_core::cubes::CpuListParser>,) -> 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) {
|
||||||
@@ -37,7 +37,7 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory:
|
|||||||
weapon_order: weapons,
|
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, cpu_counter).await?;
|
||||||
} else {
|
} else {
|
||||||
log::warn!("Failed to retrieve (for copy-construct) non-existent factory vehicle {}", factory_id);
|
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);
|
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 {
|
pub struct CrfItemPurchaseProvider {
|
||||||
factory: std::sync::Arc<oj_rc_core::factory::Factory>,
|
factory: std::sync::Arc<oj_rc_core::factory::Factory>,
|
||||||
weapon_order: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>,
|
weapon_order: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>,
|
||||||
|
cpu_counter: std::sync::Arc<oj_rc_core::cubes::CpuListParser>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@@ -58,7 +59,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, &self.weapon_order).await)
|
polariton_server::operations::result_to_op_resp::<CODE, ()>(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<oj_rc_core::factory::Factory>, weapon_order: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>) -> CrfItemPurchaseProvider {
|
pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc<oj_rc_core::factory::Factory>, weapon_order: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>, cpu_counter: std::sync::Arc<oj_rc_core::cubes::CpuListParser>) -> CrfItemPurchaseProvider {
|
||||||
CrfItemPurchaseProvider {
|
CrfItemPurchaseProvider {
|
||||||
factory: factory.to_owned(),
|
factory: factory.to_owned(),
|
||||||
weapon_order,
|
weapon_order,
|
||||||
|
cpu_counter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,11 +59,13 @@ const COMPRESSED_COLOUR_DATA_PARAM_KEY: u8 = 33; // byte arr
|
|||||||
|
|
||||||
const INVALID_ROBOT_ERR: i16 = 140;
|
const INVALID_ROBOT_ERR: i16 = 140;
|
||||||
|
|
||||||
pub(super) fn garage_machine_save_provider() -> MachineSaver {
|
pub(super) fn garage_machine_save_provider(cpu_counter: std::sync::Arc<oj_rc_core::cubes::CpuListParser>) -> MachineSaver {
|
||||||
MachineSaver
|
MachineSaver {
|
||||||
|
cpu_counter
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn do_save(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
|
async fn do_save(params: ParameterTable<()>, user: &crate::UserTy, cpu_counter: &std::sync::Arc<oj_rc_core::cubes::CpuListParser>) -> Result<ParameterTable, i16> {
|
||||||
log::debug!("machine save params: {:?}", params);
|
log::debug!("machine save params: {:?}", params);
|
||||||
let mut params = params.to_dict();
|
let mut params = params.to_dict();
|
||||||
if let Some(Typed::Int(slot_index)) = params.remove(&SLOT_PARAM_KEY) {
|
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<Par
|
|||||||
weapon_order: weapon_order_filtered,
|
weapon_order: weapon_order_filtered,
|
||||||
crf_id: None,
|
crf_id: None,
|
||||||
};
|
};
|
||||||
user_info.save_slot(vehicle_data).await?;
|
user_info.save_slot(vehicle_data, cpu_counter).await?;
|
||||||
let mut params_out = std::collections::HashMap::with_capacity(1);
|
let mut params_out = std::collections::HashMap::with_capacity(1);
|
||||||
params_out.insert(ERROR_PARAM_KEY, Typed::Int(0));
|
params_out.insert(ERROR_PARAM_KEY, Typed::Int(0));
|
||||||
return Ok(params_out.into());
|
return Ok(params_out.into());
|
||||||
@@ -99,14 +101,16 @@ async fn do_save(params: ParameterTable<()>, user: &crate::UserTy) -> Result<Par
|
|||||||
Err(INVALID_ROBOT_ERR)
|
Err(INVALID_ROBOT_ERR)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MachineSaver;
|
pub struct MachineSaver {
|
||||||
|
cpu_counter: std::sync::Arc<oj_rc_core::cubes::CpuListParser>,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl polariton_server::operations::Operation<()> for MachineSaver {
|
impl polariton_server::operations::Operation<()> for MachineSaver {
|
||||||
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_MACHINE_SAVER, ()>(do_save(params, user).await)
|
polariton_server::operations::result_to_op_resp::<CODE_MACHINE_SAVER, ()>(do_save(params, user, &self.cpu_counter).await)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler<crate::UserTy>
|
|||||||
.add(building_xp::building_xp_save_provider())
|
.add(building_xp::building_xp_save_provider())
|
||||||
.add(robot_sanction::all_robot_sanctions_provider())
|
.add(robot_sanction::all_robot_sanctions_provider())
|
||||||
.add(reconnect_game::available_reconnect_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::<32, _>::default()) // TODO handle SaveMachineColorRequest instead of ignoring it
|
||||||
.add(polariton_server::operations::Ack::<45, _>::default()) // TODO handle UpdateThumbnailVersionRequest 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))
|
.add(weapon_order::weapon_order_provider(&init_ctx.cubes))
|
||||||
@@ -209,7 +209,7 @@ 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, 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(crf_upload::crf_upload_provider(&init_ctx.factory))
|
||||||
.add(avatar_set_custom::custom_avatar_upload_handler())
|
.add(avatar_set_custom::custom_avatar_upload_handler())
|
||||||
.add(avatar_set::avatar_set_provider())
|
.add(avatar_set::avatar_set_provider())
|
||||||
|
|||||||
@@ -4,18 +4,19 @@ const CODE: u8 = 1;
|
|||||||
|
|
||||||
const PARAM_KEY: u8 = 8;
|
const PARAM_KEY: u8 = 8;
|
||||||
|
|
||||||
pub(super) fn tdm_machines_provider(factory: &std::sync::Arc<oj_rc_core::factory::Factory>, weapon_order: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>, conf: &oj_rc_core::ConfigImpl) -> AiRobots {
|
pub(super) fn tdm_machines_provider(factory: &std::sync::Arc<oj_rc_core::factory::Factory>, weapon_order: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>, conf: &oj_rc_core::ConfigImpl, cpu_counter: std::sync::Arc<oj_rc_core::cubes::CpuListParser>) -> AiRobots {
|
||||||
AiRobots {
|
AiRobots {
|
||||||
factory: factory.to_owned(),
|
factory: factory.to_owned(),
|
||||||
weapon_parser: weapon_order,
|
weapon_parser: weapon_order,
|
||||||
singleplayer_config: <oj_rc_core::ConfigImpl as oj_rc_core::ConfigProvider<()>>::singleplayer_details(conf),
|
singleplayer_config: <oj_rc_core::ConfigImpl as oj_rc_core::ConfigProvider<()>>::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<ParameterTable<()>, 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<oj_rc_core::cubes::CpuListParser>) -> Result<ParameterTable<()>, i16> {
|
||||||
let ulock = user.user()?;
|
let ulock = user.user()?;
|
||||||
let mut params = params.to_dict();
|
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())
|
Ok(params.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ pub struct AiRobots {
|
|||||||
factory: std::sync::Arc<oj_rc_core::factory::Factory>,
|
factory: std::sync::Arc<oj_rc_core::factory::Factory>,
|
||||||
weapon_parser: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>,
|
weapon_parser: std::sync::Arc<oj_rc_core::cubes::WeaponListParser>,
|
||||||
singleplayer_config: oj_rc_core::persist::config::SingleplayerConfig,
|
singleplayer_config: oj_rc_core::persist::config::SingleplayerConfig,
|
||||||
|
cpu_parser: std::sync::Arc<oj_rc_core::cubes::CpuListParser>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@@ -30,7 +32,7 @@ impl polariton_server::operations::Operation<()> for AiRobots {
|
|||||||
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.as_ref(), self.weapon_parser.as_ref(), &self.singleplayer_config).await)
|
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, self.factory.as_ref(), self.weapon_parser.as_ref(), &self.singleplayer_config, &self.cpu_parser).await)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler<crate::UserTy>
|
|||||||
.modify(oj_rc_core::polariton::RcOpModifier)
|
.modify(oj_rc_core::polariton::RcOpModifier)
|
||||||
.add(more_auth::MoreLobbyAuth)
|
.add(more_auth::MoreLobbyAuth)
|
||||||
.add(eac::EacChallengeIgnorer)
|
.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::<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)
|
.add(polariton_server::operations::Ack::<2, _>::default()) // Save singleplayer result (parameter-less response)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user