author | alfadur |
Sun, 09 Sep 2018 01:20:35 +0300 | |
changeset 13775 | 5fb40c8e5542 |
parent 13709 | e335daaa77a9 |
child 13810 | 0463a4221327 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13666 | 3 |
use crate::{ |
4 |
server::{ |
|
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
5 |
coretypes::{ |
13775 | 6 |
ClientId, RoomId, Voting, VoteType, GameCfg, |
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
7 |
MAX_HEDGEHOGS_PER_TEAM |
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
8 |
}, |
13666 | 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 |
|
13416 | 19 |
}; |
13529 | 20 |
use std::{ |
21 |
mem::swap, fs::{File, OpenOptions}, |
|
22 |
io::{Read, Write, Result, Error, ErrorKind} |
|
23 |
}; |
|
13423 | 24 |
use base64::{encode, decode}; |
13492 | 25 |
use super::common::rnd_reply; |
13423 | 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 |
||
13500 | 46 |
fn by_msg(source: &[u8]) -> ByMsg { |
47 |
ByMsg {messages: source} |
|
13423 | 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 |
||
13429 | 54 |
#[cfg(canhazslicepatterns)] |
13423 | 55 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13424 | 56 |
match msg { |
57 |
[size, typ, body..] => VALID_MESSAGES.contains(typ) |
|
58 |
&& match body { |
|
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
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:
13666
diff
changeset
|
60 |
team_indices.contains(team), |
13423 | 61 |
_ => *typ != b'h' |
13424 | 62 |
}, |
63 |
_ => false |
|
13423 | 64 |
} |
65 |
} |
|
66 |
||
13666 | 67 |
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool { |
13429 | 68 |
if let Some(typ) = msg.get(1) { |
69 |
VALID_MESSAGES.contains(typ) |
|
70 |
} else { |
|
71 |
false |
|
72 |
} |
|
73 |
} |
|
74 |
||
13423 | 75 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13429 | 76 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13423 | 77 |
} |
12147 | 78 |
|
13443 | 79 |
fn is_msg_timed(msg: &[u8]) -> bool { |
80 |
msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some() |
|
81 |
} |
|
82 |
||
13460 | 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 |
||
13494 | 93 |
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags { |
13666 | 94 |
use crate::protocol::messages::HWProtocolMessage::*; |
13494 | 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 |
||
13529 | 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 |
||
13492 | 115 |
pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) { |
13666 | 116 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 117 |
match message { |
13419 | 118 |
Part(None) => server.react(client_id, vec![ |
13416 | 119 |
MoveToLobby("part".to_string())]), |
13419 | 120 |
Part(Some(msg)) => server.react(client_id, vec![ |
13416 | 121 |
MoveToLobby(format!("part: {}", msg))]), |
122 |
Chat(msg) => { |
|
13419 | 123 |
let actions = { |
124 |
let c = &mut server.clients[client_id]; |
|
13500 | 125 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg}; |
13492 | 126 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
13419 | 127 |
}; |
128 |
server.react(client_id, actions); |
|
13416 | 129 |
}, |
13447 | 130 |
Fix => { |
131 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13494 | 132 |
if c.is_admin() { r.set_is_fixed(true) } |
13447 | 133 |
} |
134 |
} |
|
135 |
Unfix => { |
|
136 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13494 | 137 |
if c.is_admin() { r.set_is_fixed(false) } |
13447 | 138 |
} |
139 |
} |
|
140 |
Greeting(text) => { |
|
141 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13494 | 142 |
if c.is_admin() || c.is_master() && !r.is_fixed() { |
13447 | 143 |
r.greeting = text |
144 |
} |
|
145 |
} |
|
146 |
} |
|
13416 | 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())] |
|
13520 | 151 |
} else if server.rooms[room_id].is_fixed() { |
13447 | 152 |
vec![Warn("Access denied.".to_string())] |
13416 | 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(); |
|
13520 | 157 |
swap(&mut server.rooms[room_id].name, &mut old_name); |
158 |
vec![SendRoomUpdate(Some(old_name))] |
|
13416 | 159 |
}; |
13419 | 160 |
server.react(client_id, actions); |
161 |
}, |
|
162 |
ToggleReady => { |
|
13666 | 163 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13486 | 164 |
let flags = if c.is_ready() { |
13419 | 165 |
r.ready_players_number -= 1; |
166 |
"-r" |
|
167 |
} else { |
|
168 |
r.ready_players_number += 1; |
|
169 |
"+r" |
|
170 |
}; |
|
13775 | 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 |
||
13494 | 180 |
if r.is_fixed() && r.ready_players_number == r.players_number { |
13447 | 181 |
v.push(StartRoomGame(r.id)) |
182 |
} |
|
13775 | 183 |
|
184 |
c.set_is_ready(!c.is_ready()); |
|
13666 | 185 |
server.react(client_id, v); |
186 |
} |
|
13416 | 187 |
} |
13422 | 188 |
AddTeam(info) => { |
13419 | 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())) |
|
13423 | 197 |
} else if r.game_info.is_some() { |
13419 | 198 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
13494 | 199 |
} else if r.is_team_add_restricted() { |
200 |
actions.push(Warn("This room currently does not allow adding new teams.".to_string())); |
|
13419 | 201 |
} else { |
13775 | 202 |
let team = r.add_team(c.id, *info, c.protocol_number < 42); |
13419 | 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) => { |
|
13666 | 236 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13419 | 237 |
let addable_hedgehogs = r.addable_hedgehogs(); |
13666 | 238 |
let actions = if let Some((_, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
13486 | 239 |
if !c.is_master() { |
13419 | 240 |
vec![ProtocolError("You're not the room master!".to_string())] |
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
241 |
} else if number < 1 || number > MAX_HEDGEHOGS_PER_TEAM |
13419 | 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()))] |
|
13666 | 252 |
}; |
253 |
server.react(client_id, actions); |
|
254 |
} |
|
13419 | 255 |
}, |
256 |
SetTeamColor(team_name, color) => { |
|
13666 | 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) { |
|
13486 | 260 |
if !c.is_master() { |
13419 | 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()))] |
|
13666 | 272 |
}; |
13419 | 273 |
|
13666 | 274 |
if let Some(id) = owner_id { |
275 |
server.clients[id].clan = Some(color); |
|
276 |
} |
|
13419 | 277 |
|
13666 | 278 |
server.react(client_id, actions); |
279 |
}; |
|
13422 | 280 |
}, |
281 |
Cfg(cfg) => { |
|
13666 | 282 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
283 |
let actions = if r.is_fixed() { |
|
13447 | 284 |
vec![Warn("Access denied.".to_string())] |
13486 | 285 |
} else if !c.is_master() { |
13422 | 286 |
vec![ProtocolError("You're not the room master!".to_string())] |
287 |
} else { |
|
13775 | 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 |
||
13439 | 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 |
|
13666 | 304 |
}; |
305 |
server.react(client_id, actions); |
|
306 |
} |
|
13419 | 307 |
} |
13528 | 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 |
} |
|
13529 | 314 |
SaveRoom(filename) => { |
13666 | 315 |
if server.clients[client_id].is_admin() { |
316 |
let actions = match server.rooms[room_id].get_saves() { |
|
13529 | 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 |
} |
|
13666 | 329 |
}; |
330 |
server.react(client_id, actions); |
|
331 |
} |
|
13529 | 332 |
} |
333 |
LoadRoom(filename) => { |
|
13666 | 334 |
if server.clients[client_id].is_admin() { |
335 |
let actions = match read_file(&filename) { |
|
13529 | 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 |
} |
|
13666 | 348 |
}; |
349 |
server.react(client_id, actions); |
|
350 |
} |
|
13529 | 351 |
} |
13528 | 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 |
} |
|
13450 | 361 |
CallVote(None) => { |
362 |
server.react(client_id, vec![ |
|
13521 | 363 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
13450 | 364 |
.send_self().action()]) |
365 |
} |
|
366 |
CallVote(Some(kind)) => { |
|
13492 | 367 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13450 | 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 { |
|
13521 | 373 |
Some("/callvote kick: No such user!".to_string()) |
13450 | 374 |
} |
375 |
}, |
|
376 |
VoteType::Map(None) => { |
|
13521 | 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 |
} |
|
13450 | 383 |
}, |
384 |
VoteType::Map(Some(name)) => { |
|
13521 | 385 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
13530 | 386 |
None |
13521 | 387 |
} else { |
13530 | 388 |
Some("/callvote map: No such map!".to_string()) |
13521 | 389 |
} |
13450 | 390 |
}, |
391 |
VoteType::Pause => { |
|
392 |
if is_in_game { |
|
393 |
None |
|
394 |
} else { |
|
13521 | 395 |
Some("/callvote pause: No game in progress!".to_string()) |
13450 | 396 |
} |
397 |
}, |
|
398 |
VoteType::NewSeed => { |
|
399 |
None |
|
400 |
}, |
|
401 |
VoteType::HedgehogsPerTeam(number) => { |
|
402 |
match number { |
|
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
403 |
1...MAX_HEDGEHOGS_PER_TEAM => None, |
13521 | 404 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()) |
13450 | 405 |
} |
406 |
}, |
|
407 |
}; |
|
408 |
match error { |
|
409 |
None => { |
|
13460 | 410 |
let msg = voting_description(&kind); |
13450 | 411 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13520 | 412 |
server.rooms[room_id].voting = Some(voting); |
13450 | 413 |
server.react(client_id, vec![ |
13521 | 414 |
server_chat(msg).send_all().in_room(room_id).action(), |
13450 | 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) => { |
|
13520 | 424 |
server.react(client_id, vec![AddVote{ vote, is_forced: false }]); |
13450 | 425 |
} |
426 |
ForceVote(vote) => { |
|
13520 | 427 |
let is_forced = server.clients[client_id].is_admin(); |
428 |
server.react(client_id, vec![AddVote{ vote, is_forced }]); |
|
13450 | 429 |
} |
13494 | 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 |
} |
|
13423 | 436 |
StartGame => { |
13520 | 437 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13423 | 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); |
|
13443 | 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}); |
|
13423 | 449 |
|
450 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
451 |
if !em_response.is_empty() { |
|
13428 | 452 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13423 | 453 |
.send_all().in_room(r.id).but_self().action()); |
454 |
} |
|
13427 | 455 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
456 |
if let Some(ref mut info) = r.game_info { |
|
13429 | 457 |
if !em_log.is_empty() { |
13428 | 458 |
info.msg_log.push(em_log); |
459 |
} |
|
13443 | 460 |
if let Some(msg) = sync_msg { |
461 |
info.sync_msg = msg; |
|
13427 | 462 |
} |
463 |
} |
|
13423 | 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) { |
|
13486 | 471 |
if c.is_in_game() { |
472 |
c.set_is_in_game(false); |
|
13423 | 473 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
474 |
send_all().in_room(r.id).action()); |
|
13426 | 475 |
if r.game_info.is_some() { |
476 |
for team in r.client_teams(c.id) { |
|
477 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
478 |
} |
|
13423 | 479 |
} |
480 |
} |
|
481 |
} |
|
482 |
server.react(client_id, actions) |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
483 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
484 |
Rnd(v) => { |
13492 | 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()]) |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
495 |
}, |
13419 | 496 |
_ => warn!("Unimplemented!") |
12147 | 497 |
} |
498 |
} |