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