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