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