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