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