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