rust/hedgewars-server/src/protocol/messages.rs
author alfadur
Sat, 19 Jun 2021 17:48:10 +0300
changeset 15795 40929af15167
parent 15532 f1205f33bf5b
permissions -rw-r--r--
find excuses to use shiny new 🦀 features
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14907
diff changeset
     1
use crate::core::types::{GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     2
use std::{convert::From, iter::once, ops};
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     3
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
     4
#[derive(PartialEq, Eq, Clone, Debug)]
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     5
pub enum HwProtocolMessage {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14457
diff changeset
     6
    // common messages
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     7
    Ping,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     8
    Pong,
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
     9
    Quit(Option<String>),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    10
    Global(String),
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
    11
    Watch(u32),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    12
    ToggleServerRegisteredOnly,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    13
    SuperPower,
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    14
    Info(String),
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14457
diff changeset
    15
    // anteroom messages
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    16
    Nick(String),
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13481
diff changeset
    17
    Proto(u16),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    18
    Password(String, String),
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13796
diff changeset
    19
    Checker(u16, String, String),
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14457
diff changeset
    20
    // lobby messages
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    21
    List,
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    22
    Chat(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    23
    CreateRoom(String, Option<String>),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    24
    JoinRoom(String, Option<String>),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    25
    Follow(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    26
    Rnd(Vec<String>),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    27
    Kick(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    28
    Ban(String, String, u32),
15124
824472aa4d97 use tuple parsers
alfadur
parents: 15112
diff changeset
    29
    BanIp(String, String, u32),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    30
    BanNick(String, String, u32),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    31
    BanList,
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    32
    Unban(String),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    33
    SetServerVar(ServerVar),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    34
    GetServerVar,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    35
    RestartServer,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    36
    Stats,
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14457
diff changeset
    37
    // room messages
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    38
    Part(Option<String>),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    39
    Cfg(GameCfg),
13524
5359ff75da3a indulge clippy
alfadur
parents: 13520
diff changeset
    40
    AddTeam(Box<TeamInfo>),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    41
    RemoveTeam(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    42
    SetHedgehogsNumber(String, u8),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    43
    SetTeamColor(String, u8),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    44
    ToggleReady,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    45
    StartGame,
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    46
    EngineMessage(String),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    47
    RoundFinished,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    48
    ToggleRestrictJoin,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    49
    ToggleRestrictTeams,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    50
    ToggleRegisteredOnly,
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    51
    RoomName(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    52
    Delegate(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    53
    TeamChat(String),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    54
    MaxTeams(u8),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    55
    Fix,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    56
    Unfix,
15111
1e45db229f9f make greeting argument optional
alfadur
parents: 15075
diff changeset
    57
    Greeting(Option<String>),
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
    58
    CallVote(Option<VoteType>),
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
    59
    Vote(bool),
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
    60
    ForceVote(bool),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    61
    Save(String, String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    62
    Delete(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    63
    SaveRoom(String),
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    64
    LoadRoom(String),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    65
}
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    66
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14787
diff changeset
    67
#[derive(Debug, Clone, Copy)]
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    68
pub enum ProtocolFlags {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    69
    InRoom,
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    70
    RoomMaster,
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    71
    Ready,
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    72
    InGame,
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14787
diff changeset
    73
    Registered,
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    74
    Admin,
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    75
    Contributor,
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    76
}
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    77
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    78
impl ProtocolFlags {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    79
    #[inline]
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    80
    fn flag_char(&self) -> char {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    81
        match self {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    82
            ProtocolFlags::InRoom => 'i',
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    83
            ProtocolFlags::RoomMaster => 'h',
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    84
            ProtocolFlags::Ready => 'r',
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    85
            ProtocolFlags::InGame => 'g',
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14787
diff changeset
    86
            ProtocolFlags::Registered => 'u',
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    87
            ProtocolFlags::Admin => 'a',
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    88
            ProtocolFlags::Contributor => 'c',
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    89
        }
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    90
    }
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    91
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    92
    #[inline]
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    93
    fn format(prefix: char, flags: &[ProtocolFlags]) -> String {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    94
        once(prefix)
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    95
            .chain(flags.iter().map(|f| f.flag_char()))
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    96
            .collect()
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    97
    }
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    98
}
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
    99
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   100
#[inline]
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   101
pub fn add_flags(flags: &[ProtocolFlags]) -> String {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   102
    ProtocolFlags::format('+', flags)
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   103
}
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   104
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   105
#[inline]
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   106
pub fn remove_flags(flags: &[ProtocolFlags]) -> String {
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   107
    ProtocolFlags::format('-', flags)
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   108
}
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   109
50fcef24003f add typed protocol flags
alfadur
parents: 14779
diff changeset
   110
#[derive(Debug)]
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   111
pub enum HwServerMessage {
14835
57ed3981db20 add redirect message
alfadur
parents: 14795
diff changeset
   112
    Connected(u32),
57ed3981db20 add redirect message
alfadur
parents: 14795
diff changeset
   113
    Redirect(u16),
57ed3981db20 add redirect message
alfadur
parents: 14795
diff changeset
   114
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   115
    Ping,
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   116
    Pong,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   117
    Bye(String),
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   118
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   119
    Nick(String),
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13481
diff changeset
   120
    Proto(u16),
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   121
    AskPassword(String),
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   122
    ServerAuth(String),
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15124
diff changeset
   123
    LogonPassed,
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   124
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   125
    LobbyLeft(String, String),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   126
    LobbyJoined(Vec<String>),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   127
    ChatMsg { nick: String, msg: String },
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   128
    ClientFlags(String, Vec<String>),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   129
    Rooms(Vec<String>),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   130
    RoomAdd(Vec<String>),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   131
    RoomJoined(Vec<String>),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   132
    RoomLeft(String, String),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   133
    RoomRemove(String),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   134
    RoomUpdated(String, Vec<String>),
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14786
diff changeset
   135
    Joining(String),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   136
    TeamAdd(Vec<String>),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   137
    TeamRemove(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   138
    TeamAccepted(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   139
    TeamColor(String, u8),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   140
    HedgehogsNumber(String, u8),
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   141
    ConfigEntry(String, Vec<String>),
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
   142
    Kicked,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   143
    RunGame,
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13423
diff changeset
   144
    ForwardEngineMessage(Vec<String>),
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   145
    RoundFinished,
14907
c156273b57de add REPLAY_START support
alfadur
parents: 14835
diff changeset
   146
    ReplayStart,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   147
14786
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14785
diff changeset
   148
    Info(Vec<String>),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   149
    ServerMessage(String),
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   150
    ServerVars(Vec<String>),
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   151
    Notice(String),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   152
    Warning(String),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   153
    Error(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
   154
    Unreachable,
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   155
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   156
    //Deprecated messages
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   157
    LegacyReady(bool, Vec<String>),
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
   158
}
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   159
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   160
fn special_chat(nick: &str, msg: String) -> HwServerMessage {
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   161
    HwServerMessage::ChatMsg {
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   162
        nick: nick.to_string(),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   163
        msg,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   164
    }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
   165
}
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
   166
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   167
pub fn server_chat(msg: String) -> HwServerMessage {
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   168
    special_chat("[server]", msg)
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   169
}
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   170
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   171
pub fn global_chat(msg: String) -> HwServerMessage {
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   172
    special_chat("(global notice)", msg)
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   173
}
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   174
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   175
impl ServerVar {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   176
    pub fn to_protocol(&self) -> Vec<String> {
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   177
        use ServerVar::*;
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   178
        match self {
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   179
            MOTDNew(s) => vec!["MOTD_NEW".to_string(), s.clone()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   180
            MOTDOld(s) => vec!["MOTD_OLD".to_string(), s.clone()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   181
            LatestProto(n) => vec!["LATEST_PROTO".to_string(), n.to_string()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   182
        }
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   183
    }
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   184
}
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   185
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   186
impl VoteType {
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   187
    pub fn to_protocol(&self) -> Vec<String> {
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   188
        use VoteType::*;
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   189
        match self {
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   190
            Kick(nick) => vec!["KICK".to_string(), nick.clone()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   191
            Map(None) => vec!["MAP".to_string()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   192
            Map(Some(name)) => vec!["MAP".to_string(), name.clone()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   193
            Pause => vec!["PAUSE".to_string()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   194
            NewSeed => vec!["NEWSEED".to_string()],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   195
            HedgehogsPerTeam(count) => vec!["HEDGEHOGS".to_string(), count.to_string()],
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   196
        }
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   197
    }
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   198
}
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   199
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   200
impl GameCfg {
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   201
    pub fn to_protocol(&self) -> (String, Vec<String>) {
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   202
        use GameCfg::*;
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   203
        match self {
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   204
            FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   205
            MapType(t) => ("MAP".to_string(), vec![t.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   206
            MapGenerator(g) => ("MAPGEN".to_string(), vec![g.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   207
            MazeSize(s) => ("MAZE_SIZE".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   208
            Seed(s) => ("SEED".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   209
            Template(t) => ("TEMPLATE".to_string(), vec![t.to_string()]),
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   210
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   211
            Ammo(n, None) => ("AMMO".to_string(), vec![n.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   212
            Ammo(n, Some(s)) => ("AMMO".to_string(), vec![n.to_string(), s.to_string()]),
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   213
            Scheme(n, s) if s.is_empty() => ("SCHEME".to_string(), vec![n.to_string()]),
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   214
            Scheme(n, s) => ("SCHEME".to_string(), {
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   215
                let mut v = vec![n.to_string()];
15795
40929af15167 find excuses to use shiny new 🦀 features
alfadur
parents: 15532
diff changeset
   216
                v.extend(s.clone());
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   217
                v
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   218
            }),
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   219
            Script(s) => ("SCRIPT".to_string(), vec![s.to_string()]),
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   220
            Theme(t) => ("THEME".to_string(), vec![t.to_string()]),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   221
            DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]),
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   222
        }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   223
    }
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   224
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   225
    pub fn to_server_msg(&self) -> HwServerMessage {
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   226
        use self::HwServerMessage::ConfigEntry;
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   227
        let (name, args) = self.to_protocol();
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   228
        HwServerMessage::ConfigEntry(name, args)
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   229
    }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   230
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   231
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   232
impl TeamInfo {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   233
    pub fn to_protocol(&self) -> Vec<String> {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   234
        let mut info = vec![
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   235
            self.name.clone(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   236
            self.grave.clone(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   237
            self.fort.clone(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   238
            self.voice_pack.clone(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   239
            self.flag.clone(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   240
            self.owner.clone(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   241
            self.difficulty.to_string(),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   242
        ];
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   243
        let hogs = self
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   244
            .hedgehogs
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   245
            .iter()
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   246
            .flat_map(|h| once(h.name.clone()).chain(once(h.hat.clone())));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   247
        info.extend(hogs);
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   248
        info
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   249
    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   250
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   251
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   252
macro_rules! const_braces {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   253
    ($e: expr) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   254
        "{}\n"
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   255
    };
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   256
}
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   257
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   258
macro_rules! msg {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   259
    [$($part: expr),*] => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   260
        format!(concat!($(const_braces!($part)),*, "\n"), $($part),*);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   261
    };
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   262
}
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   263
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   264
impl HwProtocolMessage {
13433
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13432
diff changeset
   265
    /** Converts the message to a raw `String`, which can be sent over the network.
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13432
diff changeset
   266
     *
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13432
diff changeset
   267
     * This is the inverse of the `message` parser.
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13432
diff changeset
   268
     */
13796
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13667
diff changeset
   269
    #[cfg(test)]
13433
fb104e150878 Implement to_raw_protocol for Rnd and enable tests. Add cargo/rls build artifacts to .gitignore
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13432
diff changeset
   270
    pub(crate) fn to_raw_protocol(&self) -> String {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   271
        use self::HwProtocolMessage::*;
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   272
        match self {
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   273
            Ping => msg!["PING"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   274
            Pong => msg!["PONG"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   275
            Quit(None) => msg!["QUIT"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   276
            Quit(Some(msg)) => msg!["QUIT", msg],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   277
            Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   278
            Watch(name) => msg!["CMD", format!("WATCH {}", name)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   279
            ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   280
            SuperPower => msg!["CMD", "SUPER_POWER"],
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   281
            Info(info) => msg!["CMD", format!("INFO {}", info)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   282
            Nick(nick) => msg!("NICK", nick),
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   283
            Proto(version) => msg!["PROTO", version],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   284
            Password(p, s) => msg!["PASSWORD", p, s],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   285
            Checker(i, n, p) => msg!["CHECKER", i, n, p],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   286
            List => msg!["LIST"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   287
            Chat(msg) => msg!["CHAT", msg],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   288
            CreateRoom(name, None) => msg!["CREATE_ROOM", name],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   289
            CreateRoom(name, Some(password)) => msg!["CREATE_ROOM", name, password],
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   290
            JoinRoom(name, None) => msg!["JOIN_ROOM", name],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   291
            JoinRoom(name, Some(password)) => msg!["JOIN_ROOM", name, password],
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   292
            Follow(name) => msg!["FOLLOW", name],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   293
            Rnd(args) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   294
                if args.is_empty() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   295
                    msg!["CMD", "RND"]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   296
                } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   297
                    msg!["CMD", format!("RND {}", args.join(" "))]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   298
                }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   299
            }
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   300
            Kick(name) => msg!["KICK", name],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   301
            Ban(name, reason, time) => msg!["BAN", name, reason, time],
15124
824472aa4d97 use tuple parsers
alfadur
parents: 15112
diff changeset
   302
            BanIp(ip, reason, time) => msg!["BAN_IP", ip, reason, time],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   303
            BanNick(nick, reason, time) => msg!("BAN_NICK", nick, reason, time),
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   304
            BanList => msg!["BANLIST"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   305
            Unban(name) => msg!["UNBAN", name],
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   306
            SetServerVar(var) => construct_message(&["SET_SERVER_VAR"], &var.to_protocol()),
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   307
            GetServerVar => msg!["GET_SERVER_VAR"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   308
            RestartServer => msg!["CMD", "RESTART_SERVER YES"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   309
            Stats => msg!["CMD", "STATS"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   310
            Part(None) => msg!["PART"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   311
            Part(Some(msg)) => msg!["PART", msg],
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   312
            Cfg(config) => {
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   313
                let (name, args) = config.to_protocol();
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   314
                msg!["CFG", name, args.join("\n")]
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   315
            }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   316
            AddTeam(info) => msg![
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   317
                "ADD_TEAM",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   318
                info.name,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   319
                info.color,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   320
                info.grave,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   321
                info.fort,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   322
                info.voice_pack,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   323
                info.flag,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   324
                info.difficulty,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   325
                info.hedgehogs
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   326
                    .iter()
15795
40929af15167 find excuses to use shiny new 🦀 features
alfadur
parents: 15532
diff changeset
   327
                    .flat_map(|h| [&h.name[..], &h.hat[..]])
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   328
                    .collect::<Vec<_>>()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   329
                    .join("\n")
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   330
            ],
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   331
            RemoveTeam(name) => msg!["REMOVE_TEAM", name],
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   332
            SetHedgehogsNumber(team, number) => msg!["HH_NUM", team, number],
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   333
            SetTeamColor(team, color) => msg!["TEAM_COLOR", team, color],
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   334
            ToggleReady => msg!["TOGGLE_READY"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   335
            StartGame => msg!["START_GAME"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   336
            EngineMessage(msg) => msg!["EM", msg],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   337
            RoundFinished => msg!["ROUNDFINISHED"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   338
            ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   339
            ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   340
            ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   341
            RoomName(name) => msg!["ROOM_NAME", name],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   342
            Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   343
            TeamChat(msg) => msg!["TEAMCHAT", msg],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   344
            MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)],
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   345
            Fix => msg!["CMD", "FIX"],
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   346
            Unfix => msg!["CMD", "UNFIX"],
15111
1e45db229f9f make greeting argument optional
alfadur
parents: 15075
diff changeset
   347
            Greeting(None) => msg!["CMD", "GREETING"],
1e45db229f9f make greeting argument optional
alfadur
parents: 15075
diff changeset
   348
            Greeting(Some(msg)) => msg!["CMD", format!("GREETING {}", msg)],
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   349
            CallVote(None) => msg!["CMD", "CALLVOTE"],
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   350
            CallVote(Some(vote)) => {
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   351
                msg!["CMD", format!("CALLVOTE {}", &vote.to_protocol().join(" "))]
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   352
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   353
            Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg { "YES" } else { "NO" })],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   354
            ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg { "YES" } else { "NO" })],
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   355
            Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)],
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   356
            Delete(name) => msg!["CMD", format!("DELETE {}", name)],
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   357
            SaveRoom(name) => msg!["CMD", format!("SAVEROOM {}", name)],
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   358
            LoadRoom(name) => msg!["CMD", format!("LOADROOM {}", name)],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   359
            _ => panic!("Protocol message not yet implemented"),
13432
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   360
        }
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   361
    }
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   362
}
ee3fa3b8809d Fix cmd parsing & update tests
alfadur
parents: 13428
diff changeset
   363
13524
5359ff75da3a indulge clippy
alfadur
parents: 13520
diff changeset
   364
fn construct_message(header: &[&str], msg: &[String]) -> String {
14350
31717e1436cd recruit some newly stabilized functions
alfadur
parents: 13801
diff changeset
   365
    let mut v: Vec<_> = header.iter().cloned().collect();
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   366
    v.extend(msg.iter().map(|s| &s[..]));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   367
    v.push("\n");
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   368
    v.join("\n")
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   369
}
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   370
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   371
impl HwServerMessage {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12147
diff changeset
   372
    pub fn to_raw_protocol(&self) -> String {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   373
        use self::HwServerMessage::*;
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   374
        match self {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   375
            Ping => msg!["PING"],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   376
            Pong => msg!["PONG"],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   377
            Connected(protocol_version) => msg![
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   378
                "CONNECTED",
13667
bfc2727daead Return https
alfadur
parents: 13666
diff changeset
   379
                "Hedgewars server https://www.hedgewars.org/",
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   380
                protocol_version
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   381
            ],
14835
57ed3981db20 add redirect message
alfadur
parents: 14795
diff changeset
   382
            Redirect(port) => msg!["REDIRECT", port],
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   383
            Bye(msg) => msg!["BYE", msg],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   384
            Nick(nick) => msg!["NICK", nick],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   385
            Proto(proto) => msg!["PROTO", proto],
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14697
diff changeset
   386
            AskPassword(salt) => msg!["ASKPASSWORD", salt],
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   387
            ServerAuth(hash) => msg!["SERVER_AUTH", hash],
15532
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15124
diff changeset
   388
            LogonPassed => msg!["LOGONPASSED"],
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   389
            LobbyLeft(nick, msg) => msg!["LOBBY:LEFT", nick, msg],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   390
            LobbyJoined(nicks) => construct_message(&["LOBBY:JOINED"], &nicks),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   391
            ClientFlags(flags, nicks) => construct_message(&["CLIENT_FLAGS", flags], &nicks),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   392
            Rooms(info) => construct_message(&["ROOMS"], &info),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   393
            RoomAdd(info) => construct_message(&["ROOM", "ADD"], &info),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   394
            RoomJoined(nicks) => construct_message(&["JOINED"], &nicks),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   395
            RoomLeft(nick, msg) => msg!["LEFT", nick, msg],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   396
            RoomRemove(name) => msg!["ROOM", "DEL", name],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   397
            RoomUpdated(name, info) => construct_message(&["ROOM", "UPD", name], &info),
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14786
diff changeset
   398
            Joining(name) => msg!["JOINING", name],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   399
            TeamAdd(info) => construct_message(&["ADD_TEAM"], &info),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   400
            TeamRemove(name) => msg!["REMOVE_TEAM", name],
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   401
            TeamAccepted(name) => msg!["TEAM_ACCEPTED", name],
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   402
            TeamColor(name, color) => msg!["TEAM_COLOR", name, color],
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   403
            HedgehogsNumber(name, number) => msg!["HH_NUM", name, number],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   404
            ConfigEntry(name, values) => construct_message(&["CFG", name], &values),
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13444
diff changeset
   405
            Kicked => msg!["KICKED"],
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   406
            RunGame => msg!["RUN_GAME"],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   407
            ForwardEngineMessage(em) => construct_message(&["EM"], &em),
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   408
            RoundFinished => msg!["ROUND_FINISHED"],
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   409
            ChatMsg { nick, msg } => msg!["CHAT", nick, msg],
14786
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14785
diff changeset
   410
            Info(info) => construct_message(&["INFO"], &info),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   411
            ServerMessage(msg) => msg!["SERVER_MESSAGE", msg],
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   412
            ServerVars(vars) => construct_message(&["SERVER_VARS"], &vars),
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   413
            Notice(msg) => msg!["NOTICE", msg],
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   414
            Warning(msg) => msg!["WARNING", msg],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   415
            Error(msg) => msg!["ERROR", msg],
15112
6a1ba3540fa0 fix callvote parsing
alfadur
parents: 15111
diff changeset
   416
            ReplayStart => msg!["REPLAY_START"],
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   417
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   418
            LegacyReady(is_ready, nicks) => {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   419
                construct_message(&[if *is_ready { "READY" } else { "NOT_READY" }], &nicks)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   420
            }
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13800
diff changeset
   421
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   422
            _ => msg!["ERROR", "UNIMPLEMENTED"],
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   423
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   424
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   425
}