changeset 15533 | 0606f89698e7 |
parent 15532 | f1205f33bf5b |
child 15534 | bb93e9642b5b |
15532:f1205f33bf5b | 15533:0606f89698e7 |
---|---|
3 client::HwClient, |
3 client::HwClient, |
4 indexslab::IndexSlab, |
4 indexslab::IndexSlab, |
5 room::HwRoom, |
5 room::HwRoom, |
6 types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo, Vote, VoteType, Voting}, |
6 types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo, Vote, VoteType, Voting}, |
7 }; |
7 }; |
8 use crate::{protocol::messages::HwProtocolMessage::Greeting, utils}; |
8 use crate::utils; |
9 |
9 |
10 use bitflags::_core::hint::unreachable_unchecked; |
|
11 use bitflags::*; |
10 use bitflags::*; |
12 use log::*; |
11 use log::*; |
13 use slab::Slab; |
12 use slab::Slab; |
14 use std::{borrow::BorrowMut, cmp::min, collections::HashSet, iter, mem::replace}; |
13 use std::{borrow::BorrowMut, cmp::min, collections::HashSet, iter, mem::replace}; |
15 |
14 |
21 |
20 |
22 #[derive(Debug)] |
21 #[derive(Debug)] |
23 pub enum JoinRoomError { |
22 pub enum JoinRoomError { |
24 DoesntExist, |
23 DoesntExist, |
25 WrongProtocol, |
24 WrongProtocol, |
25 WrongPassword, |
|
26 Full, |
26 Full, |
27 Restricted, |
27 Restricted, |
28 } |
28 } |
29 |
29 |
30 #[derive(Debug)] |
30 #[derive(Debug)] |
288 &self.greetings.for_latest_protocol |
288 &self.greetings.for_latest_protocol |
289 } |
289 } |
290 } |
290 } |
291 |
291 |
292 #[inline] |
292 #[inline] |
293 pub fn get_client_nick(&self, client_id: ClientId) -> &str { |
|
294 &self.clients[client_id].nick |
|
295 } |
|
296 |
|
297 #[inline] |
|
298 pub fn create_room( |
293 pub fn create_room( |
299 &mut self, |
294 &mut self, |
300 creator_id: ClientId, |
295 creator_id: ClientId, |
301 name: String, |
296 name: String, |
302 password: Option<String>, |
297 password: Option<String>, |
318 |
313 |
319 pub fn join_room( |
314 pub fn join_room( |
320 &mut self, |
315 &mut self, |
321 client_id: ClientId, |
316 client_id: ClientId, |
322 room_id: RoomId, |
317 room_id: RoomId, |
318 room_password: Option<&str>, |
|
323 ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
319 ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
324 use JoinRoomError::*; |
320 use JoinRoomError::*; |
325 let room = &mut self.rooms[room_id]; |
321 let room = &mut self.rooms[room_id]; |
326 let client = &mut self.clients[client_id]; |
322 let client = &mut self.clients[client_id]; |
327 |
323 |
328 if client.protocol_number != room.protocol_number { |
324 if client.protocol_number != room.protocol_number { |
329 Err(WrongProtocol) |
325 Err(WrongProtocol) |
326 } else if room.password.is_some() && room_password != room.password.as_deref() { |
|
327 Err(WrongPassword) |
|
330 } else if room.is_join_restricted() { |
328 } else if room.is_join_restricted() { |
331 Err(Restricted) |
329 Err(Restricted) |
332 } else if room.players_number == u8::max_value() { |
330 } else if room.players_number == u8::max_value() { |
333 Err(Full) |
331 Err(Full) |
334 } else { |
332 } else { |
345 #[inline] |
343 #[inline] |
346 pub fn join_room_by_name( |
344 pub fn join_room_by_name( |
347 &mut self, |
345 &mut self, |
348 client_id: ClientId, |
346 client_id: ClientId, |
349 room_name: &str, |
347 room_name: &str, |
348 room_password: Option<&str>, |
|
350 ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
349 ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
351 use JoinRoomError::*; |
350 use JoinRoomError::*; |
352 let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
351 let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
353 if let Some((_, room)) = room { |
352 if let Some((_, room)) = room { |
354 let room_id = room.id; |
353 let room_id = room.id; |
355 self.join_room(client_id, room_id) |
354 self.join_room(client_id, room_id, room_password) |
356 } else { |
355 } else { |
357 Err(DoesntExist) |
356 Err(DoesntExist) |
358 } |
357 } |
359 } |
358 } |
360 |
359 |
1080 room.ready_players_number = 1; |
1079 room.ready_players_number = 1; |
1081 |
1080 |
1082 client.room_id = Some(room.id); |
1081 client.room_id = Some(room.id); |
1083 client.set_is_master(true); |
1082 client.set_is_master(true); |
1084 client.set_is_ready(true); |
1083 client.set_is_ready(true); |
1084 client.set_is_in_game(false); |
|
1085 client.set_is_joined_mid_game(false); |
1085 client.set_is_joined_mid_game(false); |
1086 client.clan = None; |
|
1087 client.teams_in_game = 0; |
|
1088 client.team_indices = vec![]; |
|
1086 |
1089 |
1087 (client, room) |
1090 (client, room) |
1088 } |
1091 } |
1089 |
1092 |
1090 fn move_to_room(client: &mut HwClient, room: &mut HwRoom) { |
1093 fn move_to_room(client: &mut HwClient, room: &mut HwRoom) { |