rust/hedgewars-server/src/server/demo.rs
author alfadur
Wed, 20 May 2020 22:50:58 +0300
changeset 15578 0b6094660557
parent 15576 3be9c98ae190
child 15580 98482c4ccf4b
permissions -rw-r--r--
convert config from haskell lists
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
     1
use crate::{
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
     2
    core::types::{GameCfg, HedgehogInfo, TeamInfo},
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
     3
    server::haskell::HaskellValue,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
     4
};
15547
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
     5
use std::{
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
     6
    fs,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
     7
    io::{self, BufReader, Read, Write},
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
     8
    str::FromStr,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
     9
};
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    10
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    11
struct Demo {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    12
    teams: Vec<TeamInfo>,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    13
    config: Vec<GameCfg>,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    14
    messages: Vec<String>,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    15
}
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    16
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    17
impl Demo {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    18
    fn save(&self, file: String) -> io::Result<()> {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    19
        Ok(unimplemented!())
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    20
    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    21
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    22
    fn load(filename: String) -> io::Result<Self> {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    23
        let mut file = fs::File::open(filename)?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    24
        let mut bytes = vec![];
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    25
        file.read_to_end(&mut bytes)?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    26
        match super::haskell::parse(&bytes[..]) {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    27
            Ok((_, value)) => haskell_to_demo(value).ok_or(io::Error::new(
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    28
                io::ErrorKind::InvalidData,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    29
                "Invalid demo structure",
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    30
            )),
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    31
            Err(_) => Err(io::Error::new(
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    32
                io::ErrorKind::InvalidData,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    33
                "Unable to parse file",
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    34
            )),
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    35
        }
15547
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    36
    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    37
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    38
    fn load_hwd(filename: String) -> io::Result<Self> {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    39
        let file = fs::File::open(filename)?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
    40
        let mut reader = io::BufReader::new(file);
15547
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    41
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    42
        #[inline]
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    43
        fn error<T>(cause: &str) -> io::Result<T> {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    44
            Err(io::Error::new(io::ErrorKind::InvalidData, cause))
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    45
        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    46
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    47
        fn read_command<'a>(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    48
            reader: &mut BufReader<fs::File>,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    49
            buffer: &'a mut [u8],
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    50
        ) -> io::Result<Option<&'a str>> {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    51
            use io::BufRead;
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    52
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    53
            let mut size = [0u8; 1];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    54
            if reader.read(&mut size)? == 0 {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    55
                Ok(None)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    56
            } else {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    57
                let text = &mut buffer[0..size[0] as _];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    58
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    59
                if reader.read(text)? < text.len() {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    60
                    Err(io::Error::new(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    61
                        io::ErrorKind::UnexpectedEof,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    62
                        "Incomplete command",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    63
                    ))
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    64
                } else {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    65
                    std::str::from_utf8(text).map(Some).map_err(|e| {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    66
                        io::Error::new(io::ErrorKind::InvalidInput, "The string is not UTF8")
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    67
                    })
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    68
                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    69
            }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    70
        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    71
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    72
        fn get_script_name(arg: &str) -> io::Result<String> {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    73
            const PREFIX: &str = "Scripts/Multiplayer/";
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    74
            const SUFFIX: &str = ".lua";
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    75
            if arg.starts_with(PREFIX) && arg.ends_with(SUFFIX) {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    76
                let script = arg[PREFIX.len()..arg.len() - SUFFIX.len()].to_string();
15570
d524b7450576 add haskell literal parser
alfadur
parents: 15547
diff changeset
    77
                Ok(script.replace('_', " "))
15547
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    78
            } else {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    79
                error("Script is not multiplayer")
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    80
            }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    81
        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    82
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    83
        fn get_game_flags(arg: &str) -> io::Result<Vec<String>> {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    84
            const FLAGS: &[u32] = &[
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    85
                0x0000_1000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    86
                0x0000_0010,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    87
                0x0000_0004,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    88
                0x0000_0008,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    89
                0x0000_0020,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    90
                0x0000_0040,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    91
                0x0000_0080,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    92
                0x0000_0100,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    93
                0x0000_0200,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    94
                0x0000_0400,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    95
                0x0000_0800,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    96
                0x0000_2000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    97
                0x0000_4000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    98
                0x0000_8000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
    99
                0x0001_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   100
                0x0002_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   101
                0x0004_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   102
                0x0008_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   103
                0x0010_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   104
                0x0020_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   105
                0x0040_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   106
                0x0080_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   107
                0x0100_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   108
                0x0200_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   109
                0x0400_0000,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   110
            ];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   111
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   112
            let flags = u32::from_str(arg).unwrap_or_default();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   113
            let game_flags = FLAGS
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   114
                .iter()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   115
                .map(|flag| (flag & flags != 0).to_string())
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   116
                .collect();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   117
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   118
            Ok(game_flags)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   119
        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   120
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   121
        let mut config = Vec::new();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   122
        let mut buffer = [0u8; u8::max_value() as _];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   123
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   124
        let mut game_flags = vec![];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   125
        let mut scheme_properties: Vec<_> = [
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   126
            "1", "1000", "100", "1", "1", "1000", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   127
            "1", "",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   128
        ]
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   129
        .iter()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   130
        .map(|p| p.to_string())
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   131
        .collect();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   132
        const SCHEME_PROPERTY_NAMES: &[&str] = &[
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   133
            "$damagepct",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   134
            "$turntime",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   135
            "",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   136
            "$sd_turns",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   137
            "$casefreq",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   138
            "$minestime",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   139
            "$minesnum",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   140
            "$minedudpct",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   141
            "$explosives",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   142
            "$airmines",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   143
            "$healthprob",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   144
            "$hcaseamount",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   145
            "$waterrise",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   146
            "$healthdec",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   147
            "$ropepct",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   148
            "$getawaytime",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   149
            "$worldedge",
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   150
        ];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   151
        const AMMO_PROPERTY_NAMES: &[&str] = &["eammloadt", "eammprob", "eammdelay", "eammreinf"];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   152
        let mut ammo_settings = vec![String::new(); AMMO_PROPERTY_NAMES.len()];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   153
        let mut teams = vec![];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   154
        let mut hog_index = 7usize;
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   155
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   156
        let mut messages = vec![];
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   157
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   158
        while let Some(cmd) = read_command(&mut reader, &mut buffer)? {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   159
            if let Some(index) = cmd.find(' ') {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   160
                match cmd.chars().next().unwrap_or_default() {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   161
                    'T' => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   162
                        if cmd != "TD" {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   163
                            let () = error("Not a demo file")?;
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   164
                        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   165
                    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   166
                    'e' => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   167
                        if let Some(index) = cmd.find(' ') {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   168
                            let (name, arg) = cmd.split_at(index);
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   169
                            match name {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   170
                                "script" => config.push(GameCfg::Script(get_script_name(arg)?)),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   171
                                "map" => config.push(GameCfg::MapType(arg.to_string())),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   172
                                "theme" => config.push(GameCfg::Theme(arg.to_string())),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   173
                                "seed" => config.push(GameCfg::Seed(arg.to_string())),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   174
                                "$gmflags" => game_flags = get_game_flags(arg)?,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   175
                                "$scriptparam" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   176
                                    *scheme_properties.last_mut().unwrap() = arg.to_string()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   177
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   178
                                "$template_filter" => config.push(GameCfg::Template(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   179
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   180
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   181
                                "$feature_size" => config.push(GameCfg::FeatureSize(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   182
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   183
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   184
                                "$map_gen" => config.push(GameCfg::MapGenerator(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   185
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   186
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   187
                                "$maze_size" => config.push(GameCfg::MazeSize(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   188
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   189
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   190
                                "addteam" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   191
                                    if let parts = arg.splitn(3, ' ').collect::<Vec<_>>() {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   192
                                        let color = parts.get(1).unwrap_or(&"1");
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   193
                                        let name = parts.get(2).unwrap_or(&"Unnamed");
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   194
                                        teams.push(TeamInfo {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   195
                                            color: (u32::from_str(color).unwrap_or(2113696)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   196
                                                / 2113696
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   197
                                                - 1)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   198
                                                as u8,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   199
                                            name: name.to_string(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   200
                                            ..TeamInfo::default()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   201
                                        })
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   202
                                    };
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   203
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   204
                                "fort" => teams
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   205
                                    .last_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   206
                                    .iter_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   207
                                    .for_each(|t| t.fort = arg.to_string()),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   208
                                "grave" => teams
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   209
                                    .last_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   210
                                    .iter_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   211
                                    .for_each(|t| t.grave = arg.to_string()),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   212
                                "addhh" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   213
                                    hog_index = (hog_index + 1) % 8;
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   214
                                    if let parts = arg.splitn(3, ' ').collect::<Vec<_>>() {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   215
                                        let health = parts.get(1).unwrap_or(&"100");
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   216
                                        teams.last_mut().iter_mut().for_each(|t| {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   217
                                            if let Some(difficulty) = parts.get(0) {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   218
                                                t.difficulty =
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   219
                                                    u8::from_str(difficulty).unwrap_or(0);
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   220
                                            }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   221
                                            if let Some(init_health) = parts.get(1) {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   222
                                                scheme_properties[2] = init_health.to_string();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   223
                                            }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   224
                                            t.hedgehogs_number = (hog_index + 1) as u8;
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   225
                                            t.hedgehogs[hog_index].name =
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   226
                                                parts.get(2).unwrap_or(&"Unnamed").to_string()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   227
                                        });
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   228
                                    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   229
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   230
                                "hat" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   231
                                    teams
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   232
                                        .last_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   233
                                        .iter_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   234
                                        .for_each(|t| t.hedgehogs[hog_index].hat = arg.to_string());
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   235
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   236
                                name => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   237
                                    if let Some(index) =
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   238
                                        SCHEME_PROPERTY_NAMES.iter().position(|n| *n == name)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   239
                                    {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   240
                                        scheme_properties[index] = arg.to_string();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   241
                                    } else if let Some(index) =
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   242
                                        AMMO_PROPERTY_NAMES.iter().position(|n| *n == name)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   243
                                    {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   244
                                        ammo_settings[index] = arg.to_string();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   245
                                    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   246
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   247
                            }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   248
                        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   249
                    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   250
                    '+' => {}
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   251
                    _ => (),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   252
                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   253
            }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   254
        }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   255
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   256
        game_flags.append(&mut scheme_properties);
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   257
        config.push(GameCfg::Scheme("ADHOG_SCHEME".to_string(), game_flags));
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   258
        config.push(GameCfg::Ammo(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   259
            "ADHOG_AMMO".to_string(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   260
            Some(ammo_settings.concat()),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   261
        ));
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   262
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   263
        Ok(Demo {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   264
            teams,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   265
            config,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   266
            messages,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   267
        })
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   268
    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   269
}
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   270
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   271
fn haskell_to_demo(value: HaskellValue) -> Option<Demo> {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   272
    use HaskellValue::*;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   273
    let mut lists = value.into_tuple()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   274
    let mut lists_iter = lists.drain(..);
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   275
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   276
    let teams_list = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   277
    let map_config = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   278
    let game_config = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   279
    let engine_messages = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   280
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   281
    let mut teams = Vec::with_capacity(teams_list.len());
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   282
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   283
    for team in teams_list {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   284
        let (_, mut fields) = team.into_struct()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   285
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   286
        let mut team_info = TeamInfo::default();
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   287
        for (name, value) in fields.drain() {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   288
            match &name[..] {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   289
                "teamowner" => team_info.owner = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   290
                "teamname" => team_info.name = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   291
                "teamcolor" => team_info.color = u8::from_str(&value.into_string()?).ok()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   292
                "teamgrave" => team_info.grave = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   293
                "teamfort" => team_info.fort = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   294
                "teamvoicepack" => team_info.voice_pack = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   295
                "teamflag" => team_info.flag = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   296
                "difficulty" => team_info.difficulty = value.into_number()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   297
                "hhnum" => team_info.hedgehogs_number = value.into_number()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   298
                "hedgehogs" => {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   299
                    for (index, hog) in value
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   300
                        .into_list()?
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   301
                        .drain(..)
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   302
                        .enumerate()
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   303
                        .take(team_info.hedgehogs.len())
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   304
                    {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   305
                        let (_, mut fields) = hog.into_anon_struct()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   306
                        let mut fields_iter = fields.drain(..);
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   307
                        team_info.hedgehogs[index] = HedgehogInfo {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   308
                            name: fields_iter.next()?.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   309
                            hat: fields_iter.next()?.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   310
                        }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   311
                    }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   312
                }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   313
                _ => (),
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   314
            }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   315
        }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   316
        teams.push(team_info)
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   317
    }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   318
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   319
    let mut config = Vec::with_capacity(map_config.len() + game_config.len());
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   320
15578
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   321
    for item in map_config {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   322
        let mut tuple = item.into_tuple()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   323
        let mut tuple_iter = tuple.drain(..);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   324
        let name = tuple_iter.next()?.into_string()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   325
        let value = tuple_iter.next()?.into_string()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   326
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   327
        let config_item = match &name[..] {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   328
            "FEATURE_SIZE" => GameCfg::FeatureSize(u32::from_str(&value).ok()?),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   329
            "MAP" => GameCfg::MapType(value),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   330
            "MAPGEN" => GameCfg::MapGenerator(u32::from_str(&value).ok()?),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   331
            "MAZE_SIZE" => GameCfg::MazeSize(u32::from_str(&value).ok()?),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   332
            "SEED" => GameCfg::Seed(value),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   333
            "TEMPLATE" => GameCfg::Template(u32::from_str(&value).ok()?),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   334
            "DRAWNMAP" => GameCfg::DrawnMap(value),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   335
            _ => None?,
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   336
        };
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   337
        config.push(config_item);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   338
    }
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   339
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   340
    for item in game_config {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   341
        let mut tuple = item.into_tuple()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   342
        let mut tuple_iter = tuple.drain(..);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   343
        let name = tuple_iter.next()?.into_string()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   344
        let value = tuple_iter.next()?;
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   345
15578
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   346
        let config_item = match &name[..] {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   347
            "AMMO" => {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   348
                let mut ammo = value.into_list()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   349
                let mut ammo_iter = ammo.drain(..);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   350
                GameCfg::Ammo(
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   351
                    ammo_iter.next()?.into_string()?,
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   352
                    ammo_iter.next().and_then(|v| v.into_string()),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   353
                )
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   354
            }
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   355
            "SCHEME" => {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   356
                let mut scheme = value.into_list()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   357
                let mut scheme_iter = scheme.drain(..);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   358
                GameCfg::Scheme(
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   359
                    scheme_iter.next()?.into_string()?,
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   360
                    scheme_iter
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   361
                        .next()?
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   362
                        .into_list()?
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   363
                        .drain(..)
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   364
                        .filter_map(|v| v.into_string())
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   365
                        .collect(),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   366
                )
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   367
            }
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   368
            "SCRIPT" => GameCfg::Script(value.into_string()?),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   369
            "THEME" => GameCfg::Theme(value.into_string()?),
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   370
            _ => None?,
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   371
        };
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   372
        config.push(config_item);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   373
    }
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   374
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   375
    let mut messages = Vec::with_capacity(engine_messages.len());
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   376
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   377
    for message in engine_messages {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   378
        messages.push(message.into_string()?);
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   379
    }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   380
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   381
    Some(Demo {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   382
        teams,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   383
        config,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   384
        messages,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   385
    })
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   386
}