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