12 use slab; |
12 use slab; |
13 use std::{borrow::BorrowMut, collections::HashSet, iter, num::NonZeroU16}; |
13 use std::{borrow::BorrowMut, collections::HashSet, iter, num::NonZeroU16}; |
14 |
14 |
15 type Slab<T> = slab::Slab<T>; |
15 type Slab<T> = slab::Slab<T>; |
16 |
16 |
|
17 #[derive(Debug)] |
17 pub enum CreateRoomError { |
18 pub enum CreateRoomError { |
18 InvalidName, |
19 InvalidName, |
19 AlreadyExists, |
20 AlreadyExists, |
20 } |
21 } |
21 |
22 |
|
23 #[derive(Debug)] |
22 pub enum JoinRoomError { |
24 pub enum JoinRoomError { |
23 DoesntExist, |
25 DoesntExist, |
24 WrongProtocol, |
26 WrongProtocol, |
25 Full, |
27 Full, |
26 Restricted, |
28 Restricted, |
27 } |
29 } |
28 |
30 |
|
31 #[derive(Debug)] |
|
32 pub struct UninitializedError(); |
|
33 #[derive(Debug)] |
29 pub struct AccessError(); |
34 pub struct AccessError(); |
30 |
35 |
31 pub struct HwAnteClient { |
36 pub struct HwAnteClient { |
32 pub nick: Option<String>, |
37 pub nick: Option<String>, |
33 pub protocol_number: Option<NonZeroU16>, |
38 pub protocol_number: Option<NonZeroU16>, |
34 pub server_salt: String, |
39 pub server_salt: String, |
35 pub is_checker: bool, |
40 pub is_checker: bool, |
36 pub is_local_admin: bool, |
41 pub is_local_admin: bool, |
|
42 pub is_registered: bool, |
|
43 pub is_admin: bool, |
|
44 pub is_contributor: bool, |
37 } |
45 } |
38 |
46 |
39 pub struct HwAnteroom { |
47 pub struct HwAnteroom { |
40 pub clients: IndexSlab<HwAnteClient>, |
48 pub clients: IndexSlab<HwAnteClient>, |
41 } |
49 } |
104 latest_protocol: 58, |
115 latest_protocol: 58, |
105 flags: ServerFlags::empty(), |
116 flags: ServerFlags::empty(), |
106 } |
117 } |
107 } |
118 } |
108 |
119 |
|
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 |
109 pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) { |
148 pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) { |
110 if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { |
149 if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { |
111 let mut client = HwClient::new(client_id, protocol.get(), nick); |
150 let mut client = HwClient::new(client_id, protocol.get(), nick); |
112 client.set_is_checker(data.is_checker); |
151 client.set_is_checker(data.is_checker); |
113 #[cfg(not(feature = "official-server"))] |
152 #[cfg(not(feature = "official-server"))] |
114 client.set_is_admin(data.is_local_admin); |
153 client.set_is_admin(data.is_local_admin); |
115 |
154 |
|
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 |
116 self.clients.insert(client_id, client); |
162 self.clients.insert(client_id, client); |
117 } |
163 } |
118 } |
164 } |
119 |
165 |
120 pub fn remove_client(&mut self, client_id: ClientId) { |
166 pub fn remove_client(&mut self, client_id: ClientId) { |
121 self.clients.remove(client_id); |
167 self.clients.remove(client_id); |
122 } |
168 } |
123 |
169 |
124 pub fn get_greetings(&self, client_id: ClientId) -> &str { |
170 pub fn get_greetings(&self, client: &HwClient) -> &str { |
125 if self.clients[client_id].protocol_number < self.latest_protocol { |
171 if client.protocol_number < self.latest_protocol { |
126 &self.greetings.for_old_protocols |
172 &self.greetings.for_old_protocols |
127 } else { |
173 } else { |
128 &self.greetings.for_latest_protocol |
174 &self.greetings.for_latest_protocol |
129 } |
175 } |
130 } |
176 } |