author | alfadur <mail@none> |
Mon, 23 Dec 2019 18:55:25 +0300 | |
changeset 15522 | 4a0b06b03199 |
parent 15519 | b3157d218ae2 |
child 15523 | f4f6060b536c |
permissions | -rw-r--r-- |
15482 | 1 |
use super::{common::rnd_reply, strings::*}; |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
2 |
use crate::core::room::GameInfo; |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
3 |
use crate::core::server::AddTeamError; |
13666 | 4 |
use crate::{ |
15074 | 5 |
core::{ |
15075 | 6 |
room::{HwRoom, RoomFlags, MAX_TEAMS_IN_ROOM}, |
15492 | 7 |
server::{ |
15518 | 8 |
ChangeMasterError, ChangeMasterResult, HwServer, LeaveRoomResult, ModifyTeamError, |
9 |
StartGameError, |
|
15492 | 10 |
}, |
15074 | 11 |
types, |
12 |
types::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM}, |
|
15075 | 13 |
}, |
14 |
protocol::messages::{ |
|
15 |
add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*, |
|
16 |
ProtocolFlags as Flags, |
|
13666 | 17 |
}, |
15482 | 18 |
utils::{is_name_illegal, to_engine_msg}, |
13416 | 19 |
}; |
14457 | 20 |
use base64::{decode, encode}; |
13805 | 21 |
use log::*; |
15026 | 22 |
use std::{cmp::min, iter::once, mem::swap}; |
13423 | 23 |
|
24 |
#[derive(Clone)] |
|
25 |
struct ByMsg<'a> { |
|
14457 | 26 |
messages: &'a [u8], |
13423 | 27 |
} |
28 |
||
14457 | 29 |
impl<'a> Iterator for ByMsg<'a> { |
30 |
type Item = &'a [u8]; |
|
13423 | 31 |
|
32 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
33 |
if let Some(size) = self.messages.get(0) { |
|
34 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
35 |
self.messages = next; |
|
36 |
Some(msg) |
|
37 |
} else { |
|
38 |
None |
|
39 |
} |
|
40 |
} |
|
41 |
} |
|
42 |
||
13524 | 43 |
fn by_msg(source: &[u8]) -> ByMsg { |
14457 | 44 |
ByMsg { messages: source } |
13423 | 45 |
} |
46 |
||
47 |
const VALID_MESSAGES: &[u8] = |
|
48 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
49 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
50 |
||
15482 | 51 |
/*#[cfg(canhazslicepatterns)] |
13423 | 52 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13424 | 53 |
match msg { |
15326 | 54 |
[size, typ, body..MAX] => { |
14457 | 55 |
VALID_MESSAGES.contains(typ) |
56 |
&& match body { |
|
15439 | 57 |
[1..=MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => { |
14457 | 58 |
team_indices.contains(team) |
59 |
} |
|
60 |
_ => *typ != b'h', |
|
61 |
} |
|
62 |
} |
|
63 |
_ => false, |
|
13423 | 64 |
} |
15482 | 65 |
}*/ |
13423 | 66 |
|
13666 | 67 |
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool { |
13429 | 68 |
if let Some(typ) = msg.get(1) { |
69 |
VALID_MESSAGES.contains(typ) |
|
70 |
} else { |
|
71 |
false |
|
72 |
} |
|
73 |
} |
|
74 |
||
13423 | 75 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13429 | 76 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13423 | 77 |
} |
12147 | 78 |
|
13443 | 79 |
fn is_msg_timed(msg: &[u8]) -> bool { |
14457 | 80 |
msg.get(1) |
81 |
.filter(|t| !NON_TIMED_MESSAGES.contains(t)) |
|
82 |
.is_some() |
|
13443 | 83 |
} |
84 |
||
13480 | 85 |
fn voting_description(kind: &VoteType) -> String { |
14457 | 86 |
format!( |
87 |
"New voting started: {}", |
|
88 |
match kind { |
|
89 |
VoteType::Kick(nick) => format!("kick {}", nick), |
|
90 |
VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()), |
|
91 |
VoteType::Pause => "pause".to_string(), |
|
92 |
VoteType::NewSeed => "new seed".to_string(), |
|
93 |
VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number), |
|
94 |
} |
|
95 |
) |
|
13480 | 96 |
} |
97 |
||
15075 | 98 |
fn room_message_flag(msg: &HwProtocolMessage) -> RoomFlags { |
99 |
use crate::protocol::messages::HwProtocolMessage::*; |
|
13523 | 100 |
match msg { |
101 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
102 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
103 |
ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, |
|
14457 | 104 |
_ => RoomFlags::empty(), |
13523 | 105 |
} |
106 |
} |
|
107 |
||
14457 | 108 |
pub fn handle( |
15075 | 109 |
server: &mut HwServer, |
14457 | 110 |
client_id: ClientId, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
111 |
response: &mut super::Response, |
14457 | 112 |
room_id: RoomId, |
15075 | 113 |
message: HwProtocolMessage, |
14457 | 114 |
) { |
15482 | 115 |
let (client, room) = server.client_and_room_mut(client_id, room_id); |
14697 | 116 |
|
15075 | 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 |
}; |
|
15482 | 124 |
|
15492 | 125 |
let result = server.leave_room(client_id); |
126 |
super::common::get_room_leave_data(server, room_id, &msg, result, response); |
|
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); |
|
15492 | 154 |
} else { |
155 |
response.warn(ACCESS_DENIED) |
|
13477 | 156 |
} |
157 |
} |
|
158 |
Unfix => { |
|
14697 | 159 |
if client.is_admin() { |
160 |
room.set_is_fixed(false); |
|
15492 | 161 |
} else { |
162 |
response.warn(ACCESS_DENIED) |
|
13477 | 163 |
} |
164 |
} |
|
165 |
Greeting(text) => { |
|
14697 | 166 |
if client.is_admin() || client.is_master() && !room.is_fixed() { |
15111 | 167 |
room.greeting = text.unwrap_or(String::new()); |
13477 | 168 |
} |
169 |
} |
|
14788 | 170 |
MaxTeams(count) => { |
171 |
if !client.is_master() { |
|
15482 | 172 |
response.warn(NOT_MASTER); |
15026 | 173 |
} else if !(2..=MAX_TEAMS_IN_ROOM).contains(&count) { |
15482 | 174 |
response.warn("/maxteams: specify number from 2 to 8"); |
14788 | 175 |
} else { |
15482 | 176 |
room.max_teams = count; |
14788 | 177 |
} |
178 |
} |
|
13416 | 179 |
RoomName(new_name) => { |
15518 | 180 |
use crate::core::server::ModifyRoomNameError; |
181 |
match server.set_room_name(client_id, room_id, new_name) { |
|
182 |
Ok(old_name) => { |
|
183 |
let (client, room) = server.client_and_room(client_id, room_id); |
|
184 |
super::common::get_room_update(Some(old_name), room, Some(client), response) |
|
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
185 |
} |
15518 | 186 |
Err(ModifyRoomNameError::AccessDenied) => response.warn(ACCESS_DENIED), |
187 |
Err(ModifyRoomNameError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME), |
|
188 |
Err(ModifyRoomNameError::DuplicateName) => response.warn(ROOM_EXISTS), |
|
14688
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 => { |
15518 | 192 |
let flags = if server.toggle_ready(client_id) { |
193 |
add_flags(&[Flags::Ready]) |
|
14697 | 194 |
} else { |
15518 | 195 |
remove_flags(&[Flags::Ready]) |
14697 | 196 |
}; |
15518 | 197 |
let (client, room) = server.client_and_room(client_id, room_id); |
13801 | 198 |
|
14697 | 199 |
let msg = if client.protocol_number < 38 { |
200 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
|
201 |
} else { |
|
14782 | 202 |
ClientFlags(flags, vec![client.nick.clone()]) |
14697 | 203 |
}; |
15518 | 204 |
response.add(msg.send_all().in_room(room_id)); |
14691 | 205 |
|
14697 | 206 |
if room.is_fixed() && room.ready_players_number == room.players_number { |
15492 | 207 |
let result = server.start_game(room_id); |
208 |
super::common::get_start_game_data(server, room_id, result, response); |
|
13666 | 209 |
} |
13416 | 210 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
211 |
AddTeam(info) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
212 |
use crate::core::server::AddTeamError; |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
213 |
match server.add_team(client_id, info) { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
214 |
Ok(team) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
215 |
response.add(TeamAccepted(team.name.clone()).send_self()); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
216 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
217 |
TeamAdd(team.to_protocol()) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
218 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
219 |
.in_room(room_id) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
220 |
.but_self(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
221 |
); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
222 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
223 |
TeamColor(team.name.clone(), team.color) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
224 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
225 |
.in_room(room_id), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
226 |
); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
227 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
228 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
229 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
230 |
.in_room(room_id), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
231 |
); |
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
232 |
|
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
233 |
let room = server.room(room_id); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
234 |
let room_master = if let Some(id) = room.master_id { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
235 |
Some(server.client(id)) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
236 |
} else { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
237 |
None |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
238 |
}; |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
239 |
super::common::get_room_update(None, room, room_master, response); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
240 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
241 |
Err(AddTeamError::TooManyTeams) => response.warn(TOO_MANY_TEAMS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
242 |
Err(AddTeamError::TooManyHedgehogs) => response.warn(TOO_MANY_HEDGEHOGS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
243 |
Err(AddTeamError::TeamAlreadyExists) => response.warn(TEAM_EXISTS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
244 |
Err(AddTeamError::GameInProgress) => response.warn(ROUND_IN_PROGRESS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
245 |
Err(AddTeamError::Restricted) => response.warn(TEAM_ADD_RESTRICTED), |
13419 | 246 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
247 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
248 |
RemoveTeam(name) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
249 |
use crate::core::server::RemoveTeamError; |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
250 |
match server.remove_team(client_id, &name) { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
251 |
Ok(()) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
252 |
let (client, room) = server.client_and_room(client_id, room_id); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
253 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
254 |
let removed_teams = vec![name]; |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
255 |
super::common::get_remove_teams_data( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
256 |
room_id, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
257 |
client.is_in_game(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
258 |
removed_teams, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
259 |
response, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
260 |
); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
261 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
262 |
match room.game_info { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
263 |
Some(ref info) if info.teams_in_game == 0 => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
264 |
let result = server.end_game(room_id); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
265 |
super::common::get_end_game_result(server, room_id, result, response); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
266 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
267 |
_ => (), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
268 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
269 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
270 |
Err(RemoveTeamError::NoTeam) => response.warn(NO_TEAM_TO_REMOVE), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
271 |
Err(RemoveTeamError::TeamNotOwned) => response.warn(TEAM_NOT_OWNED), |
15482 | 272 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
273 |
} |
13419 | 274 |
SetHedgehogsNumber(team_name, number) => { |
14697 | 275 |
let addable_hedgehogs = room.addable_hedgehogs(); |
276 |
if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
|
15026 | 277 |
let max_hedgehogs = min( |
278 |
MAX_HEDGEHOGS_PER_TEAM, |
|
279 |
addable_hedgehogs + team.hedgehogs_number, |
|
280 |
); |
|
14697 | 281 |
if !client.is_master() { |
15482 | 282 |
response.error(NOT_MASTER); |
15026 | 283 |
} else if !(1..=max_hedgehogs).contains(&number) { |
14697 | 284 |
response |
285 |
.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self()); |
|
13419 | 286 |
} else { |
14697 | 287 |
team.hedgehogs_number = number; |
288 |
response.add( |
|
289 |
HedgehogsNumber(team.name.clone(), number) |
|
290 |
.send_all() |
|
291 |
.in_room(room_id) |
|
292 |
.but_self(), |
|
293 |
); |
|
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
294 |
} |
14697 | 295 |
} else { |
15482 | 296 |
response.warn(NO_TEAM); |
13666 | 297 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
298 |
} |
13419 | 299 |
SetTeamColor(team_name, color) => { |
15518 | 300 |
match server.set_team_color(client_id, room_id, &team_name, color) { |
301 |
Ok(()) => response.add( |
|
302 |
TeamColor(team_name, color) |
|
303 |
.send_all() |
|
304 |
.in_room(room_id) |
|
305 |
.but_self(), |
|
306 |
), |
|
307 |
Err(ModifyTeamError::NoTeam) => response.warn(NO_TEAM), |
|
308 |
Err(ModifyTeamError::NotMaster) => response.error(NOT_MASTER), |
|
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
309 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
310 |
} |
13422 | 311 |
Cfg(cfg) => { |
14697 | 312 |
if room.is_fixed() { |
15482 | 313 |
response.warn(ACCESS_DENIED); |
14697 | 314 |
} else if !client.is_master() { |
15482 | 315 |
response.error(NOT_MASTER); |
14697 | 316 |
} else { |
317 |
let cfg = match cfg { |
|
318 |
GameCfg::Scheme(name, mut values) => { |
|
319 |
if client.protocol_number == 49 && values.len() >= 2 { |
|
320 |
let mut s = "X".repeat(50); |
|
321 |
s.push_str(&values.pop().unwrap()); |
|
322 |
values.push(s); |
|
13801 | 323 |
} |
14697 | 324 |
GameCfg::Scheme(name, values) |
325 |
} |
|
326 |
cfg => cfg, |
|
327 |
}; |
|
13801 | 328 |
|
14697 | 329 |
response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self()); |
330 |
room.set_config(cfg); |
|
13666 | 331 |
} |
13419 | 332 |
} |
13528 | 333 |
Save(name, location) => { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
334 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
335 |
server_chat(format!("Room config saved as {}", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
336 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
337 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
338 |
); |
14697 | 339 |
room.save_config(name, location); |
13528 | 340 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
341 |
#[cfg(feature = "official-server")] |
13529 | 342 |
SaveRoom(filename) => { |
14697 | 343 |
if client.is_admin() { |
344 |
match room.get_saves() { |
|
14781 | 345 |
Ok(contents) => response.request_io(super::IoTask::SaveRoom { |
346 |
room_id, |
|
347 |
filename, |
|
348 |
contents, |
|
349 |
}), |
|
13529 | 350 |
Err(e) => { |
351 |
warn!("Error while serializing the room configs: {}", e); |
|
15482 | 352 |
response.warn("Unable to serialize the room configs.") |
13529 | 353 |
} |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
354 |
} |
13666 | 355 |
} |
13529 | 356 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
357 |
#[cfg(feature = "official-server")] |
13529 | 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) => { |
15522 | 393 |
let names: Vec<_> = server.room(room_id).saves.keys().cloned().collect(); |
13527 | 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 { |
15439 | 416 |
1..=MAX_HEDGEHOGS_PER_TEAM => None, |
14683
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); |
14789 | 424 |
let voting = Voting::new(kind, server.room_clients(client_id).collect()); |
15522 | 425 |
let room = server.room_mut(room_id); |
14686
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, |
15074 | 430 |
types::Vote { |
14687
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, |
15074 | 445 |
types::Vote { |
14687
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, |
15074 | 456 |
types::Vote { |
14687
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 => { |
15492 | 470 |
let result = server.start_game(room_id); |
471 |
super::common::get_start_game_data(server, room_id, result, response); |
|
13423 | 472 |
} |
473 |
EngineMessage(em) => { |
|
14697 | 474 |
if client.teams_in_game > 0 { |
475 |
let decoding = decode(&em[..]).unwrap(); |
|
476 |
let messages = by_msg(&decoding); |
|
477 |
let valid = messages.filter(|m| is_msg_valid(m, &client.team_indices)); |
|
478 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
479 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| { |
|
480 |
if is_msg_empty(m) { |
|
481 |
Some(encode(m)) |
|
482 |
} else { |
|
483 |
None |
|
484 |
} |
|
485 |
}); |
|
13423 | 486 |
|
14697 | 487 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
488 |
if !em_response.is_empty() { |
|
489 |
response.add( |
|
490 |
ForwardEngineMessage(vec![em_response]) |
|
491 |
.send_all() |
|
492 |
.in_room(room.id) |
|
493 |
.but_self(), |
|
494 |
); |
|
495 |
} |
|
496 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
497 |
if let Some(ref mut info) = room.game_info { |
|
498 |
if !em_log.is_empty() { |
|
499 |
info.msg_log.push(em_log); |
|
13423 | 500 |
} |
14697 | 501 |
if let Some(msg) = sync_msg { |
502 |
info.sync_msg = msg; |
|
13427 | 503 |
} |
13423 | 504 |
} |
505 |
} |
|
506 |
} |
|
507 |
RoundFinished => { |
|
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
508 |
if let Some(team_names) = server.leave_game(client_id) { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
509 |
let (client, room) = server.client_and_room(client_id, room_id); |
14689
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 |
|
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
516 |
for team_name in team_names { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
517 |
let msg = once(b'F').chain(team_name.bytes()); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
518 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
519 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
520 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
521 |
.in_room(room_id) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
522 |
.but_self(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
523 |
); |
13423 | 524 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
525 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
526 |
if let Some(GameInfo { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
527 |
teams_in_game: 0, .. |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
528 |
}) = room.game_info |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
529 |
{ |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
530 |
let result = server.end_game(room_id); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
531 |
super::common::get_end_game_result(server, room_id, result, response); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
532 |
} |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
533 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
534 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
535 |
Rnd(v) => { |
13521 | 536 |
let result = rnd_reply(&v); |
537 |
let mut echo = vec!["/rnd".to_string()]; |
|
538 |
echo.extend(v.into_iter()); |
|
539 |
let chat_msg = ChatMsg { |
|
15487 | 540 |
nick: server.client(client_id).nick.clone(), |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
541 |
msg: echo.join(" "), |
13521 | 542 |
}; |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
543 |
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
|
544 |
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
|
545 |
} |
15487 | 546 |
Delegate(nick) => match server.change_master(client_id, room_id, nick) { |
547 |
Ok(ChangeMasterResult { |
|
548 |
old_master_id, |
|
549 |
new_master_id, |
|
550 |
}) => { |
|
551 |
if let Some(master_id) = old_master_id { |
|
552 |
response.add( |
|
553 |
ClientFlags( |
|
554 |
remove_flags(&[Flags::RoomMaster]), |
|
555 |
vec![server.client(master_id).nick.clone()], |
|
556 |
) |
|
557 |
.send_all() |
|
558 |
.in_room(room_id), |
|
559 |
); |
|
560 |
} |
|
561 |
response.add( |
|
562 |
ClientFlags( |
|
563 |
add_flags(&[Flags::RoomMaster]), |
|
564 |
vec![server.client(new_master_id).nick.clone()], |
|
565 |
) |
|
566 |
.send_all() |
|
567 |
.in_room(room_id), |
|
568 |
); |
|
569 |
} |
|
570 |
Err(ChangeMasterError::NoAccess) => { |
|
15482 | 571 |
response.warn("You're not the room master or a server admin!") |
15487 | 572 |
} |
573 |
Err(ChangeMasterError::AlreadyMaster) => { |
|
574 |
response.warn("You're already the room master.") |
|
14784 | 575 |
} |
15487 | 576 |
Err(ChangeMasterError::NoClient) => response.warn("Player is not online."), |
577 |
Err(ChangeMasterError::ClientNotInRoom) => { |
|
578 |
response.warn("The player is not in your room.") |
|
579 |
} |
|
580 |
}, |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
581 |
_ => warn!("Unimplemented!"), |
12147 | 582 |
} |
583 |
} |