rust/hedgewars-network-protocol/src/messages.rs
author Wuzzy <Wuzzy@disroot.org>
Wed, 28 Jun 2023 18:57:00 +0200
changeset 15967 f362e82cddcf
parent 15830 ea459da15b30
permissions -rw-r--r--
CS: Disable game flags incompatible with this script
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     1
use crate::types::{GameCfg, ServerVar, TeamInfo, VoteType};
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     2
use std::iter::once;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     3
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     4
#[derive(PartialEq, Eq, Clone, Debug)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     5
pub enum HwProtocolMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     6
    // common messages
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     7
    Ping,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     8
    Pong,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
     9
    Quit(Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    10
    Global(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    11
    Watch(u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    12
    ToggleServerRegisteredOnly,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    13
    SuperPower,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    14
    Info(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    15
    // anteroom messages
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    16
    Nick(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    17
    Proto(u16),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    18
    Password(String, String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    19
    Checker(u16, String, String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    20
    // lobby messages
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    21
    List,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    22
    Chat(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    23
    CreateRoom(String, Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    24
    JoinRoom(String, Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    25
    Follow(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    26
    Rnd(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    27
    Kick(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    28
    Ban(String, String, u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    29
    BanIp(String, String, u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    30
    BanNick(String, String, u32),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    31
    BanList,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    32
    Unban(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    33
    SetServerVar(ServerVar),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    34
    GetServerVar,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    35
    RestartServer,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    36
    Stats,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    37
    // room messages
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    38
    Part(Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    39
    Cfg(GameCfg),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    40
    AddTeam(Box<TeamInfo>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    41
    RemoveTeam(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    42
    SetHedgehogsNumber(String, u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    43
    SetTeamColor(String, u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    44
    ToggleReady,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    45
    StartGame,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    46
    EngineMessage(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    47
    RoundFinished,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    48
    ToggleRestrictJoin,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    49
    ToggleRestrictTeams,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    50
    ToggleRegisteredOnly,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    51
    RoomName(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    52
    Delegate(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    53
    TeamChat(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    54
    MaxTeams(u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    55
    Fix,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    56
    Unfix,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    57
    Greeting(Option<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    58
    CallVote(Option<VoteType>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    59
    Vote(bool),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    60
    ForceVote(bool),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    61
    Save(String, String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    62
    Delete(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    63
    SaveRoom(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    64
    LoadRoom(String),
15811
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
    65
    CheckerReady,
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
    66
    CheckedOk(Vec<String>),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
    67
    CheckedFail(String),
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    68
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    69
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    70
#[derive(Debug, Clone, Copy)]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    71
pub enum ProtocolFlags {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    72
    InRoom,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    73
    RoomMaster,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    74
    Ready,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    75
    InGame,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    76
    Registered,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    77
    Admin,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    78
    Contributor,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    79
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    80
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    81
impl ProtocolFlags {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    82
    #[inline]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    83
    fn flag_char(&self) -> char {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    84
        match self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    85
            ProtocolFlags::InRoom => 'i',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    86
            ProtocolFlags::RoomMaster => 'h',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    87
            ProtocolFlags::Ready => 'r',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    88
            ProtocolFlags::InGame => 'g',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    89
            ProtocolFlags::Registered => 'u',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    90
            ProtocolFlags::Admin => 'a',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    91
            ProtocolFlags::Contributor => 'c',
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    92
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    93
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    94
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    95
    #[inline]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    96
    fn format(prefix: char, flags: &[ProtocolFlags]) -> String {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    97
        once(prefix)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    98
            .chain(flags.iter().map(|f| f.flag_char()))
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
    99
            .collect()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   100
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   101
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   102
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   103
#[inline]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   104
pub fn add_flags(flags: &[ProtocolFlags]) -> String {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   105
    ProtocolFlags::format('+', flags)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   106
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   107
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   108
#[inline]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   109
pub fn remove_flags(flags: &[ProtocolFlags]) -> String {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   110
    ProtocolFlags::format('-', flags)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   111
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   112
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   113
#[derive(PartialEq, Eq, Clone, Debug)]
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   114
pub enum HwServerMessage {
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   115
    Connected(String, u32),
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   116
    Redirect(u16),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   117
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   118
    Ping,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   119
    Pong,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   120
    Bye(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   121
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   122
    Nick(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   123
    Proto(u16),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   124
    AskPassword(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   125
    ServerAuth(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   126
    LogonPassed,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   127
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   128
    LobbyLeft(String, String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   129
    LobbyJoined(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   130
    ChatMsg { nick: String, msg: String },
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   131
    ClientFlags(String, Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   132
    Rooms(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   133
    RoomAdd(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   134
    RoomJoined(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   135
    RoomLeft(String, String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   136
    RoomRemove(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   137
    RoomUpdated(String, Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   138
    Joining(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   139
    TeamAdd(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   140
    TeamRemove(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   141
    TeamAccepted(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   142
    TeamColor(String, u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   143
    HedgehogsNumber(String, u8),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   144
    ConfigEntry(String, Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   145
    Kicked,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   146
    RunGame,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   147
    ForwardEngineMessage(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   148
    RoundFinished,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   149
    ReplayStart,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   150
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   151
    Info(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   152
    ServerMessage(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   153
    ServerVars(Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   154
    Notice(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   155
    Warning(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   156
    Error(String),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   157
15811
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
   158
    Replay(Vec<String>),
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
   159
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   160
    //Deprecated messages
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   161
    LegacyReady(bool, Vec<String>),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   162
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   163
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   164
fn special_chat(nick: &str, msg: String) -> HwServerMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   165
    HwServerMessage::ChatMsg {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   166
        nick: nick.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   167
        msg,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   168
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   169
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   170
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   171
pub fn server_chat(msg: String) -> HwServerMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   172
    special_chat("[server]", msg)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   173
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   174
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   175
pub fn global_chat(msg: String) -> HwServerMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   176
    special_chat("(global notice)", msg)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   177
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   178
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   179
impl ServerVar {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   180
    pub fn to_protocol(&self) -> Vec<String> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   181
        use ServerVar::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   182
        match self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   183
            MOTDNew(s) => vec!["MOTD_NEW".to_string(), s.clone()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   184
            MOTDOld(s) => vec!["MOTD_OLD".to_string(), s.clone()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   185
            LatestProto(n) => vec!["LATEST_PROTO".to_string(), n.to_string()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   186
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   187
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   188
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   189
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   190
impl VoteType {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   191
    pub fn to_protocol(&self) -> Vec<String> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   192
        use VoteType::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   193
        match self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   194
            Kick(nick) => vec!["KICK".to_string(), nick.clone()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   195
            Map(None) => vec!["MAP".to_string()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   196
            Map(Some(name)) => vec!["MAP".to_string(), name.clone()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   197
            Pause => vec!["PAUSE".to_string()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   198
            NewSeed => vec!["NEWSEED".to_string()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   199
            HedgehogsPerTeam(count) => vec!["HEDGEHOGS".to_string(), count.to_string()],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   200
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   201
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   202
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   203
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   204
impl GameCfg {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   205
    pub fn to_protocol(&self) -> (String, Vec<String>) {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   206
        use GameCfg::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   207
        match self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   208
            FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   209
            MapType(t) => ("MAP".to_string(), vec![t.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   210
            MapGenerator(g) => ("MAPGEN".to_string(), vec![g.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   211
            MazeSize(s) => ("MAZE_SIZE".to_string(), vec![s.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   212
            Seed(s) => ("SEED".to_string(), vec![s.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   213
            Template(t) => ("TEMPLATE".to_string(), vec![t.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   214
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   215
            Ammo(n, None) => ("AMMO".to_string(), vec![n.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   216
            Ammo(n, Some(s)) => ("AMMO".to_string(), vec![n.to_string(), s.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   217
            Scheme(n, s) if s.is_empty() => ("SCHEME".to_string(), vec![n.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   218
            Scheme(n, s) => ("SCHEME".to_string(), {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   219
                let mut v = vec![n.to_string()];
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   220
                v.extend(s.clone());
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   221
                v
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   222
            }),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   223
            Script(s) => ("SCRIPT".to_string(), vec![s.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   224
            Theme(t) => ("THEME".to_string(), vec![t.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   225
            DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   226
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   227
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   228
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   229
    pub fn to_server_msg(&self) -> HwServerMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   230
        let (name, args) = self.to_protocol();
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   231
        HwServerMessage::ConfigEntry(name, args)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   232
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   233
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   234
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   235
impl TeamInfo {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   236
    pub fn to_protocol(&self) -> Vec<String> {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   237
        let mut info = vec![
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   238
            self.name.clone(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   239
            self.grave.clone(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   240
            self.fort.clone(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   241
            self.voice_pack.clone(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   242
            self.flag.clone(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   243
            self.owner.clone(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   244
            self.difficulty.to_string(),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   245
        ];
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   246
        let hogs = self
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   247
            .hedgehogs
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   248
            .iter()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   249
            .flat_map(|h| once(h.name.clone()).chain(once(h.hat.clone())));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   250
        info.extend(hogs);
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   251
        info
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   252
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   253
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   254
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   255
macro_rules! const_braces {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   256
    ($e: expr) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   257
        "{}\n"
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   258
    };
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   259
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   260
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   261
macro_rules! msg {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   262
    [$($part: expr),*] => {
15830
ea459da15b30 update protocol 📦
alfadur
parents: 15811
diff changeset
   263
        format!(concat!($(const_braces!($part)),*, "\n"), $($part),*)
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   264
    };
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   265
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   266
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   267
impl HwProtocolMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   268
    /** Converts the message to a raw `String`, which can be sent over the network.
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   269
     *
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   270
     * This is the inverse of the `message` parser.
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   271
     */
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   272
    pub fn to_raw_protocol(&self) -> String {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   273
        use self::HwProtocolMessage::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   274
        match self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   275
            Ping => msg!["PING"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   276
            Pong => msg!["PONG"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   277
            Quit(None) => msg!["QUIT"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   278
            Quit(Some(msg)) => msg!["QUIT", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   279
            Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   280
            Watch(name) => msg!["CMD", format!("WATCH {}", name)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   281
            ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   282
            SuperPower => msg!["CMD", "SUPER_POWER"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   283
            Info(info) => msg!["CMD", format!("INFO {}", info)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   284
            Nick(nick) => msg!("NICK", nick),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   285
            Proto(version) => msg!["PROTO", version],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   286
            Password(p, s) => msg!["PASSWORD", p, s],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   287
            Checker(i, n, p) => msg!["CHECKER", i, n, p],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   288
            List => msg!["LIST"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   289
            Chat(msg) => msg!["CHAT", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   290
            CreateRoom(name, None) => msg!["CREATE_ROOM", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   291
            CreateRoom(name, Some(password)) => msg!["CREATE_ROOM", name, password],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   292
            JoinRoom(name, None) => msg!["JOIN_ROOM", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   293
            JoinRoom(name, Some(password)) => msg!["JOIN_ROOM", name, password],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   294
            Follow(name) => msg!["FOLLOW", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   295
            Rnd(args) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   296
                if args.is_empty() {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   297
                    msg!["CMD", "RND"]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   298
                } else {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   299
                    msg!["CMD", format!("RND {}", args.join(" "))]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   300
                }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   301
            }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   302
            Kick(name) => msg!["KICK", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   303
            Ban(name, reason, time) => msg!["BAN", name, reason, time],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   304
            BanIp(ip, reason, time) => msg!["BAN_IP", ip, reason, time],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   305
            BanNick(nick, reason, time) => msg!("BAN_NICK", nick, reason, time),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   306
            BanList => msg!["BANLIST"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   307
            Unban(name) => msg!["UNBAN", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   308
            SetServerVar(var) => construct_message(&["SET_SERVER_VAR"], &var.to_protocol()),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   309
            GetServerVar => msg!["GET_SERVER_VAR"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   310
            RestartServer => msg!["CMD", "RESTART_SERVER YES"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   311
            Stats => msg!["CMD", "STATS"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   312
            Part(None) => msg!["PART"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   313
            Part(Some(msg)) => msg!["PART", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   314
            Cfg(config) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   315
                let (name, args) = config.to_protocol();
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   316
                msg!["CFG", name, args.join("\n")]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   317
            }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   318
            AddTeam(info) => msg![
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   319
                "ADD_TEAM",
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   320
                info.name,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   321
                info.color,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   322
                info.grave,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   323
                info.fort,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   324
                info.voice_pack,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   325
                info.flag,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   326
                info.difficulty,
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   327
                &(info.hedgehogs.iter())
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   328
                    .flat_map(|h| [&h.name[..], &h.hat[..]])
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   329
                    .collect::<Vec<_>>()
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   330
                    .join("\n")
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   331
            ],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   332
            RemoveTeam(name) => msg!["REMOVE_TEAM", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   333
            SetHedgehogsNumber(team, number) => msg!["HH_NUM", team, number],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   334
            SetTeamColor(team, color) => msg!["TEAM_COLOR", team, color],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   335
            ToggleReady => msg!["TOGGLE_READY"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   336
            StartGame => msg!["START_GAME"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   337
            EngineMessage(msg) => msg!["EM", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   338
            RoundFinished => msg!["ROUNDFINISHED"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   339
            ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   340
            ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   341
            ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   342
            RoomName(name) => msg!["ROOM_NAME", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   343
            Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   344
            TeamChat(msg) => msg!["TEAMCHAT", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   345
            MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   346
            Fix => msg!["CMD", "FIX"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   347
            Unfix => msg!["CMD", "UNFIX"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   348
            Greeting(None) => msg!["CMD", "GREETING"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   349
            Greeting(Some(msg)) => msg!["CMD", format!("GREETING {}", msg)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   350
            CallVote(None) => msg!["CMD", "CALLVOTE"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   351
            CallVote(Some(vote)) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   352
                msg!["CMD", format!("CALLVOTE {}", &vote.to_protocol().join(" "))]
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   353
            }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   354
            Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg { "YES" } else { "NO" })],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   355
            ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg { "YES" } else { "NO" })],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   356
            Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   357
            Delete(name) => msg!["CMD", format!("DELETE {}", name)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   358
            SaveRoom(name) => msg!["CMD", format!("SAVEROOM {}", name)],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   359
            LoadRoom(name) => msg!["CMD", format!("LOADROOM {}", name)],
15811
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
   360
            CheckerReady => msg!["READY"],
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
   361
            CheckedOk(args) => msg!["CHECKED", "OK", args.join("\n")],
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
   362
            CheckedFail(message) => msg!["CHECKED", "FAIL", message],
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   363
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   364
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   365
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   366
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   367
fn construct_message(header: &[&str], msg: &[String]) -> String {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   368
    let mut v: Vec<_> = header.iter().cloned().collect();
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   369
    v.extend(msg.iter().map(|s| &s[..]));
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   370
    v.push("\n");
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   371
    v.join("\n")
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   372
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   373
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   374
impl HwServerMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   375
    pub fn to_raw_protocol(&self) -> String {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   376
        use self::HwServerMessage::*;
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   377
        match self {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   378
            Ping => msg!["PING"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   379
            Pong => msg!["PONG"],
15810
ee84e417d8d0 Add parser and idempotention tests for server messages
unc0rr
parents: 15804
diff changeset
   380
            Connected(message, protocol_version) => msg!["CONNECTED", message, protocol_version],
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   381
            Redirect(port) => msg!["REDIRECT", port],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   382
            Bye(msg) => msg!["BYE", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   383
            Nick(nick) => msg!["NICK", nick],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   384
            Proto(proto) => msg!["PROTO", proto],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   385
            AskPassword(salt) => msg!["ASKPASSWORD", salt],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   386
            ServerAuth(hash) => msg!["SERVER_AUTH", hash],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   387
            LogonPassed => msg!["LOGONPASSED"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   388
            LobbyLeft(nick, msg) => msg!["LOBBY:LEFT", nick, msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   389
            LobbyJoined(nicks) => construct_message(&["LOBBY:JOINED"], &nicks),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   390
            ClientFlags(flags, nicks) => construct_message(&["CLIENT_FLAGS", flags], &nicks),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   391
            Rooms(info) => construct_message(&["ROOMS"], &info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   392
            RoomAdd(info) => construct_message(&["ROOM", "ADD"], &info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   393
            RoomJoined(nicks) => construct_message(&["JOINED"], &nicks),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   394
            RoomLeft(nick, msg) => msg!["LEFT", nick, msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   395
            RoomRemove(name) => msg!["ROOM", "DEL", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   396
            RoomUpdated(name, info) => construct_message(&["ROOM", "UPD", name], &info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   397
            Joining(name) => msg!["JOINING", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   398
            TeamAdd(info) => construct_message(&["ADD_TEAM"], &info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   399
            TeamRemove(name) => msg!["REMOVE_TEAM", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   400
            TeamAccepted(name) => msg!["TEAM_ACCEPTED", name],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   401
            TeamColor(name, color) => msg!["TEAM_COLOR", name, color],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   402
            HedgehogsNumber(name, number) => msg!["HH_NUM", name, number],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   403
            ConfigEntry(name, values) => construct_message(&["CFG", name], &values),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   404
            Kicked => msg!["KICKED"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   405
            RunGame => msg!["RUN_GAME"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   406
            ForwardEngineMessage(em) => construct_message(&["EM"], &em),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   407
            RoundFinished => msg!["ROUND_FINISHED"],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   408
            ChatMsg { nick, msg } => msg!["CHAT", nick, msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   409
            Info(info) => construct_message(&["INFO"], &info),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   410
            ServerMessage(msg) => msg!["SERVER_MESSAGE", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   411
            ServerVars(vars) => construct_message(&["SERVER_VARS"], &vars),
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   412
            Notice(msg) => msg!["NOTICE", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   413
            Warning(msg) => msg!["WARNING", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   414
            Error(msg) => msg!["ERROR", msg],
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   415
            ReplayStart => msg!["REPLAY_START"],
15811
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 15810
diff changeset
   416
            Replay(em) => construct_message(&["REPLAY"], &em),
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   417
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   418
            LegacyReady(is_ready, nicks) => {
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   419
                construct_message(&[if *is_ready { "READY" } else { "NOT_READY" }], &nicks)
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   420
            }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   421
        }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   422
    }
747278149393 Extract network protocol into a separate crate
unc0rr
parents:
diff changeset
   423
}