author | alfadur |
Mon, 02 Jul 2018 16:25:49 +0300 | |
changeset 13432 | ee3fa3b8809d |
parent 13428 | c8425fbcf1d9 |
child 13433 | fb104e150878 |
permissions | -rw-r--r-- |
12130 | 1 |
use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo}; |
12131 | 2 |
use std; |
3 |
use std::ops; |
|
4 |
use std::convert::From; |
|
12130 | 5 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
6 |
#[derive(PartialEq, Eq, Clone, Debug)] |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
7 |
pub enum HWProtocolMessage { |
12130 | 8 |
// core |
9 |
Ping, |
|
10 |
Pong, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
11 |
Quit(Option<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
12 |
//Cmd(String, Vec<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
13 |
Global(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
14 |
Watch(String), |
12130 | 15 |
ToggleServerRegisteredOnly, |
16 |
SuperPower, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
17 |
Info(String), |
12130 | 18 |
// not entered state |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
19 |
Nick(String), |
12130 | 20 |
Proto(u32), |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
21 |
Password(String, String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
22 |
Checker(u32, String, String), |
12130 | 23 |
// lobby |
24 |
List, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
25 |
Chat(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
26 |
CreateRoom(String, Option<String>), |
13416 | 27 |
JoinRoom(String, Option<String>), |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
28 |
Follow(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
29 |
Rnd(Vec<String>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
30 |
Kick(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
31 |
Ban(String, String, u32), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
32 |
BanIP(String, String, u32), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
33 |
BanNick(String, String, u32), |
12130 | 34 |
BanList, |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
35 |
Unban(String), |
12130 | 36 |
SetServerVar(ServerVar), |
37 |
GetServerVar, |
|
38 |
RestartServer, |
|
39 |
Stats, |
|
40 |
// in room |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
41 |
Part(Option<String>), |
12130 | 42 |
Cfg(GameCfg), |
43 |
AddTeam(TeamInfo), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
44 |
RemoveTeam(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
45 |
SetHedgehogsNumber(String, u8), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
46 |
SetTeamColor(String, u8), |
12130 | 47 |
ToggleReady, |
48 |
StartGame, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
49 |
EngineMessage(String), |
12130 | 50 |
RoundFinished, |
51 |
ToggleRestrictJoin, |
|
52 |
ToggleRestrictTeams, |
|
53 |
ToggleRegisteredOnly, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
54 |
RoomName(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
55 |
Delegate(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
56 |
TeamChat(String), |
12130 | 57 |
MaxTeams(u8), |
58 |
Fix, |
|
59 |
Unfix, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
60 |
Greeting(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
61 |
CallVote(Option<(String, Option<String>)>), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
62 |
Vote(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
63 |
ForceVote(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
64 |
Save(String, String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
65 |
Delete(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
66 |
SaveRoom(String), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
67 |
LoadRoom(String), |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
68 |
Malformed, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
69 |
Empty, |
12130 | 70 |
} |
12131 | 71 |
|
13416 | 72 |
#[derive(Debug)] |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
73 |
pub enum HWServerMessage { |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
74 |
Ping, |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
75 |
Pong, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
76 |
Bye(String), |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
77 |
Nick(String), |
13416 | 78 |
Proto(u32), |
79 |
LobbyLeft(String, String), |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
80 |
LobbyJoined(Vec<String>), |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
81 |
ChatMsg(String, String), |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
82 |
ClientFlags(String, Vec<String>), |
13416 | 83 |
Rooms(Vec<String>), |
84 |
RoomAdd(Vec<String>), |
|
85 |
RoomJoined(Vec<String>), |
|
86 |
RoomLeft(String, String), |
|
87 |
RoomRemove(String), |
|
88 |
RoomUpdated(String, Vec<String>), |
|
13419 | 89 |
TeamAdd(Vec<String>), |
90 |
TeamRemove(String), |
|
91 |
TeamAccepted(String), |
|
92 |
TeamColor(String, u8), |
|
93 |
HedgehogsNumber(String, u8), |
|
13422 | 94 |
ConfigEntry(String, Vec<String>), |
13423 | 95 |
RunGame, |
13428 | 96 |
ForwardEngineMessage(Vec<String>), |
13423 | 97 |
RoundFinished, |
13419 | 98 |
|
13416 | 99 |
ServerMessage(String), |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
100 |
Warning(String), |
13416 | 101 |
Error(String), |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
102 |
Connected(u32), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
103 |
Unreachable, |
12131 | 104 |
} |
12136 | 105 |
|
13422 | 106 |
impl GameCfg { |
107 |
pub fn into_server_msg(self) -> HWServerMessage { |
|
108 |
use self::HWServerMessage::ConfigEntry; |
|
109 |
use server::coretypes::GameCfg::*; |
|
110 |
match self { |
|
111 |
FeatureSize(s) => ConfigEntry("FEATURE_SIZE".to_string(), vec![s.to_string()]), |
|
112 |
MapType(t) => ConfigEntry("MAP".to_string(), vec![t.to_string()]), |
|
113 |
MapGenerator(g) => ConfigEntry("MAPGEN".to_string(), vec![g.to_string()]), |
|
114 |
MazeSize(s) => ConfigEntry("MAZE_SIZE".to_string(), vec![s.to_string()]), |
|
115 |
Seed(s) => ConfigEntry("SEED".to_string(), vec![s.to_string()]), |
|
116 |
Template(t) => ConfigEntry("TEMPLATE".to_string(), vec![t.to_string()]), |
|
117 |
||
118 |
Ammo(n, None) => ConfigEntry("AMMO".to_string(), vec![n.to_string()]), |
|
119 |
Ammo(n, Some(s)) => ConfigEntry("AMMO".to_string(), vec![n.to_string(), s.to_string()]), |
|
120 |
Scheme(n, None) => ConfigEntry("SCHEME".to_string(), vec![n.to_string()]), |
|
121 |
Scheme(n, Some(s)) => ConfigEntry("SCHEME".to_string(), { |
|
122 |
let mut v = vec![n.to_string()]; |
|
123 |
v.extend(s.into_iter()); |
|
124 |
v |
|
125 |
}), |
|
126 |
Script(s) => ConfigEntry("SCRIPT".to_string(), vec![s.to_string()]), |
|
127 |
Theme(t) => ConfigEntry("THEME".to_string(), vec![t.to_string()]), |
|
128 |
DrawnMap(m) => ConfigEntry("DRAWNMAP".to_string(), vec![m.to_string()]) |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
||
13416 | 133 |
macro_rules! const_braces { |
134 |
($e: expr) => { "{}\n" } |
|
135 |
} |
|
136 |
||
137 |
macro_rules! msg { |
|
138 |
[$($part: expr),*] => { |
|
139 |
format!(concat!($(const_braces!($part)),*, "\n"), $($part),*); |
|
140 |
}; |
|
141 |
} |
|
142 |
||
13432 | 143 |
impl<'a> HWProtocolMessage { |
144 |
pub fn to_raw_protocol(&self) -> String { |
|
145 |
use self::HWProtocolMessage::*; |
|
146 |
match self { |
|
147 |
Ping => msg!["PING"], |
|
148 |
Pong => msg!["PONG"], |
|
149 |
Quit(None) => msg!["QUIT"], |
|
150 |
Quit(Some(msg)) => msg!["QUIT", msg], |
|
151 |
Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)], |
|
152 |
Watch(name) => msg!["CMD", format!("WATCH {}", name)], |
|
153 |
ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"], |
|
154 |
SuperPower => msg!["CMD", "SUPER_POWER"], |
|
155 |
Info(info) => msg!["CMD", format!("INFO {}", info)], |
|
156 |
Nick(nick) => msg!("NICK", nick), |
|
157 |
Proto(version) => msg!["PROTO", version], |
|
158 |
Password(p, s) => msg!["PASSWORD", p, s], |
|
159 |
Checker(i, n, p) => msg!["CHECKER", i, n, p], |
|
160 |
List => msg!["LIST"], |
|
161 |
Chat(msg) => msg!["CHAT", msg], |
|
162 |
CreateRoom(name, None) => msg!["CREATE_ROOM", name], |
|
163 |
CreateRoom(name, Some(password)) => |
|
164 |
msg!["CREATE_ROOM", name, password], |
|
165 |
JoinRoom(name, None) => msg!["JOIN_ROOM", name], |
|
166 |
JoinRoom(name, Some(password)) => |
|
167 |
msg!["JOIN_ROOM", name, password], |
|
168 |
Follow(name) => msg!["FOLLOW", name], |
|
169 |
Rnd(args) => msg!["RND", args.join(" ")], |
|
170 |
Kick(name) => msg!["KICK", name], |
|
171 |
Ban(name, reason, time) => msg!["BAN", name, reason, time], |
|
172 |
BanIP(ip, reason, time) => msg!["BAN_IP", ip, reason, time], |
|
173 |
BanNick(nick, reason, time) => |
|
174 |
msg!("BAN_NICK", nick, reason, time), |
|
175 |
BanList => msg!["BANLIST"], |
|
176 |
Unban(name) => msg!["UNBAN", name], |
|
177 |
//SetServerVar(ServerVar), ??? |
|
178 |
GetServerVar => msg!["GET_SERVER_VAR"], |
|
179 |
RestartServer => msg!["CMD", "RESTART_SERVER YES"], |
|
180 |
Stats => msg!["CMD", "STATS"], |
|
181 |
Part(None) => msg!["PART"], |
|
182 |
Part(Some(msg)) => msg!["PART", msg], |
|
183 |
//Cfg(GameCfg) => |
|
184 |
//AddTeam(info) => |
|
185 |
RemoveTeam(name) => msg!["REMOVE_TEAM", name], |
|
186 |
//SetHedgehogsNumber(team, number), ?? |
|
187 |
//SetTeamColor(team, color), ?? |
|
188 |
ToggleReady => msg!["TOGGLE_READY"], |
|
189 |
StartGame => msg!["START_GAME"], |
|
190 |
EngineMessage(msg) => msg!["EM", msg], |
|
191 |
RoundFinished => msg!["ROUNDFINISHED"], |
|
192 |
ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"], |
|
193 |
ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"], |
|
194 |
ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"], |
|
195 |
RoomName(name) => msg!["ROOM_NAME", name], |
|
196 |
Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)], |
|
197 |
TeamChat(msg) => msg!["TEAMCHAT", msg], |
|
198 |
MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)] , |
|
199 |
Fix => msg!["CMD", "FIX"], |
|
200 |
Unfix => msg!["CMD", "UNFIX"], |
|
201 |
Greeting(msg) => msg!["CMD", format!("GREETING {}", msg)], |
|
202 |
//CallVote(Option<(String, Option<String>)>) =>, ?? |
|
203 |
Vote(msg) => msg!["CMD", format!("VOTE {}", msg)], |
|
204 |
ForceVote(msg) => msg!["CMD", format!("FORCE {}", msg)], |
|
205 |
//Save(String, String), ?? |
|
206 |
Delete(room) => msg!["CMD", format!("DELETE {}", room)], |
|
207 |
SaveRoom(room) => msg!["CMD", format!("SAVEROOM {}", room)], |
|
208 |
LoadRoom(room) => msg!["CMD", format!("LOADROOM {}", room)], |
|
209 |
Malformed => msg!["A", "QUICK", "BROWN", "HOG", "JUMPS", "OVER", "THE", "LAZY", "DOG"], |
|
210 |
Empty => msg![""], |
|
211 |
_ => panic!("Protocol message not yet implemented") |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
||
13419 | 216 |
fn construct_message(header: &[&str], msg: &Vec<String>) -> String { |
217 |
let mut v: Vec<_> = header.iter().map(|s| *s).collect(); |
|
218 |
v.extend(msg.iter().map(|s| &s[..])); |
|
219 |
v.push("\n"); |
|
220 |
v.join("\n") |
|
13416 | 221 |
} |
222 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
223 |
impl HWServerMessage { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
224 |
pub fn to_raw_protocol(&self) -> String { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
225 |
use self::HWServerMessage::*; |
12136 | 226 |
match self { |
13416 | 227 |
Ping => msg!["PING"], |
228 |
Pong => msg!["PONG"], |
|
229 |
Connected(protocol_version) => msg![ |
|
230 |
"CONNECTED", |
|
231 |
"Hedgewars server http://www.hedgewars.org/", |
|
232 |
protocol_version], |
|
233 |
Bye(msg) => msg!["BYE", msg], |
|
234 |
Nick(nick) => msg!["NICK", nick], |
|
235 |
Proto(proto) => msg!["PROTO", proto], |
|
236 |
LobbyLeft(nick, msg) => msg!["LOBBY:LEFT", nick, msg], |
|
13419 | 237 |
LobbyJoined(nicks) => |
238 |
construct_message(&["LOBBY:JOINED"], &nicks), |
|
239 |
ClientFlags(flags, nicks) => |
|
240 |
construct_message(&["CLIENT_FLAGS", flags], &nicks), |
|
241 |
Rooms(info) => |
|
242 |
construct_message(&["ROOMS"], &info), |
|
243 |
RoomAdd(info) => |
|
244 |
construct_message(&["ROOM", "ADD"], &info), |
|
245 |
RoomJoined(nicks) => |
|
246 |
construct_message(&["JOINED"], &nicks), |
|
13416 | 247 |
RoomLeft(nick, msg) => msg!["LEFT", nick, msg], |
248 |
RoomRemove(name) => msg!["ROOM", "DEL", name], |
|
13419 | 249 |
RoomUpdated(name, info) => |
250 |
construct_message(&["ROOM", "UPD", name], &info), |
|
251 |
TeamAdd(info) => |
|
252 |
construct_message(&["ADD_TEAM"], &info), |
|
253 |
TeamRemove(name) => msg!["REMOVE_TEAM", name], |
|
254 |
TeamAccepted(name) => msg!["TEAM_ACCEPTED", name], |
|
255 |
TeamColor(name, color) => msg!["TEAM_COLOR", name, color], |
|
256 |
HedgehogsNumber(name, number) => msg!["HH_NUM", name, number], |
|
13422 | 257 |
ConfigEntry(name, values) => |
258 |
construct_message(&["CFG", name], &values), |
|
13423 | 259 |
RunGame => msg!["RUN_GAME"], |
13428 | 260 |
ForwardEngineMessage(em) => |
261 |
construct_message(&["EM"], &em), |
|
13423 | 262 |
RoundFinished => msg!["ROUND_FINISHED"], |
13416 | 263 |
ChatMsg(nick, msg) => msg!["CHAT", nick, msg], |
264 |
ServerMessage(msg) => msg!["SERVER_MESSAGE", msg], |
|
265 |
Warning(msg) => msg!["WARNING", msg], |
|
266 |
Error(msg) => msg!["ERROR", msg], |
|
267 |
_ => msg!["ERROR", "UNIMPLEMENTED"], |
|
12136 | 268 |
} |
269 |
} |
|
270 |
} |