author | Wuzzy <almikes@aol.com> |
Wed, 05 Apr 2017 18:12:40 +0200 | |
changeset 12171 | ad5c81328b8d |
parent 12152 | 03ccb89820f3 |
child 13124 | 1e39b8749072 |
permissions | -rw-r--r-- |
12135 | 1 |
use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo}; |
12136 | 2 |
use std; |
3 |
use std::ops; |
|
4 |
use std::convert::From; |
|
12135 | 5 |
|
12136 | 6 |
#[derive(PartialEq, Debug)] |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
7 |
pub enum HWProtocolMessage { |
12135 | 8 |
// core |
9 |
Ping, |
|
10 |
Pong, |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
11 |
Quit(Option<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
12 |
//Cmd(String, Vec<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
13 |
Global(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
14 |
Watch(String), |
12135 | 15 |
ToggleServerRegisteredOnly, |
16 |
SuperPower, |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
17 |
Info(String), |
12135 | 18 |
// not entered state |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
19 |
Nick(String), |
12135 | 20 |
Proto(u32), |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
21 |
Password(String, String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
22 |
Checker(u32, String, String), |
12135 | 23 |
// lobby |
24 |
List, |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
25 |
Chat(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
26 |
CreateRoom(String, Option<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
27 |
Join(String, Option<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
28 |
Follow(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
29 |
Rnd(Vec<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
30 |
Kick(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
31 |
Ban(String, String, u32), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
32 |
BanIP(String, String, u32), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
33 |
BanNick(String, String, u32), |
12135 | 34 |
BanList, |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
35 |
Unban(String), |
12135 | 36 |
SetServerVar(ServerVar), |
37 |
GetServerVar, |
|
38 |
RestartServer, |
|
39 |
Stats, |
|
40 |
// in room |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
41 |
Part(Option<String>), |
12135 | 42 |
Cfg(GameCfg), |
43 |
AddTeam(TeamInfo), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
44 |
RemoveTeam(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
45 |
SetHedgehogsNumber(String, u8), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
46 |
SetTeamColor(String, u8), |
12135 | 47 |
ToggleReady, |
48 |
StartGame, |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
49 |
EngineMessage(String), |
12135 | 50 |
RoundFinished, |
51 |
ToggleRestrictJoin, |
|
52 |
ToggleRestrictTeams, |
|
53 |
ToggleRegisteredOnly, |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
54 |
RoomName(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
55 |
Delegate(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
56 |
TeamChat(String), |
12135 | 57 |
MaxTeams(u8), |
58 |
Fix, |
|
59 |
Unfix, |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
60 |
Greeting(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
61 |
CallVote(Option<(String, Option<String>)>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
62 |
Vote(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
63 |
ForceVote(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
64 |
Save(String, String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
65 |
Delete(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
66 |
SaveRoom(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
67 |
LoadRoom(String), |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
68 |
Malformed, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
69 |
Empty, |
12135 | 70 |
} |
12136 | 71 |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
72 |
pub enum HWServerMessage<'a> { |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
73 |
Ping, |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
74 |
Pong, |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
75 |
Bye(&'a str), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
76 |
Nick(&'a str), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
77 |
LobbyLeft(&'a str), |
12150 | 78 |
LobbyJoined(&'a [&'a str]), |
12152 | 79 |
ChatMsg(&'a str, &'a str), |
80 |
ClientFlags(&'a str, &'a [&'a str]), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
81 |
|
12152 | 82 |
Warning(&'a str), |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
83 |
Connected(u32), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
84 |
Unreachable, |
12136 | 85 |
} |
12141 | 86 |
|
87 |
fn construct_message(msg: & [&str]) -> String { |
|
88 |
let mut m = String::with_capacity(64); |
|
89 |
||
90 |
for s in msg { |
|
91 |
m.push_str(s); |
|
92 |
m.push('\n'); |
|
93 |
} |
|
94 |
m.push('\n'); |
|
95 |
||
96 |
m |
|
97 |
} |
|
98 |
||
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
99 |
impl<'a> HWServerMessage<'a> { |
12141 | 100 |
pub fn to_raw_protocol(&self) -> String { |
101 |
match self { |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
102 |
&HWServerMessage::Ping |
12141 | 103 |
=> "PING\n\n".to_string(), |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
104 |
&HWServerMessage::Pong |
12141 | 105 |
=> "PONG\n\n".to_string(), |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
106 |
&HWServerMessage::Connected(protocol_version) |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
107 |
=> construct_message(&[ |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
108 |
"CONNECTED", |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
109 |
"Hedgewars server http://www.hedgewars.org/", |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
110 |
&protocol_version.to_string() |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
111 |
]), |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
112 |
&HWServerMessage::Bye(msg) |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
113 |
=> construct_message(&["BYE", &msg]), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
114 |
&HWServerMessage::Nick(nick) |
12152 | 115 |
=> construct_message(&["NICK", &nick]), |
116 |
&HWServerMessage::LobbyLeft(nick) |
|
117 |
=> construct_message(&["LOBBY_LEFT", &nick]), |
|
118 |
&HWServerMessage::LobbyJoined(nicks) |
|
12150 | 119 |
=> { |
120 |
let mut v = vec!["LOBBY:JOINED"]; |
|
12152 | 121 |
v.extend_from_slice(nicks); |
12150 | 122 |
construct_message(&v) |
123 |
}, |
|
12152 | 124 |
&HWServerMessage::ClientFlags(flags, nicks) |
125 |
=> { |
|
126 |
let mut v = vec!["CLIENT_FLAGS"]; |
|
127 |
v.push(flags); |
|
128 |
v.extend_from_slice(nicks); |
|
129 |
construct_message(&v) |
|
130 |
}, |
|
131 |
&HWServerMessage::ChatMsg(nick, msg) |
|
132 |
=> construct_message(&["CHAT", &nick, &msg]), |
|
133 |
&HWServerMessage::Warning(msg) |
|
134 |
=> construct_message(&["WARNING", &msg]), |
|
12146 | 135 |
_ => construct_message(&["ERROR", "UNIMPLEMENTED"]), |
12141 | 136 |
} |
137 |
} |
|
138 |
} |