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 |
|
|
44 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
|
|
45 |
if let [size, typ, body..] = msg {
|
|
46 |
VALID_MESSAGES.contains(typ) &&
|
|
47 |
match body {
|
|
48 |
[1...8, team, ..] if *typ == b'h' => team_indices.contains(team),
|
|
49 |
_ => *typ != b'h'
|
|
50 |
}
|
|
51 |
} else {
|
|
52 |
false
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
fn is_msg_empty(msg: &[u8]) -> bool {
|
|
57 |
match msg {
|
|
58 |
[_, b'+', ..] => true,
|
|
59 |
_ => false
|
|
60 |
}
|
|
61 |
}
|
12147
|
62 |
|
13419
|
63 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
|
13416
|
64 |
use protocol::messages::HWProtocolMessage::*;
|
12147
|
65 |
match message {
|
13419
|
66 |
Part(None) => server.react(client_id, vec![
|
13416
|
67 |
MoveToLobby("part".to_string())]),
|
13419
|
68 |
Part(Some(msg)) => server.react(client_id, vec![
|
13416
|
69 |
MoveToLobby(format!("part: {}", msg))]),
|
|
70 |
Chat(msg) => {
|
13419
|
71 |
let actions = {
|
|
72 |
let c = &mut server.clients[client_id];
|
|
73 |
let chat_msg = ChatMsg(c.nick.clone(), msg);
|
|
74 |
if let Some(room_id) = c.room_id {
|
|
75 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()]
|
|
76 |
} else {
|
|
77 |
Vec::new()
|
|
78 |
}
|
|
79 |
};
|
|
80 |
server.react(client_id, actions);
|
13416
|
81 |
},
|
|
82 |
RoomName(new_name) => {
|
|
83 |
let actions =
|
|
84 |
if is_name_illegal(&new_name) {
|
|
85 |
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())]
|
|
86 |
} else if server.has_room(&new_name) {
|
|
87 |
vec![Warn("A room with the same name already exists.".to_string())]
|
|
88 |
} else {
|
|
89 |
let mut old_name = new_name.clone();
|
13422
|
90 |
if let (_, Some(r)) = server.client_and_room(client_id) {
|
13416
|
91 |
swap(&mut r.name, &mut old_name);
|
|
92 |
vec![SendRoomUpdate(Some(old_name))]
|
|
93 |
} else {
|
|
94 |
Vec::new()
|
|
95 |
}
|
|
96 |
};
|
13419
|
97 |
server.react(client_id, actions);
|
|
98 |
},
|
|
99 |
ToggleReady => {
|
|
100 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
101 |
let flags = if c.is_ready {
|
|
102 |
r.ready_players_number -= 1;
|
|
103 |
"-r"
|
|
104 |
} else {
|
|
105 |
r.ready_players_number += 1;
|
|
106 |
"+r"
|
|
107 |
};
|
|
108 |
c.is_ready = !c.is_ready;
|
|
109 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
|
|
110 |
.send_all().in_room(r.id).action()]
|
|
111 |
} else {
|
|
112 |
Vec::new()
|
|
113 |
};
|
|
114 |
server.react(client_id, actions);
|
13416
|
115 |
}
|
13422
|
116 |
AddTeam(info) => {
|
13419
|
117 |
let mut actions = Vec::new();
|
|
118 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
119 |
let room_id = r.id;
|
|
120 |
if r.teams.len() >= r.team_limit as usize {
|
|
121 |
actions.push(Warn("Too many teams!".to_string()))
|
|
122 |
} else if r.addable_hedgehogs() == 0 {
|
|
123 |
actions.push(Warn("Too many hedgehogs!".to_string()))
|
|
124 |
} else if r.find_team(|t| t.name == info.name) != None {
|
|
125 |
actions.push(Warn("There's already a team with same name in the list.".to_string()))
|
13423
|
126 |
} else if r.game_info.is_some() {
|
13419
|
127 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
|
|
128 |
} else {
|
|
129 |
let team = r.add_team(c.id, info);
|
|
130 |
c.teams_in_game += 1;
|
|
131 |
c.clan = Some(team.color);
|
|
132 |
actions.push(TeamAccepted(team.name.clone())
|
|
133 |
.send_self().action());
|
|
134 |
actions.push(TeamAdd(HWRoom::team_info(&c, team))
|
|
135 |
.send_all().in_room(room_id).but_self().action());
|
|
136 |
actions.push(TeamColor(team.name.clone(), team.color)
|
|
137 |
.send_all().in_room(room_id).action());
|
|
138 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
|
|
139 |
.send_all().in_room(room_id).action());
|
|
140 |
actions.push(SendRoomUpdate(None));
|
|
141 |
}
|
|
142 |
}
|
|
143 |
server.react(client_id, actions);
|
|
144 |
},
|
|
145 |
RemoveTeam(name) => {
|
|
146 |
let mut actions = Vec::new();
|
|
147 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
148 |
match r.find_team_owner(&name) {
|
|
149 |
None =>
|
|
150 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())),
|
|
151 |
Some((id, _)) if id != client_id =>
|
|
152 |
actions.push(Warn("You can't remove a team you don't own.".to_string())),
|
|
153 |
Some((_, name)) => {
|
|
154 |
c.teams_in_game -= 1;
|
|
155 |
c.clan = r.find_team_color(c.id);
|
|
156 |
actions.push(Action::RemoveTeam(name.to_string()));
|
|
157 |
}
|
|
158 |
}
|
|
159 |
};
|
|
160 |
server.react(client_id, actions);
|
|
161 |
},
|
|
162 |
SetHedgehogsNumber(team_name, number) => {
|
|
163 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
164 |
let room_id = r.id;
|
|
165 |
let addable_hedgehogs = r.addable_hedgehogs();
|
|
166 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
|
|
167 |
if !c.is_master {
|
|
168 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
169 |
} else if number < 1 || number > 8
|
|
170 |
|| number > addable_hedgehogs + team.hedgehogs_number {
|
|
171 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
|
|
172 |
.send_self().action()]
|
|
173 |
} else {
|
|
174 |
team.hedgehogs_number = number;
|
|
175 |
vec![HedgehogsNumber(team.name.clone(), number)
|
|
176 |
.send_all().in_room(room_id).but_self().action()]
|
|
177 |
}
|
|
178 |
} else {
|
|
179 |
vec![(Warn("No such team.".to_string()))]
|
|
180 |
}
|
|
181 |
} else {
|
|
182 |
Vec::new()
|
|
183 |
};
|
|
184 |
server.react(client_id, actions);
|
|
185 |
},
|
|
186 |
SetTeamColor(team_name, color) => {
|
|
187 |
let mut owner_id = None;
|
|
188 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
189 |
let room_id = r.id;
|
|
190 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
|
|
191 |
if !c.is_master {
|
|
192 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
193 |
} else if false {
|
|
194 |
Vec::new()
|
|
195 |
} else {
|
|
196 |
owner_id = Some(owner);
|
|
197 |
team.color = color;
|
|
198 |
vec![TeamColor(team.name.clone(), color)
|
|
199 |
.send_all().in_room(room_id).but_self().action()]
|
|
200 |
}
|
|
201 |
} else {
|
|
202 |
vec![(Warn("No such team.".to_string()))]
|
|
203 |
}
|
|
204 |
} else {
|
|
205 |
Vec::new()
|
|
206 |
};
|
|
207 |
|
|
208 |
if let Some(id) = owner_id {
|
|
209 |
server.clients[id].clan = Some(color);
|
|
210 |
}
|
|
211 |
|
|
212 |
server.react(client_id, actions);
|
13422
|
213 |
},
|
|
214 |
Cfg(cfg) => {
|
|
215 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
216 |
if !c.is_master {
|
|
217 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
218 |
} else {
|
|
219 |
r.set_config(cfg.clone());
|
|
220 |
vec![cfg.into_server_msg()
|
|
221 |
.send_all().in_room(r.id).but_self().action()]
|
|
222 |
}
|
|
223 |
} else {
|
|
224 |
Vec::new()
|
|
225 |
};
|
|
226 |
server.react(client_id, actions);
|
13419
|
227 |
}
|
13423
|
228 |
StartGame => {
|
|
229 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) {
|
|
230 |
vec![StartRoomGame(r.id)]
|
|
231 |
} else {
|
|
232 |
Vec::new()
|
|
233 |
};
|
|
234 |
server.react(client_id, actions);
|
|
235 |
}
|
|
236 |
EngineMessage(em) => {
|
|
237 |
let mut actions = Vec::new();
|
|
238 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
239 |
if c.teams_in_game > 0 {
|
|
240 |
let decoding = decode(&em[..]).unwrap();
|
|
241 |
let messages = by_msg(&decoding);
|
|
242 |
let valid = messages.clone().filter(|m| is_msg_valid(m, &c.team_indices));
|
|
243 |
let _non_empty = messages.filter(|m| !is_msg_empty(m));
|
|
244 |
let _last_valid_timed_msg = valid.clone().scan(None, |res, msg| match msg {
|
|
245 |
[_, b'+', ..] => Some(msg),
|
|
246 |
[_, typ, ..] if NON_TIMED_MESSAGES.contains(typ) => *res,
|
|
247 |
_ => None
|
|
248 |
}).next();
|
|
249 |
|
|
250 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
|
|
251 |
if !em_response.is_empty() {
|
|
252 |
actions.push(ForwardEngineMessage(em_response)
|
|
253 |
.send_all().in_room(r.id).but_self().action());
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|
|
257 |
server.react(client_id, actions)
|
|
258 |
}
|
|
259 |
RoundFinished => {
|
|
260 |
let mut actions = Vec::new();
|
|
261 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
262 |
if c.is_in_game {
|
|
263 |
c.is_in_game = false;
|
|
264 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]).
|
|
265 |
send_all().in_room(r.id).action());
|
|
266 |
for team in r.client_teams(c.id) {
|
|
267 |
actions.push(SendTeamRemovalMessage(team.name.clone()));
|
|
268 |
}
|
|
269 |
}
|
|
270 |
}
|
|
271 |
server.react(client_id, actions)
|
|
272 |
}
|
13419
|
273 |
_ => warn!("Unimplemented!")
|
12147
|
274 |
}
|
|
275 |
}
|