1
0
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:
NGnius (Graham)
2025-05-07 20:23:22 -04:00
parent 69c6c54650
commit 77ddeb7891
55 changed files with 2689 additions and 395 deletions

View File

@@ -22,3 +22,4 @@ serde_json.workspace = true
chrono = "0.4"
rc_core = { version = "*", path = "../rc_core" }
rand = "0.9"
async-trait.workspace = true

View File

@@ -25,7 +25,7 @@ async fn main() -> std::io::Result<()> {
log::debug!("Got cli args {:?}", args);
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).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 init_ctx = std::sync::Arc::new(InitConfig {
cubes,
users,

View File

@@ -1,13 +1,33 @@
use polariton_server::operations::SimpleFunc;
use polariton::operation::{ParameterTable, Typed};
use polariton::operation::{ParameterTable, Typed, OperationResponse};
const CODE: u8 = 177;
const PARAM_KEY: u8 = 54;
pub(super) fn garage_id_provider() -> SimpleFunc<177, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleFunc::new(|params, user: &crate::UserTy| {
let user_info = user.user()?;
let mut params = params.to_dict();
params.insert(PARAM_KEY, Typed::Str(user_info.selected_garage_uuid().into()));
Ok(params.into())
})
pub(super) fn garage_id_provider() -> GarageIdProvider {
GarageIdProvider
}
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
let user_info = user.user()?;
let mut params = params.to_dict();
params.insert(PARAM_KEY, Typed::Str(user_info.selected_garage().await.0.into()));
Ok(params.into())
}
pub struct GarageIdProvider;
#[async_trait::async_trait]
impl polariton_server::operations::Operation<()> for GarageIdProvider {
type User = crate::UserTy;
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user).await)
}
}
impl polariton_server::operations::OperationCode for GarageIdProvider {
fn op_code() -> u8 {
CODE
}
}

View File

@@ -1,18 +1,38 @@
use polariton_server::operations::SimpleFunc;
use polariton::operation::{ParameterTable, Typed};
use polariton::operation::{ParameterTable, Typed, OperationResponse};
const CODE: u8 = 40;
const SLOTS_PARAM_KEY: u8 = 44;
const SELECTED_SLOT_PARAM_KEY: u8 = 43;
const SLOT_ORDER_PARAM_KEY: u8 = 58;
pub(super) fn garage_slot_provider() -> SimpleFunc<40, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleFunc::new(|params, user: &crate::UserTy| {
let user_info = user.user()?;
let mut params = params.to_dict();
let all_slots = user_info.all_slots_by_id();
params.insert(SLOTS_PARAM_KEY, all_slots.slot_info);
params.insert(SELECTED_SLOT_PARAM_KEY, Typed::Int(user_info.selected_garage_slot() as _));
params.insert(SLOT_ORDER_PARAM_KEY, all_slots.slot_order);
Ok(params.into())
})
pub(super) fn garage_slot_provider() -> GarageSlotsProvider {
GarageSlotsProvider
}
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
let user_info = user.user()?;
let mut params = params.to_dict();
let all_slots = user_info.all_slots_by_id().await;
params.insert(SLOTS_PARAM_KEY, all_slots.slot_info);
params.insert(SELECTED_SLOT_PARAM_KEY, Typed::Int(user_info.selected_garage().await.1 as _));
params.insert(SLOT_ORDER_PARAM_KEY, all_slots.slot_order);
Ok(params.into())
}
pub struct GarageSlotsProvider;
#[async_trait::async_trait]
impl polariton_server::operations::Operation<()> for GarageSlotsProvider {
type User = crate::UserTy;
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user).await)
}
}
impl polariton_server::operations::OperationCode for GarageSlotsProvider {
fn op_code() -> u8 {
CODE
}
}

View File

@@ -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
}
}

View File

@@ -1,22 +1,42 @@
use polariton_server::operations::SimpleFunc;
use polariton::operation::{ParameterTable, Typed};
use polariton::operation::{ParameterTable, Typed, OperationResponse};
const CODE: u8 = 33;
// const USERNAME_PARAM_KEY: u8 = 30; // str
const SLOT_PARAM_KEY: u8 = 31; // int
const DATA_PARAM_KEY: u8 = 33; // byte arr
pub(super) fn garage_machine_colour_provider() -> SimpleFunc<33, 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 colour request for slot {:?}", garage_slot);
let machine = user_info.slot_by_id(*garage_slot)?;
params.insert(DATA_PARAM_KEY, machine.colour_data);
} else {
params.insert(SLOT_PARAM_KEY, Typed::Int(user_info.selected_garage_slot() as _));
}
Ok(params.into())
})
pub(super) fn garage_machine_colour_provider() -> MachineColour {
MachineColour
}
async fn do_handling(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 colour request for slot {:?}", garage_slot);
let machine = user_info.slot_by_id(*garage_slot).await?;
params.insert(DATA_PARAM_KEY, machine.colour_data);
} else {
params.insert(SLOT_PARAM_KEY, Typed::Int(user_info.selected_garage().await.1 as _));
}
Ok(params.into())
}
pub struct MachineColour;
#[async_trait::async_trait]
impl polariton_server::operations::Operation<()> for MachineColour {
type User = crate::UserTy;
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user).await)
}
}
impl polariton_server::operations::OperationCode for MachineColour {
fn op_code() -> u8 {
CODE
}
}

View File

@@ -7,18 +7,19 @@ impl MoreLobbyAuth {
const AUTH_PAYLOAD_KEY: u8 = 245;
}
#[async_trait::async_trait]
impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
type User = crate::UserTy;
fn handle(&self, params: polariton::operation::ParameterTable<C>, user: &Self::User) -> polariton::operation::OperationResponse<C> {
async fn handle_async(&self, params: polariton::operation::ParameterTable<C>, user: &Self::User) -> polariton::operation::OperationResponse<C> {
let params_dict = params.to_dict();
if let Some(Typed::Str(auth_payload)) = params_dict.get(&Self::AUTH_PAYLOAD_KEY) {
//let mut write_lock = user.write().unwrap();
if user.update_with_auth(&auth_payload.string) {
if user.update_with_auth(&auth_payload.string).await {
let mut resp_params = std::collections::HashMap::new();
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
return polariton::operation::OperationResponse {
code: 230,
code: Self::op_code(),
return_code: 0,
message: polariton::operation::Typed::Null,
params: resp_params.into(),
@@ -26,12 +27,16 @@ impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
}
}
polariton::operation::OperationResponse {
code: 230,
code: Self::op_code(),
return_code: 120,
message: polariton::operation::Typed::Null,
params: std::collections::HashMap::new().into(),
}
}
fn handle(&self, _params: polariton::operation::ParameterTable<C>, _user: &Self::User) -> polariton::operation::OperationResponse<C> {
unreachable!()
}
}
impl OperationCode for MoreLobbyAuth {

View File

@@ -21,7 +21,7 @@ pub(super) fn platform_config_provider() -> SimpleFunc<165, crate::UserTy, impl
(Typed::Str("UseDecimalSystem".into()), Typed::Bool(false.into())),
(Typed::Str("FeedbackURL".into()), Typed::Str("https://mstdn.ca/@ngram".into())),
(Typed::Str("SupportURL".into()), Typed::Str("https://git.ngni.us/OpenJam/servers".into())),
(Typed::Str("WikiURL".into()), Typed::Str("https://docs.rs/libfj/latest/libfj/".into())),
(Typed::Str("WikiURL".into()), Typed::Str("https://git.ngram.ca/OpenJam/servers/wiki".into())),
].into(),
}));
Ok(params.into())

View File

@@ -1,22 +1,40 @@
use polariton_server::operations::SimpleFunc;
use polariton::operation::{ParameterTable, Typed};
use polariton::operation::{ParameterTable, Typed, OperationResponse};
const USERNAME_PARAM_KEY: u8 = 30; // in; str
const RANK_PARAM_KEY: u8 = 84; // out; int
const CPU_PARAM_KEY: u8 = 177; // out; int
const COSMETIC_CPU_PARAM_KEY: u8 = 176; // out; int
pub(super) fn player_robot_rank_provider() -> SimpleFunc<79, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
SimpleFunc::new(|params, user: &crate::UserTy| {
let user = user.user()?;
let mut params = params.to_dict();
if let Some(Typed::Str(username)) = params.get(&USERNAME_PARAM_KEY) {
log::debug!("Get robot rank for user {}", username.string);
}
let robot = user.slot_by_id(user.selected_garage_slot() as i32)?;
params.insert(RANK_PARAM_KEY, robot.robot_rank);
params.insert(CPU_PARAM_KEY, robot.cpu);
params.insert(COSMETIC_CPU_PARAM_KEY, robot.cosmetic_cpu);
Ok(params.into())
})
pub(super) fn player_robot_rank_provider() -> PlayerRank {
PlayerRank
}
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
let user = user.user()?;
let mut params = params.to_dict();
if let Some(Typed::Str(username)) = params.get(&USERNAME_PARAM_KEY) {
log::debug!("Get robot rank for user {}", username.string);
}
let robot = user.slot_by_id(user.selected_garage().await.1 as i32).await?;
params.insert(RANK_PARAM_KEY, robot.robot_rank);
params.insert(CPU_PARAM_KEY, robot.cpu);
params.insert(COSMETIC_CPU_PARAM_KEY, robot.cosmetic_cpu);
Ok(params.into())
}
pub struct PlayerRank;
#[async_trait::async_trait]
impl polariton_server::operations::Operation<()> for PlayerRank {
type User = crate::UserTy;
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
polariton_server::operations::result_to_op_resp::<79, ()>(do_handling(params, user).await)
}
}
impl polariton_server::operations::OperationCode for PlayerRank {
fn op_code() -> u8 {
79
}
}