author | alfadur |
Sun, 09 Sep 2018 01:20:35 +0300 | |
changeset 13806 | 5fb40c8e5542 |
parent 13800 | e335daaa77a9 |
child 13810 | 0463a4221327 |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13671 | 3 |
use crate::{ |
4 |
server::{ |
|
13800
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
5 |
coretypes::{ |
13806 | 6 |
ClientId, RoomId, Voting, VoteType, GameCfg, |
13800
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
7 |
MAX_HEDGEHOGS_PER_TEAM |
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
8 |
}, |
13671 | 9 |
server::HWServer, |
10 |
room::{HWRoom, RoomFlags}, |
|
11 |
actions::{Action, Action::*} |
|
12 |
}, |
|
13 |
protocol::messages::{ |
|
14 |
HWProtocolMessage, |
|
15 |
HWServerMessage::*, |
|
16 |
server_chat |
|
17 |
}, |
|
18 |
utils::is_name_illegal |
|
13421 | 19 |
}; |
13534 | 20 |
use std::{ |
21 |
mem::swap, fs::{File, OpenOptions}, |
|
22 |
io::{Read, Write, Result, Error, ErrorKind} |
|
23 |
}; |
|
13428 | 24 |
use base64::{encode, decode}; |
13526 | 25 |
use super::common::rnd_reply; |
13428 | 26 |
|
27 |
#[derive(Clone)] |
|
28 |
struct ByMsg<'a> { |
|
29 |
messages: &'a[u8] |
|
30 |
} |
|
31 |
||
32 |
impl <'a> Iterator for ByMsg<'a> { |
|
33 |
type Item = &'a[u8]; |
|
34 |
||
35 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
36 |
if let Some(size) = self.messages.get(0) { |
|
37 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
38 |
self.messages = next; |
|
39 |
Some(msg) |
|
40 |
} else { |
|
41 |
None |
|
42 |
} |
|
43 |
} |
|
44 |
} |
|
45 |
||
13529 | 46 |
fn by_msg(source: &[u8]) -> ByMsg { |
47 |
ByMsg {messages: source} |
|
13428 | 48 |
} |
49 |
||
50 |
const VALID_MESSAGES: &[u8] = |
|
51 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
52 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
53 |
||
13434 | 54 |
#[cfg(canhazslicepatterns)] |
13428 | 55 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13429 | 56 |
match msg { |
57 |
[size, typ, body..] => VALID_MESSAGES.contains(typ) |
|
58 |
&& match body { |
|
13800
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
59 |
[1...MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => |
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
60 |
team_indices.contains(team), |
13428 | 61 |
_ => *typ != b'h' |
13429 | 62 |
}, |
63 |
_ => false |
|
13428 | 64 |
} |
65 |
} |
|
66 |
||
13671 | 67 |
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool { |
13434 | 68 |
if let Some(typ) = msg.get(1) { |
69 |
VALID_MESSAGES.contains(typ) |
|
70 |
} else { |
|
71 |
false |
|
72 |
} |
|
73 |
} |
|
74 |
||
13428 | 75 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13434 | 76 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13428 | 77 |
} |
12152 | 78 |
|
13448 | 79 |
fn is_msg_timed(msg: &[u8]) -> bool { |
80 |
msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some() |
|
81 |
} |
|
82 |
||
13485 | 83 |
fn voting_description(kind: &VoteType) -> String { |
84 |
format!("New voting started: {}", match kind { |
|
85 |
VoteType::Kick(nick) => format!("kick {}", nick), |
|
86 |
VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()), |
|
87 |
VoteType::Pause => "pause".to_string(), |
|
88 |
VoteType::NewSeed => "new seed".to_string(), |
|
89 |
VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number) |
|
90 |
}) |
|
91 |
} |
|
92 |
||
13528 | 93 |
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags { |
13671 | 94 |
use crate::protocol::messages::HWProtocolMessage::*; |
13528 | 95 |
match msg { |
96 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
97 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
98 |
ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, |
|
99 |
_ => RoomFlags::empty() |
|
100 |
} |
|
101 |
} |
|
102 |
||
13534 | 103 |
fn read_file(filename: &str) -> Result<String> { |
104 |
let mut reader = File::open(filename)?; |
|
105 |
let mut result = String::new(); |
|
106 |
reader.read_to_string(&mut result)?; |
|
107 |
Ok(result) |
|
108 |
} |
|
109 |
||
110 |
fn write_file(filename: &str, content: &str) -> Result<()> { |
|
111 |
let mut writer = OpenOptions::new().create(true).write(true).open(filename)?; |
|
112 |
writer.write_all(content.as_bytes()) |
|
113 |
} |
|
114 |
||
13526 | 115 |
pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) { |
13671 | 116 |
use crate::protocol::messages::HWProtocolMessage::*; |
12152 | 117 |
match message { |
13424 | 118 |
Part(None) => server.react(client_id, vec![ |
13421 | 119 |
MoveToLobby("part".to_string())]), |
13424 | 120 |
Part(Some(msg)) => server.react(client_id, vec![ |
13421 | 121 |
MoveToLobby(format!("part: {}", msg))]), |
122 |
Chat(msg) => { |
|
13424 | 123 |
let actions = { |
124 |
let c = &mut server.clients[client_id]; |
|
13529 | 125 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg}; |
13526 | 126 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
13424 | 127 |
}; |
128 |
server.react(client_id, actions); |
|
13421 | 129 |
}, |
13482 | 130 |
Fix => { |
131 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 132 |
if c.is_admin() { r.set_is_fixed(true) } |
13482 | 133 |
} |
134 |
} |
|
135 |
Unfix => { |
|
136 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 137 |
if c.is_admin() { r.set_is_fixed(false) } |
13482 | 138 |
} |
139 |
} |
|
140 |
Greeting(text) => { |
|
141 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 142 |
if c.is_admin() || c.is_master() && !r.is_fixed() { |
13482 | 143 |
r.greeting = text |
144 |
} |
|
145 |
} |
|
146 |
} |
|
13421 | 147 |
RoomName(new_name) => { |
148 |
let actions = |
|
149 |
if is_name_illegal(&new_name) { |
|
150 |
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())] |
|
13531 | 151 |
} else if server.rooms[room_id].is_fixed() { |
13482 | 152 |
vec![Warn("Access denied.".to_string())] |
13421 | 153 |
} else if server.has_room(&new_name) { |
154 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
155 |
} else { |
|
156 |
let mut old_name = new_name.clone(); |
|
13531 | 157 |
swap(&mut server.rooms[room_id].name, &mut old_name); |
158 |
vec![SendRoomUpdate(Some(old_name))] |
|
13421 | 159 |
}; |
13424 | 160 |
server.react(client_id, actions); |
161 |
}, |
|
162 |
ToggleReady => { |
|
13671 | 163 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13525 | 164 |
let flags = if c.is_ready() { |
13424 | 165 |
r.ready_players_number -= 1; |
166 |
"-r" |
|
167 |
} else { |
|
168 |
r.ready_players_number += 1; |
|
169 |
"+r" |
|
170 |
}; |
|
13806 | 171 |
|
172 |
let msg = if c.protocol_number < 38 { |
|
173 |
LegacyReady(c.is_ready(), vec![c.nick.clone()]) |
|
174 |
} else { |
|
175 |
ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
176 |
}; |
|
177 |
||
178 |
let mut v = vec![msg.send_all().in_room(r.id).action()]; |
|
179 |
||
13528 | 180 |
if r.is_fixed() && r.ready_players_number == r.players_number { |
13482 | 181 |
v.push(StartRoomGame(r.id)) |
182 |
} |
|
13806 | 183 |
|
184 |
c.set_is_ready(!c.is_ready()); |
|
13671 | 185 |
server.react(client_id, v); |
186 |
} |
|
13421 | 187 |
} |
13427 | 188 |
AddTeam(info) => { |
13424 | 189 |
let mut actions = Vec::new(); |
190 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
191 |
if r.teams.len() >= r.team_limit as usize { |
|
192 |
actions.push(Warn("Too many teams!".to_string())) |
|
193 |
} else if r.addable_hedgehogs() == 0 { |
|
194 |
actions.push(Warn("Too many hedgehogs!".to_string())) |
|
195 |
} else if r.find_team(|t| t.name == info.name) != None { |
|
196 |
actions.push(Warn("There's already a team with same name in the list.".to_string())) |
|
13428 | 197 |
} else if r.game_info.is_some() { |
13424 | 198 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
13528 | 199 |
} else if r.is_team_add_restricted() { |
200 |
actions.push(Warn("This room currently does not allow adding new teams.".to_string())); |
|
13424 | 201 |
} else { |
13806 | 202 |
let team = r.add_team(c.id, *info, c.protocol_number < 42); |
13424 | 203 |
c.teams_in_game += 1; |
204 |
c.clan = Some(team.color); |
|
205 |
actions.push(TeamAccepted(team.name.clone()) |
|
206 |
.send_self().action()); |
|
207 |
actions.push(TeamAdd(HWRoom::team_info(&c, team)) |
|
208 |
.send_all().in_room(room_id).but_self().action()); |
|
209 |
actions.push(TeamColor(team.name.clone(), team.color) |
|
210 |
.send_all().in_room(room_id).action()); |
|
211 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
212 |
.send_all().in_room(room_id).action()); |
|
213 |
actions.push(SendRoomUpdate(None)); |
|
214 |
} |
|
215 |
} |
|
216 |
server.react(client_id, actions); |
|
217 |
}, |
|
218 |
RemoveTeam(name) => { |
|
219 |
let mut actions = Vec::new(); |
|
220 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
221 |
match r.find_team_owner(&name) { |
|
222 |
None => |
|
223 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())), |
|
224 |
Some((id, _)) if id != client_id => |
|
225 |
actions.push(Warn("You can't remove a team you don't own.".to_string())), |
|
226 |
Some((_, name)) => { |
|
227 |
c.teams_in_game -= 1; |
|
228 |
c.clan = r.find_team_color(c.id); |
|
229 |
actions.push(Action::RemoveTeam(name.to_string())); |
|
230 |
} |
|
231 |
} |
|
232 |
}; |
|
233 |
server.react(client_id, actions); |
|
234 |
}, |
|
235 |
SetHedgehogsNumber(team_name, number) => { |
|
13671 | 236 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13424 | 237 |
let addable_hedgehogs = r.addable_hedgehogs(); |
13671 | 238 |
let actions = if let Some((_, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
13525 | 239 |
if !c.is_master() { |
13424 | 240 |
vec![ProtocolError("You're not the room master!".to_string())] |
13800
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
241 |
} else if number < 1 || number > MAX_HEDGEHOGS_PER_TEAM |
13424 | 242 |
|| number > addable_hedgehogs + team.hedgehogs_number { |
243 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
244 |
.send_self().action()] |
|
245 |
} else { |
|
246 |
team.hedgehogs_number = number; |
|
247 |
vec![HedgehogsNumber(team.name.clone(), number) |
|
248 |
.send_all().in_room(room_id).but_self().action()] |
|
249 |
} |
|
250 |
} else { |
|
251 |
vec![(Warn("No such team.".to_string()))] |
|
13671 | 252 |
}; |
253 |
server.react(client_id, actions); |
|
254 |
} |
|
13424 | 255 |
}, |
256 |
SetTeamColor(team_name, color) => { |
|
13671 | 257 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
258 |
let mut owner_id = None; |
|
259 |
let actions = if let Some((owner, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13525 | 260 |
if !c.is_master() { |
13424 | 261 |
vec![ProtocolError("You're not the room master!".to_string())] |
262 |
} else if false { |
|
263 |
Vec::new() |
|
264 |
} else { |
|
265 |
owner_id = Some(owner); |
|
266 |
team.color = color; |
|
267 |
vec![TeamColor(team.name.clone(), color) |
|
268 |
.send_all().in_room(room_id).but_self().action()] |
|
269 |
} |
|
270 |
} else { |
|
271 |
vec![(Warn("No such team.".to_string()))] |
|
13671 | 272 |
}; |
13424 | 273 |
|
13671 | 274 |
if let Some(id) = owner_id { |
275 |
server.clients[id].clan = Some(color); |
|
276 |
} |
|
13424 | 277 |
|
13671 | 278 |
server.react(client_id, actions); |
279 |
}; |
|
13427 | 280 |
}, |
281 |
Cfg(cfg) => { |
|
13671 | 282 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
283 |
let actions = if r.is_fixed() { |
|
13482 | 284 |
vec![Warn("Access denied.".to_string())] |
13525 | 285 |
} else if !c.is_master() { |
13427 | 286 |
vec![ProtocolError("You're not the room master!".to_string())] |
287 |
} else { |
|
13806 | 288 |
let cfg = match cfg { |
289 |
GameCfg::Scheme(name, mut values) => { |
|
290 |
if c.protocol_number == 49 && values.len() >= 2 { |
|
291 |
let mut s = "X".repeat(50); |
|
292 |
s.push_str(&values.pop().unwrap()); |
|
293 |
values.push(s); |
|
294 |
} |
|
295 |
GameCfg::Scheme(name, values) |
|
296 |
} |
|
297 |
cfg => cfg |
|
298 |
}; |
|
299 |
||
13444 | 300 |
let v = vec![cfg.to_server_msg() |
301 |
.send_all().in_room(r.id).but_self().action()]; |
|
302 |
r.set_config(cfg); |
|
303 |
v |
|
13671 | 304 |
}; |
305 |
server.react(client_id, actions); |
|
306 |
} |
|
13424 | 307 |
} |
13533 | 308 |
Save(name, location) => { |
309 |
let actions = vec![server_chat(format!("Room config saved as {}", name)) |
|
310 |
.send_all().in_room(room_id).action()]; |
|
311 |
server.rooms[room_id].save_config(name, location); |
|
312 |
server.react(client_id, actions); |
|
313 |
} |
|
13534 | 314 |
SaveRoom(filename) => { |
13671 | 315 |
if server.clients[client_id].is_admin() { |
316 |
let actions = match server.rooms[room_id].get_saves() { |
|
13534 | 317 |
Ok(text) => match write_file(&filename, &text) { |
318 |
Ok(_) => vec![server_chat("Room configs saved successfully.".to_string()) |
|
319 |
.send_self().action()], |
|
320 |
Err(e) => { |
|
321 |
warn!("Error while writing the config file \"{}\": {}", filename, e); |
|
322 |
vec![Warn("Unable to save the room configs.".to_string())] |
|
323 |
} |
|
324 |
} |
|
325 |
Err(e) => { |
|
326 |
warn!("Error while serializing the room configs: {}", e); |
|
327 |
vec![Warn("Unable to serialize the room configs.".to_string())] |
|
328 |
} |
|
13671 | 329 |
}; |
330 |
server.react(client_id, actions); |
|
331 |
} |
|
13534 | 332 |
} |
333 |
LoadRoom(filename) => { |
|
13671 | 334 |
if server.clients[client_id].is_admin() { |
335 |
let actions = match read_file(&filename) { |
|
13534 | 336 |
Ok(text) => match server.rooms[room_id].set_saves(&text) { |
337 |
Ok(_) => vec![server_chat("Room configs loaded successfully.".to_string()) |
|
338 |
.send_self().action()], |
|
339 |
Err(e) => { |
|
340 |
warn!("Error while deserializing the room configs: {}", e); |
|
341 |
vec![Warn("Unable to deserialize the room configs.".to_string())] |
|
342 |
} |
|
343 |
} |
|
344 |
Err(e) => { |
|
345 |
warn!("Error while reading the config file \"{}\": {}", filename, e); |
|
346 |
vec![Warn("Unable to load the room configs.".to_string())] |
|
347 |
} |
|
13671 | 348 |
}; |
349 |
server.react(client_id, actions); |
|
350 |
} |
|
13534 | 351 |
} |
13533 | 352 |
Delete(name) => { |
353 |
let actions = if !server.rooms[room_id].delete_config(&name) { |
|
354 |
vec![Warn(format!("Save doesn't exist: {}", name))] |
|
355 |
} else { |
|
356 |
vec![server_chat(format!("Room config {} has been deleted", name)) |
|
357 |
.send_all().in_room(room_id).action()] |
|
358 |
}; |
|
359 |
server.react(client_id, actions); |
|
360 |
} |
|
13483 | 361 |
CallVote(None) => { |
362 |
server.react(client_id, vec![ |
|
13532 | 363 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
13483 | 364 |
.send_self().action()]) |
365 |
} |
|
366 |
CallVote(Some(kind)) => { |
|
13526 | 367 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13483 | 368 |
let error = match &kind { |
369 |
VoteType::Kick(nick) => { |
|
370 |
if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() { |
|
371 |
None |
|
372 |
} else { |
|
13532 | 373 |
Some("/callvote kick: No such user!".to_string()) |
13483 | 374 |
} |
375 |
}, |
|
376 |
VoteType::Map(None) => { |
|
13532 | 377 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
378 |
if names.is_empty() { |
|
379 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
380 |
} else { |
|
381 |
Some(format!("Available maps: {}", names.join(", "))) |
|
382 |
} |
|
13483 | 383 |
}, |
384 |
VoteType::Map(Some(name)) => { |
|
13532 | 385 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
13535 | 386 |
None |
13532 | 387 |
} else { |
13535 | 388 |
Some("/callvote map: No such map!".to_string()) |
13532 | 389 |
} |
13483 | 390 |
}, |
391 |
VoteType::Pause => { |
|
392 |
if is_in_game { |
|
393 |
None |
|
394 |
} else { |
|
13532 | 395 |
Some("/callvote pause: No game in progress!".to_string()) |
13483 | 396 |
} |
397 |
}, |
|
398 |
VoteType::NewSeed => { |
|
399 |
None |
|
400 |
}, |
|
401 |
VoteType::HedgehogsPerTeam(number) => { |
|
402 |
match number { |
|
13800
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13671
diff
changeset
|
403 |
1...MAX_HEDGEHOGS_PER_TEAM => None, |
13532 | 404 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()) |
13483 | 405 |
} |
406 |
}, |
|
407 |
}; |
|
408 |
match error { |
|
409 |
None => { |
|
13485 | 410 |
let msg = voting_description(&kind); |
13483 | 411 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13531 | 412 |
server.rooms[room_id].voting = Some(voting); |
13483 | 413 |
server.react(client_id, vec![ |
13532 | 414 |
server_chat(msg).send_all().in_room(room_id).action(), |
13483 | 415 |
AddVote{ vote: true, is_forced: false}]); |
416 |
} |
|
417 |
Some(msg) => { |
|
418 |
server.react(client_id, vec![ |
|
419 |
server_chat(msg).send_self().action()]) |
|
420 |
} |
|
421 |
} |
|
422 |
} |
|
423 |
Vote(vote) => { |
|
13531 | 424 |
server.react(client_id, vec![AddVote{ vote, is_forced: false }]); |
13483 | 425 |
} |
426 |
ForceVote(vote) => { |
|
13531 | 427 |
let is_forced = server.clients[client_id].is_admin(); |
428 |
server.react(client_id, vec![AddVote{ vote, is_forced }]); |
|
13483 | 429 |
} |
13528 | 430 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
431 |
if server.clients[client_id].is_master() { |
|
432 |
server.rooms[room_id].flags.toggle(room_message_flag(&message)); |
|
433 |
} |
|
434 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
435 |
} |
|
13428 | 436 |
StartGame => { |
13531 | 437 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13428 | 438 |
} |
439 |
EngineMessage(em) => { |
|
440 |
let mut actions = Vec::new(); |
|
441 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
442 |
if c.teams_in_game > 0 { |
|
443 |
let decoding = decode(&em[..]).unwrap(); |
|
444 |
let messages = by_msg(&decoding); |
|
13448 | 445 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
446 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
447 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
448 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13428 | 449 |
|
450 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
451 |
if !em_response.is_empty() { |
|
13433 | 452 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13428 | 453 |
.send_all().in_room(r.id).but_self().action()); |
454 |
} |
|
13432 | 455 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
456 |
if let Some(ref mut info) = r.game_info { |
|
13434 | 457 |
if !em_log.is_empty() { |
13433 | 458 |
info.msg_log.push(em_log); |
459 |
} |
|
13448 | 460 |
if let Some(msg) = sync_msg { |
461 |
info.sync_msg = msg; |
|
13432 | 462 |
} |
463 |
} |
|
13428 | 464 |
} |
465 |
} |
|
466 |
server.react(client_id, actions) |
|
467 |
} |
|
468 |
RoundFinished => { |
|
469 |
let mut actions = Vec::new(); |
|
470 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 471 |
if c.is_in_game() { |
472 |
c.set_is_in_game(false); |
|
13428 | 473 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
474 |
send_all().in_room(r.id).action()); |
|
13431 | 475 |
if r.game_info.is_some() { |
476 |
for team in r.client_teams(c.id) { |
|
477 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
478 |
} |
|
13428 | 479 |
} |
480 |
} |
|
481 |
} |
|
482 |
server.react(client_id, actions) |
|
13449
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13448
diff
changeset
|
483 |
}, |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
484 |
Rnd(v) => { |
13526 | 485 |
let result = rnd_reply(&v); |
486 |
let mut echo = vec!["/rnd".to_string()]; |
|
487 |
echo.extend(v.into_iter()); |
|
488 |
let chat_msg = ChatMsg { |
|
489 |
nick: server.clients[client_id].nick.clone(), |
|
490 |
msg: echo.join(" ") |
|
491 |
}; |
|
492 |
server.react(client_id, vec![ |
|
493 |
chat_msg.send_all().in_room(room_id).action(), |
|
494 |
result.send_all().in_room(room_id).action()]) |
|
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
495 |
}, |
13424 | 496 |
_ => warn!("Unimplemented!") |
12152 | 497 |
} |
498 |
} |