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