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