rust/hedgewars-server/src/server/coretypes.rs
changeset 15074 c5a6e8566425
parent 15073 7732013ce64c
child 15075 e935b1ad23f3
equal deleted inserted replaced
15073:7732013ce64c 15074:c5a6e8566425
     1 use serde_derive::{Deserialize, Serialize};
       
     2 
       
     3 pub type ClientId = usize;
       
     4 pub type RoomId = usize;
       
     5 
       
     6 pub const MAX_HEDGEHOGS_PER_TEAM: u8 = 8;
       
     7 
       
     8 #[derive(PartialEq, Eq, Clone, Debug)]
       
     9 pub enum ServerVar {
       
    10     MOTDNew(String),
       
    11     MOTDOld(String),
       
    12     LatestProto(u16),
       
    13 }
       
    14 
       
    15 #[derive(PartialEq, Eq, Clone, Debug)]
       
    16 pub enum GameCfg {
       
    17     FeatureSize(u32),
       
    18     MapType(String),
       
    19     MapGenerator(u32),
       
    20     MazeSize(u32),
       
    21     Seed(String),
       
    22     Template(u32),
       
    23 
       
    24     Ammo(String, Option<String>),
       
    25     Scheme(String, Vec<String>),
       
    26     Script(String),
       
    27     Theme(String),
       
    28     DrawnMap(String),
       
    29 }
       
    30 
       
    31 #[derive(PartialEq, Eq, Clone, Debug)]
       
    32 pub struct TeamInfo {
       
    33     pub owner: String,
       
    34     pub name: String,
       
    35     pub color: u8,
       
    36     pub grave: String,
       
    37     pub fort: String,
       
    38     pub voice_pack: String,
       
    39     pub flag: String,
       
    40     pub difficulty: u8,
       
    41     pub hedgehogs_number: u8,
       
    42     pub hedgehogs: [HedgehogInfo; MAX_HEDGEHOGS_PER_TEAM as usize],
       
    43 }
       
    44 
       
    45 #[derive(PartialEq, Eq, Clone, Debug)]
       
    46 pub struct HedgehogInfo {
       
    47     pub name: String,
       
    48     pub hat: String,
       
    49 }
       
    50 
       
    51 #[derive(Clone, Serialize, Deserialize)]
       
    52 pub struct Ammo {
       
    53     pub name: String,
       
    54     pub settings: Option<String>,
       
    55 }
       
    56 
       
    57 #[derive(Clone, Serialize, Deserialize)]
       
    58 pub struct Scheme {
       
    59     pub name: String,
       
    60     pub settings: Vec<String>,
       
    61 }
       
    62 
       
    63 #[derive(Clone, Serialize, Deserialize)]
       
    64 pub struct RoomConfig {
       
    65     pub feature_size: u32,
       
    66     pub map_type: String,
       
    67     pub map_generator: u32,
       
    68     pub maze_size: u32,
       
    69     pub seed: String,
       
    70     pub template: u32,
       
    71 
       
    72     pub ammo: Ammo,
       
    73     pub scheme: Scheme,
       
    74     pub script: String,
       
    75     pub theme: String,
       
    76     pub drawn_map: Option<String>,
       
    77 }
       
    78 
       
    79 impl RoomConfig {
       
    80     pub fn new() -> RoomConfig {
       
    81         RoomConfig {
       
    82             feature_size: 12,
       
    83             map_type: "+rnd+".to_string(),
       
    84             map_generator: 0,
       
    85             maze_size: 0,
       
    86             seed: "seed".to_string(),
       
    87             template: 0,
       
    88 
       
    89             ammo: Ammo {
       
    90                 name: "Default".to_string(),
       
    91                 settings: None,
       
    92             },
       
    93             scheme: Scheme {
       
    94                 name: "Default".to_string(),
       
    95                 settings: Vec::new(),
       
    96             },
       
    97             script: "Normal".to_string(),
       
    98             theme: "\u{1f994}".to_string(),
       
    99             drawn_map: None,
       
   100         }
       
   101     }
       
   102 
       
   103     pub fn set_config(&mut self, cfg: GameCfg) {
       
   104         match cfg {
       
   105             GameCfg::FeatureSize(s) => self.feature_size = s,
       
   106             GameCfg::MapType(t) => self.map_type = t,
       
   107             GameCfg::MapGenerator(g) => self.map_generator = g,
       
   108             GameCfg::MazeSize(s) => self.maze_size = s,
       
   109             GameCfg::Seed(s) => self.seed = s,
       
   110             GameCfg::Template(t) => self.template = t,
       
   111 
       
   112             GameCfg::Ammo(n, s) => {
       
   113                 self.ammo = Ammo {
       
   114                     name: n,
       
   115                     settings: s,
       
   116                 }
       
   117             }
       
   118             GameCfg::Scheme(n, s) => {
       
   119                 self.scheme = Scheme {
       
   120                     name: n,
       
   121                     settings: s,
       
   122                 }
       
   123             }
       
   124             GameCfg::Script(s) => self.script = s,
       
   125             GameCfg::Theme(t) => self.theme = t,
       
   126             GameCfg::DrawnMap(m) => self.drawn_map = Some(m),
       
   127         };
       
   128     }
       
   129 
       
   130     pub fn to_map_config(&self) -> Vec<String> {
       
   131         vec![
       
   132             self.feature_size.to_string(),
       
   133             self.map_type.to_string(),
       
   134             self.map_generator.to_string(),
       
   135             self.maze_size.to_string(),
       
   136             self.seed.to_string(),
       
   137             self.template.to_string(),
       
   138         ]
       
   139     }
       
   140 
       
   141     pub fn to_game_config(&self) -> Vec<GameCfg> {
       
   142         use crate::server::coretypes::GameCfg::*;
       
   143         let mut v = vec![
       
   144             Ammo(self.ammo.name.to_string(), self.ammo.settings.clone()),
       
   145             Scheme(self.scheme.name.to_string(), self.scheme.settings.clone()),
       
   146             Script(self.script.to_string()),
       
   147             Theme(self.theme.to_string()),
       
   148         ];
       
   149         if let Some(ref m) = self.drawn_map {
       
   150             v.push(DrawnMap(m.to_string()))
       
   151         }
       
   152         v
       
   153     }
       
   154 }
       
   155 
       
   156 pub struct Replay {
       
   157     pub config: RoomConfig,
       
   158     pub teams: Vec<TeamInfo>,
       
   159     pub message_log: Vec<String>,
       
   160 }
       
   161 
       
   162 #[derive(PartialEq, Eq, Clone, Debug)]
       
   163 pub enum VoteType {
       
   164     Kick(String),
       
   165     Map(Option<String>),
       
   166     Pause,
       
   167     NewSeed,
       
   168     HedgehogsPerTeam(u8),
       
   169 }
       
   170 
       
   171 pub struct Vote {
       
   172     pub is_pro: bool,
       
   173     pub is_forced: bool,
       
   174 }
       
   175 
       
   176 #[derive(Clone, Debug)]
       
   177 pub struct Voting {
       
   178     pub ttl: u32,
       
   179     pub voters: Vec<ClientId>,
       
   180     pub votes: Vec<(ClientId, bool)>,
       
   181     pub kind: VoteType,
       
   182 }
       
   183 
       
   184 impl Voting {
       
   185     pub fn new(kind: VoteType, voters: Vec<ClientId>) -> Voting {
       
   186         Voting {
       
   187             kind,
       
   188             voters,
       
   189             ttl: 2,
       
   190             votes: Vec::new(),
       
   191         }
       
   192     }
       
   193 }