author | alfadur |
Sun, 17 May 2020 04:07:10 +0300 | |
changeset 15591 | 4b2f3228f13b |
parent 15575 | 852682c2003f |
child 15719 | 25371cae9fd7 |
permissions | -rw-r--r-- |
13416 | 1 |
use super::{ |
15542 | 2 |
anteroom::HwAnteroomClient, |
15096 | 3 |
client::HwClient, |
14714 | 4 |
indexslab::IndexSlab, |
15096 | 5 |
room::HwRoom, |
15547 | 6 |
types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo, Vote, VoteType, Voting}, |
13416 | 7 |
}; |
15555 | 8 |
use crate::utils; |
14714 | 9 |
|
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
10 |
use bitflags::*; |
13810 | 11 |
use log::*; |
15539 | 12 |
use slab::Slab; |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
13 |
use std::{borrow::BorrowMut, cmp::min, collections::HashSet, iter, mem::replace}; |
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, |
|
15555 | 25 |
WrongPassword, |
15463 | 26 |
Full, |
27 |
Restricted, |
|
15556 | 28 |
RegistrationRequired, |
15463 | 29 |
} |
30 |
||
15540 | 31 |
#[derive(Debug)] |
15504 | 32 |
pub enum LeaveRoomResult { |
33 |
RoomRemoved, |
|
34 |
RoomRemains { |
|
35 |
is_empty: bool, |
|
36 |
was_master: bool, |
|
37 |
was_in_game: bool, |
|
38 |
new_master: Option<ClientId>, |
|
39 |
removed_teams: Vec<String>, |
|
40 |
}, |
|
41 |
} |
|
42 |
||
43 |
#[derive(Debug)] |
|
15509 | 44 |
pub struct ChangeMasterResult { |
45 |
pub old_master_id: Option<ClientId>, |
|
46 |
pub new_master_id: ClientId, |
|
47 |
} |
|
48 |
||
49 |
#[derive(Debug)] |
|
50 |
pub enum ChangeMasterError { |
|
51 |
NoAccess, |
|
52 |
AlreadyMaster, |
|
53 |
NoClient, |
|
54 |
ClientNotInRoom, |
|
55 |
} |
|
56 |
||
57 |
#[derive(Debug)] |
|
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
58 |
pub enum AddTeamError { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
59 |
TooManyTeams, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
60 |
TooManyHedgehogs, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
61 |
TeamAlreadyExists, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
62 |
GameInProgress, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
63 |
Restricted, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
64 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
65 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
66 |
#[derive(Debug)] |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
67 |
pub enum RemoveTeamError { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
68 |
NoTeam, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
69 |
TeamNotOwned, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
70 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
71 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
72 |
#[derive(Debug)] |
15540 | 73 |
pub enum ModifyTeamError { |
74 |
NoTeam, |
|
75 |
NotMaster, |
|
76 |
} |
|
77 |
||
78 |
#[derive(Debug)] |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
79 |
pub enum SetTeamCountError { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
80 |
InvalidNumber, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
81 |
NotMaster, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
82 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
83 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
84 |
#[derive(Debug)] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
85 |
pub enum SetHedgehogsError { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
86 |
NoTeam, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
87 |
InvalidNumber(u8), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
88 |
NotMaster, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
89 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
90 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
91 |
#[derive(Debug)] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
92 |
pub enum SetConfigError { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
93 |
NotMaster, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
94 |
RoomFixed, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
95 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
96 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
97 |
#[derive(Debug)] |
15540 | 98 |
pub enum ModifyRoomNameError { |
99 |
AccessDenied, |
|
100 |
InvalidName, |
|
101 |
DuplicateName, |
|
102 |
} |
|
103 |
||
104 |
#[derive(Debug)] |
|
15547 | 105 |
pub enum StartVoteError { |
106 |
VotingInProgress, |
|
107 |
} |
|
108 |
||
109 |
#[derive(Debug)] |
|
110 |
pub enum VoteResult { |
|
111 |
Submitted, |
|
112 |
Succeeded(VoteType), |
|
113 |
Failed, |
|
114 |
} |
|
115 |
||
116 |
#[derive(Debug)] |
|
117 |
pub enum VoteError { |
|
118 |
NoVoting, |
|
119 |
AlreadyVoted, |
|
120 |
} |
|
121 |
||
122 |
#[derive(Debug)] |
|
15514 | 123 |
pub enum StartGameError { |
124 |
NotEnoughClans, |
|
125 |
NotReady, |
|
126 |
AlreadyInGame, |
|
127 |
} |
|
128 |
||
129 |
#[derive(Debug)] |
|
15538 | 130 |
pub struct EndGameResult { |
131 |
pub left_teams: Vec<String>, |
|
132 |
pub unreadied_nicks: Vec<String>, |
|
133 |
} |
|
134 |
||
135 |
#[derive(Debug)] |
|
15465 | 136 |
pub struct UninitializedError(); |
137 |
#[derive(Debug)] |
|
15463 | 138 |
pub struct AccessError(); |
139 |
||
14804 | 140 |
pub struct ServerGreetings { |
141 |
pub for_latest_protocol: String, |
|
142 |
pub for_old_protocols: String, |
|
143 |
} |
|
144 |
||
145 |
impl ServerGreetings { |
|
146 |
fn new() -> Self { |
|
147 |
Self { |
|
148 |
for_latest_protocol: "\u{1f994} is watching".to_string(), |
|
149 |
for_old_protocols: "\u{1f994} is watching".to_string(), |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
154 |
bitflags! { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
155 |
pub struct ServerFlags: u8 { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
156 |
const REGISTERED_ONLY = 0b0000_1000; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
157 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
158 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
159 |
|
15554 | 160 |
struct HwChecker { |
161 |
pub id: ClientId, |
|
162 |
pub is_ready: bool, |
|
163 |
} |
|
164 |
||
165 |
impl HwChecker { |
|
166 |
pub fn new(id: ClientId) -> Self { |
|
167 |
Self { |
|
168 |
id, |
|
169 |
is_ready: false, |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
||
15096 | 174 |
pub struct HwServer { |
15542 | 175 |
clients: IndexSlab<HwClient>, |
15544 | 176 |
rooms: Slab<HwRoom>, |
15554 | 177 |
checkers: IndexSlab<HwChecker>, |
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
178 |
latest_protocol: u16, |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
179 |
flags: ServerFlags, |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
180 |
greetings: ServerGreetings, |
12126 | 181 |
} |
182 |
||
15096 | 183 |
impl HwServer { |
14801 | 184 |
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
|
185 |
let rooms = Slab::with_capacity(rooms_limit); |
14714 | 186 |
let clients = IndexSlab::with_capacity(clients_limit); |
15554 | 187 |
let checkers = IndexSlab::new(); |
14715 | 188 |
Self { |
14478 | 189 |
clients, |
190 |
rooms, |
|
15554 | 191 |
checkers, |
14804 | 192 |
greetings: ServerGreetings::new(), |
193 |
latest_protocol: 58, |
|
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
194 |
flags: ServerFlags::empty(), |
14715 | 195 |
} |
12126 | 196 |
} |
197 |
||
15465 | 198 |
#[inline] |
199 |
pub fn client(&self, client_id: ClientId) -> &HwClient { |
|
200 |
&self.clients[client_id] |
|
201 |
} |
|
202 |
||
203 |
#[inline] |
|
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
204 |
pub fn has_client(&self, client_id: ClientId) -> bool { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
205 |
self.clients.contains(client_id) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
206 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
207 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
208 |
#[inline] |
15556 | 209 |
pub fn iter_clients(&self) -> impl Iterator<Item = &HwClient> + Clone { |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
210 |
self.clients.iter().map(|(_, c)| c) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
211 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
212 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
213 |
#[inline] |
15465 | 214 |
pub fn room(&self, room_id: RoomId) -> &HwRoom { |
215 |
&self.rooms[room_id] |
|
216 |
} |
|
217 |
||
218 |
#[inline] |
|
15544 | 219 |
pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> { |
220 |
self.rooms.get(room_id) |
|
221 |
} |
|
222 |
||
223 |
#[inline] |
|
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
224 |
fn get_room_mut(&mut self, room_id: RoomId) -> Option<&mut HwRoom> { |
15544 | 225 |
self.rooms.get_mut(room_id) |
226 |
} |
|
227 |
||
228 |
#[inline] |
|
229 |
pub fn iter_rooms(&self) -> impl Iterator<Item = &HwRoom> { |
|
230 |
self.rooms.iter().map(|(_, r)| r) |
|
231 |
} |
|
232 |
||
233 |
#[inline] |
|
15504 | 234 |
pub fn client_and_room(&self, client_id: ClientId, room_id: RoomId) -> (&HwClient, &HwRoom) { |
235 |
(&self.clients[client_id], &self.rooms[room_id]) |
|
236 |
} |
|
237 |
||
238 |
#[inline] |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
239 |
fn client_and_room_mut(&mut self, client_id: ClientId) -> Option<(&mut HwClient, &mut HwRoom)> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
240 |
let client = &mut self.clients[client_id]; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
241 |
if let Some(room_id) = client.room_id { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
242 |
Some((client, &mut self.rooms[room_id])) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
243 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
244 |
None |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
245 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
246 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
247 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
248 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
249 |
pub fn get_room_control(&mut self, client_id: ClientId) -> Option<HwRoomControl> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
250 |
HwRoomControl::new(self, client_id) |
15504 | 251 |
} |
252 |
||
253 |
#[inline] |
|
15465 | 254 |
pub fn is_admin(&self, client_id: ClientId) -> bool { |
255 |
self.clients |
|
256 |
.get(client_id) |
|
257 |
.map(|c| c.is_admin()) |
|
258 |
.unwrap_or(false) |
|
259 |
} |
|
260 |
||
15542 | 261 |
pub fn add_client(&mut self, client_id: ClientId, data: HwAnteroomClient) { |
15554 | 262 |
if data.is_checker { |
263 |
self.checkers.insert(client_id, HwChecker::new(client_id)); |
|
264 |
} else if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { |
|
15096 | 265 |
let mut client = HwClient::new(client_id, protocol.get(), nick); |
15197 | 266 |
#[cfg(not(feature = "official-server"))] |
267 |
client.set_is_admin(data.is_local_admin); |
|
268 |
||
15465 | 269 |
#[cfg(feature = "official-server")] |
270 |
{ |
|
15553 | 271 |
client.set_is_registered(data.is_registered); |
272 |
client.set_is_admin(data.is_admin); |
|
273 |
client.set_is_contributor(data.is_contributor); |
|
15465 | 274 |
} |
275 |
||
14714 | 276 |
self.clients.insert(client_id, client); |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
277 |
} |
12126 | 278 |
} |
12127 | 279 |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
280 |
pub fn remove_client(&mut self, client_id: ClientId) { |
14717 | 281 |
self.clients.remove(client_id); |
12127 | 282 |
} |
283 |
||
15465 | 284 |
pub fn get_greetings(&self, client: &HwClient) -> &str { |
285 |
if client.protocol_number < self.latest_protocol { |
|
14804 | 286 |
&self.greetings.for_old_protocols |
287 |
} else { |
|
288 |
&self.greetings.for_latest_protocol |
|
289 |
} |
|
290 |
} |
|
291 |
||
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
292 |
#[inline] |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
293 |
pub fn create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
294 |
&mut self, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
295 |
creator_id: ClientId, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
296 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
297 |
password: Option<String>, |
15463 | 298 |
) -> Result<(&HwClient, &HwRoom), CreateRoomError> { |
299 |
use CreateRoomError::*; |
|
300 |
if utils::is_name_illegal(&name) { |
|
301 |
Err(InvalidName) |
|
302 |
} else if self.has_room(&name) { |
|
303 |
Err(AlreadyExists) |
|
304 |
} else { |
|
305 |
Ok(create_room( |
|
306 |
&mut self.clients[creator_id], |
|
307 |
&mut self.rooms, |
|
308 |
name, |
|
309 |
password, |
|
310 |
)) |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
pub fn join_room( |
|
315 |
&mut self, |
|
316 |
client_id: ClientId, |
|
317 |
room_id: RoomId, |
|
15555 | 318 |
room_password: Option<&str>, |
15463 | 319 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
320 |
use JoinRoomError::*; |
|
321 |
let room = &mut self.rooms[room_id]; |
|
322 |
let client = &mut self.clients[client_id]; |
|
323 |
||
324 |
if client.protocol_number != room.protocol_number { |
|
325 |
Err(WrongProtocol) |
|
15556 | 326 |
} else if room.password.is_some() |
327 |
&& room_password != room.password.as_deref() |
|
328 |
&& !client.has_super_power() |
|
329 |
{ |
|
15555 | 330 |
Err(WrongPassword) |
15463 | 331 |
} else if room.is_join_restricted() { |
332 |
Err(Restricted) |
|
15556 | 333 |
} else if room.is_registration_required() { |
334 |
Err(RegistrationRequired) |
|
15463 | 335 |
} else if room.players_number == u8::max_value() { |
336 |
Err(Full) |
|
337 |
} else { |
|
338 |
move_to_room(client, room); |
|
339 |
let room_id = room.id; |
|
340 |
Ok(( |
|
341 |
&self.clients[client_id], |
|
342 |
&self.rooms[room_id], |
|
15556 | 343 |
self.iter_clients() |
344 |
.filter(move |c| c.room_id == Some(room_id)), |
|
15463 | 345 |
)) |
346 |
} |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
347 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
348 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
349 |
#[inline] |
15463 | 350 |
pub fn join_room_by_name( |
351 |
&mut self, |
|
352 |
client_id: ClientId, |
|
353 |
room_name: &str, |
|
15555 | 354 |
room_password: Option<&str>, |
15463 | 355 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
356 |
use JoinRoomError::*; |
|
357 |
let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
|
358 |
if let Some((_, room)) = room { |
|
359 |
let room_id = room.id; |
|
15555 | 360 |
self.join_room(client_id, room_id, room_password) |
15463 | 361 |
} else { |
362 |
Err(DoesntExist) |
|
363 |
} |
|
364 |
} |
|
365 |
||
15540 | 366 |
pub fn enable_super_power(&mut self, client_id: ClientId) -> bool { |
367 |
let client = &mut self.clients[client_id]; |
|
368 |
if client.is_admin() { |
|
369 |
client.set_has_super_power(true); |
|
370 |
} |
|
371 |
client.is_admin() |
|
372 |
} |
|
373 |
||
15463 | 374 |
#[inline] |
375 |
pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> { |
|
376 |
if self.clients[client_id].is_admin() { |
|
377 |
match var { |
|
378 |
ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg, |
|
379 |
ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg, |
|
380 |
ServerVar::LatestProto(n) => self.latest_protocol = n, |
|
381 |
} |
|
382 |
Ok(()) |
|
383 |
} else { |
|
384 |
Err(AccessError()) |
|
385 |
} |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
386 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
387 |
|
15463 | 388 |
#[inline] |
389 |
pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> { |
|
390 |
if self.clients[client_id].is_admin() { |
|
391 |
Ok([ |
|
392 |
ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()), |
|
393 |
ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()), |
|
394 |
ServerVar::LatestProto(self.latest_protocol), |
|
395 |
]) |
|
396 |
} else { |
|
397 |
Err(AccessError()) |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> { |
|
402 |
if self.clients[client_id].is_admin() { |
|
403 |
let mut protocols: HashSet<_> = self |
|
404 |
.clients |
|
405 |
.iter() |
|
406 |
.map(|(_, c)| c.protocol_number) |
|
407 |
.chain(self.rooms.iter().map(|(_, r)| r.protocol_number)) |
|
408 |
.collect(); |
|
409 |
let mut protocols: Vec<_> = protocols.drain().collect(); |
|
410 |
protocols.sort(); |
|
411 |
Ok(protocols) |
|
412 |
} else { |
|
413 |
Err(AccessError()) |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
#[inline] |
|
13416 | 418 |
pub fn has_room(&self, name: &str) -> bool { |
14718 | 419 |
self.find_room(name).is_some() |
13416 | 420 |
} |
421 |
||
15463 | 422 |
#[inline] |
15096 | 423 |
pub fn find_room(&self, name: &str) -> Option<&HwRoom> { |
14478 | 424 |
self.rooms |
425 |
.iter() |
|
426 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 427 |
} |
428 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
429 |
fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> { |
14478 | 430 |
self.rooms |
431 |
.iter_mut() |
|
432 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 433 |
} |
434 |
||
15096 | 435 |
pub fn find_client(&self, nick: &str) -> Option<&HwClient> { |
14478 | 436 |
self.clients |
437 |
.iter() |
|
438 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 439 |
} |
440 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
441 |
fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> { |
14478 | 442 |
self.clients |
443 |
.iter_mut() |
|
444 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 445 |
} |
446 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
447 |
pub fn iter_client_ids(&self) -> impl Iterator<Item = ClientId> + '_ { |
14810 | 448 |
self.clients.iter().map(|(id, _)| id) |
449 |
} |
|
450 |
||
451 |
pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a |
|
452 |
where |
|
15096 | 453 |
F: Fn(&(usize, &HwClient)) -> bool + 'a, |
14810 | 454 |
{ |
455 |
self.clients.iter().filter(f).map(|(_, c)| c.id) |
|
456 |
} |
|
457 |
||
458 |
pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a |
|
459 |
where |
|
15096 | 460 |
F: Fn(&(usize, &HwRoom)) -> bool + 'a, |
14810 | 461 |
{ |
462 |
self.rooms.iter().filter(f).map(|(_, c)| c.id) |
|
463 |
} |
|
464 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
465 |
pub fn collect_client_ids<F>(&self, f: F) -> Vec<ClientId> |
14478 | 466 |
where |
15096 | 467 |
F: Fn(&(usize, &HwClient)) -> bool, |
14478 | 468 |
{ |
14810 | 469 |
self.filter_clients(f).collect() |
13416 | 470 |
} |
471 |
||
14802 | 472 |
pub fn collect_nicks<F>(&self, f: F) -> Vec<String> |
473 |
where |
|
15096 | 474 |
F: Fn(&(usize, &HwClient)) -> bool, |
14802 | 475 |
{ |
476 |
self.clients |
|
477 |
.iter() |
|
478 |
.filter(f) |
|
479 |
.map(|(_, c)| c.nick.clone()) |
|
480 |
.collect() |
|
14715 | 481 |
} |
482 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
483 |
pub fn lobby_client_ids(&self) -> impl Iterator<Item = ClientId> + '_ { |
14810 | 484 |
self.filter_clients(|(_, c)| c.room_id == None) |
14802 | 485 |
} |
486 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
487 |
pub fn room_client_ids(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ { |
14810 | 488 |
self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) |
13416 | 489 |
} |
490 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
491 |
pub fn protocol_client_ids(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ { |
14810 | 492 |
self.filter_clients(move |(_, c)| c.protocol_number == protocol) |
493 |
} |
|
494 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
495 |
pub fn protocol_room_ids(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ { |
14810 | 496 |
self.filter_rooms(move |(_, r)| r.protocol_number == protocol) |
13416 | 497 |
} |
498 |
||
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
499 |
pub fn other_client_ids_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
13416 | 500 |
let room_id = self.clients[self_id].room_id; |
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
501 |
self.collect_client_ids(|(id, c)| *id != self_id && c.room_id == room_id) |
13416 | 502 |
} |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
503 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
504 |
pub fn is_registered_only(&self) -> bool { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
505 |
self.flags.contains(ServerFlags::REGISTERED_ONLY) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
506 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
507 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
508 |
pub fn set_is_registered_only(&mut self, value: bool) { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
509 |
self.flags.set(ServerFlags::REGISTERED_ONLY, value) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
510 |
} |
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
511 |
|
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
512 |
pub fn set_room_saves(&mut self, room_id: RoomId, text: &str) -> Result<(), serde_yaml::Error> { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
513 |
if let Some(room) = self.rooms.get_mut(room_id) { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
514 |
room.set_saves(text) |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
515 |
} else { |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
516 |
Ok(()) |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
517 |
} |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
518 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
519 |
} |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
520 |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
521 |
pub struct HwRoomControl<'a> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
522 |
server: &'a mut HwServer, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
523 |
client_id: ClientId, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
524 |
room_id: RoomId, |
15574 | 525 |
is_room_removed: bool, |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
526 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
527 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
528 |
impl<'a> HwRoomControl<'a> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
529 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
530 |
pub fn new(server: &'a mut HwServer, client_id: ClientId) -> Option<Self> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
531 |
if let Some(room_id) = server.clients[client_id].room_id { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
532 |
Some(Self { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
533 |
server, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
534 |
client_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
535 |
room_id, |
15574 | 536 |
is_room_removed: false, |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
537 |
}) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
538 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
539 |
None |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
540 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
541 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
542 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
543 |
#[inline] |
15573 | 544 |
pub fn cleanup_room(self) { |
545 |
if self.is_room_removed { |
|
546 |
self.server.rooms.remove(self.room_id); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
#[inline] |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
551 |
pub fn server(&self) -> &HwServer { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
552 |
self.server |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
553 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
554 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
555 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
556 |
pub fn client(&self) -> &HwClient { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
557 |
&self.server.clients[self.client_id] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
558 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
559 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
560 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
561 |
fn client_mut(&mut self) -> &mut HwClient { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
562 |
&mut self.server.clients[self.client_id] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
563 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
564 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
565 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
566 |
pub fn room(&self) -> &HwRoom { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
567 |
&self.server.rooms[self.room_id] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
568 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
569 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
570 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
571 |
fn room_mut(&mut self) -> &mut HwRoom { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
572 |
&mut self.server.rooms[self.room_id] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
573 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
574 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
575 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
576 |
pub fn get(&self) -> (&HwClient, &HwRoom) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
577 |
(self.client(), self.room()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
578 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
579 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
580 |
#[inline] |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
581 |
fn get_mut(&mut self) -> (&mut HwClient, &mut HwRoom) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
582 |
( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
583 |
&mut self.server.clients[self.client_id], |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
584 |
&mut self.server.rooms[self.room_id], |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
585 |
) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
586 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
587 |
|
15547 | 588 |
pub fn change_client<'b: 'a>(self, client_id: ClientId) -> Option<HwRoomControl<'a>> { |
589 |
let room_id = self.room_id; |
|
590 |
HwRoomControl::new(self.server, client_id).filter(|c| c.room_id == room_id) |
|
591 |
} |
|
592 |
||
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
593 |
pub fn leave_room(&mut self) -> LeaveRoomResult { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
594 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
595 |
room.players_number -= 1; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
596 |
client.room_id = None; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
597 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
598 |
let is_empty = room.players_number == 0; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
599 |
let is_fixed = room.is_fixed(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
600 |
let was_master = room.master_id == Some(client.id); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
601 |
let was_in_game = client.is_in_game(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
602 |
let mut removed_teams = vec![]; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
603 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
604 |
if is_empty && !is_fixed { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
605 |
if client.is_ready() && room.ready_players_number > 0 { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
606 |
room.ready_players_number -= 1; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
607 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
608 |
|
15591 | 609 |
if let Some(ref mut info) = room.game_info { |
610 |
removed_teams = info |
|
611 |
.client_teams(client.id) |
|
612 |
.map(|t| t.name.clone()) |
|
613 |
.collect(); |
|
614 |
info.mark_left_teams(removed_teams.iter()); |
|
615 |
} else { |
|
616 |
removed_teams = room |
|
617 |
.client_teams(client.id) |
|
618 |
.map(|t| t.name.clone()) |
|
619 |
.collect(); |
|
620 |
for team_name in &removed_teams { |
|
621 |
room.remove_team(team_name); |
|
622 |
} |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
623 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
624 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
625 |
if client.is_master() && !is_fixed { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
626 |
client.set_is_master(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
627 |
room.master_id = None; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
628 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
629 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
630 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
631 |
client.set_is_ready(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
632 |
client.set_is_in_game(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
633 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
634 |
if !is_fixed { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
635 |
if room.players_number == 0 { |
15573 | 636 |
self.is_room_removed = true |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
637 |
} else if room.master_id == None { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
638 |
let protocol_number = room.protocol_number; |
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
639 |
let new_master_id = self.server.room_client_ids(self.room_id).next(); |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
640 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
641 |
if let Some(new_master_id) = new_master_id { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
642 |
let room = self.room_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
643 |
room.master_id = Some(new_master_id); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
644 |
let new_master = &mut self.server.clients[new_master_id]; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
645 |
new_master.set_is_master(true); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
646 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
647 |
if protocol_number < 42 { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
648 |
let nick = new_master.nick.clone(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
649 |
self.room_mut().name = nick; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
650 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
651 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
652 |
let room = self.room_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
653 |
room.set_join_restriction(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
654 |
room.set_team_add_restriction(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
655 |
room.set_unregistered_players_restriction(true); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
656 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
657 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
658 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
659 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
660 |
if is_empty && !is_fixed { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
661 |
LeaveRoomResult::RoomRemoved |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
662 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
663 |
LeaveRoomResult::RoomRemains { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
664 |
is_empty, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
665 |
was_master, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
666 |
was_in_game, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
667 |
new_master: self.room().master_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
668 |
removed_teams, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
669 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
670 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
671 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
672 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
673 |
pub fn change_master( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
674 |
&mut self, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
675 |
new_master_nick: String, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
676 |
) -> Result<ChangeMasterResult, ChangeMasterError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
677 |
use ChangeMasterError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
678 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
679 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
680 |
if client.is_admin() || room.master_id == Some(client.id) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
681 |
let new_master_id = self |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
682 |
.server |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
683 |
.clients |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
684 |
.iter() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
685 |
.find(|(_, c)| c.nick == new_master_nick) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
686 |
.map(|(id, _)| id); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
687 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
688 |
match new_master_id { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
689 |
Some(new_master_id) if new_master_id == self.client_id => Err(AlreadyMaster), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
690 |
Some(new_master_id) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
691 |
let new_master = &mut self.server.clients[new_master_id]; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
692 |
if new_master.room_id == Some(self.room_id) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
693 |
self.server.clients[new_master_id].set_is_master(true); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
694 |
let old_master_id = self.room().master_id; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
695 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
696 |
if let Some(master_id) = old_master_id { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
697 |
self.server.clients[master_id].set_is_master(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
698 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
699 |
self.room_mut().master_id = Some(new_master_id); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
700 |
Ok(ChangeMasterResult { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
701 |
old_master_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
702 |
new_master_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
703 |
}) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
704 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
705 |
Err(ClientNotInRoom) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
706 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
707 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
708 |
None => Err(NoClient), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
709 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
710 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
711 |
Err(NoAccess) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
712 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
713 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
714 |
|
15547 | 715 |
pub fn start_vote(&mut self, kind: VoteType) -> Result<(), StartVoteError> { |
716 |
use StartVoteError::*; |
|
717 |
match self.room().voting { |
|
718 |
Some(_) => Err(VotingInProgress), |
|
719 |
None => { |
|
15548
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15547
diff
changeset
|
720 |
let voting = Voting::new(kind, self.server.room_client_ids(self.room_id).collect()); |
15547 | 721 |
self.room_mut().voting = Some(voting); |
722 |
Ok(()) |
|
723 |
} |
|
724 |
} |
|
725 |
} |
|
726 |
||
727 |
pub fn vote(&mut self, vote: Vote) -> Result<VoteResult, VoteError> { |
|
728 |
use self::{VoteError::*, VoteResult::*}; |
|
729 |
let client_id = self.client_id; |
|
730 |
if let Some(ref mut voting) = self.room_mut().voting { |
|
731 |
if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) { |
|
732 |
voting.votes.push((client_id, vote.is_pro)); |
|
733 |
let i = voting.votes.iter(); |
|
734 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
|
735 |
let contra = i.filter(|(_, v)| !*v).count(); |
|
736 |
let success_quota = voting.voters.len() / 2 + 1; |
|
737 |
if vote.is_forced && vote.is_pro || pro >= success_quota { |
|
738 |
let voting = self.room_mut().voting.take().unwrap(); |
|
739 |
Ok(Succeeded(voting.kind)) |
|
740 |
} else if vote.is_forced && !vote.is_pro |
|
741 |
|| contra > voting.voters.len() - success_quota |
|
742 |
{ |
|
743 |
Ok(Failed) |
|
744 |
} else { |
|
745 |
Ok(Submitted) |
|
746 |
} |
|
747 |
} else { |
|
748 |
Err(AlreadyVoted) |
|
749 |
} |
|
750 |
} else { |
|
751 |
Err(NoVoting) |
|
752 |
} |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
753 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
754 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
755 |
pub fn toggle_flag(&mut self, flags: super::room::RoomFlags) -> bool { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
756 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
757 |
if client.is_master() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
758 |
room.flags.toggle(flags); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
759 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
760 |
client.is_master() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
761 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
762 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
763 |
pub fn fix_room(&mut self) -> Result<(), AccessError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
764 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
765 |
if client.is_admin() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
766 |
room.set_is_fixed(true); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
767 |
room.set_join_restriction(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
768 |
room.set_team_add_restriction(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
769 |
room.set_unregistered_players_restriction(true); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
770 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
771 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
772 |
Err(AccessError()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
773 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
774 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
775 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
776 |
pub fn unfix_room(&mut self) -> Result<(), AccessError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
777 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
778 |
if client.is_admin() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
779 |
room.set_is_fixed(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
780 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
781 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
782 |
Err(AccessError()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
783 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
784 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
785 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
786 |
pub fn set_room_name(&mut self, mut name: String) -> Result<String, ModifyRoomNameError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
787 |
use ModifyRoomNameError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
788 |
let room_exists = self.server.has_room(&name); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
789 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
790 |
if room.is_fixed() || room.master_id != Some(client.id) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
791 |
Err(AccessDenied) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
792 |
} else if utils::is_name_illegal(&name) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
793 |
Err(InvalidName) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
794 |
} else if room_exists { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
795 |
Err(DuplicateName) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
796 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
797 |
std::mem::swap(&mut room.name, &mut name); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
798 |
Ok(name) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
799 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
800 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
801 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
802 |
pub fn set_room_greeting(&mut self, greeting: Option<String>) -> Result<(), AccessError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
803 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
804 |
if client.is_admin() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
805 |
room.greeting = greeting.unwrap_or(String::new()); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
806 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
807 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
808 |
Err(AccessError()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
809 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
810 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
811 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
812 |
pub fn set_room_max_teams(&mut self, count: u8) -> Result<(), SetTeamCountError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
813 |
use SetTeamCountError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
814 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
815 |
if !client.is_master() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
816 |
Err(NotMaster) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
817 |
} else if !(2..=super::room::MAX_TEAMS_IN_ROOM).contains(&count) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
818 |
Err(InvalidNumber) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
819 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
820 |
room.max_teams = count; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
821 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
822 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
823 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
824 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
825 |
pub fn set_team_hedgehogs_number( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
826 |
&mut self, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
827 |
team_name: &str, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
828 |
number: u8, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
829 |
) -> Result<(), SetHedgehogsError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
830 |
use SetHedgehogsError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
831 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
832 |
let addable_hedgehogs = room.addable_hedgehogs(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
833 |
if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
834 |
let max_hedgehogs = min( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
835 |
super::room::MAX_HEDGEHOGS_IN_ROOM, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
836 |
addable_hedgehogs + team.hedgehogs_number, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
837 |
); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
838 |
if !client.is_master() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
839 |
Err(NotMaster) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
840 |
} else if !(1..=max_hedgehogs).contains(&number) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
841 |
Err(InvalidNumber(team.hedgehogs_number)) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
842 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
843 |
team.hedgehogs_number = number; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
844 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
845 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
846 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
847 |
Err(NoTeam) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
848 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
849 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
850 |
|
15547 | 851 |
pub fn set_hedgehogs_number(&mut self, number: u8) -> Vec<String> { |
852 |
self.room_mut().set_hedgehogs_number(number) |
|
853 |
} |
|
854 |
||
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
855 |
pub fn add_team(&mut self, mut info: Box<TeamInfo>) -> Result<&TeamInfo, AddTeamError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
856 |
use AddTeamError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
857 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
858 |
if room.teams.len() >= room.max_teams as usize { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
859 |
Err(TooManyTeams) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
860 |
} else if room.addable_hedgehogs() == 0 { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
861 |
Err(TooManyHedgehogs) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
862 |
} else if room.find_team(|t| t.name == info.name) != None { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
863 |
Err(TeamAlreadyExists) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
864 |
} else if room.game_info.is_some() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
865 |
Err(GameInProgress) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
866 |
} else if room.is_team_add_restricted() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
867 |
Err(Restricted) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
868 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
869 |
info.owner = client.nick.clone(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
870 |
let team = room.add_team(client.id, *info, client.protocol_number < 42); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
871 |
client.teams_in_game += 1; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
872 |
client.clan = Some(team.color); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
873 |
Ok(team) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
874 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
875 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
876 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
877 |
pub fn remove_team(&mut self, team_name: &str) -> Result<(), RemoveTeamError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
878 |
use RemoveTeamError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
879 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
880 |
match room.find_team_owner(team_name) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
881 |
None => Err(NoTeam), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
882 |
Some((id, _)) if id != client.id => Err(RemoveTeamError::TeamNotOwned), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
883 |
Some(_) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
884 |
client.teams_in_game -= 1; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
885 |
client.clan = room.find_team_color(client.id); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
886 |
room.remove_team(team_name); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
887 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
888 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
889 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
890 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
891 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
892 |
pub fn set_team_color(&mut self, team_name: &str, color: u8) -> Result<(), ModifyTeamError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
893 |
use ModifyTeamError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
894 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
895 |
if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
896 |
if !client.is_master() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
897 |
Err(NotMaster) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
898 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
899 |
team.color = color; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
900 |
self.server.clients[owner].clan = Some(color); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
901 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
902 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
903 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
904 |
Err(NoTeam) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
905 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
906 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
907 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
908 |
pub fn set_config(&mut self, cfg: GameCfg) -> Result<(), SetConfigError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
909 |
use SetConfigError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
910 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
911 |
if room.is_fixed() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
912 |
Err(RoomFixed) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
913 |
} else if !client.is_master() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
914 |
Err(NotMaster) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
915 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
916 |
let cfg = match cfg { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
917 |
GameCfg::Scheme(name, mut values) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
918 |
if client.protocol_number == 49 && values.len() >= 2 { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
919 |
let mut s = "X".repeat(50); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
920 |
s.push_str(&values.pop().unwrap()); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
921 |
values.push(s); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
922 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
923 |
GameCfg::Scheme(name, values) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
924 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
925 |
cfg => cfg, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
926 |
}; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
927 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
928 |
room.set_config(cfg); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
929 |
Ok(()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
930 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
931 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
932 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
933 |
pub fn save_config(&mut self, name: String, location: String) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
934 |
self.room_mut().save_config(name, location); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
935 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
936 |
|
15547 | 937 |
pub fn load_config(&mut self, name: &str) -> Option<&str> { |
938 |
self.room_mut().load_config(name) |
|
939 |
} |
|
940 |
||
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
941 |
pub fn delete_config(&mut self, name: &str) -> bool { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
942 |
self.room_mut().delete_config(name) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
943 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
944 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
945 |
pub fn toggle_ready(&mut self) -> bool { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
946 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
947 |
client.set_is_ready(!client.is_ready()); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
948 |
if client.is_ready() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
949 |
room.ready_players_number += 1; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
950 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
951 |
room.ready_players_number -= 1; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
952 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
953 |
client.is_ready() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
954 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
955 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
956 |
pub fn start_game(&mut self) -> Result<Vec<String>, StartGameError> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
957 |
use StartGameError::*; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
958 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = self |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
959 |
.server |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
960 |
.clients |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
961 |
.iter() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
962 |
.map(|(id, c)| (id, c.nick.clone())) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
963 |
.unzip(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
964 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
965 |
let room = self.room_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
966 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
967 |
if !room.has_multiple_clans() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
968 |
Err(NotEnoughClans) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
969 |
} else if room.protocol_number <= 43 && room.players_number != room.ready_players_number { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
970 |
Err(NotReady) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
971 |
} else if room.game_info.is_some() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
972 |
Err(AlreadyInGame) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
973 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
974 |
room.start_round(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
975 |
for id in room_clients { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
976 |
let team_indices = self.room().client_team_indices(id); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
977 |
let c = &mut self.server.clients[id]; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
978 |
c.set_is_in_game(true); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
979 |
c.team_indices = team_indices; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
980 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
981 |
Ok(room_nicks) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
982 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
983 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
984 |
|
15547 | 985 |
pub fn toggle_pause(&mut self) -> bool { |
986 |
if let Some(ref mut info) = self.room_mut().game_info { |
|
987 |
info.is_paused = !info.is_paused; |
|
988 |
} |
|
989 |
self.room_mut().game_info.is_some() |
|
990 |
} |
|
991 |
||
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
992 |
pub fn leave_game(&mut self) -> Option<Vec<String>> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
993 |
let (client, room) = self.get_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
994 |
let client_left = client.is_in_game(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
995 |
if client_left { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
996 |
client.set_is_in_game(false); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
997 |
|
15575 | 998 |
if let Some(ref mut info) = room.game_info { |
999 |
let team_names: Vec<_> = info |
|
1000 |
.client_teams(client.id) |
|
1001 |
.map(|t| t.name.clone()) |
|
1002 |
.collect(); |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1003 |
|
15591 | 1004 |
info.mark_left_teams(team_names.iter()); |
15575 | 1005 |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1006 |
Some(team_names) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1007 |
} else { |
15575 | 1008 |
None |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1009 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1010 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1011 |
None |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1012 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1013 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1014 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1015 |
pub fn end_game(&mut self) -> Option<EndGameResult> { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1016 |
let room = self.room_mut(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1017 |
room.ready_players_number = room.master_id.is_some() as u8; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1018 |
|
15563 | 1019 |
if let Some(mut info) = replace(&mut room.game_info, None) { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1020 |
let room_id = room.id; |
15563 | 1021 |
for team_name in &info.left_teams { |
1022 |
room.remove_team(team_name); |
|
1023 |
} |
|
1024 |
||
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1025 |
let unreadied_nicks: Vec<_> = self |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1026 |
.server |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1027 |
.clients |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1028 |
.iter_mut() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1029 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1030 |
.map(|(_, c)| { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1031 |
c.set_is_ready(c.is_master()); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1032 |
c |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1033 |
}) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1034 |
.filter_map(|c| { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1035 |
if !c.is_master() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1036 |
Some(c.nick.clone()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1037 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1038 |
None |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1039 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1040 |
}) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1041 |
.collect(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1042 |
|
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1043 |
Some(EndGameResult { |
15563 | 1044 |
left_teams: replace(&mut info.left_teams, vec![]), |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1045 |
unreadied_nicks, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1046 |
}) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1047 |
} else { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1048 |
None |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1049 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1050 |
} |
15549 | 1051 |
|
1052 |
pub fn log_engine_msg(&mut self, log_msg: String, sync_msg: Option<Option<String>>) { |
|
1053 |
if let Some(ref mut info) = self.room_mut().game_info { |
|
1054 |
if !log_msg.is_empty() { |
|
1055 |
info.msg_log.push(log_msg); |
|
1056 |
} |
|
1057 |
if let Some(msg) = sync_msg { |
|
1058 |
info.sync_msg = msg; |
|
1059 |
} |
|
1060 |
} |
|
1061 |
} |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1062 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
1063 |
|
15096 | 1064 |
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
|
1065 |
let entry = rooms.vacant_entry(); |
15096 | 1066 |
let room = HwRoom::new(entry.key()); |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1067 |
entry.insert(room) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1068 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1069 |
|
15463 | 1070 |
fn create_room<'a, 'b>( |
1071 |
client: &'a mut HwClient, |
|
1072 |
rooms: &'b mut Slab<HwRoom>, |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1073 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1074 |
password: Option<String>, |
15463 | 1075 |
) -> (&'a HwClient, &'b HwRoom) { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1076 |
let room = allocate_room(rooms); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1077 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1078 |
room.master_id = Some(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1079 |
room.name = name; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1080 |
room.password = password; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1081 |
room.protocol_number = client.protocol_number; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1082 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1083 |
room.players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1084 |
room.ready_players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1085 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1086 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1087 |
client.set_is_master(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1088 |
client.set_is_ready(true); |
15555 | 1089 |
client.set_is_in_game(false); |
1090 |
client.clan = None; |
|
1091 |
client.teams_in_game = 0; |
|
1092 |
client.team_indices = vec![]; |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1093 |
|
15463 | 1094 |
(client, room) |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1095 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1096 |
|
15096 | 1097 |
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
|
1098 |
debug_assert!(client.room_id != Some(room.id)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1099 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1100 |
room.players_number += 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1101 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1102 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1103 |
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
|
1104 |
|
15562 | 1105 |
if let Some(ref mut info) = room.game_info { |
1106 |
let teams = info.client_teams(client.id); |
|
1107 |
client.teams_in_game = teams.clone().count() as u8; |
|
1108 |
client.clan = teams.clone().next().map(|t| t.color); |
|
1109 |
let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect(); |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1110 |
|
15562 | 1111 |
if !team_names.is_empty() { |
1112 |
info.left_teams.retain(|name| !team_names.contains(&name)); |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1113 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1114 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
1115 |
} |