author | alfadur |
Wed, 10 Apr 2019 23:56:53 +0300 | |
changeset 14785 | a1077e8d26f4 |
parent 14784 | 8390d5e4e39c |
child 14787 | 0e64acbc3f8b |
permissions | -rw-r--r-- |
13666 | 1 |
use crate::{ |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
2 |
protocol::messages::server_chat, |
13666 | 3 |
protocol::messages::{ |
14782 | 4 |
add_flags, remove_flags, |
14457 | 5 |
HWProtocolMessage::{self, Rnd}, |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
6 |
HWServerMessage::{self, *}, |
14782 | 7 |
ProtocolFlags as Flags, |
14457 | 8 |
}, |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
9 |
server::{ |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
10 |
client::HWClient, |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
11 |
core::HWServer, |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
12 |
coretypes::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType}, |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
13 |
room::HWRoom, |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
14 |
}, |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
15 |
utils::to_engine_msg, |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
16 |
}; |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
17 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
18 |
use super::Response; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
19 |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
20 |
use crate::server::coretypes::RoomConfig; |
14697 | 21 |
use rand::{self, seq::SliceRandom, thread_rng, Rng}; |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
22 |
use std::{iter::once, mem::replace}; |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
23 |
|
13524 | 24 |
pub fn rnd_reply(options: &[String]) -> HWServerMessage { |
13521 | 25 |
let mut rng = thread_rng(); |
14697 | 26 |
|
13521 | 27 |
let reply = if options.is_empty() { |
14697 | 28 |
(*&["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
|
29 |
} else { |
14697 | 30 |
options.choose(&mut rng).unwrap().clone() |
13521 | 31 |
}; |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
32 |
|
13521 | 33 |
ChatMsg { |
14697 | 34 |
nick: "[random]".to_string(), |
35 |
msg: reply, |
|
13521 | 36 |
} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
37 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
38 |
|
14781 | 39 |
pub fn join_lobby(server: &mut HWServer, response: &mut Response) { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
40 |
let client_id = response.client_id(); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
41 |
|
14781 | 42 |
let lobby_nicks: Vec<_> = server.collect_nicks(|(_, c)| c.room_id.is_none()); |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
43 |
|
14781 | 44 |
let joined_msg = LobbyJoined(lobby_nicks); |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
45 |
|
14781 | 46 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
47 |
let flags_msg = ClientFlags( |
|
14782 | 48 |
add_flags(&[Flags::InRoom]), |
14781 | 49 |
server.collect_nicks(|(_, c)| c.room_id.is_some()), |
50 |
); |
|
14783 | 51 |
let server_msg = ServerMessage(server.get_greetings(client_id).to_string()); |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
52 |
|
14781 | 53 |
let rooms_msg = Rooms( |
54 |
server |
|
55 |
.rooms |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
56 |
.iter() |
14781 | 57 |
.flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id]))) |
58 |
.collect(), |
|
59 |
); |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
60 |
|
14781 | 61 |
response.add(everyone_msg.send_all().but_self()); |
62 |
response.add(joined_msg.send_self()); |
|
63 |
response.add(flags_msg.send_self()); |
|
64 |
response.add(server_msg.send_self()); |
|
65 |
response.add(rooms_msg.send_self()); |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
66 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
67 |
|
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
68 |
pub fn remove_teams( |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
69 |
room: &mut HWRoom, |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
70 |
team_names: Vec<String>, |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
71 |
is_in_game: bool, |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
72 |
response: &mut Response, |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
73 |
) { |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
74 |
if let Some(ref mut info) = room.game_info { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
75 |
for team_name in &team_names { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
76 |
info.left_teams.push(team_name.clone()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
77 |
|
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
78 |
if is_in_game { |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
79 |
let msg = once(b'F').chain(team_name.bytes()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
80 |
response.add( |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
81 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
82 |
.send_all() |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
83 |
.in_room(room.id) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
84 |
.but_self(), |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
85 |
); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
86 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
87 |
info.teams_in_game -= 1; |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
88 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
89 |
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:
14673
diff
changeset
|
90 |
if let Some(m) = &info.sync_msg { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
91 |
info.msg_log.push(m.clone()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
92 |
info.sync_msg = None |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
93 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
94 |
info.msg_log.push(remove_msg.clone()); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
95 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
96 |
response.add( |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
97 |
ForwardEngineMessage(vec![remove_msg]) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
98 |
.send_all() |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
99 |
.in_room(room.id) |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
100 |
.but_self(), |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
101 |
); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
102 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
103 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
104 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
105 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
106 |
for team_name in team_names { |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
107 |
room.remove_team(&team_name); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
108 |
response.add(TeamRemove(team_name).send_all().in_room(room.id)); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
109 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
110 |
} |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
111 |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
112 |
fn remove_client_from_room( |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
113 |
client: &mut HWClient, |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
114 |
room: &mut HWRoom, |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
115 |
response: &mut Response, |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
116 |
msg: &str, |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
117 |
) { |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
118 |
if room.players_number > 1 || room.is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
119 |
room.players_number -= 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
120 |
if client.is_ready() && room.ready_players_number > 0 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
121 |
room.ready_players_number -= 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
122 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
123 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
124 |
let team_names: Vec<_> = room |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
125 |
.client_teams(client.id) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
126 |
.map(|t| t.name.clone()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
127 |
.collect(); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
128 |
remove_teams(room, team_names, client.is_in_game(), response); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
129 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
130 |
if room.players_number > 0 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
131 |
response.add( |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
132 |
RoomLeft(client.nick.clone(), msg.to_string()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
133 |
.send_all() |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
134 |
.in_room(room.id) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
135 |
.but_self(), |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
136 |
); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
137 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
138 |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
139 |
if client.is_master() && !room.is_fixed() { |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
140 |
client.set_is_master(false); |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
141 |
response.add( |
14783 | 142 |
ClientFlags( |
143 |
remove_flags(&[Flags::RoomMaster]), |
|
144 |
vec![client.nick.clone()], |
|
145 |
) |
|
146 |
.send_all() |
|
147 |
.in_room(room.id), |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
148 |
); |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
149 |
room.master_id = None; |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
150 |
} |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
151 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
152 |
|
14694 | 153 |
client.room_id = None; |
154 |
||
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
155 |
let update_msg = if room.players_number == 0 && !room.is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
156 |
RoomRemove(room.name.clone()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
157 |
} else { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
158 |
RoomUpdated(room.name.clone(), room.info(Some(&client))) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
159 |
}; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
160 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
161 |
|
14782 | 162 |
response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all()); |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
163 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14674
diff
changeset
|
164 |
|
14784 | 165 |
pub fn change_master( |
166 |
server: &mut HWServer, |
|
167 |
from_id: ClientId, |
|
168 |
to_id: ClientId, |
|
169 |
room_id: RoomId, |
|
170 |
response: &mut Response, |
|
171 |
) { |
|
172 |
server.clients[from_id].set_is_master(false); |
|
173 |
server.clients[to_id].set_is_master(true); |
|
174 |
server.rooms[room_id].master_id = Some(to_id); |
|
175 |
||
176 |
response.add( |
|
177 |
ClientFlags( |
|
178 |
add_flags(&[Flags::RoomMaster]), |
|
179 |
vec![server.clients[to_id].nick.clone()], |
|
180 |
) |
|
181 |
.send_all() |
|
182 |
.in_room(room_id), |
|
183 |
); |
|
184 |
response.add( |
|
185 |
ClientFlags( |
|
186 |
remove_flags(&[Flags::RoomMaster]), |
|
187 |
vec![server.clients[from_id].nick.clone()], |
|
188 |
) |
|
189 |
.send_all() |
|
190 |
.in_room(room_id), |
|
191 |
); |
|
192 |
} |
|
193 |
||
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
194 |
pub fn exit_room(server: &mut HWServer, client_id: ClientId, response: &mut Response, msg: &str) { |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
195 |
let client = &mut server.clients[client_id]; |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
196 |
|
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
197 |
if let Some(room_id) = client.room_id { |
14694 | 198 |
let room = &mut server.rooms[room_id]; |
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
199 |
|
14694 | 200 |
remove_client_from_room(client, room, response, msg); |
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
201 |
|
14694 | 202 |
if !room.is_fixed() && room.master_id == None { |
14781 | 203 |
if let Some(new_master_id) = server.collect_room_clients(room_id).first().cloned() { |
14694 | 204 |
let new_master_nick = server.clients[new_master_id].nick.clone(); |
205 |
let room = &mut server.rooms[room_id]; |
|
206 |
room.master_id = Some(new_master_id); |
|
207 |
server.clients[new_master_id].set_is_master(true); |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
208 |
|
14694 | 209 |
if room.protocol_number < 42 { |
210 |
room.name = new_master_nick.clone(); |
|
211 |
} |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
212 |
|
14694 | 213 |
room.set_join_restriction(false); |
214 |
room.set_team_add_restriction(false); |
|
215 |
room.set_unregistered_players_restriction(true); |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
216 |
|
14694 | 217 |
response.add( |
14782 | 218 |
ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick]) |
14694 | 219 |
.send_all() |
220 |
.in_room(room.id), |
|
221 |
); |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
222 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
223 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
224 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
225 |
} |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
226 |
|
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
227 |
pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) { |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
228 |
let client_id = response.client_id(); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
229 |
let client = &mut server.clients[client_id]; |
14697 | 230 |
let nick = client.nick.clone(); |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
231 |
|
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
232 |
exit_room(server, client_id, response, &msg); |
14674
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
233 |
|
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
234 |
server.remove_client(client_id); |
b87c71ccd17d
Server action refactoring part 5 of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
235 |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
236 |
response.add(LobbyLeft(nick, msg.to_string()).send_all()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
237 |
response.add(Bye("User quit: ".to_string() + &msg).send_self()); |
14696 | 238 |
response.remove_client(client_id); |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
239 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
240 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
241 |
pub fn get_room_update( |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
242 |
room_name: Option<String>, |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
243 |
room: &HWRoom, |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
244 |
master: Option<&HWClient>, |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
245 |
response: &mut Response, |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
246 |
) { |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
247 |
let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master)); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
248 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
249 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
250 |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
251 |
pub fn get_room_config_impl(config: &RoomConfig, to_client: ClientId, response: &mut Response) { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
252 |
response.add(ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config()).send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
253 |
for cfg in config.to_game_config() { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
254 |
response.add(cfg.to_server_msg().send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
255 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
256 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
257 |
|
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
258 |
pub fn get_room_config(room: &HWRoom, to_client: ClientId, response: &mut Response) { |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
259 |
get_room_config_impl(room.active_config(), to_client, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
260 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
261 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
262 |
pub fn get_teams<'a, I>(teams: I, to_client: ClientId, response: &mut Response) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
263 |
where |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
264 |
I: Iterator<Item = &'a TeamInfo>, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
265 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
266 |
for team in teams { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
267 |
response.add(TeamAdd(team.to_protocol()).send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
268 |
response.add(TeamColor(team.name.clone(), team.color).send(to_client)); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
269 |
response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send(to_client)); |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
270 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
271 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
272 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
273 |
pub fn get_room_teams( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
274 |
server: &HWServer, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
275 |
room_id: RoomId, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
276 |
to_client: ClientId, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
277 |
response: &mut Response, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
278 |
) { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
279 |
let room = &server.rooms[room_id]; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
280 |
let current_teams = match room.game_info { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
281 |
Some(ref info) => &info.teams_at_start, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
282 |
None => &room.teams, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
283 |
}; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
284 |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
285 |
get_teams(current_teams.iter().map(|(_, t)| t), to_client, response); |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
286 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
287 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
288 |
pub fn get_room_flags( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
289 |
server: &HWServer, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
290 |
room_id: RoomId, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
291 |
to_client: ClientId, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
292 |
response: &mut Response, |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
293 |
) { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
294 |
let room = &server.rooms[room_id]; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
295 |
if let Some(id) = room.master_id { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
296 |
response.add( |
14782 | 297 |
ClientFlags( |
298 |
add_flags(&[Flags::RoomMaster]), |
|
299 |
vec![server.clients[id].nick.clone()], |
|
300 |
) |
|
301 |
.send(to_client), |
|
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
302 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
303 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
304 |
let nicks: Vec<_> = server |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
305 |
.clients |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
306 |
.iter() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
307 |
.filter(|(_, c)| c.room_id == Some(room_id) && c.is_ready()) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
308 |
.map(|(_, c)| c.nick.clone()) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
309 |
.collect(); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
310 |
if !nicks.is_empty() { |
14782 | 311 |
response.add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send(to_client)); |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
312 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
313 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
314 |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
315 |
pub fn apply_voting_result( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
316 |
server: &mut HWServer, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
317 |
room_id: RoomId, |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
318 |
response: &mut Response, |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
319 |
kind: VoteType, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
320 |
) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
321 |
match kind { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
322 |
VoteType::Kick(nick) => { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
323 |
if let Some(client) = server.find_client(&nick) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
324 |
if client.room_id == Some(room_id) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
325 |
let id = client.id; |
14690
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
326 |
response.add(Kicked.send(id)); |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14689
diff
changeset
|
327 |
exit_room(server, id, response, "kicked"); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
328 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
329 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
330 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
331 |
VoteType::Map(None) => (), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
332 |
VoteType::Map(Some(name)) => { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
333 |
if let Some(location) = server.rooms[room_id].load_config(&name) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
334 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
335 |
server_chat(location.to_string()) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
336 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
337 |
.in_room(room_id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
338 |
); |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
339 |
let room = &server.rooms[room_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
340 |
let room_master = if let Some(id) = room.master_id { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
341 |
Some(&server.clients[id]) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
342 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
343 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
344 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
345 |
get_room_update(None, room, room_master, response); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
346 |
|
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
347 |
for (_, client) in server.clients.iter() { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
348 |
if client.room_id == Some(room_id) { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
349 |
super::common::get_room_config(&server.rooms[room_id], client.id, response); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
350 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
351 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
352 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
353 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
354 |
VoteType::Pause => { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
355 |
if let Some(ref mut info) = server.rooms[room_id].game_info { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
356 |
info.is_paused = !info.is_paused; |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
357 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
358 |
server_chat("Pause toggled.".to_string()) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
359 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
360 |
.in_room(room_id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
361 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
362 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
363 |
ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
364 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
365 |
.in_room(room_id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
366 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
367 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
368 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
369 |
VoteType::NewSeed => { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
370 |
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:
14686
diff
changeset
|
371 |
let cfg = GameCfg::Seed(seed); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
372 |
response.add(cfg.to_server_msg().send_all().in_room(room_id)); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
373 |
server.rooms[room_id].set_config(cfg); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
374 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
375 |
VoteType::HedgehogsPerTeam(number) => { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
376 |
let r = &mut server.rooms[room_id]; |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
377 |
let nicks = r.set_hedgehogs_number(number); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
378 |
|
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
379 |
response.extend( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
380 |
nicks |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
381 |
.into_iter() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
382 |
.map(|n| HedgehogsNumber(n, number).send_all().in_room(room_id)), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
383 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
384 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
385 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
386 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
387 |
|
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
388 |
fn add_vote(room: &mut HWRoom, response: &mut Response, vote: Vote) -> Option<bool> { |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
389 |
let client_id = response.client_id; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
390 |
let mut result = None; |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
391 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
392 |
if let Some(ref mut voting) = room.voting { |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
393 |
if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) { |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
394 |
response.add(server_chat("Your vote has been counted.".to_string()).send_self()); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
395 |
voting.votes.push((client_id, vote.is_pro)); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
396 |
let i = voting.votes.iter(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
397 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
398 |
let contra = i.filter(|(_, v)| !*v).count(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
399 |
let success_quota = voting.voters.len() / 2 + 1; |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
400 |
if vote.is_forced && vote.is_pro || pro >= success_quota { |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
401 |
result = Some(true); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
402 |
} 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:
14686
diff
changeset
|
403 |
{ |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
404 |
result = Some(false); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
405 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
406 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
407 |
response.add(server_chat("You already have voted.".to_string()).send_self()); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
408 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
409 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
410 |
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:
14683
diff
changeset
|
411 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
412 |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
413 |
result |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
414 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
415 |
|
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
416 |
pub fn submit_vote(server: &mut HWServer, vote: Vote, response: &mut Response) { |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
417 |
let client_id = response.client_id; |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
418 |
let client = &server.clients[client_id]; |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
419 |
|
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
420 |
if let Some(room_id) = client.room_id { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
421 |
let room = &mut server.rooms[room_id]; |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
422 |
|
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
423 |
if let Some(res) = add_vote(room, response, vote) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
424 |
response.add( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
425 |
server_chat("Voting closed.".to_string()) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
426 |
.send_all() |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
427 |
.in_room(room.id), |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
428 |
); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
429 |
let voting = replace(&mut room.voting, None).unwrap(); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
430 |
if res { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
431 |
apply_voting_result(server, room_id, response, voting.kind); |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
432 |
} |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
433 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
434 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
435 |
} |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
436 |
|
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
437 |
pub fn start_game(server: &mut HWServer, room_id: RoomId, response: &mut Response) { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
438 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
439 |
.clients |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
440 |
.iter() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
441 |
.map(|(id, c)| (id, c.nick.clone())) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
442 |
.unzip(); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
443 |
let room = &mut server.rooms[room_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
444 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
445 |
if !room.has_multiple_clans() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
446 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
447 |
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:
14687
diff
changeset
|
448 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
449 |
} 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:
14687
diff
changeset
|
450 |
response.add(Warning("Not all players are ready".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
451 |
} else if room.game_info.is_some() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
452 |
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:
14687
diff
changeset
|
453 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
454 |
room.start_round(); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
455 |
for id in room_clients { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
456 |
let c = &mut server.clients[id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
457 |
c.set_is_in_game(false); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
458 |
c.team_indices = room.client_team_indices(c.id); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
459 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
460 |
response.add(RunGame.send_all().in_room(room.id)); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
461 |
response.add( |
14782 | 462 |
ClientFlags(add_flags(&[Flags::InGame]), room_nicks) |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
463 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
464 |
.in_room(room.id), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
465 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
466 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
467 |
let room_master = if let Some(id) = room.master_id { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
468 |
Some(&server.clients[id]) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
469 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
470 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
471 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
472 |
get_room_update(None, room, room_master, response); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
473 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
474 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
475 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
476 |
pub fn end_game(server: &mut HWServer, room_id: RoomId, response: &mut Response) { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
477 |
let room = &mut server.rooms[room_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
478 |
room.ready_players_number = 1; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
479 |
let room_master = if let Some(id) = room.master_id { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
480 |
Some(&server.clients[id]) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
481 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
482 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
483 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
484 |
get_room_update(None, room, room_master, response); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
485 |
response.add(RoundFinished.send_all().in_room(room_id)); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
486 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
487 |
if let Some(info) = replace(&mut room.game_info, None) { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
488 |
for (_, client) in server.clients.iter() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
489 |
if client.room_id == Some(room_id) && client.is_joined_mid_game() { |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
490 |
super::common::get_room_config(room, client.id, response); |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
491 |
response.extend( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
492 |
info.left_teams |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
493 |
.iter() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
494 |
.map(|name| TeamRemove(name.clone()).send(client.id)), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
495 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
496 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
497 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
498 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
499 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
500 |
let nicks: Vec<_> = server |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
501 |
.clients |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
502 |
.iter_mut() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
503 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
504 |
.map(|(_, c)| { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
505 |
c.set_is_ready(c.is_master()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
506 |
c.set_is_joined_mid_game(false); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
507 |
c |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
508 |
}) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
509 |
.filter_map(|c| { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
510 |
if !c.is_master() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
511 |
Some(c.nick.clone()) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
512 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
513 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
514 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
515 |
}) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
516 |
.collect(); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
517 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
518 |
if !nicks.is_empty() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
519 |
let msg = if room.protocol_number < 38 { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
520 |
LegacyReady(false, nicks) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
521 |
} else { |
14782 | 522 |
ClientFlags(remove_flags(&[Flags::Ready]), nicks) |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
523 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
524 |
response.add(msg.send_all().in_room(room_id)); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
525 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
526 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
527 |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
528 |
#[cfg(test)] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
529 |
mod tests { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
530 |
use super::*; |
13796 | 531 |
use crate::protocol::messages::HWServerMessage::ChatMsg; |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
532 |
use crate::server::actions::PendingMessage; |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
533 |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
534 |
fn reply2string(r: HWServerMessage) -> String { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
535 |
match r { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
536 |
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
|
537 |
_ => panic!("expected a ChatMsg"), |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
538 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
539 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
540 |
|
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
541 |
fn run_handle_test(opts: Vec<String>) { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
542 |
let opts2 = opts.clone(); |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
543 |
for opt in opts { |
13521 | 544 |
while reply2string(rnd_reply(&opts2)) != opt {} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
545 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
546 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
547 |
|
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
548 |
/// This test terminates almost surely. |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
549 |
#[test] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
550 |
fn test_handle_rnd_empty() { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
551 |
run_handle_test(vec![]) |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
552 |
} |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
553 |
|
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
554 |
/// This test terminates almost surely. |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
555 |
#[test] |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
556 |
fn test_handle_rnd_nonempty() { |
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
557 |
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
|
558 |
} |
13446
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
559 |
|
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
560 |
/// 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
|
561 |
#[test] |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
562 |
fn test_distribution() { |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
563 |
let eps = 0.000001; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
564 |
let lim = 0.5; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
565 |
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
|
566 |
let mut ones = 0; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
567 |
let mut tries = 0; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
568 |
|
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
569 |
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
|
570 |
tries += 1; |
13521 | 571 |
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
|
572 |
ones += 1; |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
573 |
} |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
574 |
} |
dd2e51f7303d
Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13445
diff
changeset
|
575 |
} |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff
changeset
|
576 |
} |