author | alfadur |
Wed, 10 Apr 2019 18:12:30 +0300 | |
changeset 14783 | b3adc030104b |
parent 14782 | 50fcef24003f |
child 14784 | 8390d5e4e39c |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
14457 | 3 |
use super::common::rnd_reply; |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
4 |
use crate::utils::to_engine_msg; |
13666 | 5 |
use crate::{ |
14782 | 6 |
protocol::messages::{ |
7 |
add_flags, remove_flags, server_chat, HWProtocolMessage, HWServerMessage::*, |
|
8 |
ProtocolFlags as Flags, |
|
9 |
}, |
|
13666 | 10 |
server::{ |
14374 | 11 |
core::HWServer, |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
12 |
coretypes, |
14457 | 13 |
coretypes::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM}, |
13666 | 14 |
room::{HWRoom, RoomFlags}, |
15 |
}, |
|
14457 | 16 |
utils::is_name_illegal, |
13416 | 17 |
}; |
14457 | 18 |
use base64::{decode, encode}; |
13805 | 19 |
use log::*; |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
20 |
use std::iter::once; |
14457 | 21 |
use std::mem::swap; |
13423 | 22 |
|
23 |
#[derive(Clone)] |
|
24 |
struct ByMsg<'a> { |
|
14457 | 25 |
messages: &'a [u8], |
13423 | 26 |
} |
27 |
||
14457 | 28 |
impl<'a> Iterator for ByMsg<'a> { |
29 |
type Item = &'a [u8]; |
|
13423 | 30 |
|
31 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
32 |
if let Some(size) = self.messages.get(0) { |
|
33 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
34 |
self.messages = next; |
|
35 |
Some(msg) |
|
36 |
} else { |
|
37 |
None |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
||
13524 | 42 |
fn by_msg(source: &[u8]) -> ByMsg { |
14457 | 43 |
ByMsg { messages: source } |
13423 | 44 |
} |
45 |
||
46 |
const VALID_MESSAGES: &[u8] = |
|
47 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
48 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
49 |
||
13429 | 50 |
#[cfg(canhazslicepatterns)] |
13423 | 51 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13424 | 52 |
match msg { |
14457 | 53 |
[size, typ, body..] => { |
54 |
VALID_MESSAGES.contains(typ) |
|
55 |
&& match body { |
|
56 |
[1...MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => { |
|
57 |
team_indices.contains(team) |
|
58 |
} |
|
59 |
_ => *typ != b'h', |
|
60 |
} |
|
61 |
} |
|
62 |
_ => false, |
|
13423 | 63 |
} |
64 |
} |
|
65 |
||
13666 | 66 |
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool { |
13429 | 67 |
if let Some(typ) = msg.get(1) { |
68 |
VALID_MESSAGES.contains(typ) |
|
69 |
} else { |
|
70 |
false |
|
71 |
} |
|
72 |
} |
|
73 |
||
13423 | 74 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13429 | 75 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13423 | 76 |
} |
12147 | 77 |
|
13443 | 78 |
fn is_msg_timed(msg: &[u8]) -> bool { |
14457 | 79 |
msg.get(1) |
80 |
.filter(|t| !NON_TIMED_MESSAGES.contains(t)) |
|
81 |
.is_some() |
|
13443 | 82 |
} |
83 |
||
13480 | 84 |
fn voting_description(kind: &VoteType) -> String { |
14457 | 85 |
format!( |
86 |
"New voting started: {}", |
|
87 |
match kind { |
|
88 |
VoteType::Kick(nick) => format!("kick {}", nick), |
|
89 |
VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()), |
|
90 |
VoteType::Pause => "pause".to_string(), |
|
91 |
VoteType::NewSeed => "new seed".to_string(), |
|
92 |
VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number), |
|
93 |
} |
|
94 |
) |
|
13480 | 95 |
} |
96 |
||
13523 | 97 |
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags { |
13666 | 98 |
use crate::protocol::messages::HWProtocolMessage::*; |
13523 | 99 |
match msg { |
100 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
101 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
102 |
ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, |
|
14457 | 103 |
_ => RoomFlags::empty(), |
13523 | 104 |
} |
105 |
} |
|
106 |
||
14457 | 107 |
pub fn handle( |
108 |
server: &mut HWServer, |
|
109 |
client_id: ClientId, |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
110 |
response: &mut super::Response, |
14457 | 111 |
room_id: RoomId, |
112 |
message: HWProtocolMessage, |
|
113 |
) { |
|
14697 | 114 |
let client = &mut server.clients[client_id]; |
115 |
let room = &mut server.rooms[room_id]; |
|
116 |
||
13666 | 117 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 118 |
match message { |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
119 |
Part(msg) => { |
14697 | 120 |
let msg = match msg { |
121 |
Some(s) => format!("part: {}", s), |
|
122 |
None => "part".to_string(), |
|
123 |
}; |
|
124 |
super::common::exit_room(server, client_id, response, &msg); |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
125 |
} |
13416 | 126 |
Chat(msg) => { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
127 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
128 |
ChatMsg { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
129 |
nick: client.nick.clone(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
130 |
msg, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
131 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
132 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
133 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
134 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
135 |
} |
13477 | 136 |
Fix => { |
14697 | 137 |
if client.is_admin() { |
138 |
room.set_is_fixed(true); |
|
139 |
room.set_join_restriction(false); |
|
140 |
room.set_team_add_restriction(false); |
|
141 |
room.set_unregistered_players_restriction(true); |
|
13477 | 142 |
} |
143 |
} |
|
144 |
Unfix => { |
|
14697 | 145 |
if client.is_admin() { |
146 |
room.set_is_fixed(false); |
|
13477 | 147 |
} |
148 |
} |
|
149 |
Greeting(text) => { |
|
14697 | 150 |
if client.is_admin() || client.is_master() && !room.is_fixed() { |
151 |
room.greeting = text; |
|
13477 | 152 |
} |
153 |
} |
|
13416 | 154 |
RoomName(new_name) => { |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
155 |
if is_name_illegal(&new_name) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
156 |
response.add(Warning("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
157 |
} else if server.has_room(&new_name) { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
158 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
159 |
Warning("A room with the same name already exists.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
160 |
); |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
161 |
} else { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
162 |
let room = &mut server.rooms[room_id]; |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
163 |
if room.is_fixed() || room.master_id != Some(client_id) { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
164 |
response.add(Warning("Access denied.".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
165 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
166 |
let mut old_name = new_name.clone(); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
167 |
let client = &server.clients[client_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
168 |
swap(&mut room.name, &mut old_name); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
169 |
super::common::get_room_update(Some(old_name), room, Some(&client), response); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
170 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
171 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
172 |
} |
13419 | 173 |
ToggleReady => { |
14697 | 174 |
let flags = if client.is_ready() { |
175 |
room.ready_players_number -= 1; |
|
14782 | 176 |
remove_flags(&[Flags::Ready]) |
14697 | 177 |
} else { |
178 |
room.ready_players_number += 1; |
|
14782 | 179 |
add_flags(&[Flags::Ready]) |
14697 | 180 |
}; |
13801 | 181 |
|
14697 | 182 |
let msg = if client.protocol_number < 38 { |
183 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
|
184 |
} else { |
|
14782 | 185 |
ClientFlags(flags, vec![client.nick.clone()]) |
14697 | 186 |
}; |
187 |
response.add(msg.send_all().in_room(room.id)); |
|
188 |
client.set_is_ready(!client.is_ready()); |
|
14691 | 189 |
|
14697 | 190 |
if room.is_fixed() && room.ready_players_number == room.players_number { |
191 |
super::common::start_game(server, room_id, response); |
|
13666 | 192 |
} |
13416 | 193 |
} |
13422 | 194 |
AddTeam(info) => { |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
195 |
if room.teams.len() >= room.team_limit as usize { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
196 |
response.add(Warning("Too many teams!".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
197 |
} else if room.addable_hedgehogs() == 0 { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
198 |
response.add(Warning("Too many hedgehogs!".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
199 |
} else if room.find_team(|t| t.name == info.name) != None { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
200 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
201 |
Warning("There's already a team with same name in the list.".to_string()) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
202 |
.send_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
203 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
204 |
} else if room.game_info.is_some() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
205 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
206 |
Warning("Joining not possible: Round is in progress.".to_string()).send_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
207 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
208 |
} else if room.is_team_add_restricted() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
209 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
210 |
Warning("This room currently does not allow adding new teams.".to_string()) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
211 |
.send_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
212 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
213 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
214 |
let team = room.add_team(client.id, *info, client.protocol_number < 42); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
215 |
client.teams_in_game += 1; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
216 |
client.clan = Some(team.color); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
217 |
response.add(TeamAccepted(team.name.clone()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
218 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
219 |
TeamAdd(HWRoom::team_info(&client, team)) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
220 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
221 |
.in_room(room_id) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
222 |
.but_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
223 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
224 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
225 |
TeamColor(team.name.clone(), team.color) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
226 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
227 |
.in_room(room_id), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
228 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
229 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
230 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
231 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
232 |
.in_room(room_id), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
233 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
234 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
235 |
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
|
236 |
Some(&server.clients[id]) |
13419 | 237 |
} else { |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
238 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
239 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
240 |
super::common::get_room_update(None, room, room_master, response); |
13419 | 241 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
242 |
} |
14697 | 243 |
RemoveTeam(name) => match room.find_team_owner(&name) { |
244 |
None => response.add( |
|
245 |
Warning("Error: The team you tried to remove does not exist.".to_string()) |
|
246 |
.send_self(), |
|
247 |
), |
|
248 |
Some((id, _)) if id != client_id => response |
|
249 |
.add(Warning("You can't remove a team you don't own.".to_string()).send_self()), |
|
250 |
Some((_, name)) => { |
|
251 |
client.teams_in_game -= 1; |
|
252 |
client.clan = room.find_team_color(client.id); |
|
253 |
super::common::remove_teams( |
|
254 |
room, |
|
255 |
vec![name.to_string()], |
|
256 |
client.is_in_game(), |
|
257 |
response, |
|
258 |
); |
|
14691 | 259 |
|
14697 | 260 |
match room.game_info { |
261 |
Some(ref info) if info.teams_in_game == 0 => { |
|
262 |
super::common::end_game(server, room_id, response) |
|
13419 | 263 |
} |
14697 | 264 |
_ => (), |
13419 | 265 |
} |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
266 |
} |
14697 | 267 |
}, |
13419 | 268 |
SetHedgehogsNumber(team_name, number) => { |
14697 | 269 |
let addable_hedgehogs = room.addable_hedgehogs(); |
270 |
if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
|
271 |
if !client.is_master() { |
|
272 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
|
273 |
} else if number < 1 |
|
274 |
|| number > MAX_HEDGEHOGS_PER_TEAM |
|
275 |
|| number > addable_hedgehogs + team.hedgehogs_number |
|
276 |
{ |
|
277 |
response |
|
278 |
.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self()); |
|
13419 | 279 |
} else { |
14697 | 280 |
team.hedgehogs_number = number; |
281 |
response.add( |
|
282 |
HedgehogsNumber(team.name.clone(), number) |
|
283 |
.send_all() |
|
284 |
.in_room(room_id) |
|
285 |
.but_self(), |
|
286 |
); |
|
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
287 |
} |
14697 | 288 |
} else { |
289 |
response.add(Warning("No such team.".to_string()).send_self()); |
|
13666 | 290 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
291 |
} |
13419 | 292 |
SetTeamColor(team_name, color) => { |
14697 | 293 |
if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
294 |
if !client.is_master() { |
|
295 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
|
13419 | 296 |
} else { |
14697 | 297 |
team.color = color; |
298 |
response.add( |
|
299 |
TeamColor(team.name.clone(), color) |
|
300 |
.send_all() |
|
301 |
.in_room(room_id) |
|
302 |
.but_self(), |
|
303 |
); |
|
304 |
server.clients[owner].clan = Some(color); |
|
13666 | 305 |
} |
14697 | 306 |
} else { |
307 |
response.add(Warning("No such team.".to_string()).send_self()); |
|
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
308 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
309 |
} |
13422 | 310 |
Cfg(cfg) => { |
14697 | 311 |
if room.is_fixed() { |
312 |
response.add(Warning("Access denied.".to_string()).send_self()); |
|
313 |
} else if !client.is_master() { |
|
314 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
|
315 |
} else { |
|
316 |
let cfg = match cfg { |
|
317 |
GameCfg::Scheme(name, mut values) => { |
|
318 |
if client.protocol_number == 49 && values.len() >= 2 { |
|
319 |
let mut s = "X".repeat(50); |
|
320 |
s.push_str(&values.pop().unwrap()); |
|
321 |
values.push(s); |
|
13801 | 322 |
} |
14697 | 323 |
GameCfg::Scheme(name, values) |
324 |
} |
|
325 |
cfg => cfg, |
|
326 |
}; |
|
13801 | 327 |
|
14697 | 328 |
response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self()); |
329 |
room.set_config(cfg); |
|
13666 | 330 |
} |
13419 | 331 |
} |
13528 | 332 |
Save(name, location) => { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
333 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
334 |
server_chat(format!("Room config saved as {}", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
335 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
336 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
337 |
); |
14697 | 338 |
room.save_config(name, location); |
13528 | 339 |
} |
13529 | 340 |
SaveRoom(filename) => { |
14697 | 341 |
if client.is_admin() { |
342 |
match room.get_saves() { |
|
14781 | 343 |
Ok(contents) => response.request_io(super::IoTask::SaveRoom { |
344 |
room_id, |
|
345 |
filename, |
|
346 |
contents, |
|
347 |
}), |
|
13529 | 348 |
Err(e) => { |
349 |
warn!("Error while serializing the room configs: {}", e); |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
350 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
351 |
Warning("Unable to serialize the room configs.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
352 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
353 |
) |
13529 | 354 |
} |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
355 |
} |
13666 | 356 |
} |
13529 | 357 |
} |
358 |
LoadRoom(filename) => { |
|
14697 | 359 |
if client.is_admin() { |
14781 | 360 |
response.request_io(super::IoTask::LoadRoom { room_id, filename }); |
13666 | 361 |
} |
13529 | 362 |
} |
13528 | 363 |
Delete(name) => { |
14697 | 364 |
if !room.delete_config(&name) { |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
365 |
response.add(Warning(format!("Save doesn't exist: {}", name)).send_self()); |
13528 | 366 |
} else { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
367 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
368 |
server_chat(format!("Room config {} has been deleted", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
369 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
370 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
371 |
); |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
372 |
} |
13528 | 373 |
} |
13478 | 374 |
CallVote(None) => { |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
375 |
response.add(server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
376 |
.send_self()); |
13478 | 377 |
} |
378 |
CallVote(Some(kind)) => { |
|
14697 | 379 |
let is_in_game = room.game_info.is_some(); |
13478 | 380 |
let error = match &kind { |
381 |
VoteType::Kick(nick) => { |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
382 |
if server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
383 |
.find_client(&nick) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
384 |
.filter(|c| c.room_id == Some(room_id)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
385 |
.is_some() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
386 |
{ |
13478 | 387 |
None |
388 |
} else { |
|
13527 | 389 |
Some("/callvote kick: No such user!".to_string()) |
13478 | 390 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
391 |
} |
13478 | 392 |
VoteType::Map(None) => { |
13527 | 393 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
394 |
if names.is_empty() { |
|
395 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
396 |
} else { |
|
397 |
Some(format!("Available maps: {}", names.join(", "))) |
|
398 |
} |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
399 |
} |
13478 | 400 |
VoteType::Map(Some(name)) => { |
14697 | 401 |
if room.saves.get(&name[..]).is_some() { |
13530 | 402 |
None |
13527 | 403 |
} else { |
13530 | 404 |
Some("/callvote map: No such map!".to_string()) |
13527 | 405 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
406 |
} |
13478 | 407 |
VoteType::Pause => { |
408 |
if is_in_game { |
|
409 |
None |
|
410 |
} else { |
|
13527 | 411 |
Some("/callvote pause: No game in progress!".to_string()) |
13478 | 412 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
413 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
414 |
VoteType::NewSeed => None, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
415 |
VoteType::HedgehogsPerTeam(number) => match number { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
416 |
1...MAX_HEDGEHOGS_PER_TEAM => None, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
417 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()), |
13478 | 418 |
}, |
419 |
}; |
|
14697 | 420 |
|
13478 | 421 |
match error { |
422 |
None => { |
|
13480 | 423 |
let msg = voting_description(&kind); |
14781 | 424 |
let voting = Voting::new(kind, server.collect_room_clients(client_id)); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
425 |
let room = &mut server.rooms[room_id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
426 |
room.voting = Some(voting); |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
427 |
response.add(server_chat(msg).send_all().in_room(room_id)); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
428 |
super::common::submit_vote( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
429 |
server, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
430 |
coretypes::Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
431 |
is_pro: true, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
432 |
is_forced: false, |
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 |
response, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
435 |
); |
13478 | 436 |
} |
437 |
Some(msg) => { |
|
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
438 |
response.add(server_chat(msg).send_self()); |
13478 | 439 |
} |
440 |
} |
|
441 |
} |
|
442 |
Vote(vote) => { |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
443 |
super::common::submit_vote( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
444 |
server, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
445 |
coretypes::Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
446 |
is_pro: vote, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
447 |
is_forced: false, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
448 |
}, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
449 |
response, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
450 |
); |
13478 | 451 |
} |
452 |
ForceVote(vote) => { |
|
14697 | 453 |
let is_forced = client.is_admin(); |
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
454 |
super::common::submit_vote( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
455 |
server, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
456 |
coretypes::Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
457 |
is_pro: vote, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
458 |
is_forced, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
459 |
}, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
460 |
response, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14686
diff
changeset
|
461 |
); |
13478 | 462 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
463 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
464 |
if client.is_master() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
465 |
room.flags.toggle(room_message_flag(&message)); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
466 |
super::common::get_room_update(None, room, Some(&client), response); |
13523 | 467 |
} |
468 |
} |
|
13423 | 469 |
StartGame => { |
14691 | 470 |
super::common::start_game(server, room_id, response); |
13423 | 471 |
} |
472 |
EngineMessage(em) => { |
|
14697 | 473 |
if client.teams_in_game > 0 { |
474 |
let decoding = decode(&em[..]).unwrap(); |
|
475 |
let messages = by_msg(&decoding); |
|
476 |
let valid = messages.filter(|m| is_msg_valid(m, &client.team_indices)); |
|
477 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
478 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| { |
|
479 |
if is_msg_empty(m) { |
|
480 |
Some(encode(m)) |
|
481 |
} else { |
|
482 |
None |
|
483 |
} |
|
484 |
}); |
|
13423 | 485 |
|
14697 | 486 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
487 |
if !em_response.is_empty() { |
|
488 |
response.add( |
|
489 |
ForwardEngineMessage(vec![em_response]) |
|
490 |
.send_all() |
|
491 |
.in_room(room.id) |
|
492 |
.but_self(), |
|
493 |
); |
|
494 |
} |
|
495 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
496 |
if let Some(ref mut info) = room.game_info { |
|
497 |
if !em_log.is_empty() { |
|
498 |
info.msg_log.push(em_log); |
|
13423 | 499 |
} |
14697 | 500 |
if let Some(msg) = sync_msg { |
501 |
info.sync_msg = msg; |
|
13427 | 502 |
} |
13423 | 503 |
} |
504 |
} |
|
505 |
} |
|
506 |
RoundFinished => { |
|
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
507 |
let mut game_ended = false; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
508 |
if client.is_in_game() { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
509 |
client.set_is_in_game(false); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
510 |
response.add( |
14782 | 511 |
ClientFlags(remove_flags(&[Flags::InGame]), vec![client.nick.clone()]) |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
512 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
513 |
.in_room(room.id), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
514 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
515 |
let team_names: Vec<_> = room |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
516 |
.client_teams(client_id) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
517 |
.map(|t| t.name.clone()) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
518 |
.collect(); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
519 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
520 |
if let Some(ref mut info) = room.game_info { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
521 |
info.teams_in_game -= team_names.len() as u8; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
522 |
if info.teams_in_game == 0 { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
523 |
game_ended = true; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
524 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
525 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
526 |
for team_name in team_names { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
527 |
let msg = once(b'F').chain(team_name.bytes()); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
528 |
response.add( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
529 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
530 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
531 |
.in_room(room_id) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
532 |
.but_self(), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
533 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
534 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
535 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
536 |
if let Some(m) = &info.sync_msg { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
537 |
info.msg_log.push(m.clone()); |
13426 | 538 |
} |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
539 |
if info.sync_msg.is_some() { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
540 |
info.sync_msg = None |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
541 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
542 |
info.msg_log.push(remove_msg.clone()); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
543 |
response.add( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
544 |
ForwardEngineMessage(vec![remove_msg]) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
545 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
546 |
.in_room(room_id) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
547 |
.but_self(), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
548 |
); |
13423 | 549 |
} |
550 |
} |
|
551 |
} |
|
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
552 |
if game_ended { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
553 |
super::common::end_game(server, room_id, response) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
554 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
555 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
556 |
Rnd(v) => { |
13521 | 557 |
let result = rnd_reply(&v); |
558 |
let mut echo = vec!["/rnd".to_string()]; |
|
559 |
echo.extend(v.into_iter()); |
|
560 |
let chat_msg = ChatMsg { |
|
561 |
nick: server.clients[client_id].nick.clone(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
562 |
msg: echo.join(" "), |
13521 | 563 |
}; |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
564 |
response.add(chat_msg.send_all().in_room(room_id)); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
565 |
response.add(result.send_all().in_room(room_id)); |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
566 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
567 |
_ => warn!("Unimplemented!"), |
12147 | 568 |
} |
569 |
} |