From 215c88cb49efd7b08b6babcc8f86256ce173e5bd Mon Sep 17 00:00:00 2001 From: Sofia Date: Wed, 29 Jul 2026 20:16:48 +0300 Subject: [PATCH] Move GameOptions to game/opts.rs --- rust/src/game/mod.rs | 86 +---------------------------- rust/src/game/opts.rs | 86 +++++++++++++++++++++++++++++ rust/src/net/network_manager.rs | 5 +- rust/src/net/protocol.rs | 2 +- rust/src/player/local_player.rs | 2 +- rust/src/player/mod.rs | 2 +- rust/src/player/remote_player.rs | 2 +- rust/src/ui/lobby.rs | 5 +- rust/src/ui/lobby_player_listing.rs | 5 +- 9 files changed, 105 insertions(+), 90 deletions(-) create mode 100644 rust/src/game/opts.rs diff --git a/rust/src/game/mod.rs b/rust/src/game/mod.rs index 0f0e1af..8271adf 100644 --- a/rust/src/game/mod.rs +++ b/rust/src/game/mod.rs @@ -10,6 +10,7 @@ use crate::{ }, bgm::{Music, MusicManager}, chat::{Chat, ChatMessage}, + game::opts::{GameOption, GameOptionValue, GameOptions}, map::Map, map_resource::MapResource, net::{ @@ -32,6 +33,8 @@ use crate::{ ui::cli::{CliColor, Command, CommandLinePanel}, }; +pub mod opts; + pub const GAME_MANAGER_GLOBAL: &str = "GameManagerGlobal"; #[derive(GodotClass)] @@ -152,89 +155,6 @@ impl GameManager { } } -#[derive(Debug)] -pub struct GameOptions { - pub values: HashMap, -} - -impl Default for GameOptions { - fn default() -> Self { - let mut opts = HashMap::new(); - opts.insert(GameOption::AllowAnyMap, GameOptionValue::Boolean(false)); - opts.insert(GameOption::AllowAnyTeam, GameOptionValue::Boolean(false)); - opts.insert(GameOption::FriendlyFire, GameOptionValue::Boolean(true)); - opts.insert(GameOption::GoalsNeededToWin, GameOptionValue::Number(5.)); - opts.insert(GameOption::RespawnTimer, GameOptionValue::Number(5.)); - opts.insert( - GameOption::TelegrenadesAllowed, - GameOptionValue::Boolean(true), - ); - Self { values: opts } - } -} - -impl GameOptions { - pub fn is_true(&self, opt: GameOption) -> bool { - let opt = self - .values - .get(&opt) - .copied() - .unwrap_or(GameOptionValue::Boolean(false)); - match opt { - GameOptionValue::Boolean(value) => value, - _ => false, - } - } - - pub fn get_float(&self, opt: GameOption) -> f64 { - let opt = self - .values - .get(&opt) - .copied() - .unwrap_or(GameOptionValue::Number(0.)); - match opt { - GameOptionValue::Number(value) => value, - _ => 0., - } - } -} - -#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Copy, Clone, Serialize, Deserialize)] -pub enum GameOption { - /// Allow anyone to select a map, not just the host - AllowAnyMap, - /// Allow anyone to select anyone's team, not just themselves and the host - AllowAnyTeam, - /// Allow anyone to shoot anyone, not just other team's members - FriendlyFire, - /// The number of goals needed to win a match - GoalsNeededToWin, - /// Whether telegrenades are allowed or not. - TelegrenadesAllowed, - /// How long in seconds does it take for a player to respawn - RespawnTimer, -} - -impl ToString for GameOption { - fn to_string(&self) -> String { - match self { - GameOption::AllowAnyMap => "Allow anyone to select the map", - GameOption::AllowAnyTeam => "Allow anyone to select any teams", - GameOption::FriendlyFire => "Friendly fire", - GameOption::GoalsNeededToWin => "Goals needed to win", - GameOption::TelegrenadesAllowed => "Telegrenades allowed", - GameOption::RespawnTimer => "Respawn Time (seconds)", - } - .to_owned() - } -} - -#[derive(Debug, Copy, Clone, Serialize, Deserialize)] -pub enum GameOptionValue { - Boolean(bool), - Number(f64), -} - pub struct RespawnBallCommand; impl Command for RespawnBallCommand { fn execute(&self, _: &mut CommandLinePanel, _: &[&str]) { diff --git a/rust/src/game/opts.rs b/rust/src/game/opts.rs new file mode 100644 index 0000000..4375067 --- /dev/null +++ b/rust/src/game/opts.rs @@ -0,0 +1,86 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug)] +pub struct GameOptions { + pub values: HashMap, +} + +impl Default for GameOptions { + fn default() -> Self { + let mut opts = HashMap::new(); + opts.insert(GameOption::AllowAnyMap, GameOptionValue::Boolean(false)); + opts.insert(GameOption::AllowAnyTeam, GameOptionValue::Boolean(false)); + opts.insert(GameOption::FriendlyFire, GameOptionValue::Boolean(true)); + opts.insert(GameOption::GoalsNeededToWin, GameOptionValue::Number(5.)); + opts.insert(GameOption::RespawnTimer, GameOptionValue::Number(5.)); + opts.insert( + GameOption::TelegrenadesAllowed, + GameOptionValue::Boolean(true), + ); + Self { values: opts } + } +} + +impl GameOptions { + pub fn is_true(&self, opt: GameOption) -> bool { + let opt = self + .values + .get(&opt) + .copied() + .unwrap_or(GameOptionValue::Boolean(false)); + match opt { + GameOptionValue::Boolean(value) => value, + _ => false, + } + } + + pub fn get_float(&self, opt: GameOption) -> f64 { + let opt = self + .values + .get(&opt) + .copied() + .unwrap_or(GameOptionValue::Number(0.)); + match opt { + GameOptionValue::Number(value) => value, + _ => 0., + } + } +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Copy, Clone, Serialize, Deserialize)] +pub enum GameOption { + /// Allow anyone to select a map, not just the host + AllowAnyMap, + /// Allow anyone to select anyone's team, not just themselves and the host + AllowAnyTeam, + /// Allow anyone to shoot anyone, not just other team's members + FriendlyFire, + /// The number of goals needed to win a match + GoalsNeededToWin, + /// Whether telegrenades are allowed or not. + TelegrenadesAllowed, + /// How long in seconds does it take for a player to respawn + RespawnTimer, +} + +impl ToString for GameOption { + fn to_string(&self) -> String { + match self { + GameOption::AllowAnyMap => "Allow anyone to select the map", + GameOption::AllowAnyTeam => "Allow anyone to select any teams", + GameOption::FriendlyFire => "Friendly fire", + GameOption::GoalsNeededToWin => "Goals needed to win", + GameOption::TelegrenadesAllowed => "Telegrenades allowed", + GameOption::RespawnTimer => "Respawn Time (seconds)", + } + .to_owned() + } +} + +#[derive(Debug, Copy, Clone, Serialize, Deserialize)] +pub enum GameOptionValue { + Boolean(bool), + Number(f64), +} diff --git a/rust/src/net/network_manager.rs b/rust/src/net/network_manager.rs index 6450793..97d3590 100644 --- a/rust/src/net/network_manager.rs +++ b/rust/src/net/network_manager.rs @@ -10,7 +10,10 @@ use teanet::{Peer, PeerConfig, PeerMessage}; use crate::{ announcer::Announcement, - game::{Game, GameManager, GameOption, GameOptionValue, NetPlayer}, + game::{ + Game, GameManager, NetPlayer, + opts::{GameOption, GameOptionValue}, + }, game_settings::GameSettingsManager, net::{net_stats::Stats, util::NetVector3}, player::{DamageSource, NetTransform, weapon::WeaponType}, diff --git a/rust/src/net/protocol.rs b/rust/src/net/protocol.rs index 7ce61d9..bc59511 100644 --- a/rust/src/net/protocol.rs +++ b/rust/src/net/protocol.rs @@ -4,7 +4,7 @@ use godot::obj::Singleton; use teanet::PeerMessage; use crate::{ - game::{Game, GameManager, GameOption}, + game::{Game, GameManager, opts::GameOption}, game_settings::GameSettingsManager, net::network_manager::{NetworkManager, Package, PeerKind}, popup_queue::{Popup, PopupQueue}, diff --git a/rust/src/player/local_player.rs b/rust/src/player/local_player.rs index 576998f..81c79c2 100644 --- a/rust/src/player/local_player.rs +++ b/rust/src/player/local_player.rs @@ -10,7 +10,7 @@ use godot::{ }; use crate::{ - game::{Game, GameManager, GameOption}, + game::{Game, GameManager, opts::GameOption}, game_settings::{GameSettings, GameSettingsManager}, killbox::KillboxKind, net::{ diff --git a/rust/src/player/mod.rs b/rust/src/player/mod.rs index e96e0cf..bd251b7 100644 --- a/rust/src/player/mod.rs +++ b/rust/src/player/mod.rs @@ -8,7 +8,7 @@ use godot::{ use serde::{Deserialize, Serialize}; use crate::{ - game::{Game, GameOption}, + game::{Game, opts::GameOption}, killbox::KillboxKind, net::{ network_manager::NetworkManager, diff --git a/rust/src/player/remote_player.rs b/rust/src/player/remote_player.rs index 4339a59..cdcbf3c 100644 --- a/rust/src/player/remote_player.rs +++ b/rust/src/player/remote_player.rs @@ -8,7 +8,7 @@ use godot::{ }; use crate::{ - game::{Game, GameOption}, + game::{Game, opts::GameOption}, killbox::KillboxKind, pickup::PickupKind, player::{ diff --git a/rust/src/ui/lobby.rs b/rust/src/ui/lobby.rs index 09ff120..168c319 100644 --- a/rust/src/ui/lobby.rs +++ b/rust/src/ui/lobby.rs @@ -10,7 +10,10 @@ use godot::{ use crate::{ bgm::{Music, MusicManager}, - game::{Game, GameManager, GameOption, GameOptionValue, GameOptions}, + game::{ + Game, GameManager, + opts::{GameOption, GameOptionValue, GameOptions}, + }, net::network_manager::{NetworkManager, Package, PeerKind}, ui::lobby_player_listing::LobbyPlayerListing, }; diff --git a/rust/src/ui/lobby_player_listing.rs b/rust/src/ui/lobby_player_listing.rs index 18cba6e..e54694e 100644 --- a/rust/src/ui/lobby_player_listing.rs +++ b/rust/src/ui/lobby_player_listing.rs @@ -4,7 +4,10 @@ use godot::{ }; use crate::{ - game::{Game, GameOption, GameOptions}, + game::{ + Game, + opts::{GameOption, GameOptions}, + }, net::network_manager::NetworkManager, team_resource::TeamResource, };