author | alfadur <mail@none> |
Mon, 23 Dec 2019 23:47:06 +0300 | |
changeset 15545 | f4f6060b536c |
parent 15544 | 4a0b06b03199 |
child 15547 | 16d3c9acd715 |
permissions | -rw-r--r-- |
13666 | 1 |
use crate::{ |
15096 | 2 |
core::{ |
3 |
client::HwClient, |
|
4 |
room::HwRoom, |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
5 |
server::{EndGameResult, HwServer, JoinRoomError, LeaveRoomResult, StartGameError}, |
15096 | 6 |
types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType}, |
7 |
}, |
|
13666 | 8 |
protocol::messages::{ |
15096 | 9 |
add_flags, remove_flags, server_chat, |
10 |
HwProtocolMessage::{self, Rnd}, |
|
11 |
HwServerMessage::{self, *}, |
|
14803 | 12 |
ProtocolFlags as Flags, |
14478 | 13 |
}, |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
14 |
utils::to_engine_msg, |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
15 |
}; |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
16 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
17 |
use super::Response; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
18 |
|
15095 | 19 |
use crate::core::types::RoomConfig; |
14718 | 20 |
use rand::{self, seq::SliceRandom, thread_rng, Rng}; |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
21 |
use std::{iter::once, mem::replace}; |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
22 |
|
15096 | 23 |
pub fn rnd_reply(options: &[String]) -> HwServerMessage { |
13492 | 24 |
let mut rng = thread_rng(); |
14718 | 25 |
|
13492 | 26 |
let reply = if options.is_empty() { |
14718 | 27 |
(*&["heads", "tails"].choose(&mut rng).unwrap()).to_string() |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
28 |
} else { |
14718 | 29 |
options.choose(&mut rng).unwrap().clone() |
13492 | 30 |
}; |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
31 |
|
13492 | 32 |
ChatMsg { |
14718 | 33 |
nick: "[random]".to_string(), |
34 |
msg: reply, |
|
13492 | 35 |
} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
36 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
37 |
|
15465 | 38 |
pub fn get_lobby_join_data(server: &HwServer, response: &mut Response) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
39 |
let client_id = response.client_id(); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
40 |
|
15465 | 41 |
let client = server.client(client_id); |
14812 | 42 |
let nick = vec![client.nick.clone()]; |
43 |
let mut flags = vec![]; |
|
44 |
if client.is_registered() { |
|
45 |
flags.push(Flags::Registered) |
|
46 |
} |
|
47 |
if client.is_admin() { |
|
48 |
flags.push(Flags::Admin) |
|
49 |
} |
|
50 |
if client.is_contributor() { |
|
51 |
flags.push(Flags::Contributor) |
|
52 |
} |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
53 |
|
14812 | 54 |
let all_nicks: Vec<_> = server.collect_nicks(|_| true); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
55 |
|
14812 | 56 |
let mut flag_selectors = [ |
57 |
( |
|
58 |
Flags::Registered, |
|
59 |
server.collect_nicks(|(_, c)| c.is_registered()), |
|
60 |
), |
|
61 |
(Flags::Admin, server.collect_nicks(|(_, c)| c.is_admin())), |
|
62 |
( |
|
63 |
Flags::Contributor, |
|
64 |
server.collect_nicks(|(_, c)| c.is_contributor()), |
|
65 |
), |
|
66 |
( |
|
67 |
Flags::InRoom, |
|
68 |
server.collect_nicks(|(_, c)| c.room_id.is_some()), |
|
69 |
), |
|
70 |
]; |
|
71 |
||
15465 | 72 |
let server_msg = ServerMessage(server.get_greetings(client).to_string()); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
73 |
|
14802 | 74 |
let rooms_msg = Rooms( |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
75 |
server |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
76 |
.iter_rooms() |
15544 | 77 |
.filter(|r| r.protocol_number == client.protocol_number) |
78 |
.flat_map(|r| r.info(r.master_id.map(|id| server.client(id)))) |
|
14802 | 79 |
.collect(), |
80 |
); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
81 |
|
14812 | 82 |
response.add(LobbyJoined(nick).send_all().but_self()); |
83 |
response.add( |
|
84 |
ClientFlags(add_flags(&flags), all_nicks.clone()) |
|
85 |
.send_all() |
|
86 |
.but_self(), |
|
87 |
); |
|
88 |
||
89 |
response.add(LobbyJoined(all_nicks).send_self()); |
|
90 |
for (flag, nicks) in &mut flag_selectors { |
|
14912 | 91 |
if !nicks.is_empty() { |
92 |
response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self()); |
|
93 |
} |
|
14812 | 94 |
} |
95 |
||
14802 | 96 |
response.add(server_msg.send_self()); |
97 |
response.add(rooms_msg.send_self()); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
98 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
99 |
|
15463 | 100 |
pub fn get_room_join_data<'a, I: Iterator<Item = &'a HwClient> + Clone>( |
101 |
client: &HwClient, |
|
102 |
room: &HwRoom, |
|
103 |
room_clients: I, |
|
14808 | 104 |
response: &mut Response, |
105 |
) { |
|
15463 | 106 |
#[inline] |
107 |
fn collect_nicks<'a, I, F>(clients: I, f: F) -> Vec<String> |
|
108 |
where |
|
109 |
I: Iterator<Item = &'a HwClient>, |
|
110 |
F: Fn(&&'a HwClient) -> bool, |
|
111 |
{ |
|
112 |
clients.filter(f).map(|c| &c.nick).cloned().collect() |
|
113 |
} |
|
14808 | 114 |
|
15463 | 115 |
let nick = client.nick.clone(); |
116 |
response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room.id)); |
|
14808 | 117 |
response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all()); |
15463 | 118 |
let nicks = collect_nicks(room_clients.clone(), |c| c.room_id == Some(room.id)); |
14808 | 119 |
response.add(RoomJoined(nicks).send_self()); |
120 |
||
15463 | 121 |
get_room_teams(room, client.id, response); |
122 |
get_room_config(room, client.id, response); |
|
14820 | 123 |
|
124 |
let mut flag_selectors = [ |
|
125 |
( |
|
126 |
Flags::RoomMaster, |
|
15463 | 127 |
collect_nicks(room_clients.clone(), |c| c.is_master()), |
14820 | 128 |
), |
15463 | 129 |
( |
130 |
Flags::Ready, |
|
131 |
collect_nicks(room_clients.clone(), |c| c.is_ready()), |
|
132 |
), |
|
133 |
( |
|
134 |
Flags::InGame, |
|
135 |
collect_nicks(room_clients.clone(), |c| c.is_in_game()), |
|
136 |
), |
|
14820 | 137 |
]; |
138 |
||
139 |
for (flag, nicks) in &mut flag_selectors { |
|
140 |
response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self()); |
|
141 |
} |
|
14808 | 142 |
|
143 |
if !room.greeting.is_empty() { |
|
144 |
response.add( |
|
145 |
ChatMsg { |
|
146 |
nick: "[greeting]".to_string(), |
|
147 |
msg: room.greeting.clone(), |
|
148 |
} |
|
149 |
.send_self(), |
|
150 |
); |
|
151 |
} |
|
152 |
} |
|
153 |
||
15463 | 154 |
pub fn get_room_join_error(error: JoinRoomError, response: &mut Response) { |
155 |
use super::strings::*; |
|
156 |
match error { |
|
157 |
JoinRoomError::DoesntExist => response.warn(NO_ROOM), |
|
158 |
JoinRoomError::WrongProtocol => response.warn(WRONG_PROTOCOL), |
|
159 |
JoinRoomError::Full => response.warn(ROOM_FULL), |
|
160 |
JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED), |
|
161 |
} |
|
162 |
} |
|
163 |
||
15504 | 164 |
pub fn get_remove_teams_data( |
165 |
room_id: RoomId, |
|
166 |
was_in_game: bool, |
|
167 |
removed_teams: Vec<String>, |
|
168 |
response: &mut Response, |
|
169 |
) { |
|
170 |
if was_in_game { |
|
171 |
for team_name in &removed_teams { |
|
172 |
let msg = once(b'F').chain(team_name.bytes()); |
|
173 |
response.add( |
|
174 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
|
175 |
.send_all() |
|
176 |
.in_room(room_id) |
|
177 |
.but_self(), |
|
178 |
); |
|
14711
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
179 |
|
15504 | 180 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
14711
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
181 |
|
15504 | 182 |
response.add( |
183 |
ForwardEngineMessage(vec![remove_msg]) |
|
184 |
.send_all() |
|
185 |
.in_room(room_id) |
|
186 |
.but_self(), |
|
187 |
); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
for team_name in removed_teams { |
|
192 |
response.add(TeamRemove(team_name).send_all().in_room(room_id)); |
|
193 |
} |
|
194 |
} |
|
14711
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
195 |
|
15514 | 196 |
pub fn get_room_leave_result( |
15504 | 197 |
server: &HwServer, |
198 |
room: &HwRoom, |
|
199 |
leave_message: &str, |
|
200 |
result: LeaveRoomResult, |
|
201 |
response: &mut Response, |
|
202 |
) { |
|
203 |
let client = server.client(response.client_id); |
|
204 |
response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all()); |
|
205 |
||
15538 | 206 |
match result { |
15504 | 207 |
LeaveRoomResult::RoomRemoved => { |
208 |
response.add( |
|
209 |
RoomRemove(room.name.clone()) |
|
210 |
.send_all() |
|
211 |
.with_protocol(room.protocol_number), |
|
212 |
); |
|
213 |
} |
|
14711
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
214 |
|
15504 | 215 |
LeaveRoomResult::RoomRemains { |
216 |
is_empty, |
|
217 |
was_master, |
|
218 |
new_master, |
|
219 |
was_in_game, |
|
220 |
removed_teams, |
|
221 |
} => { |
|
222 |
if !is_empty { |
|
223 |
response.add( |
|
224 |
RoomLeft(client.nick.clone(), leave_message.to_string()) |
|
225 |
.send_all() |
|
226 |
.in_room(room.id) |
|
227 |
.but_self(), |
|
228 |
); |
|
229 |
} |
|
14819 | 230 |
|
15504 | 231 |
if was_master { |
232 |
response.add( |
|
233 |
ClientFlags( |
|
234 |
remove_flags(&[Flags::RoomMaster]), |
|
235 |
vec![client.nick.clone()], |
|
236 |
) |
|
237 |
.send_all() |
|
238 |
.in_room(room.id), |
|
239 |
); |
|
14819 | 240 |
|
15504 | 241 |
if let Some(new_master_id) = new_master { |
242 |
let new_master_nick = server.client(new_master_id).nick.clone(); |
|
14819 | 243 |
response.add( |
244 |
ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick]) |
|
245 |
.send_all() |
|
246 |
.in_room(room.id), |
|
247 |
); |
|
14715 | 248 |
} |
14711
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
249 |
} |
15504 | 250 |
|
251 |
get_remove_teams_data(room.id, was_in_game, removed_teams, response); |
|
252 |
||
253 |
response.add( |
|
254 |
RoomUpdated(room.name.clone(), room.info(Some(&client))) |
|
255 |
.send_all() |
|
256 |
.with_protocol(room.protocol_number), |
|
257 |
); |
|
14711
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
258 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
259 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
260 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14710
diff
changeset
|
261 |
|
15096 | 262 |
pub fn remove_client(server: &mut HwServer, response: &mut Response, msg: String) { |
14695
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
263 |
let client_id = response.client_id(); |
15504 | 264 |
let client = server.client(client_id); |
14718 | 265 |
let nick = client.nick.clone(); |
14695
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
266 |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
267 |
if let Some(mut room_control) = server.get_room_control(client_id) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
268 |
let room_id = room_control.room().id; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
269 |
let result = room_control.leave_room(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
270 |
get_room_leave_result(server, server.room(room_id), &msg, result, response); |
15504 | 271 |
} |
14695
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
272 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
273 |
server.remove_client(client_id); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
274 |
|
15465 | 275 |
response.add(LobbyLeft(nick, msg.clone()).send_all()); |
276 |
response.add(Bye(msg).send_self()); |
|
14717 | 277 |
response.remove_client(client_id); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
278 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
279 |
|
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
280 |
pub fn get_room_update( |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
281 |
room_name: Option<String>, |
15096 | 282 |
room: &HwRoom, |
283 |
master: Option<&HwClient>, |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
284 |
response: &mut Response, |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
285 |
) { |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
286 |
let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master)); |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
287 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
288 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
289 |
|
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
290 |
pub fn get_room_config_impl(config: &RoomConfig, to_client: ClientId, response: &mut Response) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
291 |
response.add(ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config()).send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
292 |
for cfg in config.to_game_config() { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
293 |
response.add(cfg.to_server_msg().send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
294 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
295 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
296 |
|
15096 | 297 |
pub fn get_room_config(room: &HwRoom, to_client: ClientId, response: &mut Response) { |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
298 |
get_room_config_impl(room.active_config(), to_client, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
299 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
300 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
301 |
pub fn get_teams<'a, I>(teams: I, to_client: ClientId, response: &mut Response) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
302 |
where |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
303 |
I: Iterator<Item = &'a TeamInfo>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
304 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
305 |
for team in teams { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
306 |
response.add(TeamAdd(team.to_protocol()).send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
307 |
response.add(TeamColor(team.name.clone(), team.color).send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
308 |
response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send(to_client)); |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
309 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
310 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
311 |
|
15463 | 312 |
pub fn get_room_teams(room: &HwRoom, to_client: ClientId, response: &mut Response) { |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
313 |
let current_teams = match room.game_info { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
314 |
Some(ref info) => &info.teams_at_start, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
315 |
None => &room.teams, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
316 |
}; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
317 |
|
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
318 |
get_teams(current_teams.iter().map(|(_, t)| t), to_client, response); |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
319 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
320 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
321 |
pub fn get_room_flags( |
15096 | 322 |
server: &HwServer, |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
323 |
room_id: RoomId, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
324 |
to_client: ClientId, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
325 |
response: &mut Response, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
326 |
) { |
15544 | 327 |
let room = server.room(room_id); |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
328 |
if let Some(id) = room.master_id { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
329 |
response.add( |
14803 | 330 |
ClientFlags( |
331 |
add_flags(&[Flags::RoomMaster]), |
|
15514 | 332 |
vec![server.client(id).nick.clone()], |
14803 | 333 |
) |
334 |
.send(to_client), |
|
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
335 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
336 |
} |
15514 | 337 |
let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id) && c.is_ready()); |
338 |
||
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
339 |
if !nicks.is_empty() { |
14803 | 340 |
response.add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send(to_client)); |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
341 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
342 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
343 |
|
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
344 |
pub fn apply_voting_result( |
15096 | 345 |
server: &mut HwServer, |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
346 |
room_id: RoomId, |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
347 |
response: &mut Response, |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
348 |
kind: VoteType, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
349 |
) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
350 |
match kind { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
351 |
VoteType::Kick(nick) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
352 |
if let Some(kicked_client) = server.find_client(&nick) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
353 |
let kicked_id = kicked_client.id; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
354 |
if kicked_client.room_id == Some(room_id) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
355 |
if let Some(mut room_control) = server.get_room_control(kicked_client.id) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
356 |
response.add(Kicked.send(kicked_id)); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
357 |
let result = room_control.leave_room(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
358 |
get_room_leave_result( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
359 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
360 |
room_control.room(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
361 |
"kicked", |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
362 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
363 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
364 |
); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
365 |
} |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
366 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
367 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
368 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
369 |
VoteType::Map(None) => (), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
370 |
VoteType::Map(Some(name)) => { |
15544 | 371 |
if let Some(location) = server.room_mut(room_id).load_config(&name) { |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
372 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
373 |
server_chat(location.to_string()) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
374 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
375 |
.in_room(room_id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
376 |
); |
15544 | 377 |
let room = &server.room(room_id); |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
378 |
let room_master = if let Some(id) = room.master_id { |
15514 | 379 |
Some(server.client(id)) |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
380 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
381 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
382 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
383 |
get_room_update(None, room, room_master, response); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
384 |
|
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15538
diff
changeset
|
385 |
for client in server.iter_clients() { |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
386 |
if client.room_id == Some(room_id) { |
15544 | 387 |
super::common::get_room_config(server.room(room_id), client.id, response); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
388 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
389 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
390 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
391 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
392 |
VoteType::Pause => { |
15544 | 393 |
if let Some(ref mut info) = server.room_mut(room_id).game_info { |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
394 |
info.is_paused = !info.is_paused; |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
395 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
396 |
server_chat("Pause toggled.".to_string()) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
397 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
398 |
.in_room(room_id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
399 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
400 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
401 |
ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
402 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
403 |
.in_room(room_id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
404 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
405 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
406 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
407 |
VoteType::NewSeed => { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
408 |
let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
409 |
let cfg = GameCfg::Seed(seed); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
410 |
response.add(cfg.to_server_msg().send_all().in_room(room_id)); |
15544 | 411 |
server.room_mut(room_id).set_config(cfg); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
412 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
413 |
VoteType::HedgehogsPerTeam(number) => { |
15544 | 414 |
let r = server.room_mut(room_id); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
415 |
let nicks = r.set_hedgehogs_number(number); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
416 |
|
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
417 |
response.extend( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
418 |
nicks |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
419 |
.into_iter() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
420 |
.map(|n| HedgehogsNumber(n, number).send_all().in_room(room_id)), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
421 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
422 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
423 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
424 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
425 |
|
15096 | 426 |
fn add_vote(room: &mut HwRoom, response: &mut Response, vote: Vote) -> Option<bool> { |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
427 |
let client_id = response.client_id; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
428 |
let mut result = None; |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
429 |
|
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
430 |
if let Some(ref mut voting) = room.voting { |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
431 |
if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) { |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
432 |
response.add(server_chat("Your vote has been counted.".to_string()).send_self()); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
433 |
voting.votes.push((client_id, vote.is_pro)); |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
434 |
let i = voting.votes.iter(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
435 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
436 |
let contra = i.filter(|(_, v)| !*v).count(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
437 |
let success_quota = voting.voters.len() / 2 + 1; |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
438 |
if vote.is_forced && vote.is_pro || pro >= success_quota { |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
439 |
result = Some(true); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
440 |
} else if vote.is_forced && !vote.is_pro || contra > voting.voters.len() - success_quota |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
441 |
{ |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
442 |
result = Some(false); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
443 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
444 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
445 |
response.add(server_chat("You already have voted.".to_string()).send_self()); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
446 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
447 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
448 |
response.add(server_chat("There's no voting going on.".to_string()).send_self()); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
449 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
450 |
|
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
451 |
result |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
452 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
453 |
|
15096 | 454 |
pub fn submit_vote(server: &mut HwServer, vote: Vote, response: &mut Response) { |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
455 |
let client_id = response.client_id; |
15514 | 456 |
let client = server.client(client_id); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
457 |
|
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
458 |
if let Some(room_id) = client.room_id { |
15514 | 459 |
let room = server.room_mut(room_id); |
14708
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
460 |
|
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
461 |
if let Some(res) = add_vote(room, response, vote) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
462 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
463 |
server_chat("Voting closed.".to_string()) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
464 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
465 |
.in_room(room.id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
466 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
467 |
let voting = replace(&mut room.voting, None).unwrap(); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
468 |
if res { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
469 |
apply_voting_result(server, room_id, response, voting.kind); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14707
diff
changeset
|
470 |
} |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
471 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
472 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
473 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
474 |
|
15514 | 475 |
pub fn get_start_game_data( |
476 |
server: &HwServer, |
|
477 |
room_id: RoomId, |
|
478 |
result: Result<Vec<String>, StartGameError>, |
|
479 |
response: &mut Response, |
|
480 |
) { |
|
481 |
match result { |
|
482 |
Ok(room_nicks) => { |
|
483 |
let room = server.room(room_id); |
|
484 |
response.add(RunGame.send_all().in_room(room.id)); |
|
485 |
response.add( |
|
486 |
ClientFlags(add_flags(&[Flags::InGame]), room_nicks) |
|
487 |
.send_all() |
|
488 |
.in_room(room.id), |
|
489 |
); |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
490 |
|
15514 | 491 |
let room_master = room.master_id.map(|id| server.client(id)); |
492 |
get_room_update(None, room, room_master, response); |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
493 |
} |
15514 | 494 |
Err(StartGameError::NotEnoughClans) => { |
495 |
response.warn("The game can't be started with less than two clans!") |
|
496 |
} |
|
497 |
Err(StartGameError::NotEnoughTeams) => (), |
|
498 |
Err(StartGameError::NotReady) => response.warn("Not all players are ready"), |
|
15538 | 499 |
Err(StartGameError::AlreadyInGame) => response.warn("The game is already in progress"), |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
500 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
501 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
502 |
|
15538 | 503 |
pub fn get_end_game_result( |
504 |
server: &HwServer, |
|
505 |
room_id: RoomId, |
|
506 |
result: EndGameResult, |
|
507 |
response: &mut Response, |
|
508 |
) { |
|
509 |
let room = server.room(room_id); |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
510 |
let room_master = if let Some(id) = room.master_id { |
15538 | 511 |
Some(server.client(id)) |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
512 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
513 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
514 |
}; |
15538 | 515 |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
516 |
get_room_update(None, room, room_master, response); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
517 |
response.add(RoundFinished.send_all().in_room(room_id)); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
518 |
|
15538 | 519 |
for client_id in result.joined_mid_game_clients { |
520 |
super::common::get_room_config(room, client_id, response); |
|
521 |
response.extend( |
|
522 |
result |
|
523 |
.left_teams |
|
524 |
.iter() |
|
525 |
.map(|name| TeamRemove(name.clone()).send(client_id)), |
|
526 |
); |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
527 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
528 |
|
15538 | 529 |
if !result.unreadied_nicks.is_empty() { |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
530 |
let msg = if room.protocol_number < 38 { |
15538 | 531 |
LegacyReady(false, result.unreadied_nicks) |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
532 |
} else { |
15538 | 533 |
ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks) |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
534 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
535 |
response.add(msg.send_all().in_room(room_id)); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
536 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
537 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
538 |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
539 |
#[cfg(test)] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
540 |
mod tests { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
541 |
use super::*; |
15132 | 542 |
use crate::handlers::actions::PendingMessage; |
15096 | 543 |
use crate::protocol::messages::HwServerMessage::ChatMsg; |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
544 |
|
15096 | 545 |
fn reply2string(r: HwServerMessage) -> String { |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
546 |
match r { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
547 |
ChatMsg { msg: p, .. } => String::from(p), |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
548 |
_ => panic!("expected a ChatMsg"), |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
549 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
550 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
551 |
|
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
552 |
fn run_handle_test(opts: Vec<String>) { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
553 |
let opts2 = opts.clone(); |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
554 |
for opt in opts { |
13492 | 555 |
while reply2string(rnd_reply(&opts2)) != opt {} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
556 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
557 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
558 |
|
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
559 |
/// This test terminates almost surely. |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
560 |
#[test] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
561 |
fn test_handle_rnd_empty() { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
562 |
run_handle_test(vec![]) |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
563 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
564 |
|
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
565 |
/// This test terminates almost surely. |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
566 |
#[test] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
567 |
fn test_handle_rnd_nonempty() { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
568 |
run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()]) |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
569 |
} |
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
570 |
|
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
571 |
/// This test terminates almost surely (strong law of large numbers) |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
572 |
#[test] |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
573 |
fn test_distribution() { |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
574 |
let eps = 0.000001; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
575 |
let lim = 0.5; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
576 |
let opts = vec![0.to_string(), 1.to_string()]; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
577 |
let mut ones = 0; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
578 |
let mut tries = 0; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
579 |
|
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
580 |
while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps { |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
581 |
tries += 1; |
13492 | 582 |
if reply2string(rnd_reply(&opts)) == 1.to_string() { |
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
583 |
ones += 1; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
584 |
} |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
585 |
} |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
586 |
} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
587 |
} |