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