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