mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Create RC database layer
This commit is contained in:
1 parent
69c6c54650
commit
77ddeb7891
55 files changed
+2689
-395
No files matched your search
@@ -1,5 +1,7 @@
|
||||
use polariton_server::operations::SimpleFunc;
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
use polariton::operation::{ParameterTable, Typed, OperationResponse};
|
||||
|
||||
const CODE_MACHINE_PROVIDER: u8 = 43;
|
||||
const CODE_MACHINE_SAVER: u8 = 41;
|
||||
|
||||
const SLOT_PARAM_KEY: u8 = 45; // uint
|
||||
const DATA_PARAM_KEY: u8 = 49; // byte arr
|
||||
@@ -10,25 +12,44 @@ const CONTROL_TYPE_PARAM_KEY: u8 = 59; // int
|
||||
const CONTROL_OPTIONS_PARAM_KEY: u8 = 60; // bool arr
|
||||
const MASTERY_LEVEL_PARAM_KEY: u8 = 18; // int
|
||||
|
||||
pub(super) fn garage_machine_provider() -> SimpleFunc<43, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
|
||||
SimpleFunc::new(|params, user: &crate::UserTy| {
|
||||
let mut params = params.to_dict();
|
||||
let user_info = user.user()?;
|
||||
if let Some(Typed::Int(garage_slot)) = params.get(&SLOT_PARAM_KEY) {
|
||||
log::debug!("Got machine request for slot {:?}", garage_slot);
|
||||
let machine = user_info.slot_by_id(*garage_slot)?;
|
||||
params.insert(DATA_PARAM_KEY, machine.data);
|
||||
params.insert(CUBES_COUNT_PARAM_KEY, machine.cube_count);
|
||||
params.insert(WEAPON_ORDER_PARAM_KEY, machine.weapon_order);
|
||||
params.insert(MOVEMENT_CATEGORIES_PARAM_KEY, machine.movement_categories);
|
||||
params.insert(CONTROL_TYPE_PARAM_KEY, machine.control_type);
|
||||
params.insert(CONTROL_OPTIONS_PARAM_KEY, machine.control_options);
|
||||
params.insert(MASTERY_LEVEL_PARAM_KEY, machine.mastery_level);
|
||||
} else {
|
||||
params.insert(SLOT_PARAM_KEY, Typed::Int(user_info.selected_garage_slot() as _));
|
||||
}
|
||||
Ok(params.into())
|
||||
})
|
||||
pub(super) fn garage_machine_provider() -> MachineProvider {
|
||||
MachineProvider
|
||||
}
|
||||
|
||||
async fn do_get(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
|
||||
let mut params = params.to_dict();
|
||||
let user_info = user.user()?;
|
||||
if let Some(Typed::Int(garage_slot)) = params.get(&SLOT_PARAM_KEY) {
|
||||
log::debug!("Got machine request for slot {:?}", garage_slot);
|
||||
let machine = user_info.slot_by_id(*garage_slot).await?;
|
||||
params.insert(DATA_PARAM_KEY, machine.data);
|
||||
params.insert(CUBES_COUNT_PARAM_KEY, machine.cube_count);
|
||||
params.insert(WEAPON_ORDER_PARAM_KEY, machine.weapon_order);
|
||||
params.insert(MOVEMENT_CATEGORIES_PARAM_KEY, machine.movement_categories);
|
||||
params.insert(CONTROL_TYPE_PARAM_KEY, machine.control_type);
|
||||
params.insert(CONTROL_OPTIONS_PARAM_KEY, machine.control_options);
|
||||
params.insert(MASTERY_LEVEL_PARAM_KEY, machine.mastery_level);
|
||||
} else {
|
||||
params.insert(SLOT_PARAM_KEY, Typed::Int(user_info.selected_garage().await.1 as _));
|
||||
}
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
pub struct MachineProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl polariton_server::operations::Operation<()> for MachineProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<CODE_MACHINE_PROVIDER, ()>(do_get(params, user).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for MachineProvider {
|
||||
fn op_code() -> u8 {
|
||||
CODE_MACHINE_PROVIDER
|
||||
}
|
||||
}
|
||||
|
||||
const ERROR_PARAM_KEY: u8 = 47; // int
|
||||
@@ -38,39 +59,58 @@ const COMPRESSED_COLOUR_DATA_PARAM_KEY: u8 = 33; // byte arr
|
||||
|
||||
const INVALID_ROBOT_ERR: i16 = 140;
|
||||
|
||||
pub(super) fn garage_machine_save_provider() -> SimpleFunc<41, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
|
||||
SimpleFunc::new(|params, user: &crate::UserTy| {
|
||||
log::debug!("machine save params: {:?}", params);
|
||||
let mut params = params.to_dict();
|
||||
if let Some(Typed::Int(slot_index)) = params.remove(&SLOT_PARAM_KEY) {
|
||||
if let Some(Typed::Bytes(robot_data)) = params.remove(&COMPRESSED_ROBOT_DATA_PARAM_KEY) {
|
||||
if let Some(Typed::Bytes(colour_data)) = params.remove(&COMPRESSED_COLOUR_DATA_PARAM_KEY) {
|
||||
if let Some(Typed::Arr(weapon_order)) = params.remove(&WEAPON_ORDER_PARAM_KEY) {
|
||||
let weapon_order_filtered: Vec<_> = weapon_order.items.into_iter().filter_map(|ty| if let Typed::Int(i) = ty { Some(i) } else { None }).collect();
|
||||
let user_info = user.user()?;
|
||||
let vehicle_data = rc_core::persist::user::VehicleData {
|
||||
id: slot_index,
|
||||
robot_data: robot_data.vec,
|
||||
colour_data: colour_data.vec,
|
||||
weapon_order: weapon_order_filtered,
|
||||
};
|
||||
user_info.save_slot(vehicle_data)?;
|
||||
let mut params_out = std::collections::HashMap::with_capacity(1);
|
||||
params_out.insert(ERROR_PARAM_KEY, Typed::Int(0));
|
||||
return Ok(params_out.into());
|
||||
} else {
|
||||
log::warn!("weapon order is not this type (or does not exist)");
|
||||
}
|
||||
pub(super) fn garage_machine_save_provider() -> MachineSaver {
|
||||
MachineSaver
|
||||
}
|
||||
|
||||
async fn do_save(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
|
||||
log::debug!("machine save params: {:?}", params);
|
||||
let mut params = params.to_dict();
|
||||
if let Some(Typed::Int(slot_index)) = params.remove(&SLOT_PARAM_KEY) {
|
||||
if let Some(Typed::Bytes(robot_data)) = params.remove(&COMPRESSED_ROBOT_DATA_PARAM_KEY) {
|
||||
if let Some(Typed::Bytes(colour_data)) = params.remove(&COMPRESSED_COLOUR_DATA_PARAM_KEY) {
|
||||
if let Some(Typed::Arr(weapon_order)) = params.remove(&WEAPON_ORDER_PARAM_KEY) {
|
||||
let weapon_order_filtered: Vec<_> = weapon_order.items.into_iter().filter_map(|ty| if let Typed::Int(i) = ty { Some(i) } else { None }).collect();
|
||||
let user_info = user.user()?;
|
||||
let vehicle_data = rc_core::persist::user::VehicleData {
|
||||
slot: slot_index,
|
||||
robot_data: robot_data.vec,
|
||||
colour_data: colour_data.vec,
|
||||
weapon_order: weapon_order_filtered,
|
||||
};
|
||||
user_info.save_slot(vehicle_data).await?;
|
||||
let mut params_out = std::collections::HashMap::with_capacity(1);
|
||||
params_out.insert(ERROR_PARAM_KEY, Typed::Int(0));
|
||||
return Ok(params_out.into());
|
||||
} else {
|
||||
log::warn!("colour data is not this type (or does not exist)");
|
||||
log::warn!("weapon order is not this type (or does not exist)");
|
||||
}
|
||||
} else {
|
||||
log::warn!("robot data is not this type (or does not exist)");
|
||||
log::warn!("colour data is not this type (or does not exist)");
|
||||
}
|
||||
} else {
|
||||
log::warn!("slot is not this type (or does not exist)");
|
||||
log::warn!("robot data is not this type (or does not exist)");
|
||||
}
|
||||
Err(INVALID_ROBOT_ERR)
|
||||
})
|
||||
} else {
|
||||
log::warn!("slot is not this type (or does not exist)");
|
||||
}
|
||||
Err(INVALID_ROBOT_ERR)
|
||||
}
|
||||
|
||||
pub struct MachineSaver;
|
||||
|
||||
#[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::<CODE_MACHINE_SAVER, ()>(do_save(params, user).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for MachineSaver {
|
||||
fn op_code() -> u8 {
|
||||
CODE_MACHINE_SAVER
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user