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