gameServer2/src/protocol/messages.rs
author unc0rr
Sat, 04 Feb 2017 19:52:38 +0300
changeset 12147 03ccb89820f3
parent 12145 a482c7a5f6e3
child 13119 1e39b8749072
permissions -rw-r--r--
Room creation halfplemented
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
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
     6
#[derive(PartialEq, 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>),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    27
    Join(String, Option<String>),
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
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    72
pub enum HWServerMessage<'a> {
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    73
    Ping,
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    74
    Pong,
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    75
    Bye(&'a str),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    76
    Nick(&'a str),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    77
    LobbyLeft(&'a str),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12142
diff changeset
    78
    LobbyJoined(&'a [&'a str]),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
    79
    ChatMsg(&'a str, &'a str),
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
    80
    ClientFlags(&'a str, &'a [&'a str]),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    81
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
    82
    Warning(&'a str),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    83
    Connected(u32),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    84
    Unreachable,
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    85
}
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    86
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    87
fn construct_message(msg: & [&str]) -> String {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    88
    let mut m = String::with_capacity(64);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    89
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    90
    for s in msg {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    91
        m.push_str(s);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    92
        m.push('\n');
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    93
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    94
    m.push('\n');
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    95
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    96
    m
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    97
}
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    98
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    99
impl<'a> HWServerMessage<'a> {
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   100
    pub fn to_raw_protocol(&self) -> String {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   101
        match self {
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   102
            &HWServerMessage::Ping
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   103
                => "PING\n\n".to_string(),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   104
            &HWServerMessage::Pong
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   105
                => "PONG\n\n".to_string(),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   106
            &HWServerMessage::Connected(protocol_version)
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   107
                => construct_message(&[
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   108
                    "CONNECTED",
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   109
                    "Hedgewars server http://www.hedgewars.org/",
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   110
                    &protocol_version.to_string()
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   111
                ]),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   112
            &HWServerMessage::Bye(msg)
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   113
                => construct_message(&["BYE", &msg]),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   114
            &HWServerMessage::Nick(nick)
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   115
                => construct_message(&["NICK", &nick]),
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   116
            &HWServerMessage::LobbyLeft(nick)
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   117
                => construct_message(&["LOBBY_LEFT", &nick]),
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   118
            &HWServerMessage::LobbyJoined(nicks)
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12142
diff changeset
   119
                => {
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12142
diff changeset
   120
                let mut v = vec!["LOBBY:JOINED"];
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   121
                v.extend_from_slice(nicks);
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12142
diff changeset
   122
                construct_message(&v)
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12142
diff changeset
   123
            },
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   124
            &HWServerMessage::ClientFlags(flags, nicks)
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   125
            => {
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   126
                let mut v = vec!["CLIENT_FLAGS"];
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   127
                v.push(flags);
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   128
                v.extend_from_slice(nicks);
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   129
                construct_message(&v)
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   130
            },
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   131
            &HWServerMessage::ChatMsg(nick, msg)
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   132
                => construct_message(&["CHAT", &nick, &msg]),
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   133
            &HWServerMessage::Warning(msg)
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12145
diff changeset
   134
                => construct_message(&["WARNING", &msg]),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12137
diff changeset
   135
            _ => construct_message(&["ERROR", "UNIMPLEMENTED"]),
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   136
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   137
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   138
}