rust/hedgewars-server/src/protocol/messages.rs
author Wuzzy <Wuzzy2@mail.ru>
Wed, 19 Dec 2018 05:50:02 +0100
changeset 14480 2113296b7a29
parent 14462 98ef2913ec73
child 14702 f64e21f164a5
permissions -rw-r--r--
Keep track of singleplayer high scores in challenges
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     1
use crate::server::coretypes::{GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType};
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     2
use std::{convert::From, iter::once, ops};
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     3
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
     4
#[derive(PartialEq, Eq, Clone, Debug)]
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
     5
pub enum HWProtocolMessage {
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     6
    // core
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     7
    Ping,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     8
    Pong,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
     9
    Quit(Option<String>),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    10
    //Cmd(String, Vec<String>),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    11
    Global(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    12
    Watch(String),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    13
    ToggleServerRegisteredOnly,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    14
    SuperPower,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    15
    Info(String),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    16
    // not entered state
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    17
    Nick(String),
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13486
diff changeset
    18
    Proto(u16),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    19
    Password(String, String),
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13801
diff changeset
    20
    Checker(u16, String, String),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    21
    // lobby
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    22
    List,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    23
    Chat(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    24
    CreateRoom(String, Option<String>),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    25
    JoinRoom(String, Option<String>),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    26
    Follow(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    27
    Rnd(Vec<String>),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    28
    Kick(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    29
    Ban(String, String, u32),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    30
    BanIP(String, String, u32),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    31
    BanNick(String, String, u32),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    32
    BanList,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    33
    Unban(String),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    34
    SetServerVar(ServerVar),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    35
    GetServerVar,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    36
    RestartServer,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    37
    Stats,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    38
    // in room
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    39
    Part(Option<String>),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    40
    Cfg(GameCfg),
13529
5359ff75da3a indulge clippy
alfadur
parents: 13525
diff changeset
    41
    AddTeam(Box<TeamInfo>),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    42
    RemoveTeam(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    43
    SetHedgehogsNumber(String, u8),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    44
    SetTeamColor(String, u8),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    45
    ToggleReady,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    46
    StartGame,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    47
    EngineMessage(String),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    48
    RoundFinished,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    49
    ToggleRestrictJoin,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    50
    ToggleRestrictTeams,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    51
    ToggleRegisteredOnly,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    52
    RoomName(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    53
    Delegate(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    54
    TeamChat(String),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    55
    MaxTeams(u8),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    56
    Fix,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    57
    Unfix,
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    58
    Greeting(String),
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
    59
    CallVote(Option<VoteType>),
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
    60
    Vote(bool),
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
    61
    ForceVote(bool),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    62
    Save(String, String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    63
    Delete(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    64
    SaveRoom(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    65
    LoadRoom(String),
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    66
    Malformed,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    67
    Empty,
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    68
}
12136
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    69
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    70
#[derive(Debug)]
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
    71
pub enum HWServerMessage {
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    72
    Ping,
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    73
    Pong,
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
    74
    Bye(String),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
    75
    Nick(String),
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13486
diff changeset
    76
    Proto(u16),
13805
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
    77
    ServerAuth(String),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    78
    LobbyLeft(String, String),
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
    79
    LobbyJoined(Vec<String>),
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    80
    ChatMsg { nick: String, msg: String },
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
    81
    ClientFlags(String, Vec<String>),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    82
    Rooms(Vec<String>),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    83
    RoomAdd(Vec<String>),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    84
    RoomJoined(Vec<String>),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    85
    RoomLeft(String, String),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    86
    RoomRemove(String),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    87
    RoomUpdated(String, Vec<String>),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    88
    TeamAdd(Vec<String>),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    89
    TeamRemove(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    90
    TeamAccepted(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    91
    TeamColor(String, u8),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    92
    HedgehogsNumber(String, u8),
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    93
    ConfigEntry(String, Vec<String>),
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
    94
    Kicked,
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
    95
    RunGame,
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13428
diff changeset
    96
    ForwardEngineMessage(Vec<String>),
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
    97
    RoundFinished,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    98
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    99
    ServerMessage(String),
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   100
    Notice(String),
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
   101
    Warning(String),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   102
    Error(String),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
   103
    Connected(u32),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
   104
    Unreachable,
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   105
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   106
    //Deprecated messages
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   107
    LegacyReady(bool, Vec<String>),
12136
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
   108
}
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   109
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   110
pub fn server_chat(msg: String) -> HWServerMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   111
    HWServerMessage::ChatMsg {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   112
        nick: "[server]".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   113
        msg,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   114
    }
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
   115
}
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
   116
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   117
impl GameCfg {
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   118
    pub fn to_protocol(&self) -> (String, Vec<String>) {
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13536
diff changeset
   119
        use crate::server::coretypes::GameCfg::*;
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   120
        match self {
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   121
            FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   122
            MapType(t) => ("MAP".to_string(), vec![t.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   123
            MapGenerator(g) => ("MAPGEN".to_string(), vec![g.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   124
            MazeSize(s) => ("MAZE_SIZE".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   125
            Seed(s) => ("SEED".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   126
            Template(t) => ("TEMPLATE".to_string(), vec![t.to_string()]),
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   127
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   128
            Ammo(n, None) => ("AMMO".to_string(), vec![n.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   129
            Ammo(n, Some(s)) => ("AMMO".to_string(), vec![n.to_string(), s.to_string()]),
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   130
            Scheme(n, s) if s.is_empty() => ("SCHEME".to_string(), vec![n.to_string()]),
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   131
            Scheme(n, s) => ("SCHEME".to_string(), {
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   132
                let mut v = vec![n.to_string()];
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   133
                v.extend(s.clone().into_iter());
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   134
                v
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   135
            }),
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   136
            Script(s) => ("SCRIPT".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   137
            Theme(t) => ("THEME".to_string(), vec![t.to_string()]),
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   138
            DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]),
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   139
        }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   140
    }
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   141
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   142
    pub fn to_server_msg(&self) -> HWServerMessage {
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   143
        use self::HWServerMessage::ConfigEntry;
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   144
        let (name, args) = self.to_protocol();
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   145
        HWServerMessage::ConfigEntry(name, args)
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   146
    }
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   147
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   148
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   149
macro_rules! const_braces {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   150
    ($e: expr) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   151
        "{}\n"
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   152
    };
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   153
}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   154
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   155
macro_rules! msg {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   156
    [$($part: expr),*] => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   157
        format!(concat!($(const_braces!($part)),*, "\n"), $($part),*);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   158
    };
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   159
}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   160
13801
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13672
diff changeset
   161
#[cfg(test)]
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   162
macro_rules! several {
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   163
    [$part: expr] => { once($part) };
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   164
    [$part: expr, $($other: expr),*] => { once($part).chain(several![$($other),*]) };
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   165
}
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   166
13438
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   167
impl HWProtocolMessage {
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   168
    /** Converts the message to a raw `String`, which can be sent over the network.
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   169
     *
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   170
     * This is the inverse of the `message` parser.
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   171
     */
13801
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13672
diff changeset
   172
    #[cfg(test)]
13438
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13437
diff changeset
   173
    pub(crate) fn to_raw_protocol(&self) -> String {
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   174
        use self::HWProtocolMessage::*;
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   175
        match self {
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   176
            Ping => msg!["PING"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   177
            Pong => msg!["PONG"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   178
            Quit(None) => msg!["QUIT"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   179
            Quit(Some(msg)) => msg!["QUIT", msg],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   180
            Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   181
            Watch(name) => msg!["CMD", format!("WATCH {}", name)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   182
            ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   183
            SuperPower => msg!["CMD", "SUPER_POWER"],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   184
            Info(info) => msg!["CMD", format!("INFO {}", info)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   185
            Nick(nick) => msg!("NICK", nick),
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   186
            Proto(version) => msg!["PROTO", version],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   187
            Password(p, s) => msg!["PASSWORD", p, s],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   188
            Checker(i, n, p) => msg!["CHECKER", i, n, p],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   189
            List => msg!["LIST"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   190
            Chat(msg) => msg!["CHAT", msg],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   191
            CreateRoom(name, None) => msg!["CREATE_ROOM", name],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   192
            CreateRoom(name, Some(password)) => msg!["CREATE_ROOM", name, password],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   193
            JoinRoom(name, None) => msg!["JOIN_ROOM", name],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   194
            JoinRoom(name, Some(password)) => msg!["JOIN_ROOM", name, password],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   195
            Follow(name) => msg!["FOLLOW", name],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   196
            Rnd(args) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   197
                if args.is_empty() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   198
                    msg!["CMD", "RND"]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   199
                } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   200
                    msg!["CMD", format!("RND {}", args.join(" "))]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   201
                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   202
            }
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   203
            Kick(name) => msg!["KICK", name],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   204
            Ban(name, reason, time) => msg!["BAN", name, reason, time],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   205
            BanIP(ip, reason, time) => msg!["BAN_IP", ip, reason, time],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   206
            BanNick(nick, reason, time) => msg!("BAN_NICK", nick, reason, time),
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   207
            BanList => msg!["BANLIST"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   208
            Unban(name) => msg!["UNBAN", name],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   209
            //SetServerVar(ServerVar), ???
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   210
            GetServerVar => msg!["GET_SERVER_VAR"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   211
            RestartServer => msg!["CMD", "RESTART_SERVER YES"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   212
            Stats => msg!["CMD", "STATS"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   213
            Part(None) => msg!["PART"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   214
            Part(Some(msg)) => msg!["PART", msg],
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   215
            Cfg(config) => {
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   216
                let (name, args) = config.to_protocol();
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   217
                msg!["CFG", name, args.join("\n")]
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   218
            }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   219
            AddTeam(info) => msg![
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   220
                "ADD_TEAM",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   221
                info.name,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   222
                info.color,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   223
                info.grave,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   224
                info.fort,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   225
                info.voice_pack,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   226
                info.flag,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   227
                info.difficulty,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   228
                info.hedgehogs
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   229
                    .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   230
                    .flat_map(|h| several![&h.name[..], &h.hat[..]])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   231
                    .collect::<Vec<_>>()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   232
                    .join("\n")
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   233
            ],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   234
            RemoveTeam(name) => msg!["REMOVE_TEAM", name],
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   235
            SetHedgehogsNumber(team, number) => msg!["HH_NUM", team, number],
c4f917c6be51 add missing message tests
alfadur
parents: 13438
diff changeset
   236
            SetTeamColor(team, color) => msg!["TEAM_COLOR", team, color],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   237
            ToggleReady => msg!["TOGGLE_READY"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   238
            StartGame => msg!["START_GAME"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   239
            EngineMessage(msg) => msg!["EM", msg],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   240
            RoundFinished => msg!["ROUNDFINISHED"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   241
            ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   242
            ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   243
            ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   244
            RoomName(name) => msg!["ROOM_NAME", name],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   245
            Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   246
            TeamChat(msg) => msg!["TEAMCHAT", msg],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   247
            MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   248
            Fix => msg!["CMD", "FIX"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   249
            Unfix => msg!["CMD", "UNFIX"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   250
            Greeting(msg) => msg!["CMD", format!("GREETING {}", msg)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   251
            //CallVote(Option<(String, Option<String>)>) =>, ??
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   252
            Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg { "YES" } else { "NO" })],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   253
            ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg { "YES" } else { "NO" })],
13533
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13532
diff changeset
   254
            Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)],
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13532
diff changeset
   255
            Delete(name) => msg!["CMD", format!("DELETE {}", name)],
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13532
diff changeset
   256
            SaveRoom(name) => msg!["CMD", format!("SAVEROOM {}", name)],
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13532
diff changeset
   257
            LoadRoom(name) => msg!["CMD", format!("LOADROOM {}", name)],
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   258
            Malformed => msg!["A", "QUICK", "BROWN", "HOG", "JUMPS", "OVER", "THE", "LAZY", "DOG"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   259
            Empty => msg![""],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   260
            _ => panic!("Protocol message not yet implemented"),
13437
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   261
        }
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   262
    }
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   263
}
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13433
diff changeset
   264
13529
5359ff75da3a indulge clippy
alfadur
parents: 13525
diff changeset
   265
fn construct_message(header: &[&str], msg: &[String]) -> String {
14355
31717e1436cd recruit some newly stabilized functions
alfadur
parents: 13806
diff changeset
   266
    let mut v: Vec<_> = header.iter().cloned().collect();
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   267
    v.extend(msg.iter().map(|s| &s[..]));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   268
    v.push("\n");
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   269
    v.join("\n")
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   270
}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   271
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
   272
impl HWServerMessage {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
   273
    pub fn to_raw_protocol(&self) -> String {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12152
diff changeset
   274
        use self::HWServerMessage::*;
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   275
        match self {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   276
            Ping => msg!["PING"],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   277
            Pong => msg!["PONG"],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   278
            Connected(protocol_version) => msg![
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   279
                "CONNECTED",
13672
bfc2727daead Return https
alfadur
parents: 13671
diff changeset
   280
                "Hedgewars server https://www.hedgewars.org/",
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   281
                protocol_version
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   282
            ],
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   283
            Bye(msg) => msg!["BYE", msg],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   284
            Nick(nick) => msg!["NICK", nick],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   285
            Proto(proto) => msg!["PROTO", proto],
13805
0118b7412570 use openssl for password message handling
alfadur
parents: 13803
diff changeset
   286
            ServerAuth(hash) => msg!["SERVER_AUTH", hash],
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   287
            LobbyLeft(nick, msg) => msg!["LOBBY:LEFT", nick, msg],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   288
            LobbyJoined(nicks) => construct_message(&["LOBBY:JOINED"], &nicks),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   289
            ClientFlags(flags, nicks) => construct_message(&["CLIENT_FLAGS", flags], &nicks),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   290
            Rooms(info) => construct_message(&["ROOMS"], &info),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   291
            RoomAdd(info) => construct_message(&["ROOM", "ADD"], &info),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   292
            RoomJoined(nicks) => construct_message(&["JOINED"], &nicks),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   293
            RoomLeft(nick, msg) => msg!["LEFT", nick, msg],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   294
            RoomRemove(name) => msg!["ROOM", "DEL", name],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   295
            RoomUpdated(name, info) => construct_message(&["ROOM", "UPD", name], &info),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   296
            TeamAdd(info) => construct_message(&["ADD_TEAM"], &info),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   297
            TeamRemove(name) => msg!["REMOVE_TEAM", name],
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   298
            TeamAccepted(name) => msg!["TEAM_ACCEPTED", name],
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   299
            TeamColor(name, color) => msg!["TEAM_COLOR", name, color],
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   300
            HedgehogsNumber(name, number) => msg!["HH_NUM", name, number],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   301
            ConfigEntry(name, values) => construct_message(&["CFG", name], &values),
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13449
diff changeset
   302
            Kicked => msg!["KICKED"],
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   303
            RunGame => msg!["RUN_GAME"],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   304
            ForwardEngineMessage(em) => construct_message(&["EM"], &em),
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   305
            RoundFinished => msg!["ROUND_FINISHED"],
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   306
            ChatMsg { nick, msg } => msg!["CHAT", nick, msg],
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   307
            ServerMessage(msg) => msg!["SERVER_MESSAGE", msg],
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   308
            Notice(msg) => msg!["NOTICE", msg],
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   309
            Warning(msg) => msg!["WARNING", msg],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   310
            Error(msg) => msg!["ERROR", msg],
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   311
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   312
            LegacyReady(is_ready, nicks) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   313
                construct_message(&[if *is_ready { "READY" } else { "NOT_READY" }], &nicks)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   314
            }
13806
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13805
diff changeset
   315
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   316
            _ => msg!["ERROR", "UNIMPLEMENTED"],
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   317
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   318
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   319
}