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