1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00
Files
ojrc/rc_core/src/persist/config/traits.rs

294 lines
9.1 KiB
Rust
Raw Normal View History

2025-03-15 11:03:35 -04:00
use polariton::operation::Typed;
#[async_trait::async_trait]
pub trait ConfigProvider<C: Clone> {
2025-03-15 11:03:35 -04:00
fn cube_list(&self) -> Typed<C>;
fn movement_list(&self) -> Typed<C>;
fn weapon_list(&self) -> Typed<C>;
fn weapon_upgrade_list(&self) -> Typed<C>;
fn weapon_keys(&self) -> Typed<C>;
2025-03-15 11:03:35 -04:00
fn tech_tree_nodes(&self, unlocked_cubes: &std::collections::HashSet<u32>) -> Typed<C>;
fn ids(&self) -> Vec<u32>;
2025-03-15 17:21:18 -04:00
fn regen_config(&self) -> Typed<C>;
fn after_battle_vote_config(&self) -> Typed<C>;
fn game_mode_config(&self) -> Typed<C>;
fn campaigns_parameters(&self) -> Typed<C>;
fn campaign_waves(&self) -> Typed<C>;
fn campaign_version(&self) -> Typed<C>;
fn campaign_details(&self) -> CompleteCampaignProvider;
fn client_config(&self) -> Typed<C>;
fn login_messages(&self) -> DevMessageProvider<C>;
2025-04-14 21:17:41 -04:00
fn public_channels(&self) -> Typed<C>;
2025-05-07 20:23:22 -04:00
fn server_config(&self) -> ServerConfig;
fn garage_upgrades(&self) -> GarageUpgrades;
async fn factory(&self) -> Result<crate::factory::Factory, Box<dyn std::error::Error + 'static>>;
fn cubes(&self) -> &'_ std::collections::HashMap<String, crate::persist::Cube>;
fn chat_system_config(&self) -> ChatSystemConfig;
fn gamemode_events(&self) -> GameEventSequence;
fn singleplayer_details(&self) -> SingleplayerConfig;
fn players_per_game(&self) -> usize;
fn is_multiplayer_enabled(&self) -> bool;
// FIXME don't use serializable types in traits
fn network_config(&self) -> crate::persist::NetworkConf;
}
pub struct CompleteCampaignProvider {
map: std::collections::HashMap<String, std::collections::HashMap<i32, crate::data::campaign::CampaignWavesDifficultyData>>,
}
impl CompleteCampaignProvider {
pub fn new(map: std::collections::HashMap<String, std::collections::HashMap<i32, crate::data::campaign::CampaignWavesDifficultyData>>) -> Self {
Self { map }
}
pub fn get<C>(&self, id: &str, difficulty: &i32) -> Result<Typed<C>, i16> {
if let Some(campaign) = self.map.get(id) {
if let Some(details) = campaign.get(difficulty) {
Ok(details.as_transmissible())
} else {
log::warn!("Couldn't find difficulty {} in campaign `{}`", difficulty, id);
Err(crate::data::error_codes::WebServicesError::DatabaseError as i16)
}
} else {
log::warn!("Couldn't find campaign {} (ignoring difficulty {})", id, difficulty);
Err(crate::data::error_codes::WebServicesError::DatabaseError as i16)
}
}
2025-03-15 11:03:35 -04:00
}
pub struct DevMessageProvider<C: Clone> {
messages: Vec<TypedDevMessage<C>>,
}
impl <C: Clone> DevMessageProvider<C> {
pub fn new(messages: Vec<(String, i32)>) -> Self {
Self {
messages: messages.into_iter().map(|(msg, time)| {
let bytes: Vec<u8> = msg.as_bytes().into();
TypedDevMessage {
message: Typed::Bytes(bytes.into()),
display_time: Typed::Int(time),
}
}
).collect(),
}
}
pub fn get(&self, index: usize) -> TypedDevMessage<C> {
// TODO maybe make this less obtuse -- it works for random, but isn't really obvious for anything else
if self.messages.is_empty() {
self.get_empty()
} else if self.messages.len() == 1 {
self.messages[0].clone()
} else {
let actual_index = index % self.messages.len(); // guarantees index is within allowed range of messages
self.messages[actual_index].clone()
}
}
pub fn get_empty(&self) -> TypedDevMessage<C> {
TypedDevMessage {
message: Typed::Bytes(Vec::default().into()),
display_time: Typed::Int(-1),
}
}
}
#[derive(Clone, Debug)]
pub struct TypedDevMessage<C> {
pub message: Typed<C>,
pub display_time: Typed<C>,
}
2025-05-07 20:23:22 -04:00
pub struct ServerConfig {
pub database: String,
2025-05-12 20:56:24 -04:00
pub auto_signup: bool,
2025-05-07 20:23:22 -04:00
}
#[derive(Clone, Debug)]
pub struct GarageUpgrades {
pub increments: Vec<GarageUpgradeIncrement>,
}
#[derive(Clone, Debug)]
pub struct GarageUpgradeIncrement {
pub cpu: u32,
pub cost: u32,
}
impl GarageUpgrades {
pub fn slot_upgrades<C>(&self) -> Typed<C> {
Typed::HashMap(vec![
(Typed::Str("cpuIncreaseCost".into()), Typed::Dict(polariton::operation::Dict {
key_ty: polariton::serdes::TypePrefix::Int, // int
val_ty: polariton::serdes::TypePrefix::Int, // int
// (CPU limit, upgrade cost)
items: self.increments.iter().map(|inc| (Typed::Int(inc.cpu as _), Typed::Int(inc.cost as _))).collect(),
}))
].into())
}
}
#[derive(Clone, Debug)]
pub struct ChatSystemConfig {
pub command_channel: String,
pub commands: Vec<crate::persist::ChatCommand>,
}
#[derive(Clone, Debug)]
pub struct GameEventSequence {
pub strategy: GameRotationStrategy,
pub modes: Vec<GameEvents>,
pub index: usize,
pub started: i64,
}
impl GameEventSequence {
pub fn now(&mut self) -> GameEventTransmissible {
let time_now = chrono::Utc::now().timestamp();
let mut item_now = &self.modes[self.index];
if time_now >= (item_now.duration.as_secs() as i64) + self.started {
// needs refresh
self.index = self.strategy.next(self.index, self.modes.len());
item_now = &self.modes[self.index];
self.started = time_now;
}
let remaining_ticks = ((item_now.duration.as_secs() as i64) - (time_now - self.started)) * 10_000_000;
GameEventTransmissible {
maps: Typed::Arr(polariton::operation::Arr {
ty: polariton::serdes::TypePrefix::Str,
items: vec![
Typed::Str(crate::data::game_mode::GameMap::from_persist(item_now.singleplayer.map).as_str().into()),
Typed::Str(crate::data::game_mode::GameMap::from_persist(item_now.multiplayer.map).as_str().into()),
],
}),
visibilities: Typed::Arr(polariton::operation::Arr {
ty: polariton::serdes::TypePrefix::Int,
items: vec![
Typed::Int(crate::data::game_mode::MapVisibility::from_persist(item_now.singleplayer.visibility) as _),
Typed::Int(crate::data::game_mode::MapVisibility::from_persist(item_now.multiplayer.visibility) as _),
],
}),
modes: Typed::Arr(polariton::operation::Arr {
ty: polariton::serdes::TypePrefix::Int,
items: vec![
Typed::Int(crate::data::game_mode::GameMode::from_persist(item_now.singleplayer.mode) as _),
Typed::Int(crate::data::game_mode::GameMode::from_persist(item_now.multiplayer.mode) as _),
],
}),
auto_heals: Typed::Arr(polariton::operation::Arr {
ty: polariton::serdes::TypePrefix::Bool,
items: vec![
Typed::Bool(item_now.singleplayer.auto_heal),
Typed::Bool(item_now.multiplayer.auto_heal),
],
}),
remaining_ticks: Typed::Long(remaining_ticks),
}
}
}
pub struct GameEventTransmissible {
pub maps: Typed,
pub visibilities: Typed,
pub modes: Typed,
pub auto_heals: Typed,
pub remaining_ticks: Typed,
}
#[derive(Clone, Debug)]
pub enum GameRotationStrategy {
Sequence,
Random,
}
impl GameRotationStrategy {
pub(super) fn next(&self, last: usize, count: usize) -> usize {
match self {
Self::Sequence => (last+1) % count,
Self::Random => {
use rand::prelude::*;
let mut rng = rand::rng();
let num: u64 = rng.random();
let num = num.clamp(0, usize::MAX as u64) as usize;
num % count
}
}
}
}
#[derive(Clone, Debug)]
pub struct GameEvents {
pub singleplayer: GameEvent,
pub multiplayer: GameEvent,
pub duration: std::time::Duration,
}
#[derive(Clone, Debug)]
pub struct GameEvent {
pub map: GameMap,
pub visibility: GameVisibility,
pub mode: GameType,
pub auto_heal: bool,
}
#[derive(Clone, Debug, Copy)]
pub enum GameMap {
Mars1,
Mars2,
Mars3,
Neptune1,
Neptune2,
Neptune3,
Earth1,
Earth2,
}
#[derive(Clone, Debug, Copy)]
pub enum GameVisibility {
Good,
Poor,
Bad,
}
#[derive(Clone, Debug, Copy)]
pub enum GameType {
BattleArena,
SuddenDeath,
Pit,
TestMode,
SinglePlayer,
TeamDeathmatch,
Campaign,
}
#[derive(Clone, Debug)]
pub struct SingleplayerConfig {
pub max_teammates: u32,
pub max_enemies: u32,
pub vehicles: Vec<VehicleInfo>,
}
#[derive(Clone, Debug)]
pub struct VehicleInfo {
pub name: Option<String>,
pub username: String,
pub id: VehicleDescriptor,
}
#[derive(Clone, Debug)]
pub enum VehicleDescriptor {
Factory {
factory: u32,
},
Database {
garage: i32,
},
Raw {
cube_data: Vec<u8>,
colour_data: Vec<u8>,
}
// TODO File
}