1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00

Add minimal robot factory functionality and arc archive support for #6

This commit is contained in:
NG (Graham)
2025-05-17 23:06:07 -04:00
parent 3f0c30f6c3
commit 3f1438c068
34 changed files with 869 additions and 4 deletions

View File

@@ -24,3 +24,4 @@ jsonwebtoken = "9"
argon2 = { version = "0.5", features = [ "std" ] }
rc_database = { version = "0.2", path = "../rc_database" }
rc_factory = { version = "0.2", path = "../rc_factory" }

View File

@@ -28,6 +28,22 @@ pub fn encode_7_bit_i32(mut src: i32) -> Vec<u8> {
out
}
pub fn decode_7_bit_i32(reader: &mut dyn std::io::Read) -> std::io::Result<i32> {
let mut buf = [0u8; 1];
let mut out: i32 = 0;
for _ in 0..5 {
reader.read_exact(&mut buf)?;
let byte = buf[0];
let has_more = byte & 0x80;
let number = byte & 0x7F;
out = (out << 7) | (number as i32);
if has_more == 0 {
return Ok(out);
}
}
Ok(out)
}
pub fn write_str_for_binreader(s: &str, writer: &mut dyn std::io::Write) -> std::io::Result<usize> {
let s_bytes = s.as_bytes();
let mut total_len = writer.write(&encode_7_bit_i32(s_bytes.len() as i32))?;
@@ -35,6 +51,13 @@ pub fn write_str_for_binreader(s: &str, writer: &mut dyn std::io::Write) -> std:
Ok(total_len)
}
pub fn read_str_for_binwriter(reader: &mut dyn std::io::Read) -> std::io::Result<String> {
let len = decode_7_bit_i32(reader)?;
let mut buf = vec![0u8; len as usize];
reader.read_exact(&mut buf)?;
Ok(String::from_utf8_lossy(&buf).into_owned())
}
pub fn cube_id_to_str(id: u32) -> String {
hex::encode(id.to_be_bytes()).into()
}

View File

@@ -0,0 +1,33 @@
pub enum Factory {
Arc(rc_factory::arc::ArcAdapter),
Custom(Box<dyn rc_factory::VehicleFactoryAdapter + Send + Sync + 'static>),
None,
}
#[async_trait::async_trait]
impl rc_factory::VehicleFactoryAdapter for Factory {
async fn vehicle(&self, id: u32) -> Result<Option<rc_factory::VehicleInfo>, Box<dyn std::error::Error>> {
match self {
Self::Arc(x) => x.vehicle(id).await,
Self::Custom(x) => x.vehicle(id).await,
Self::None => Ok(None),
}
}
async fn list(&self, query: libfj::robocraft::ListQuery) -> Result<Vec<rc_factory::VehicleQueryInfo>, Box<dyn std::error::Error>> {
match self {
Self::Arc(x) => x.list(query).await,
Self::Custom(x) => x.list(query).await,
Self::None => Ok(Vec::default()),
}
}
}
impl Factory {
pub async fn from_config(conf: &crate::persist::FactoryConfig) -> Result<Self, Box<dyn std::error::Error + 'static>> {
Ok(match &conf.adapter {
crate::persist::AdapterSettings::Arc(x) => Self::Arc(rc_factory::arc::ArcAdapter::init(&x.uri, x.show_expired).await?),
crate::persist::AdapterSettings::None => Self::None,
})
}
}

View File

@@ -0,0 +1,2 @@
mod adapter_enum;
pub use adapter_enum::Factory;

View File

@@ -9,3 +9,5 @@ pub use persist::user::{UserImpl, UserProvider, UserAuthenticator};
pub use persist::config::{ConfigImpl, ConfigProvider};
pub mod polariton;
pub mod factory;

View File

@@ -5,7 +5,7 @@ use serde::{Serialize, Deserialize};
use polariton::operation::{Typed, Dict};
use polariton::serdes::TypePrefix;
use super::super::{MovementCategoryData, MovementData, Cube, ItemCategory, ItemTier, BattleConfig, Settings, ChatConfig};
use super::super::{MovementCategoryData, MovementData, Cube, ItemCategory, ItemTier, BattleConfig, Settings, ChatConfig, FactoryConfig};
const CUBE_CONFIG_FILENAME: &str = "config.json";
@@ -16,6 +16,7 @@ pub struct CubeConfig {
lerp_value: f32,
battle: BattleConfig,
chat: ChatConfig,
factory: FactoryConfig,
settings: Settings,
}
@@ -28,6 +29,7 @@ impl CubeConfig {
}
}
#[async_trait::async_trait]
impl <C: Clone> super::ConfigProvider<C> for CubeConfig {
fn cube_list(&self) -> Typed<C> {
Typed::Dict(Dict {
@@ -271,4 +273,8 @@ impl <C: Clone> super::ConfigProvider<C> for CubeConfig {
}).collect(),
}
}
async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>> {
crate::factory::Factory::from_config(&self.factory).await
}
}

View File

@@ -1,5 +1,6 @@
use polariton::operation::Typed;
#[async_trait::async_trait]
pub trait ConfigProvider<C: Clone> {
fn cube_list(&self) -> Typed<C>;
fn movement_list(&self) -> Typed<C>;
@@ -20,6 +21,7 @@ pub trait ConfigProvider<C: Clone> {
fn public_channels(&self) -> Typed<C>;
fn server_config(&self) -> ServerConfig;
fn garage_upgrades(&self) -> GarageUpgrades;
async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>>;
}
pub struct CompleteCampaignProvider {

View File

@@ -32,6 +32,9 @@ pub use settings::Settings;
mod chat;
pub use chat::ChatConfig;
mod vehicle_factory;
pub use vehicle_factory::{FactoryConfig, AdapterSettings, ArcFactorySettings};
pub(self) const VALID_ROBOT: &[u8] = &[64,
0,
0,

View File

@@ -387,6 +387,7 @@ impl <C: Clone> super::User<C> for UserData {
slot: polariton::operation::Typed::Int(model.slot as _),
bay_cpu: polariton::operation::Typed::Int(model.bay_cpu as _),
mastery_level: polariton::operation::Typed::Int(model.mastery_level as _),
slot_i: model.slot as _,
})
}

View File

@@ -100,6 +100,7 @@ pub struct NewSlotData<C> {
pub slot: polariton::operation::Typed<C>,
pub bay_cpu: polariton::operation::Typed<C>,
pub mastery_level: polariton::operation::Typed<C>,
pub slot_i: i32,
}
pub struct VehicleData {

View File

@@ -0,0 +1,30 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FactoryConfig {
#[serde(default = "default_variant")]
pub adapter: AdapterSettings,
}
fn default_variant() -> AdapterSettings {
AdapterSettings::None
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "variant")]
pub enum AdapterSettings {
#[serde(alias = "sqlite")]
Arc(ArcFactorySettings),
None,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ArcFactorySettings {
pub uri: String,
#[serde(default = "default_true")]
pub show_expired: bool,
}
fn default_true() -> bool {
true
}