author | sheepluva |
Sun, 08 Dec 2019 02:09:19 +0100 | |
branch | hedgeroid |
changeset 15520 | de08a3ae4f8d |
parent 15446 | 61a0bd0bb021 |
child 15487 | 4cc9ec732392 |
permissions | -rw-r--r-- |
13421 | 1 |
use super::{ |
15080 | 2 |
client::HwClient, |
14698 | 3 |
indexslab::IndexSlab, |
15080 | 4 |
room::HwRoom, |
15444 | 5 |
types::{ClientId, RoomId, ServerVar}, |
13421 | 6 |
}; |
15080 | 7 |
use crate::{protocol::messages::HwProtocolMessage::Greeting, utils}; |
14698 | 8 |
|
15444 | 9 |
use crate::core::server::JoinRoomError::WrongProtocol; |
14791
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
10 |
use bitflags::*; |
13810 | 11 |
use log::*; |
14462 | 12 |
use slab; |
15444 | 13 |
use std::{borrow::BorrowMut, collections::HashSet, iter, num::NonZeroU16}; |
12131 | 14 |
|
12857 | 15 |
type Slab<T> = slab::Slab<T>; |
12132 | 16 |
|
15446 | 17 |
#[derive(Debug)] |
15444 | 18 |
pub enum CreateRoomError { |
19 |
InvalidName, |
|
20 |
AlreadyExists, |
|
21 |
} |
|
22 |
||
15446 | 23 |
#[derive(Debug)] |
15444 | 24 |
pub enum JoinRoomError { |
25 |
DoesntExist, |
|
26 |
WrongProtocol, |
|
27 |
Full, |
|
28 |
Restricted, |
|
29 |
} |
|
30 |
||
15446 | 31 |
#[derive(Debug)] |
32 |
pub struct UninitializedError(); |
|
33 |
#[derive(Debug)] |
|
15444 | 34 |
pub struct AccessError(); |
35 |
||
15080 | 36 |
pub struct HwAnteClient { |
14698 | 37 |
pub nick: Option<String>, |
38 |
pub protocol_number: Option<NonZeroU16>, |
|
39 |
pub server_salt: String, |
|
14786 | 40 |
pub is_checker: bool, |
15181 | 41 |
pub is_local_admin: bool, |
15446 | 42 |
pub is_registered: bool, |
43 |
pub is_admin: bool, |
|
44 |
pub is_contributor: bool, |
|
14698 | 45 |
} |
46 |
||
15080 | 47 |
pub struct HwAnteroom { |
48 |
pub clients: IndexSlab<HwAnteClient>, |
|
14698 | 49 |
} |
50 |
||
15080 | 51 |
impl HwAnteroom { |
14698 | 52 |
pub fn new(clients_limit: usize) -> Self { |
53 |
let clients = IndexSlab::with_capacity(clients_limit); |
|
15080 | 54 |
HwAnteroom { clients } |
14698 | 55 |
} |
56 |
||
15181 | 57 |
pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) { |
15080 | 58 |
let client = HwAnteClient { |
14698 | 59 |
nick: None, |
60 |
protocol_number: None, |
|
61 |
server_salt: salt, |
|
14786 | 62 |
is_checker: false, |
15181 | 63 |
is_local_admin, |
15446 | 64 |
is_registered: false, |
65 |
is_admin: false, |
|
66 |
is_contributor: false, |
|
14698 | 67 |
}; |
68 |
self.clients.insert(client_id, client); |
|
69 |
} |
|
70 |
||
15080 | 71 |
pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteClient> { |
15444 | 72 |
let client = self.clients.remove(client_id); |
14698 | 73 |
client |
74 |
} |
|
75 |
} |
|
76 |
||
14788 | 77 |
pub struct ServerGreetings { |
78 |
pub for_latest_protocol: String, |
|
79 |
pub for_old_protocols: String, |
|
80 |
} |
|
81 |
||
82 |
impl ServerGreetings { |
|
83 |
fn new() -> Self { |
|
84 |
Self { |
|
85 |
for_latest_protocol: "\u{1f994} is watching".to_string(), |
|
86 |
for_old_protocols: "\u{1f994} is watching".to_string(), |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
||
14791
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
91 |
bitflags! { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
92 |
pub struct ServerFlags: u8 { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
93 |
const REGISTERED_ONLY = 0b0000_1000; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
94 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
95 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
96 |
|
15080 | 97 |
pub struct HwServer { |
98 |
pub clients: IndexSlab<HwClient>, |
|
99 |
pub rooms: Slab<HwRoom>, |
|
100 |
pub anteroom: HwAnteroom, |
|
14788 | 101 |
pub latest_protocol: u16, |
14791
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
102 |
pub flags: ServerFlags, |
14788 | 103 |
pub greetings: ServerGreetings, |
12131 | 104 |
} |
105 |
||
15080 | 106 |
impl HwServer { |
14785 | 107 |
pub fn new(clients_limit: usize, rooms_limit: usize) -> Self { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
108 |
let rooms = Slab::with_capacity(rooms_limit); |
14698 | 109 |
let clients = IndexSlab::with_capacity(clients_limit); |
14699 | 110 |
Self { |
14462 | 111 |
clients, |
112 |
rooms, |
|
15080 | 113 |
anteroom: HwAnteroom::new(clients_limit), |
14788 | 114 |
greetings: ServerGreetings::new(), |
115 |
latest_protocol: 58, |
|
14791
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
116 |
flags: ServerFlags::empty(), |
14699 | 117 |
} |
12131 | 118 |
} |
119 |
||
15446 | 120 |
#[inline] |
121 |
pub fn client(&self, client_id: ClientId) -> &HwClient { |
|
122 |
&self.clients[client_id] |
|
123 |
} |
|
124 |
||
125 |
#[inline] |
|
126 |
pub fn client_mut(&mut self, client_id: ClientId) -> &mut HwClient { |
|
127 |
&mut self.clients[client_id] |
|
128 |
} |
|
129 |
||
130 |
#[inline] |
|
131 |
pub fn room(&self, room_id: RoomId) -> &HwRoom { |
|
132 |
&self.rooms[room_id] |
|
133 |
} |
|
134 |
||
135 |
#[inline] |
|
136 |
pub fn room_mut(&mut self, room_id: RoomId) -> &mut HwRoom { |
|
137 |
&mut self.rooms[room_id] |
|
138 |
} |
|
139 |
||
140 |
#[inline] |
|
141 |
pub fn is_admin(&self, client_id: ClientId) -> bool { |
|
142 |
self.clients |
|
143 |
.get(client_id) |
|
144 |
.map(|c| c.is_admin()) |
|
145 |
.unwrap_or(false) |
|
146 |
} |
|
147 |
||
15080 | 148 |
pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) { |
14698 | 149 |
if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { |
15080 | 150 |
let mut client = HwClient::new(client_id, protocol.get(), nick); |
14786 | 151 |
client.set_is_checker(data.is_checker); |
15181 | 152 |
#[cfg(not(feature = "official-server"))] |
153 |
client.set_is_admin(data.is_local_admin); |
|
154 |
||
15446 | 155 |
#[cfg(feature = "official-server")] |
156 |
{ |
|
157 |
client.set_is_registered(info.is_registered); |
|
158 |
client.set_is_admin(info.is_admin); |
|
159 |
client.set_is_contributor(info.is_contributor); |
|
160 |
} |
|
161 |
||
14698 | 162 |
self.clients.insert(client_id, client); |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
163 |
} |
12131 | 164 |
} |
12132 | 165 |
|
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
166 |
pub fn remove_client(&mut self, client_id: ClientId) { |
14701 | 167 |
self.clients.remove(client_id); |
12132 | 168 |
} |
169 |
||
15446 | 170 |
pub fn get_greetings(&self, client: &HwClient) -> &str { |
171 |
if client.protocol_number < self.latest_protocol { |
|
14788 | 172 |
&self.greetings.for_old_protocols |
173 |
} else { |
|
174 |
&self.greetings.for_latest_protocol |
|
175 |
} |
|
176 |
} |
|
177 |
||
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
178 |
#[inline] |
15444 | 179 |
pub fn get_client_nick(&self, client_id: ClientId) -> &str { |
180 |
&self.clients[client_id].nick |
|
181 |
} |
|
182 |
||
183 |
#[inline] |
|
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
184 |
pub fn create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
185 |
&mut self, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
186 |
creator_id: ClientId, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
187 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
188 |
password: Option<String>, |
15444 | 189 |
) -> Result<(&HwClient, &HwRoom), CreateRoomError> { |
190 |
use CreateRoomError::*; |
|
191 |
if utils::is_name_illegal(&name) { |
|
192 |
Err(InvalidName) |
|
193 |
} else if self.has_room(&name) { |
|
194 |
Err(AlreadyExists) |
|
195 |
} else { |
|
196 |
Ok(create_room( |
|
197 |
&mut self.clients[creator_id], |
|
198 |
&mut self.rooms, |
|
199 |
name, |
|
200 |
password, |
|
201 |
)) |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
pub fn join_room( |
|
206 |
&mut self, |
|
207 |
client_id: ClientId, |
|
208 |
room_id: RoomId, |
|
209 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
|
210 |
use JoinRoomError::*; |
|
211 |
let room = &mut self.rooms[room_id]; |
|
212 |
let client = &mut self.clients[client_id]; |
|
213 |
||
214 |
if client.protocol_number != room.protocol_number { |
|
215 |
Err(WrongProtocol) |
|
216 |
} else if room.is_join_restricted() { |
|
217 |
Err(Restricted) |
|
218 |
} else if room.players_number == u8::max_value() { |
|
219 |
Err(Full) |
|
220 |
} else { |
|
221 |
move_to_room(client, room); |
|
222 |
let room_id = room.id; |
|
223 |
Ok(( |
|
224 |
&self.clients[client_id], |
|
225 |
&self.rooms[room_id], |
|
226 |
self.clients.iter().map(|(_, c)| c), |
|
227 |
)) |
|
228 |
} |
|
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
229 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
230 |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14509
diff
changeset
|
231 |
#[inline] |
15444 | 232 |
pub fn join_room_by_name( |
233 |
&mut self, |
|
234 |
client_id: ClientId, |
|
235 |
room_name: &str, |
|
236 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
|
237 |
use JoinRoomError::*; |
|
238 |
let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
|
239 |
if let Some((_, room)) = room { |
|
240 |
let room_id = room.id; |
|
241 |
self.join_room(client_id, room_id) |
|
242 |
} else { |
|
243 |
Err(DoesntExist) |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
#[inline] |
|
248 |
pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> { |
|
249 |
if self.clients[client_id].is_admin() { |
|
250 |
match var { |
|
251 |
ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg, |
|
252 |
ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg, |
|
253 |
ServerVar::LatestProto(n) => self.latest_protocol = n, |
|
254 |
} |
|
255 |
Ok(()) |
|
256 |
} else { |
|
257 |
Err(AccessError()) |
|
258 |
} |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14509
diff
changeset
|
259 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14509
diff
changeset
|
260 |
|
15444 | 261 |
#[inline] |
262 |
pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> { |
|
263 |
if self.clients[client_id].is_admin() { |
|
264 |
Ok([ |
|
265 |
ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()), |
|
266 |
ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()), |
|
267 |
ServerVar::LatestProto(self.latest_protocol), |
|
268 |
]) |
|
269 |
} else { |
|
270 |
Err(AccessError()) |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> { |
|
275 |
if self.clients[client_id].is_admin() { |
|
276 |
let mut protocols: HashSet<_> = self |
|
277 |
.clients |
|
278 |
.iter() |
|
279 |
.map(|(_, c)| c.protocol_number) |
|
280 |
.chain(self.rooms.iter().map(|(_, r)| r.protocol_number)) |
|
281 |
.collect(); |
|
282 |
let mut protocols: Vec<_> = protocols.drain().collect(); |
|
283 |
protocols.sort(); |
|
284 |
Ok(protocols) |
|
285 |
} else { |
|
286 |
Err(AccessError()) |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
#[inline] |
|
13421 | 291 |
pub fn has_room(&self, name: &str) -> bool { |
14702 | 292 |
self.find_room(name).is_some() |
13421 | 293 |
} |
294 |
||
15444 | 295 |
#[inline] |
15080 | 296 |
pub fn find_room(&self, name: &str) -> Option<&HwRoom> { |
14462 | 297 |
self.rooms |
298 |
.iter() |
|
299 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13421 | 300 |
} |
301 |
||
15080 | 302 |
pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> { |
14462 | 303 |
self.rooms |
304 |
.iter_mut() |
|
305 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13421 | 306 |
} |
307 |
||
15080 | 308 |
pub fn find_client(&self, nick: &str) -> Option<&HwClient> { |
14462 | 309 |
self.clients |
310 |
.iter() |
|
311 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13483 | 312 |
} |
313 |
||
15080 | 314 |
pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> { |
14462 | 315 |
self.clients |
316 |
.iter_mut() |
|
317 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13483 | 318 |
} |
319 |
||
14794 | 320 |
pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
321 |
self.clients.iter().map(|(id, _)| id) |
|
322 |
} |
|
323 |
||
324 |
pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a |
|
325 |
where |
|
15080 | 326 |
F: Fn(&(usize, &HwClient)) -> bool + 'a, |
14794 | 327 |
{ |
328 |
self.clients.iter().filter(f).map(|(_, c)| c.id) |
|
329 |
} |
|
330 |
||
331 |
pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a |
|
332 |
where |
|
15080 | 333 |
F: Fn(&(usize, &HwRoom)) -> bool + 'a, |
14794 | 334 |
{ |
335 |
self.rooms.iter().filter(f).map(|(_, c)| c.id) |
|
336 |
} |
|
337 |
||
14786 | 338 |
pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId> |
14462 | 339 |
where |
15080 | 340 |
F: Fn(&(usize, &HwClient)) -> bool, |
14462 | 341 |
{ |
14794 | 342 |
self.filter_clients(f).collect() |
13421 | 343 |
} |
344 |
||
14786 | 345 |
pub fn collect_nicks<F>(&self, f: F) -> Vec<String> |
346 |
where |
|
15080 | 347 |
F: Fn(&(usize, &HwClient)) -> bool, |
14786 | 348 |
{ |
349 |
self.clients |
|
350 |
.iter() |
|
351 |
.filter(f) |
|
352 |
.map(|(_, c)| c.nick.clone()) |
|
353 |
.collect() |
|
14699 | 354 |
} |
355 |
||
14794 | 356 |
pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
357 |
self.filter_clients(|(_, c)| c.room_id == None) |
|
14786 | 358 |
} |
359 |
||
14794 | 360 |
pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ { |
361 |
self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) |
|
13421 | 362 |
} |
363 |
||
14794 | 364 |
pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ { |
365 |
self.filter_clients(move |(_, c)| c.protocol_number == protocol) |
|
366 |
} |
|
367 |
||
368 |
pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ { |
|
369 |
self.filter_rooms(move |(_, r)| r.protocol_number == protocol) |
|
13421 | 370 |
} |
371 |
||
372 |
pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
|
373 |
let room_id = self.clients[self_id].room_id; |
|
14786 | 374 |
self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id) |
13421 | 375 |
} |
14791
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
376 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
377 |
pub fn is_registered_only(&self) -> bool { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
378 |
self.flags.contains(ServerFlags::REGISTERED_ONLY) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
379 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
380 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
381 |
pub fn set_is_registered_only(&mut self, value: bool) { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
382 |
self.flags.set(ServerFlags::REGISTERED_ONLY, value) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14788
diff
changeset
|
383 |
} |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13447
diff
changeset
|
384 |
} |
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
385 |
|
15080 | 386 |
fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom { |
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
387 |
let entry = rooms.vacant_entry(); |
15080 | 388 |
let room = HwRoom::new(entry.key()); |
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
389 |
entry.insert(room) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
390 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
391 |
|
15444 | 392 |
fn create_room<'a, 'b>( |
393 |
client: &'a mut HwClient, |
|
394 |
rooms: &'b mut Slab<HwRoom>, |
|
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
395 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
396 |
password: Option<String>, |
15444 | 397 |
) -> (&'a HwClient, &'b HwRoom) { |
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
398 |
let room = allocate_room(rooms); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
399 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
400 |
room.master_id = Some(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
401 |
room.name = name; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
402 |
room.password = password; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
403 |
room.protocol_number = client.protocol_number; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
404 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
405 |
room.players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
406 |
room.ready_players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
407 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
408 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
409 |
client.set_is_master(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
410 |
client.set_is_ready(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
411 |
client.set_is_joined_mid_game(false); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
412 |
|
15444 | 413 |
(client, room) |
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
414 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
415 |
|
15080 | 416 |
fn move_to_room(client: &mut HwClient, room: &mut HwRoom) { |
14509
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
417 |
debug_assert!(client.room_id != Some(room.id)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
418 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
419 |
room.players_number += 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
420 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
421 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
422 |
client.set_is_joined_mid_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
423 |
client.set_is_in_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
424 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
425 |
if let Some(ref mut info) = room.game_info { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
426 |
let teams = info.client_teams(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
427 |
client.teams_in_game = teams.clone().count() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
428 |
client.clan = teams.clone().next().map(|t| t.color); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
429 |
let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
430 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
431 |
if !team_names.is_empty() { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
432 |
info.left_teams.retain(|name| !team_names.contains(&name)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
433 |
info.teams_in_game += team_names.len() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
434 |
room.teams = info |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
435 |
.teams_at_start |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
436 |
.iter() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
437 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
438 |
.cloned() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
439 |
.collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
440 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
441 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
442 |
} |