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