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