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