author | alfadur <mail@none> |
Sat, 21 Dec 2019 00:26:17 +0300 | |
changeset 15540 | e705d30e0f10 |
parent 15539 | abd5eb807166 |
child 15541 | b3157d218ae2 |
permissions | -rw-r--r-- |
13416 | 1 |
use super::{ |
15096 | 2 |
client::HwClient, |
14714 | 3 |
indexslab::IndexSlab, |
15096 | 4 |
room::HwRoom, |
15463 | 5 |
types::{ClientId, RoomId, ServerVar}, |
13416 | 6 |
}; |
15096 | 7 |
use crate::{protocol::messages::HwProtocolMessage::Greeting, utils}; |
14714 | 8 |
|
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
9 |
use bitflags::*; |
15539 | 10 |
use chrono::{offset, DateTime}; |
13810 | 11 |
use log::*; |
15539 | 12 |
use slab::Slab; |
15538 | 13 |
use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace, num::NonZeroU16}; |
12126 | 14 |
|
15465 | 15 |
#[derive(Debug)] |
15463 | 16 |
pub enum CreateRoomError { |
17 |
InvalidName, |
|
18 |
AlreadyExists, |
|
19 |
} |
|
20 |
||
15465 | 21 |
#[derive(Debug)] |
15463 | 22 |
pub enum JoinRoomError { |
23 |
DoesntExist, |
|
24 |
WrongProtocol, |
|
25 |
Full, |
|
26 |
Restricted, |
|
27 |
} |
|
28 |
||
15540 | 29 |
#[derive(Debug)] |
15504 | 30 |
pub enum LeaveRoomResult { |
31 |
RoomRemoved, |
|
32 |
RoomRemains { |
|
33 |
is_empty: bool, |
|
34 |
was_master: bool, |
|
35 |
was_in_game: bool, |
|
36 |
new_master: Option<ClientId>, |
|
37 |
removed_teams: Vec<String>, |
|
38 |
}, |
|
39 |
} |
|
40 |
||
41 |
#[derive(Debug)] |
|
42 |
pub enum LeaveRoomError { |
|
43 |
NoRoom, |
|
44 |
} |
|
45 |
||
15465 | 46 |
#[derive(Debug)] |
15509 | 47 |
pub struct ChangeMasterResult { |
48 |
pub old_master_id: Option<ClientId>, |
|
49 |
pub new_master_id: ClientId, |
|
50 |
} |
|
51 |
||
52 |
#[derive(Debug)] |
|
53 |
pub enum ChangeMasterError { |
|
54 |
NoAccess, |
|
55 |
AlreadyMaster, |
|
56 |
NoClient, |
|
57 |
ClientNotInRoom, |
|
58 |
} |
|
59 |
||
60 |
#[derive(Debug)] |
|
15540 | 61 |
pub enum ModifyTeamError { |
62 |
NoTeam, |
|
63 |
NotMaster, |
|
64 |
} |
|
65 |
||
66 |
#[derive(Debug)] |
|
67 |
pub enum ModifyRoomNameError { |
|
68 |
AccessDenied, |
|
69 |
InvalidName, |
|
70 |
DuplicateName, |
|
71 |
} |
|
72 |
||
73 |
#[derive(Debug)] |
|
15514 | 74 |
pub enum StartGameError { |
75 |
NotEnoughClans, |
|
76 |
NotEnoughTeams, |
|
77 |
NotReady, |
|
78 |
AlreadyInGame, |
|
79 |
} |
|
80 |
||
81 |
#[derive(Debug)] |
|
15538 | 82 |
pub struct EndGameResult { |
83 |
pub joined_mid_game_clients: Vec<ClientId>, |
|
84 |
pub left_teams: Vec<String>, |
|
85 |
pub unreadied_nicks: Vec<String>, |
|
86 |
} |
|
87 |
||
88 |
#[derive(Debug)] |
|
15465 | 89 |
pub struct UninitializedError(); |
90 |
#[derive(Debug)] |
|
15463 | 91 |
pub struct AccessError(); |
92 |
||
15096 | 93 |
pub struct HwAnteClient { |
14714 | 94 |
pub nick: Option<String>, |
95 |
pub protocol_number: Option<NonZeroU16>, |
|
96 |
pub server_salt: String, |
|
14802 | 97 |
pub is_checker: bool, |
15197 | 98 |
pub is_local_admin: bool, |
15465 | 99 |
pub is_registered: bool, |
100 |
pub is_admin: bool, |
|
101 |
pub is_contributor: bool, |
|
14714 | 102 |
} |
103 |
||
15539 | 104 |
struct Ipv4AddrRange { |
105 |
min: [u8; 4], |
|
106 |
max: [u8; 4], |
|
107 |
} |
|
108 |
||
109 |
impl Ipv4AddrRange { |
|
110 |
fn contains(&self, addr: [u8; 4]) -> bool { |
|
111 |
(0..4).all(|i| self.min[i] <= addr[i] && addr[i] <= self.max[i]) |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
struct BanCollection { |
|
116 |
ban_ips: Vec<Ipv4AddrRange>, |
|
117 |
ban_timeouts: Vec<DateTime<offset::Utc>>, |
|
118 |
ban_reasons: Vec<String>, |
|
119 |
} |
|
120 |
||
121 |
impl BanCollection { |
|
122 |
fn new() -> Self { |
|
123 |
Self { |
|
124 |
ban_ips: vec![], |
|
125 |
ban_timeouts: vec![], |
|
126 |
ban_reasons: vec![], |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
fn find(&self, addr: [u8; 4]) -> Option<String> { |
|
131 |
let time = offset::Utc::now(); |
|
132 |
self.ban_ips |
|
133 |
.iter() |
|
134 |
.enumerate() |
|
135 |
.find(|(i, r)| r.contains(addr) && time < self.ban_timeouts[*i]) |
|
136 |
.map(|(i, _)| self.ban_reasons[i].clone()) |
|
137 |
} |
|
138 |
} |
|
139 |
||
15096 | 140 |
pub struct HwAnteroom { |
141 |
pub clients: IndexSlab<HwAnteClient>, |
|
15539 | 142 |
bans: BanCollection, |
14714 | 143 |
} |
144 |
||
15096 | 145 |
impl HwAnteroom { |
14714 | 146 |
pub fn new(clients_limit: usize) -> Self { |
147 |
let clients = IndexSlab::with_capacity(clients_limit); |
|
15539 | 148 |
HwAnteroom { |
149 |
clients, |
|
150 |
bans: BanCollection::new(), |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
pub fn find_ip_ban(&self, addr: [u8; 4]) -> Option<String> { |
|
155 |
self.bans.find(addr) |
|
14714 | 156 |
} |
157 |
||
15197 | 158 |
pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) { |
15096 | 159 |
let client = HwAnteClient { |
14714 | 160 |
nick: None, |
161 |
protocol_number: None, |
|
162 |
server_salt: salt, |
|
14802 | 163 |
is_checker: false, |
15197 | 164 |
is_local_admin, |
15465 | 165 |
is_registered: false, |
166 |
is_admin: false, |
|
167 |
is_contributor: false, |
|
14714 | 168 |
}; |
169 |
self.clients.insert(client_id, client); |
|
170 |
} |
|
171 |
||
15096 | 172 |
pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteClient> { |
15463 | 173 |
let client = self.clients.remove(client_id); |
14714 | 174 |
client |
175 |
} |
|
176 |
} |
|
177 |
||
14804 | 178 |
pub struct ServerGreetings { |
179 |
pub for_latest_protocol: String, |
|
180 |
pub for_old_protocols: String, |
|
181 |
} |
|
182 |
||
183 |
impl ServerGreetings { |
|
184 |
fn new() -> Self { |
|
185 |
Self { |
|
186 |
for_latest_protocol: "\u{1f994} is watching".to_string(), |
|
187 |
for_old_protocols: "\u{1f994} is watching".to_string(), |
|
188 |
} |
|
189 |
} |
|
190 |
} |
|
191 |
||
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
192 |
bitflags! { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
193 |
pub struct ServerFlags: u8 { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
194 |
const REGISTERED_ONLY = 0b0000_1000; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
195 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
196 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
197 |
|
15096 | 198 |
pub struct HwServer { |
199 |
pub clients: IndexSlab<HwClient>, |
|
200 |
pub rooms: Slab<HwRoom>, |
|
201 |
pub anteroom: HwAnteroom, |
|
14804 | 202 |
pub latest_protocol: u16, |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
203 |
pub flags: ServerFlags, |
14804 | 204 |
pub greetings: ServerGreetings, |
12126 | 205 |
} |
206 |
||
15096 | 207 |
impl HwServer { |
14801 | 208 |
pub fn new(clients_limit: usize, rooms_limit: usize) -> Self { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
209 |
let rooms = Slab::with_capacity(rooms_limit); |
14714 | 210 |
let clients = IndexSlab::with_capacity(clients_limit); |
14715 | 211 |
Self { |
14478 | 212 |
clients, |
213 |
rooms, |
|
15096 | 214 |
anteroom: HwAnteroom::new(clients_limit), |
14804 | 215 |
greetings: ServerGreetings::new(), |
216 |
latest_protocol: 58, |
|
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
217 |
flags: ServerFlags::empty(), |
14715 | 218 |
} |
12126 | 219 |
} |
220 |
||
15465 | 221 |
#[inline] |
222 |
pub fn client(&self, client_id: ClientId) -> &HwClient { |
|
223 |
&self.clients[client_id] |
|
224 |
} |
|
225 |
||
226 |
#[inline] |
|
15540 | 227 |
fn client_mut(&mut self, client_id: ClientId) -> &mut HwClient { |
15465 | 228 |
&mut self.clients[client_id] |
229 |
} |
|
230 |
||
231 |
#[inline] |
|
232 |
pub fn room(&self, room_id: RoomId) -> &HwRoom { |
|
233 |
&self.rooms[room_id] |
|
234 |
} |
|
235 |
||
236 |
#[inline] |
|
237 |
pub fn room_mut(&mut self, room_id: RoomId) -> &mut HwRoom { |
|
238 |
&mut self.rooms[room_id] |
|
239 |
} |
|
240 |
||
241 |
#[inline] |
|
15504 | 242 |
pub fn client_and_room(&self, client_id: ClientId, room_id: RoomId) -> (&HwClient, &HwRoom) { |
243 |
(&self.clients[client_id], &self.rooms[room_id]) |
|
244 |
} |
|
245 |
||
246 |
#[inline] |
|
247 |
pub fn client_and_room_mut( |
|
248 |
&mut self, |
|
249 |
client_id: ClientId, |
|
250 |
room_id: RoomId, |
|
251 |
) -> (&mut HwClient, &mut HwRoom) { |
|
252 |
(&mut self.clients[client_id], &mut self.rooms[room_id]) |
|
253 |
} |
|
254 |
||
255 |
#[inline] |
|
15465 | 256 |
pub fn is_admin(&self, client_id: ClientId) -> bool { |
257 |
self.clients |
|
258 |
.get(client_id) |
|
259 |
.map(|c| c.is_admin()) |
|
260 |
.unwrap_or(false) |
|
261 |
} |
|
262 |
||
15096 | 263 |
pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) { |
14714 | 264 |
if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { |
15096 | 265 |
let mut client = HwClient::new(client_id, protocol.get(), nick); |
14802 | 266 |
client.set_is_checker(data.is_checker); |
15197 | 267 |
#[cfg(not(feature = "official-server"))] |
268 |
client.set_is_admin(data.is_local_admin); |
|
269 |
||
15465 | 270 |
#[cfg(feature = "official-server")] |
271 |
{ |
|
272 |
client.set_is_registered(info.is_registered); |
|
273 |
client.set_is_admin(info.is_admin); |
|
274 |
client.set_is_contributor(info.is_contributor); |
|
275 |
} |
|
276 |
||
14714 | 277 |
self.clients.insert(client_id, client); |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
278 |
} |
12126 | 279 |
} |
12127 | 280 |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
281 |
pub fn remove_client(&mut self, client_id: ClientId) { |
14717 | 282 |
self.clients.remove(client_id); |
12127 | 283 |
} |
284 |
||
15465 | 285 |
pub fn get_greetings(&self, client: &HwClient) -> &str { |
286 |
if client.protocol_number < self.latest_protocol { |
|
14804 | 287 |
&self.greetings.for_old_protocols |
288 |
} else { |
|
289 |
&self.greetings.for_latest_protocol |
|
290 |
} |
|
291 |
} |
|
292 |
||
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
293 |
#[inline] |
15463 | 294 |
pub fn get_client_nick(&self, client_id: ClientId) -> &str { |
295 |
&self.clients[client_id].nick |
|
296 |
} |
|
297 |
||
298 |
#[inline] |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
299 |
pub fn create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
300 |
&mut self, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
301 |
creator_id: ClientId, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
302 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
303 |
password: Option<String>, |
15463 | 304 |
) -> Result<(&HwClient, &HwRoom), CreateRoomError> { |
305 |
use CreateRoomError::*; |
|
306 |
if utils::is_name_illegal(&name) { |
|
307 |
Err(InvalidName) |
|
308 |
} else if self.has_room(&name) { |
|
309 |
Err(AlreadyExists) |
|
310 |
} else { |
|
311 |
Ok(create_room( |
|
312 |
&mut self.clients[creator_id], |
|
313 |
&mut self.rooms, |
|
314 |
name, |
|
315 |
password, |
|
316 |
)) |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
pub fn join_room( |
|
321 |
&mut self, |
|
322 |
client_id: ClientId, |
|
323 |
room_id: RoomId, |
|
324 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
|
325 |
use JoinRoomError::*; |
|
326 |
let room = &mut self.rooms[room_id]; |
|
327 |
let client = &mut self.clients[client_id]; |
|
328 |
||
329 |
if client.protocol_number != room.protocol_number { |
|
330 |
Err(WrongProtocol) |
|
331 |
} else if room.is_join_restricted() { |
|
332 |
Err(Restricted) |
|
333 |
} else if room.players_number == u8::max_value() { |
|
334 |
Err(Full) |
|
335 |
} else { |
|
336 |
move_to_room(client, room); |
|
337 |
let room_id = room.id; |
|
338 |
Ok(( |
|
339 |
&self.clients[client_id], |
|
340 |
&self.rooms[room_id], |
|
341 |
self.clients.iter().map(|(_, c)| c), |
|
342 |
)) |
|
343 |
} |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
344 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
345 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
346 |
#[inline] |
15463 | 347 |
pub fn join_room_by_name( |
348 |
&mut self, |
|
349 |
client_id: ClientId, |
|
350 |
room_name: &str, |
|
351 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
|
352 |
use JoinRoomError::*; |
|
353 |
let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
|
354 |
if let Some((_, room)) = room { |
|
355 |
let room_id = room.id; |
|
356 |
self.join_room(client_id, room_id) |
|
357 |
} else { |
|
358 |
Err(DoesntExist) |
|
359 |
} |
|
360 |
} |
|
361 |
||
15504 | 362 |
pub fn leave_room(&mut self, client_id: ClientId) -> Result<LeaveRoomResult, LeaveRoomError> { |
363 |
let client = &mut self.clients[client_id]; |
|
364 |
if let Some(room_id) = client.room_id { |
|
365 |
let room = &mut self.rooms[room_id]; |
|
366 |
||
367 |
room.players_number -= 1; |
|
368 |
client.room_id = None; |
|
369 |
||
370 |
let is_empty = room.players_number == 0; |
|
371 |
let is_fixed = room.is_fixed(); |
|
372 |
let was_master = room.master_id == Some(client_id); |
|
373 |
let was_in_game = client.is_in_game(); |
|
374 |
let mut removed_teams = vec![]; |
|
375 |
||
376 |
if is_empty && !is_fixed { |
|
377 |
if client.is_ready() && room.ready_players_number > 0 { |
|
378 |
room.ready_players_number -= 1; |
|
379 |
} |
|
380 |
||
381 |
removed_teams = room |
|
382 |
.client_teams(client.id) |
|
383 |
.map(|t| t.name.clone()) |
|
384 |
.collect(); |
|
385 |
||
386 |
for team_name in &removed_teams { |
|
387 |
room.remove_team(team_name); |
|
388 |
} |
|
389 |
||
390 |
if client.is_master() && !is_fixed { |
|
391 |
client.set_is_master(false); |
|
392 |
room.master_id = None; |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
client.set_is_ready(false); |
|
397 |
client.set_is_in_game(false); |
|
398 |
||
399 |
if !is_fixed { |
|
400 |
if room.players_number == 0 { |
|
401 |
self.rooms.remove(room_id); |
|
402 |
} else if room.master_id == None { |
|
403 |
let new_master_id = self.room_clients(room_id).next(); |
|
404 |
if let Some(new_master_id) = new_master_id { |
|
405 |
let room = &mut self.rooms[room_id]; |
|
406 |
room.master_id = Some(new_master_id); |
|
407 |
let new_master = &mut self.clients[new_master_id]; |
|
408 |
new_master.set_is_master(true); |
|
409 |
||
410 |
if room.protocol_number < 42 { |
|
411 |
room.name = new_master.nick.clone(); |
|
412 |
} |
|
413 |
||
414 |
room.set_join_restriction(false); |
|
415 |
room.set_team_add_restriction(false); |
|
416 |
room.set_unregistered_players_restriction(true); |
|
417 |
} |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
if is_empty && !is_fixed { |
|
422 |
Ok(LeaveRoomResult::RoomRemoved) |
|
423 |
} else { |
|
424 |
Ok(LeaveRoomResult::RoomRemains { |
|
425 |
is_empty, |
|
426 |
was_master, |
|
427 |
was_in_game, |
|
428 |
new_master: self.rooms[room_id].master_id, |
|
429 |
removed_teams, |
|
430 |
}) |
|
431 |
} |
|
432 |
} else { |
|
433 |
Err(LeaveRoomError::NoRoom) |
|
434 |
} |
|
435 |
} |
|
436 |
||
15509 | 437 |
pub fn change_master( |
438 |
&mut self, |
|
439 |
client_id: ClientId, |
|
440 |
room_id: RoomId, |
|
441 |
new_master_nick: String, |
|
442 |
) -> Result<ChangeMasterResult, ChangeMasterError> { |
|
443 |
let client = &mut self.clients[client_id]; |
|
444 |
let room = &mut self.rooms[room_id]; |
|
445 |
||
446 |
if client.is_admin() || room.master_id == Some(client_id) { |
|
447 |
let new_master_id = self |
|
448 |
.clients |
|
449 |
.iter() |
|
450 |
.find(|(_, c)| c.nick == new_master_nick) |
|
451 |
.map(|(id, _)| id); |
|
452 |
||
453 |
match new_master_id { |
|
454 |
Some(new_master_id) if new_master_id == client_id => { |
|
455 |
Err(ChangeMasterError::AlreadyMaster) |
|
456 |
} |
|
457 |
Some(new_master_id) => { |
|
458 |
let new_master = &mut self.clients[new_master_id]; |
|
459 |
if new_master.room_id == Some(room_id) { |
|
460 |
self.clients[new_master_id].set_is_master(true); |
|
461 |
let old_master_id = room.master_id; |
|
462 |
if let Some(master_id) = old_master_id { |
|
463 |
self.clients[master_id].set_is_master(false); |
|
464 |
} |
|
465 |
room.master_id = Some(new_master_id); |
|
466 |
Ok(ChangeMasterResult { |
|
467 |
old_master_id, |
|
468 |
new_master_id, |
|
469 |
}) |
|
470 |
} else { |
|
471 |
Err(ChangeMasterError::ClientNotInRoom) |
|
472 |
} |
|
473 |
} |
|
474 |
None => Err(ChangeMasterError::NoClient), |
|
475 |
} |
|
476 |
} else { |
|
477 |
Err(ChangeMasterError::NoAccess) |
|
478 |
} |
|
479 |
} |
|
480 |
||
15514 | 481 |
pub fn start_game(&mut self, room_id: RoomId) -> Result<Vec<String>, StartGameError> { |
482 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = self |
|
483 |
.clients |
|
484 |
.iter() |
|
485 |
.map(|(id, c)| (id, c.nick.clone())) |
|
486 |
.unzip(); |
|
487 |
||
488 |
let room = &mut self.rooms[room_id]; |
|
489 |
||
490 |
if !room.has_multiple_clans() { |
|
491 |
Err(StartGameError::NotEnoughClans) |
|
492 |
} else if room.protocol_number <= 43 && room.players_number != room.ready_players_number { |
|
493 |
Err(StartGameError::NotReady) |
|
494 |
} else if room.game_info.is_some() { |
|
495 |
Err(StartGameError::AlreadyInGame) |
|
496 |
} else { |
|
497 |
room.start_round(); |
|
498 |
for id in room_clients { |
|
499 |
let c = &mut self.clients[id]; |
|
500 |
c.set_is_in_game(true); |
|
501 |
c.team_indices = room.client_team_indices(c.id); |
|
502 |
} |
|
503 |
Ok(room_nicks) |
|
504 |
} |
|
505 |
} |
|
506 |
||
15538 | 507 |
pub fn end_game(&mut self, room_id: RoomId) -> EndGameResult { |
508 |
let room = &mut self.rooms[room_id]; |
|
509 |
room.ready_players_number = 1; |
|
510 |
||
511 |
if let Some(info) = replace(&mut room.game_info, None) { |
|
512 |
let joined_mid_game_clients = self |
|
513 |
.clients |
|
514 |
.iter() |
|
515 |
.filter(|(_, c)| c.room_id == Some(room_id) && c.is_joined_mid_game()) |
|
516 |
.map(|(_, c)| c.id) |
|
517 |
.collect(); |
|
518 |
||
519 |
let unreadied_nicks: Vec<_> = self |
|
520 |
.clients |
|
521 |
.iter_mut() |
|
522 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
|
523 |
.map(|(_, c)| { |
|
524 |
c.set_is_ready(c.is_master()); |
|
525 |
c.set_is_joined_mid_game(false); |
|
526 |
c |
|
527 |
}) |
|
528 |
.filter_map(|c| { |
|
529 |
if !c.is_master() { |
|
530 |
Some(c.nick.clone()) |
|
531 |
} else { |
|
532 |
None |
|
533 |
} |
|
534 |
}) |
|
535 |
.collect(); |
|
536 |
||
537 |
EndGameResult { |
|
538 |
joined_mid_game_clients, |
|
539 |
left_teams: info.left_teams.clone(), |
|
540 |
unreadied_nicks, |
|
541 |
} |
|
542 |
} else { |
|
543 |
unreachable!() |
|
544 |
} |
|
545 |
} |
|
546 |
||
15540 | 547 |
pub fn enable_super_power(&mut self, client_id: ClientId) -> bool { |
548 |
let client = &mut self.clients[client_id]; |
|
549 |
if client.is_admin() { |
|
550 |
client.set_has_super_power(true); |
|
551 |
} |
|
552 |
client.is_admin() |
|
553 |
} |
|
554 |
||
555 |
pub fn set_room_name( |
|
556 |
&mut self, |
|
557 |
client_id: ClientId, |
|
558 |
room_id: RoomId, |
|
559 |
mut name: String, |
|
560 |
) -> Result<String, ModifyRoomNameError> { |
|
561 |
let room_exists = self.has_room(&name); |
|
562 |
let room = &mut self.rooms[room_id]; |
|
563 |
if room.is_fixed() || room.master_id != Some(client_id) { |
|
564 |
Err(ModifyRoomNameError::AccessDenied) |
|
565 |
} else if utils::is_name_illegal(&name) { |
|
566 |
Err(ModifyRoomNameError::InvalidName) |
|
567 |
} else if room_exists { |
|
568 |
Err(ModifyRoomNameError::DuplicateName) |
|
569 |
} else { |
|
570 |
std::mem::swap(&mut room.name, &mut name); |
|
571 |
Ok(name) |
|
572 |
} |
|
573 |
} |
|
574 |
||
575 |
pub fn add_team(&mut self, client_id: ClientId) {} |
|
576 |
||
577 |
pub fn set_team_color( |
|
578 |
&mut self, |
|
579 |
client_id: ClientId, |
|
580 |
room_id: RoomId, |
|
581 |
team_name: &str, |
|
582 |
color: u8, |
|
583 |
) -> Result<(), ModifyTeamError> { |
|
584 |
let client = &self.clients[client_id]; |
|
585 |
let room = &mut self.rooms[room_id]; |
|
586 |
if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
|
587 |
if !client.is_master() { |
|
588 |
Err(ModifyTeamError::NotMaster) |
|
589 |
} else { |
|
590 |
team.color = color; |
|
591 |
self.clients[owner].clan = Some(color); |
|
592 |
Ok(()) |
|
593 |
} |
|
594 |
} else { |
|
595 |
Err(ModifyTeamError::NoTeam) |
|
596 |
} |
|
597 |
} |
|
598 |
||
599 |
pub fn toggle_ready(&mut self, client_id: ClientId) -> bool { |
|
600 |
let client = &mut self.clients[client_id]; |
|
601 |
if let Some(room_id) = client.room_id { |
|
602 |
let room = &mut self.rooms[room_id]; |
|
603 |
||
604 |
client.set_is_ready(!client.is_ready()); |
|
605 |
if client.is_ready() { |
|
606 |
room.ready_players_number += 1; |
|
607 |
} else { |
|
608 |
room.ready_players_number -= 1; |
|
609 |
} |
|
610 |
} |
|
611 |
client.is_ready() |
|
612 |
} |
|
613 |
||
15463 | 614 |
#[inline] |
615 |
pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> { |
|
616 |
if self.clients[client_id].is_admin() { |
|
617 |
match var { |
|
618 |
ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg, |
|
619 |
ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg, |
|
620 |
ServerVar::LatestProto(n) => self.latest_protocol = n, |
|
621 |
} |
|
622 |
Ok(()) |
|
623 |
} else { |
|
624 |
Err(AccessError()) |
|
625 |
} |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
626 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
627 |
|
15463 | 628 |
#[inline] |
629 |
pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> { |
|
630 |
if self.clients[client_id].is_admin() { |
|
631 |
Ok([ |
|
632 |
ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()), |
|
633 |
ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()), |
|
634 |
ServerVar::LatestProto(self.latest_protocol), |
|
635 |
]) |
|
636 |
} else { |
|
637 |
Err(AccessError()) |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> { |
|
642 |
if self.clients[client_id].is_admin() { |
|
643 |
let mut protocols: HashSet<_> = self |
|
644 |
.clients |
|
645 |
.iter() |
|
646 |
.map(|(_, c)| c.protocol_number) |
|
647 |
.chain(self.rooms.iter().map(|(_, r)| r.protocol_number)) |
|
648 |
.collect(); |
|
649 |
let mut protocols: Vec<_> = protocols.drain().collect(); |
|
650 |
protocols.sort(); |
|
651 |
Ok(protocols) |
|
652 |
} else { |
|
653 |
Err(AccessError()) |
|
654 |
} |
|
655 |
} |
|
656 |
||
657 |
#[inline] |
|
13416 | 658 |
pub fn has_room(&self, name: &str) -> bool { |
14718 | 659 |
self.find_room(name).is_some() |
13416 | 660 |
} |
661 |
||
15463 | 662 |
#[inline] |
15096 | 663 |
pub fn find_room(&self, name: &str) -> Option<&HwRoom> { |
14478 | 664 |
self.rooms |
665 |
.iter() |
|
666 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 667 |
} |
668 |
||
15096 | 669 |
pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> { |
14478 | 670 |
self.rooms |
671 |
.iter_mut() |
|
672 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 673 |
} |
674 |
||
15096 | 675 |
pub fn find_client(&self, nick: &str) -> Option<&HwClient> { |
14478 | 676 |
self.clients |
677 |
.iter() |
|
678 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 679 |
} |
680 |
||
15096 | 681 |
pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> { |
14478 | 682 |
self.clients |
683 |
.iter_mut() |
|
684 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 685 |
} |
686 |
||
14810 | 687 |
pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
688 |
self.clients.iter().map(|(id, _)| id) |
|
689 |
} |
|
690 |
||
691 |
pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a |
|
692 |
where |
|
15096 | 693 |
F: Fn(&(usize, &HwClient)) -> bool + 'a, |
14810 | 694 |
{ |
695 |
self.clients.iter().filter(f).map(|(_, c)| c.id) |
|
696 |
} |
|
697 |
||
698 |
pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a |
|
699 |
where |
|
15096 | 700 |
F: Fn(&(usize, &HwRoom)) -> bool + 'a, |
14810 | 701 |
{ |
702 |
self.rooms.iter().filter(f).map(|(_, c)| c.id) |
|
703 |
} |
|
704 |
||
14802 | 705 |
pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId> |
14478 | 706 |
where |
15096 | 707 |
F: Fn(&(usize, &HwClient)) -> bool, |
14478 | 708 |
{ |
14810 | 709 |
self.filter_clients(f).collect() |
13416 | 710 |
} |
711 |
||
14802 | 712 |
pub fn collect_nicks<F>(&self, f: F) -> Vec<String> |
713 |
where |
|
15096 | 714 |
F: Fn(&(usize, &HwClient)) -> bool, |
14802 | 715 |
{ |
716 |
self.clients |
|
717 |
.iter() |
|
718 |
.filter(f) |
|
719 |
.map(|(_, c)| c.nick.clone()) |
|
720 |
.collect() |
|
14715 | 721 |
} |
722 |
||
14810 | 723 |
pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
724 |
self.filter_clients(|(_, c)| c.room_id == None) |
|
14802 | 725 |
} |
726 |
||
14810 | 727 |
pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ { |
728 |
self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) |
|
13416 | 729 |
} |
730 |
||
14810 | 731 |
pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ { |
732 |
self.filter_clients(move |(_, c)| c.protocol_number == protocol) |
|
733 |
} |
|
734 |
||
735 |
pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ { |
|
736 |
self.filter_rooms(move |(_, r)| r.protocol_number == protocol) |
|
13416 | 737 |
} |
738 |
||
739 |
pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
|
740 |
let room_id = self.clients[self_id].room_id; |
|
14802 | 741 |
self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id) |
13416 | 742 |
} |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
743 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
744 |
pub fn is_registered_only(&self) -> bool { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
745 |
self.flags.contains(ServerFlags::REGISTERED_ONLY) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
746 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
747 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
748 |
pub fn set_is_registered_only(&mut self, value: bool) { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
749 |
self.flags.set(ServerFlags::REGISTERED_ONLY, value) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
750 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
751 |
} |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
752 |
|
15096 | 753 |
fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
754 |
let entry = rooms.vacant_entry(); |
15096 | 755 |
let room = HwRoom::new(entry.key()); |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
756 |
entry.insert(room) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
757 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
758 |
|
15463 | 759 |
fn create_room<'a, 'b>( |
760 |
client: &'a mut HwClient, |
|
761 |
rooms: &'b mut Slab<HwRoom>, |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
762 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
763 |
password: Option<String>, |
15463 | 764 |
) -> (&'a HwClient, &'b HwRoom) { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
765 |
let room = allocate_room(rooms); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
766 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
767 |
room.master_id = Some(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
768 |
room.name = name; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
769 |
room.password = password; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
770 |
room.protocol_number = client.protocol_number; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
771 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
772 |
room.players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
773 |
room.ready_players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
774 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
775 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
776 |
client.set_is_master(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
777 |
client.set_is_ready(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
778 |
client.set_is_joined_mid_game(false); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
779 |
|
15463 | 780 |
(client, room) |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
781 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
782 |
|
15096 | 783 |
fn move_to_room(client: &mut HwClient, room: &mut HwRoom) { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
784 |
debug_assert!(client.room_id != Some(room.id)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
785 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
786 |
room.players_number += 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
787 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
788 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
789 |
client.set_is_joined_mid_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
790 |
client.set_is_in_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
791 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
792 |
if let Some(ref mut info) = room.game_info { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
793 |
let teams = info.client_teams(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
794 |
client.teams_in_game = teams.clone().count() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
795 |
client.clan = teams.clone().next().map(|t| t.color); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
796 |
let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
797 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
798 |
if !team_names.is_empty() { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
799 |
info.left_teams.retain(|name| !team_names.contains(&name)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
800 |
info.teams_in_game += team_names.len() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
801 |
room.teams = info |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
802 |
.teams_at_start |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
803 |
.iter() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
804 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
805 |
.cloned() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
806 |
.collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
807 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
808 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
809 |
} |