rust/hedgewars-network-protocol/src/types.rs
author unC0Rr
Wed, 18 Sep 2024 14:10:51 +0200
branchtransitional_engine
changeset 16036 7b8d96fc8799
parent 15830 ea459da15b30
permissions -rw-r--r--
Remove old code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     1
use serde_derive::{Deserialize, Serialize};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     2
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     3
pub const MAX_HEDGEHOGS_PER_TEAM: u8 = 8;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     4
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     5
#[derive(PartialEq, Eq, Clone, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     6
pub enum ServerVar {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     7
    MOTDNew(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     8
    MOTDOld(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     9
    LatestProto(u16),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    10
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    11
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    12
#[derive(PartialEq, Eq, Clone, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    13
pub enum GameCfg {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    14
    FeatureSize(u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    15
    MapType(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    16
    MapGenerator(u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    17
    MazeSize(u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    18
    Seed(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    19
    Template(u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    20
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    21
    Ammo(String, Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    22
    Scheme(String, Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    23
    Script(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    24
    Theme(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    25
    DrawnMap(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    26
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    27
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    28
#[derive(PartialEq, Eq, Clone, Debug, Default)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    29
pub struct TeamInfo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    30
    pub owner: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    31
    pub name: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    32
    pub color: u8,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    33
    pub grave: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    34
    pub fort: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    35
    pub voice_pack: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    36
    pub flag: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    37
    pub difficulty: u8,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    38
    pub hedgehogs_number: u8,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    39
    pub hedgehogs: [HedgehogInfo; MAX_HEDGEHOGS_PER_TEAM as usize],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    40
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    41
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    42
#[derive(PartialEq, Eq, Clone, Debug, Default)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    43
pub struct HedgehogInfo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    44
    pub name: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    45
    pub hat: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    46
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    47
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    48
#[derive(Clone, Serialize, Deserialize, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    49
pub struct Ammo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    50
    pub name: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    51
    pub settings: Option<String>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    52
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    53
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    54
#[derive(Clone, Serialize, Deserialize, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    55
pub struct Scheme {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    56
    pub name: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    57
    pub settings: Vec<String>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    58
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    59
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    60
#[derive(Clone, Serialize, Deserialize, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    61
pub struct RoomConfig {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    62
    pub feature_size: u32,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    63
    pub map_type: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    64
    pub map_generator: u32,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    65
    pub maze_size: u32,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    66
    pub seed: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    67
    pub template: u32,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    68
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    69
    pub ammo: Ammo,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    70
    pub scheme: Scheme,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    71
    pub script: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    72
    pub theme: String,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    73
    pub drawn_map: Option<String>,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    74
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    75
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    76
impl RoomConfig {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    77
    pub fn new() -> RoomConfig {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    78
        RoomConfig {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    79
            feature_size: 12,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    80
            map_type: "+rnd+".to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    81
            map_generator: 0,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    82
            maze_size: 0,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    83
            seed: "seed".to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    84
            template: 0,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    85
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    86
            ammo: Ammo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    87
                name: "Default".to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    88
                settings: None,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    89
            },
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    90
            scheme: Scheme {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    91
                name: "Default".to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    92
                settings: Vec::new(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    93
            },
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    94
            script: "Normal".to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    95
            theme: "\u{1f994}".to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    96
            drawn_map: None,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    97
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    98
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    99
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   100
    pub fn set_config(&mut self, cfg: GameCfg) {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   101
        match cfg {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   102
            GameCfg::FeatureSize(s) => self.feature_size = s,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   103
            GameCfg::MapType(t) => self.map_type = t,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   104
            GameCfg::MapGenerator(g) => self.map_generator = g,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   105
            GameCfg::MazeSize(s) => self.maze_size = s,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   106
            GameCfg::Seed(s) => self.seed = s,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   107
            GameCfg::Template(t) => self.template = t,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   108
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   109
            GameCfg::Ammo(n, s) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   110
                self.ammo = Ammo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   111
                    name: n,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   112
                    settings: s,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   113
                }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   114
            }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   115
            GameCfg::Scheme(n, s) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   116
                self.scheme = Scheme {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   117
                    name: n,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   118
                    settings: s,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   119
                }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   120
            }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   121
            GameCfg::Script(s) => self.script = s,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   122
            GameCfg::Theme(t) => self.theme = t,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   123
            GameCfg::DrawnMap(m) => self.drawn_map = Some(m),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   124
        };
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   125
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   126
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   127
    pub fn to_map_config(&self) -> Vec<String> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   128
        vec![
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   129
            self.feature_size.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   130
            self.map_type.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   131
            self.map_generator.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   132
            self.maze_size.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   133
            self.seed.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   134
            self.template.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   135
        ]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   136
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   137
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   138
    pub fn to_game_config(&self) -> Vec<GameCfg> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   139
        use GameCfg::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   140
        let mut v = vec![
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   141
            Ammo(self.ammo.name.to_string(), self.ammo.settings.clone()),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   142
            Scheme(self.scheme.name.to_string(), self.scheme.settings.clone()),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   143
            Script(self.script.to_string()),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   144
            Theme(self.theme.to_string()),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   145
        ];
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   146
        if let Some(ref m) = self.drawn_map {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   147
            v.push(DrawnMap(m.to_string()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   148
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   149
        v
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   150
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   151
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   152
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   153
#[derive(PartialEq, Eq, Clone, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   154
pub enum VoteType {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   155
    Kick(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   156
    Map(Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   157
    Pause,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   158
    NewSeed,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   159
    HedgehogsPerTeam(u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   160
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   161
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   162
pub struct Vote {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   163
    pub is_pro: bool,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   164
    pub is_forced: bool,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   165
}