rust/hedgewars-server/src/server/demo.rs
author unC0Rr
Sat, 28 Sep 2024 22:27:13 +0200
changeset 16037 2b4f361e3891
parent 15989 fb389df02e3e
permissions -rw-r--r--
Wrap chat lines into div
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
15989
fb389df02e3e add some more todo!s
alfadur
parents: 15938
diff changeset
   138
        //todo!("read messages from file");
15880
3360cc4c162d Fix a couple issues
unC0Rr
parents: 15831
diff changeset
   139
        let messages = vec![];
15547
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
        while let Some(cmd) = read_command(&mut reader, &mut buffer)? {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   142
            if let Some(index) = cmd.find(' ') {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   143
                match cmd.chars().next().unwrap_or_default() {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   144
                    'T' => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   145
                        if cmd != "TD" {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   146
                            let () = error("Not a demo file")?;
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
                    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   149
                    'e' => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   150
                        if let Some(index) = cmd.find(' ') {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   151
                            let (name, arg) = cmd.split_at(index);
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   152
                            match name {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   153
                                "script" => config.push(GameCfg::Script(get_script_name(arg)?)),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   154
                                "map" => config.push(GameCfg::MapType(arg.to_string())),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   155
                                "theme" => config.push(GameCfg::Theme(arg.to_string())),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   156
                                "seed" => config.push(GameCfg::Seed(arg.to_string())),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   157
                                "$gmflags" => game_flags = get_game_flags(arg)?,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   158
                                "$scriptparam" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   159
                                    *scheme_properties.last_mut().unwrap() = arg.to_string()
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
                                "$template_filter" => config.push(GameCfg::Template(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   162
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   163
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   164
                                "$feature_size" => config.push(GameCfg::FeatureSize(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   165
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   166
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   167
                                "$map_gen" => config.push(GameCfg::MapGenerator(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   168
                                    u32::from_str(arg).unwrap_or_default(),
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
                                "$maze_size" => config.push(GameCfg::MazeSize(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   171
                                    u32::from_str(arg).unwrap_or_default(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   172
                                )),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   173
                                "addteam" => {
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   174
                                    let parts = arg.splitn(3, ' ').collect::<Vec<_>>();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   175
                                    let color = parts.get(1).unwrap_or(&"1");
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   176
                                    let name = parts.get(2).unwrap_or(&"Unnamed");
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   177
                                    teams.push(TeamInfo {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   178
                                        color: (u32::from_str(color).unwrap_or(2113696) / 2113696
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   179
                                            - 1)
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   180
                                            as u8,
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   181
                                        name: name.to_string(),
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   182
                                        ..TeamInfo::default()
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   183
                                    });
15547
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   184
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   185
                                "fort" => teams
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   186
                                    .last_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   187
                                    .iter_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   188
                                    .for_each(|t| t.fort = arg.to_string()),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   189
                                "grave" => teams
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   190
                                    .last_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   191
                                    .iter_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   192
                                    .for_each(|t| t.grave = arg.to_string()),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   193
                                "addhh" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   194
                                    hog_index = (hog_index + 1) % 8;
15831
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   195
                                    let parts = arg.splitn(3, ' ').collect::<Vec<_>>();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   196
                                    let health = parts.get(1).unwrap_or(&"100");
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   197
                                    teams.last_mut().iter_mut().for_each(|t| {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   198
                                        if let Some(difficulty) = parts.get(0) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   199
                                            t.difficulty = u8::from_str(difficulty).unwrap_or(0);
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   200
                                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   201
                                        if let Some(init_health) = parts.get(1) {
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   202
                                            scheme_properties[2] = init_health.to_string();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   203
                                        }
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   204
                                        t.hedgehogs_number = (hog_index + 1) as u8;
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   205
                                        t.hedgehogs[hog_index].name =
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   206
                                            parts.get(2).unwrap_or(&"Unnamed").to_string();
7d0f747afcb8 move server network to tokio
alfadur
parents: 15804
diff changeset
   207
                                    });
15547
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
                                "hat" => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   210
                                    teams
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   211
                                        .last_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   212
                                        .iter_mut()
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   213
                                        .for_each(|t| t.hedgehogs[hog_index].hat = arg.to_string());
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   214
                                }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   215
                                name => {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   216
                                    if let Some(index) =
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   217
                                        SCHEME_PROPERTY_NAMES.iter().position(|n| *n == name)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   218
                                    {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   219
                                        scheme_properties[index] = arg.to_string();
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   220
                                    } else if let Some(index) =
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   221
                                        AMMO_PROPERTY_NAMES.iter().position(|n| *n == name)
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   222
                                    {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   223
                                        ammo_settings[index] = arg.to_string();
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
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   235
        game_flags.append(&mut scheme_properties);
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   236
        config.push(GameCfg::Scheme("ADHOG_SCHEME".to_string(), game_flags));
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   237
        config.push(GameCfg::Ammo(
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   238
            "ADHOG_AMMO".to_string(),
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   239
            Some(ammo_settings.concat()),
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
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   242
        Ok(Demo {
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   243
            teams,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   244
            config,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   245
            messages,
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   246
        })
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   247
    }
863059f61793 add loading config from hwd to server
alfadur
parents:
diff changeset
   248
}
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   249
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   250
fn replay_to_haskell(mut replay: Replay) -> HaskellValue {
15581
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   251
    use HaskellValue as Hs;
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   252
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   253
    let mut teams = Vec::with_capacity(replay.teams.len());
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   254
    for team in replay.teams {
15581
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   255
        let mut fields = HashMap::<String, HaskellValue>::new();
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   256
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   257
        fields.insert("teamowner".to_string(), Hs::String(team.owner));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   258
        fields.insert("teamname".to_string(), Hs::String(team.name));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   259
        fields.insert("teamcolor".to_string(), Hs::Number(team.color));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   260
        fields.insert("teamgrave".to_string(), Hs::String(team.grave));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   261
        fields.insert("teamvoicepack".to_string(), Hs::String(team.voice_pack));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   262
        fields.insert("teamflag".to_string(), Hs::String(team.flag));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   263
        fields.insert("difficulty".to_string(), Hs::Number(team.difficulty));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   264
        fields.insert("hhnum".to_string(), Hs::Number(team.hedgehogs_number));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   265
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   266
        let hogs = team
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   267
            .hedgehogs
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   268
            .iter()
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   269
            .map(|hog| Hs::AnonStruct {
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   270
                name: "HedgehogInfo".to_string(),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   271
                fields: vec![Hs::String(hog.name.clone()), Hs::String(hog.hat.clone())],
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   272
            })
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   273
            .collect();
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   274
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   275
        fields.insert("hedgehogs".to_string(), Hs::List(hogs));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   276
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   277
        teams.push(Hs::Struct {
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   278
            name: "TeamInfo".to_string(),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   279
            fields,
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
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   283
    let mut map_config = vec![];
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   284
    let mut game_config = vec![];
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   285
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   286
    let mut save_map_config = |name: &str, value: String| {
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   287
        map_config.push(Hs::Tuple(vec![
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   288
            Hs::String(name.to_string()),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   289
            Hs::String(value),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   290
        ]));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   291
    };
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   292
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   293
    let config = replay.config;
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   294
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   295
    save_map_config("FEATURE_SIZE", config.feature_size.to_string());
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   296
    save_map_config("MAP", config.map_type);
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   297
    save_map_config("MAPGEN", config.map_generator.to_string());
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   298
    save_map_config("MAZE_SIZE", config.maze_size.to_string());
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   299
    save_map_config("SEED", config.seed);
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   300
    save_map_config("TEMPLATE", config.template.to_string());
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   301
    if let Some(drawn_map) = config.drawn_map {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   302
        save_map_config("DRAWNMAP", drawn_map);
15581
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   303
    }
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   304
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   305
    let mut save_game_config = |name: &str, mut value: Vec<String>| {
15880
3360cc4c162d Fix a couple issues
unC0Rr
parents: 15831
diff changeset
   306
        game_config.push(Hs::Tuple(vec![
15581
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   307
            Hs::String(name.to_string()),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   308
            Hs::List(value.drain(..).map(Hs::String).collect()),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   309
        ]));
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   310
    };
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   311
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   312
    match config.ammo {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   313
        Ammo {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   314
            name,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   315
            settings: Some(settings),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   316
        } => save_game_config("AMMO", vec![name, settings.clone()]),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   317
        Ammo { name, .. } => save_game_config("AMMO", vec![name.clone()]),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   318
    }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   319
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   320
    match config.scheme {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   321
        Scheme { name, settings } => {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   322
            let mut values = vec![name];
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   323
            values.extend_from_slice(&settings);
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   324
            save_game_config("SCHEME", values);
15581
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   325
        }
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   326
    }
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   327
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   328
    save_game_config("SCRIPT", vec![config.script]);
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   329
    save_game_config("THEME", vec![config.theme]);
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   330
15581
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   331
    Hs::Tuple(vec![
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   332
        Hs::List(teams),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   333
        Hs::List(map_config),
ab095fc0256c convert replay to haskell
alfadur
parents: 15580
diff changeset
   334
        Hs::List(game_config),
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   335
        Hs::List(replay.message_log.drain(..).map(Hs::String).collect()),
15581
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
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   339
fn haskell_to_replay(value: HaskellValue) -> Option<Replay> {
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   340
    use HaskellValue::*;
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   341
    let mut config = RoomConfig::new();
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   342
    let mut lists = value.into_tuple()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   343
    let mut lists_iter = lists.drain(..);
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   344
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   345
    let teams_list = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   346
    let map_config = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   347
    let game_config = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   348
    let engine_messages = lists_iter.next()?.into_list()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   349
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   350
    let mut teams = Vec::with_capacity(teams_list.len());
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   351
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   352
    for team in teams_list {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   353
        let (_, mut fields) = team.into_struct()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   354
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   355
        let mut team_info = TeamInfo::default();
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   356
        for (name, value) in fields.drain() {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   357
            match &name[..] {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   358
                "teamowner" => team_info.owner = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   359
                "teamname" => team_info.name = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   360
                "teamcolor" => team_info.color = u8::from_str(&value.into_string()?).ok()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   361
                "teamgrave" => team_info.grave = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   362
                "teamfort" => team_info.fort = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   363
                "teamvoicepack" => team_info.voice_pack = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   364
                "teamflag" => team_info.flag = value.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   365
                "difficulty" => team_info.difficulty = value.into_number()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   366
                "hhnum" => team_info.hedgehogs_number = value.into_number()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   367
                "hedgehogs" => {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   368
                    for (index, hog) in value
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   369
                        .into_list()?
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   370
                        .drain(..)
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   371
                        .enumerate()
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   372
                        .take(team_info.hedgehogs.len())
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 fields) = hog.into_anon_struct()?;
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   375
                        let mut fields_iter = fields.drain(..);
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   376
                        team_info.hedgehogs[index] = HedgehogInfo {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   377
                            name: fields_iter.next()?.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   378
                            hat: fields_iter.next()?.into_string()?,
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   379
                        }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   380
                    }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   381
                }
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
        }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   385
        teams.push(team_info)
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   386
    }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   387
15578
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   388
    for item in map_config {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   389
        let mut tuple = item.into_tuple()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   390
        let mut tuple_iter = tuple.drain(..);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   391
        let name = tuple_iter.next()?.into_string()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   392
        let value = tuple_iter.next()?.into_string()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   393
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   394
        match &name[..] {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   395
            "FEATURE_SIZE" => config.feature_size = u32::from_str(&value).ok()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   396
            "MAP" => config.map_type = value,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   397
            "MAPGEN" => config.map_generator = u32::from_str(&value).ok()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   398
            "MAZE_SIZE" => config.maze_size = u32::from_str(&value).ok()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   399
            "SEED" => config.seed = value,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   400
            "TEMPLATE" => config.template = u32::from_str(&value).ok()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   401
            "DRAWNMAP" => config.drawn_map = Some(value),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   402
            _ => {}
15578
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
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   406
    for item in game_config {
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   407
        let mut tuple = item.into_tuple()?;
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   408
        let mut tuple_iter = tuple.drain(..);
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   409
        let name = tuple_iter.next()?.into_string()?;
15580
98482c4ccf4b fix config loading
alfadur
parents: 15578
diff changeset
   410
        let mut value = tuple_iter.next()?.into_list()?;
98482c4ccf4b fix config loading
alfadur
parents: 15578
diff changeset
   411
        let mut value_iter = value.drain(..);
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   412
15578
0b6094660557 convert config from haskell lists
alfadur
parents: 15576
diff changeset
   413
        let config_item = match &name[..] {
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   414
            "AMMO" => {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   415
                config.ammo = Ammo {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   416
                    name: value_iter.next()?.into_string()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   417
                    settings: value_iter.next().and_then(|v| v.into_string()),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   418
                }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   419
            }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   420
            "SCHEME" => {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   421
                config.scheme = Scheme {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   422
                    name: value_iter.next()?.into_string()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   423
                    settings: value_iter.filter_map(|v| v.into_string()).collect(),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   424
                }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   425
            }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   426
            "SCRIPT" => config.script = value_iter.next()?.into_string()?,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   427
            "THEME" => config.theme = value_iter.next()?.into_string()?,
15578
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
    }
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   431
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   432
    let mut messages = Vec::with_capacity(engine_messages.len());
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   433
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   434
    for message in engine_messages {
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   435
        messages.push(message.into_string()?);
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   436
    }
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   437
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   438
    Some(Replay {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   439
        config,
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   440
        teams,
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   441
        message_log: messages,
15576
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   442
    })
3be9c98ae190 convert teams from haskell list
alfadur
parents: 15570
diff changeset
   443
}
15796
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   444
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   445
impl Replay {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   446
    pub fn save(self, filename: String) -> io::Result<()> {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   447
        let text = format!("{}", replay_to_haskell(self));
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   448
        let mut file = fs::File::open(filename)?;
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   449
        file.write(text.as_bytes())?;
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   450
        Ok(())
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   451
    }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   452
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   453
    pub fn load(filename: &str) -> io::Result<Self> {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   454
        let mut file = fs::File::open(filename)?;
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   455
        let mut bytes = vec![];
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   456
        file.read_to_end(&mut bytes)?;
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   457
        match super::haskell::parse(&bytes[..]) {
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   458
            Ok((_, value)) => haskell_to_replay(value).ok_or(io::Error::new(
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   459
                io::ErrorKind::InvalidData,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   460
                "Invalid replay structure",
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   461
            )),
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   462
            Err(_) => Err(io::Error::new(
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   463
                io::ErrorKind::InvalidData,
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   464
                "Unable to parse file",
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
    }
2939d8599418 load haskell directly into Replay
alfadur
parents: 15581
diff changeset
   468
}