12147
|
1 |
use mio;
|
|
2 |
|
13416
|
3 |
use protocol::messages::{
|
|
4 |
HWProtocolMessage,
|
|
5 |
HWServerMessage::*
|
|
6 |
};
|
13419
|
7 |
use server::{
|
|
8 |
server::HWServer,
|
|
9 |
client::ClientId,
|
|
10 |
room::HWRoom,
|
|
11 |
actions::{Action, Action::*}
|
|
12 |
};
|
13416
|
13 |
use utils::is_name_illegal;
|
|
14 |
use std::mem::swap;
|
13423
|
15 |
use base64::{encode, decode};
|
|
16 |
|
|
17 |
#[derive(Clone)]
|
|
18 |
struct ByMsg<'a> {
|
|
19 |
messages: &'a[u8]
|
|
20 |
}
|
|
21 |
|
|
22 |
impl <'a> Iterator for ByMsg<'a> {
|
|
23 |
type Item = &'a[u8];
|
|
24 |
|
|
25 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
|
|
26 |
if let Some(size) = self.messages.get(0) {
|
|
27 |
let (msg, next) = self.messages.split_at(*size as usize + 1);
|
|
28 |
self.messages = next;
|
|
29 |
Some(msg)
|
|
30 |
} else {
|
|
31 |
None
|
|
32 |
}
|
|
33 |
}
|
|
34 |
}
|
|
35 |
|
|
36 |
fn by_msg(source: &Vec<u8>) -> ByMsg {
|
|
37 |
ByMsg {messages: &source[..]}
|
|
38 |
}
|
|
39 |
|
|
40 |
const VALID_MESSAGES: &[u8] =
|
|
41 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A";
|
|
42 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb";
|
|
43 |
|
13429
|
44 |
#[cfg(canhazslicepatterns)]
|
13423
|
45 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
|
13424
|
46 |
match msg {
|
|
47 |
[size, typ, body..] => VALID_MESSAGES.contains(typ)
|
|
48 |
&& match body {
|
13423
|
49 |
[1...8, team, ..] if *typ == b'h' => team_indices.contains(team),
|
|
50 |
_ => *typ != b'h'
|
13424
|
51 |
},
|
|
52 |
_ => false
|
13423
|
53 |
}
|
|
54 |
}
|
|
55 |
|
13429
|
56 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
|
|
57 |
if let Some(typ) = msg.get(1) {
|
|
58 |
VALID_MESSAGES.contains(typ)
|
|
59 |
} else {
|
|
60 |
false
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
13423
|
64 |
fn is_msg_empty(msg: &[u8]) -> bool {
|
13429
|
65 |
msg.get(1).filter(|t| **t == b'+').is_some()
|
13423
|
66 |
}
|
12147
|
67 |
|
13419
|
68 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
|
13416
|
69 |
use protocol::messages::HWProtocolMessage::*;
|
12147
|
70 |
match message {
|
13419
|
71 |
Part(None) => server.react(client_id, vec![
|
13416
|
72 |
MoveToLobby("part".to_string())]),
|
13419
|
73 |
Part(Some(msg)) => server.react(client_id, vec![
|
13416
|
74 |
MoveToLobby(format!("part: {}", msg))]),
|
|
75 |
Chat(msg) => {
|
13419
|
76 |
let actions = {
|
|
77 |
let c = &mut server.clients[client_id];
|
|
78 |
let chat_msg = ChatMsg(c.nick.clone(), msg);
|
|
79 |
if let Some(room_id) = c.room_id {
|
|
80 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()]
|
|
81 |
} else {
|
|
82 |
Vec::new()
|
|
83 |
}
|
|
84 |
};
|
|
85 |
server.react(client_id, actions);
|
13416
|
86 |
},
|
|
87 |
RoomName(new_name) => {
|
|
88 |
let actions =
|
|
89 |
if is_name_illegal(&new_name) {
|
|
90 |
vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
|
|
91 |
} else if server.has_room(&new_name) {
|
|
92 |
vec![Warn("A room with the same name already exists.".to_string())]
|
|
93 |
} else {
|
|
94 |
let mut old_name = new_name.clone();
|
13422
|
95 |
if let (_, Some(r)) = server.client_and_room(client_id) {
|
13416
|
96 |
swap(&mut r.name, &mut old_name);
|
|
97 |
vec![SendRoomUpdate(Some(old_name))]
|
|
98 |
} else {
|
|
99 |
Vec::new()
|
|
100 |
}
|
|
101 |
};
|
13419
|
102 |
server.react(client_id, actions);
|
|
103 |
},
|
|
104 |
ToggleReady => {
|
|
105 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
106 |
let flags = if c.is_ready {
|
|
107 |
r.ready_players_number -= 1;
|
|
108 |
"-r"
|
|
109 |
} else {
|
|
110 |
r.ready_players_number += 1;
|
|
111 |
"+r"
|
|
112 |
};
|
|
113 |
c.is_ready = !c.is_ready;
|
|
114 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
|
|
115 |
.send_all().in_room(r.id).action()]
|
|
116 |
} else {
|
|
117 |
Vec::new()
|
|
118 |
};
|
|
119 |
server.react(client_id, actions);
|
13416
|
120 |
}
|
13422
|
121 |
AddTeam(info) => {
|
13419
|
122 |
let mut actions = Vec::new();
|
|
123 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
124 |
let room_id = r.id;
|
|
125 |
if r.teams.len() >= r.team_limit as usize {
|
|
126 |
actions.push(Warn("Too many teams!".to_string()))
|
|
127 |
} else if r.addable_hedgehogs() == 0 {
|
|
128 |
actions.push(Warn("Too many hedgehogs!".to_string()))
|
|
129 |
} else if r.find_team(|t| t.name == info.name) != None {
|
|
130 |
actions.push(Warn("There's already a team with same name in the list.".to_string()))
|
13423
|
131 |
} else if r.game_info.is_some() {
|
13419
|
132 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
|
|
133 |
} else {
|
|
134 |
let team = r.add_team(c.id, info);
|
|
135 |
c.teams_in_game += 1;
|
|
136 |
c.clan = Some(team.color);
|
|
137 |
actions.push(TeamAccepted(team.name.clone())
|
|
138 |
.send_self().action());
|
|
139 |
actions.push(TeamAdd(HWRoom::team_info(&c, team))
|
|
140 |
.send_all().in_room(room_id).but_self().action());
|
|
141 |
actions.push(TeamColor(team.name.clone(), team.color)
|
|
142 |
.send_all().in_room(room_id).action());
|
|
143 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
|
|
144 |
.send_all().in_room(room_id).action());
|
|
145 |
actions.push(SendRoomUpdate(None));
|
|
146 |
}
|
|
147 |
}
|
|
148 |
server.react(client_id, actions);
|
|
149 |
},
|
|
150 |
RemoveTeam(name) => {
|
|
151 |
let mut actions = Vec::new();
|
|
152 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
153 |
match r.find_team_owner(&name) {
|
|
154 |
None =>
|
|
155 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())),
|
|
156 |
Some((id, _)) if id != client_id =>
|
|
157 |
actions.push(Warn("You can't remove a team you don't own.".to_string())),
|
|
158 |
Some((_, name)) => {
|
|
159 |
c.teams_in_game -= 1;
|
|
160 |
c.clan = r.find_team_color(c.id);
|
|
161 |
actions.push(Action::RemoveTeam(name.to_string()));
|
|
162 |
}
|
|
163 |
}
|
|
164 |
};
|
|
165 |
server.react(client_id, actions);
|
|
166 |
},
|
|
167 |
SetHedgehogsNumber(team_name, number) => {
|
|
168 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
169 |
let room_id = r.id;
|
|
170 |
let addable_hedgehogs = r.addable_hedgehogs();
|
|
171 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
|
|
172 |
if !c.is_master {
|
|
173 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
174 |
} else if number < 1 || number > 8
|
|
175 |
|| number > addable_hedgehogs + team.hedgehogs_number {
|
|
176 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
|
|
177 |
.send_self().action()]
|
|
178 |
} else {
|
|
179 |
team.hedgehogs_number = number;
|
|
180 |
vec![HedgehogsNumber(team.name.clone(), number)
|
|
181 |
.send_all().in_room(room_id).but_self().action()]
|
|
182 |
}
|
|
183 |
} else {
|
|
184 |
vec![(Warn("No such team.".to_string()))]
|
|
185 |
}
|
|
186 |
} else {
|
|
187 |
Vec::new()
|
|
188 |
};
|
|
189 |
server.react(client_id, actions);
|
|
190 |
},
|
|
191 |
SetTeamColor(team_name, color) => {
|
|
192 |
let mut owner_id = None;
|
|
193 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
194 |
let room_id = r.id;
|
|
195 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
|
|
196 |
if !c.is_master {
|
|
197 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
198 |
} else if false {
|
|
199 |
Vec::new()
|
|
200 |
} else {
|
|
201 |
owner_id = Some(owner);
|
|
202 |
team.color = color;
|
|
203 |
vec![TeamColor(team.name.clone(), color)
|
|
204 |
.send_all().in_room(room_id).but_self().action()]
|
|
205 |
}
|
|
206 |
} else {
|
|
207 |
vec![(Warn("No such team.".to_string()))]
|
|
208 |
}
|
|
209 |
} else {
|
|
210 |
Vec::new()
|
|
211 |
};
|
|
212 |
|
|
213 |
if let Some(id) = owner_id {
|
|
214 |
server.clients[id].clan = Some(color);
|
|
215 |
}
|
|
216 |
|
|
217 |
server.react(client_id, actions);
|
13422
|
218 |
},
|
|
219 |
Cfg(cfg) => {
|
|
220 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
221 |
if !c.is_master {
|
|
222 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
223 |
} else {
|
13439
|
224 |
let v = vec![cfg.to_server_msg()
|
|
225 |
.send_all().in_room(r.id).but_self().action()];
|
|
226 |
r.set_config(cfg);
|
|
227 |
v
|
13422
|
228 |
}
|
|
229 |
} else {
|
|
230 |
Vec::new()
|
|
231 |
};
|
|
232 |
server.react(client_id, actions);
|
13419
|
233 |
}
|
13423
|
234 |
StartGame => {
|
|
235 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) {
|
|
236 |
vec![StartRoomGame(r.id)]
|
|
237 |
} else {
|
|
238 |
Vec::new()
|
|
239 |
};
|
|
240 |
server.react(client_id, actions);
|
|
241 |
}
|
|
242 |
EngineMessage(em) => {
|
|
243 |
let mut actions = Vec::new();
|
|
244 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
245 |
if c.teams_in_game > 0 {
|
|
246 |
let decoding = decode(&em[..]).unwrap();
|
|
247 |
let messages = by_msg(&decoding);
|
|
248 |
let valid = messages.clone().filter(|m| is_msg_valid(m, &c.team_indices));
|
13427
|
249 |
let non_empty = messages.filter(|m| !is_msg_empty(m));
|
13429
|
250 |
let last_msg = None;
|
13423
|
251 |
|
|
252 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
|
|
253 |
if !em_response.is_empty() {
|
13428
|
254 |
actions.push(ForwardEngineMessage(vec![em_response])
|
13423
|
255 |
.send_all().in_room(r.id).but_self().action());
|
|
256 |
}
|
13427
|
257 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
|
|
258 |
if let Some(ref mut info) = r.game_info {
|
13429
|
259 |
if !em_log.is_empty() {
|
13428
|
260 |
info.msg_log.push(em_log);
|
|
261 |
}
|
13427
|
262 |
if last_msg.is_some() {
|
|
263 |
info.last_msg = last_msg;
|
|
264 |
}
|
|
265 |
}
|
13423
|
266 |
}
|
|
267 |
}
|
|
268 |
server.react(client_id, actions)
|
|
269 |
}
|
|
270 |
RoundFinished => {
|
|
271 |
let mut actions = Vec::new();
|
|
272 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
273 |
if c.is_in_game {
|
|
274 |
c.is_in_game = false;
|
|
275 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]).
|
|
276 |
send_all().in_room(r.id).action());
|
13426
|
277 |
if r.game_info.is_some() {
|
|
278 |
for team in r.client_teams(c.id) {
|
|
279 |
actions.push(SendTeamRemovalMessage(team.name.clone()));
|
|
280 |
}
|
13423
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
server.react(client_id, actions)
|
|
285 |
}
|
13419
|
286 |
_ => warn!("Unimplemented!")
|
12147
|
287 |
}
|
|
288 |
}
|