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