author | unc0rr |
Wed, 23 Jun 2021 23:41:51 +0200 | |
changeset 15804 | 747278149393 |
parent 15795 | 40929af15167 |
child 15989 | fb389df02e3e |
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; |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
3 |
use crate::core::server::{AddTeamError, SetTeamCountError}; |
15804 | 4 |
use crate::handlers::actions::ToPendingMessage; |
13666 | 5 |
use crate::{ |
15074 | 6 |
core::{ |
15075 | 7 |
room::{HwRoom, RoomFlags, MAX_TEAMS_IN_ROOM}, |
15492 | 8 |
server::{ |
15525 | 9 |
ChangeMasterError, ChangeMasterResult, HwRoomControl, HwServer, LeaveRoomResult, |
10 |
ModifyTeamError, StartGameError, |
|
15492 | 11 |
}, |
15804 | 12 |
types::{ClientId, RoomId, Voting}, |
13666 | 13 |
}, |
15482 | 14 |
utils::{is_name_illegal, to_engine_msg}, |
13416 | 15 |
}; |
14457 | 16 |
use base64::{decode, encode}; |
15804 | 17 |
use hedgewars_network_protocol::{ |
18 |
messages::{ |
|
19 |
add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*, |
|
20 |
ProtocolFlags as Flags, |
|
21 |
}, |
|
22 |
types, |
|
23 |
types::{GameCfg, VoteType, MAX_HEDGEHOGS_PER_TEAM}, |
|
24 |
}; |
|
13805 | 25 |
use log::*; |
15026 | 26 |
use std::{cmp::min, iter::once, mem::swap}; |
13423 | 27 |
|
28 |
#[derive(Clone)] |
|
29 |
struct ByMsg<'a> { |
|
14457 | 30 |
messages: &'a [u8], |
13423 | 31 |
} |
32 |
||
14457 | 33 |
impl<'a> Iterator for ByMsg<'a> { |
34 |
type Item = &'a [u8]; |
|
13423 | 35 |
|
36 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
37 |
if let Some(size) = self.messages.get(0) { |
|
38 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
39 |
self.messages = next; |
|
40 |
Some(msg) |
|
41 |
} else { |
|
42 |
None |
|
43 |
} |
|
44 |
} |
|
45 |
} |
|
46 |
||
13524 | 47 |
fn by_msg(source: &[u8]) -> ByMsg { |
14457 | 48 |
ByMsg { messages: source } |
13423 | 49 |
} |
50 |
||
51 |
const VALID_MESSAGES: &[u8] = |
|
52 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
53 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
54 |
||
55 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
|
13424 | 56 |
match msg { |
15530 | 57 |
[size, typ, body @ ..] => { |
14457 | 58 |
VALID_MESSAGES.contains(typ) |
59 |
&& match body { |
|
15439 | 60 |
[1..=MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => { |
14457 | 61 |
team_indices.contains(team) |
62 |
} |
|
63 |
_ => *typ != b'h', |
|
64 |
} |
|
65 |
} |
|
66 |
_ => false, |
|
13423 | 67 |
} |
15528
17ad5d43e820
update subslice pattern to the new syntax
alfadur <mail@none>
parents:
15527
diff
changeset
|
68 |
} |
13423 | 69 |
|
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 { |
15804 | 94 |
use hedgewars_network_protocol::messages::HwProtocolMessage::*; |
13523 | 95 |
match msg { |
96 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
97 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
15534 | 98 |
ToggleRegisteredOnly => RoomFlags::REGISTRATION_REQUIRED, |
14457 | 99 |
_ => RoomFlags::empty(), |
13523 | 100 |
} |
101 |
} |
|
102 |
||
14457 | 103 |
pub fn handle( |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
104 |
mut room_control: HwRoomControl, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
105 |
response: &mut super::Response, |
15075 | 106 |
message: HwProtocolMessage, |
14457 | 107 |
) { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
108 |
let (client, room) = room_control.get(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
109 |
let (client_id, room_id) = (client.id, room.id); |
14697 | 110 |
|
15804 | 111 |
use hedgewars_network_protocol::messages::HwProtocolMessage::*; |
12147 | 112 |
match message { |
14675
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
113 |
Part(msg) => { |
14697 | 114 |
let msg = match msg { |
115 |
Some(s) => format!("part: {}", s), |
|
116 |
None => "part".to_string(), |
|
117 |
}; |
|
15482 | 118 |
|
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
119 |
let result = room_control.leave_room(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
120 |
super::common::get_room_leave_result( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
121 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
122 |
room_control.room(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
123 |
&msg, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
124 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
125 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
126 |
); |
15551 | 127 |
room_control.cleanup_room(); |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
128 |
} |
13416 | 129 |
Chat(msg) => { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
130 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
131 |
ChatMsg { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
132 |
nick: client.nick.clone(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
133 |
msg, |
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 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
136 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
137 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
138 |
} |
14788 | 139 |
TeamChat(msg) => { |
15531 | 140 |
if room.game_info.is_some() { |
14788 | 141 |
if let Some(clan_color) = room.find_team_color(client_id) { |
142 |
let engine_msg = |
|
143 |
to_engine_msg(format!("b{}]{}\x20\x20", client.nick, msg).bytes()); |
|
144 |
let team = room.clan_team_owners(clan_color).collect(); |
|
145 |
response.add(ForwardEngineMessage(vec![engine_msg]).send_many(team)) |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
13477 | 149 |
Fix => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
150 |
if let Err(_) = room_control.fix_room() { |
15492 | 151 |
response.warn(ACCESS_DENIED) |
13477 | 152 |
} |
153 |
} |
|
154 |
Unfix => { |
|
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
155 |
if let Err(_) = room_control.unfix_room() { |
15492 | 156 |
response.warn(ACCESS_DENIED) |
13477 | 157 |
} |
158 |
} |
|
159 |
Greeting(text) => { |
|
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
160 |
if let Err(_) = room_control.set_room_greeting(text) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
161 |
response.warn(ACCESS_DENIED) |
13477 | 162 |
} |
163 |
} |
|
14788 | 164 |
MaxTeams(count) => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
165 |
use crate::core::server::SetTeamCountError; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
166 |
match room_control.set_room_max_teams(count) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
167 |
Ok(()) => {} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
168 |
Err(SetTeamCountError::NotMaster) => response.warn(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
169 |
Err(SetTeamCountError::InvalidNumber) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
170 |
response.warn("/maxteams: specify number from 2 to 8") |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
171 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
172 |
}; |
14788 | 173 |
} |
13416 | 174 |
RoomName(new_name) => { |
15518 | 175 |
use crate::core::server::ModifyRoomNameError; |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
176 |
match room_control.set_room_name(new_name) { |
15518 | 177 |
Ok(old_name) => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
178 |
let (client, room) = room_control.get(); |
15518 | 179 |
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
|
180 |
} |
15518 | 181 |
Err(ModifyRoomNameError::AccessDenied) => response.warn(ACCESS_DENIED), |
182 |
Err(ModifyRoomNameError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME), |
|
183 |
Err(ModifyRoomNameError::DuplicateName) => response.warn(ROOM_EXISTS), |
|
14688
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14687
diff
changeset
|
184 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
185 |
} |
13419 | 186 |
ToggleReady => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
187 |
let flags = if room_control.toggle_ready() { |
15518 | 188 |
add_flags(&[Flags::Ready]) |
14697 | 189 |
} else { |
15518 | 190 |
remove_flags(&[Flags::Ready]) |
14697 | 191 |
}; |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
192 |
let (client, room) = room_control.get(); |
13801 | 193 |
|
14697 | 194 |
let msg = if client.protocol_number < 38 { |
195 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
|
196 |
} else { |
|
14782 | 197 |
ClientFlags(flags, vec![client.nick.clone()]) |
14697 | 198 |
}; |
15518 | 199 |
response.add(msg.send_all().in_room(room_id)); |
14691 | 200 |
|
14697 | 201 |
if room.is_fixed() && room.ready_players_number == room.players_number { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
202 |
let result = room_control.start_game(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
203 |
super::common::get_start_game_data( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
204 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
205 |
room_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
206 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
207 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
208 |
); |
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; |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
213 |
match room_control.add_team(info) { |
15519
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 |
|
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
233 |
let room = room_control.room(); |
15525 | 234 |
let room_master = room.master_id.map(|id| room_control.server().client(id)); |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
235 |
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
|
236 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
237 |
Err(AddTeamError::TooManyTeams) => response.warn(TOO_MANY_TEAMS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
238 |
Err(AddTeamError::TooManyHedgehogs) => response.warn(TOO_MANY_HEDGEHOGS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
239 |
Err(AddTeamError::TeamAlreadyExists) => response.warn(TEAM_EXISTS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
240 |
Err(AddTeamError::Restricted) => response.warn(TEAM_ADD_RESTRICTED), |
13419 | 241 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
242 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
243 |
RemoveTeam(name) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
244 |
use crate::core::server::RemoveTeamError; |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
245 |
match room_control.remove_team(&name) { |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
246 |
Ok(()) => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
247 |
let (client, room) = room_control.get(); |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
248 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
249 |
let removed_teams = vec![name]; |
15569 | 250 |
super::common::get_remove_teams_data(room_id, false, removed_teams, response); |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
251 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
252 |
Err(RemoveTeamError::NoTeam) => response.warn(NO_TEAM_TO_REMOVE), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
253 |
Err(RemoveTeamError::TeamNotOwned) => response.warn(TEAM_NOT_OWNED), |
15482 | 254 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
255 |
} |
13419 | 256 |
SetHedgehogsNumber(team_name, number) => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
257 |
use crate::core::server::SetHedgehogsError; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
258 |
match room_control.set_team_hedgehogs_number(&team_name, number) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
259 |
Ok(()) => { |
14697 | 260 |
response.add( |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
261 |
HedgehogsNumber(team_name.clone(), number) |
14697 | 262 |
.send_all() |
263 |
.in_room(room_id) |
|
264 |
.but_self(), |
|
265 |
); |
|
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
266 |
} |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
267 |
Err(SetHedgehogsError::NotMaster) => response.error(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
268 |
Err(SetHedgehogsError::NoTeam) => response.warn(NO_TEAM), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
269 |
Err(SetHedgehogsError::InvalidNumber(previous_number)) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
270 |
response.add(HedgehogsNumber(team_name.clone(), previous_number).send_self()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
271 |
} |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
272 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
273 |
} |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
274 |
SetTeamColor(team_name, color) => match room_control.set_team_color(&team_name, color) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
275 |
Ok(()) => response.add( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
276 |
TeamColor(team_name, color) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
277 |
.send_all() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
278 |
.in_room(room_id) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
279 |
.but_self(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
280 |
), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
281 |
Err(ModifyTeamError::NoTeam) => response.warn(NO_TEAM), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
282 |
Err(ModifyTeamError::NotMaster) => response.error(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
283 |
}, |
13422 | 284 |
Cfg(cfg) => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
285 |
use crate::core::server::SetConfigError; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
286 |
let msg = cfg.to_server_msg(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
287 |
match room_control.set_config(cfg) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
288 |
Ok(()) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
289 |
response.add(msg.send_all().in_room(room_control.room().id).but_self()); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
290 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
291 |
Err(SetConfigError::NotMaster) => response.error(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
292 |
Err(SetConfigError::RoomFixed) => response.warn(ACCESS_DENIED), |
13666 | 293 |
} |
13419 | 294 |
} |
13528 | 295 |
Save(name, location) => { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
296 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
297 |
server_chat(format!("Room config saved as {}", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
298 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
299 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
300 |
); |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
301 |
room_control.save_config(name, location); |
13528 | 302 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
303 |
#[cfg(feature = "official-server")] |
13529 | 304 |
SaveRoom(filename) => { |
14697 | 305 |
if client.is_admin() { |
306 |
match room.get_saves() { |
|
14781 | 307 |
Ok(contents) => response.request_io(super::IoTask::SaveRoom { |
308 |
room_id, |
|
309 |
filename, |
|
310 |
contents, |
|
311 |
}), |
|
13529 | 312 |
Err(e) => { |
313 |
warn!("Error while serializing the room configs: {}", e); |
|
15482 | 314 |
response.warn("Unable to serialize the room configs.") |
13529 | 315 |
} |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
316 |
} |
13666 | 317 |
} |
13529 | 318 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
319 |
#[cfg(feature = "official-server")] |
13529 | 320 |
LoadRoom(filename) => { |
14697 | 321 |
if client.is_admin() { |
14781 | 322 |
response.request_io(super::IoTask::LoadRoom { room_id, filename }); |
13666 | 323 |
} |
13529 | 324 |
} |
13528 | 325 |
Delete(name) => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
326 |
if !room_control.delete_config(&name) { |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
327 |
response.add(Warning(format!("Save doesn't exist: {}", name)).send_self()); |
13528 | 328 |
} else { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
329 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
330 |
server_chat(format!("Room config {} has been deleted", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
331 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
332 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
333 |
); |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
334 |
} |
13528 | 335 |
} |
13478 | 336 |
CallVote(None) => { |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
337 |
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
|
338 |
.send_self()); |
13478 | 339 |
} |
15525 | 340 |
CallVote(Some(kind)) => { |
341 |
use crate::core::server::StartVoteError; |
|
342 |
let room_id = room_control.room().id; |
|
343 |
if super::common::check_vote( |
|
344 |
room_control.server(), |
|
345 |
room_control.room(), |
|
346 |
&kind, |
|
347 |
response, |
|
348 |
) { |
|
349 |
match room_control.start_vote(kind.clone()) { |
|
350 |
Ok(()) => { |
|
351 |
let msg = voting_description(&kind); |
|
352 |
response.add(server_chat(msg).send_all().in_room(room_id)); |
|
353 |
let vote_result = room_control.vote(types::Vote { |
|
354 |
is_pro: true, |
|
355 |
is_forced: false, |
|
356 |
}); |
|
357 |
super::common::handle_vote(room_control, vote_result, response); |
|
13478 | 358 |
} |
15525 | 359 |
Err(StartVoteError::VotingInProgress) => { |
360 |
response.add( |
|
361 |
server_chat("There is already voting in progress".to_string()) |
|
362 |
.send_self(), |
|
363 |
); |
|
13478 | 364 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
365 |
} |
13478 | 366 |
} |
15525 | 367 |
} |
368 |
Vote(vote) => { |
|
369 |
let vote_result = room_control.vote(types::Vote { |
|
370 |
is_pro: vote, |
|
371 |
is_forced: false, |
|
372 |
}); |
|
373 |
super::common::handle_vote(room_control, vote_result, response); |
|
374 |
} |
|
375 |
ForceVote(vote) => { |
|
14697 | 376 |
let is_forced = client.is_admin(); |
15525 | 377 |
let vote_result = room_control.vote(types::Vote { |
378 |
is_pro: vote, |
|
379 |
is_forced, |
|
380 |
}); |
|
381 |
super::common::handle_vote(room_control, vote_result, response); |
|
382 |
} |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
383 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
384 |
if room_control.toggle_flag(room_message_flag(&message)) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
385 |
let (client, room) = room_control.get(); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
386 |
super::common::get_room_update(None, room, Some(&client), response); |
13523 | 387 |
} |
388 |
} |
|
13423 | 389 |
StartGame => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
390 |
let result = room_control.start_game(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
391 |
super::common::get_start_game_data(room_control.server(), room_id, result, response); |
13423 | 392 |
} |
15527 | 393 |
EngineMessage(em) => { |
14697 | 394 |
if client.teams_in_game > 0 { |
395 |
let decoding = decode(&em[..]).unwrap(); |
|
396 |
let messages = by_msg(&decoding); |
|
397 |
let valid = messages.filter(|m| is_msg_valid(m, &client.team_indices)); |
|
398 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
399 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| { |
|
400 |
if is_msg_empty(m) { |
|
401 |
Some(encode(m)) |
|
402 |
} else { |
|
403 |
None |
|
404 |
} |
|
405 |
}); |
|
13423 | 406 |
|
14697 | 407 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
408 |
if !em_response.is_empty() { |
|
409 |
response.add( |
|
410 |
ForwardEngineMessage(vec![em_response]) |
|
411 |
.send_all() |
|
412 |
.in_room(room.id) |
|
413 |
.but_self(), |
|
414 |
); |
|
415 |
} |
|
416 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
15527 | 417 |
|
418 |
room_control.log_engine_msg(em_log, sync_msg); |
|
13423 | 419 |
} |
15527 | 420 |
} |
13423 | 421 |
RoundFinished => { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
422 |
if let Some(team_names) = room_control.leave_game() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
423 |
let (client, room) = room_control.get(); |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
424 |
response.add( |
14782 | 425 |
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
|
426 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
427 |
.in_room(room.id), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
428 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
429 |
|
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
430 |
for team_name in team_names { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
431 |
let msg = once(b'F').chain(team_name.bytes()); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
432 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
433 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
434 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
435 |
.in_room(room_id) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
436 |
.but_self(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
437 |
); |
13423 | 438 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
439 |
|
15541 | 440 |
if let Some(0) = room.teams_in_game() { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
441 |
if let Some(result) = room_control.end_game() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
442 |
super::common::get_end_game_result( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
443 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
444 |
room_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
445 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
446 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
447 |
); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
448 |
} |
15519
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15518
diff
changeset
|
449 |
} |
14689
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
450 |
} |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
451 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
452 |
Rnd(v) => { |
13521 | 453 |
let result = rnd_reply(&v); |
454 |
let mut echo = vec!["/rnd".to_string()]; |
|
15795 | 455 |
echo.extend(v); |
13521 | 456 |
let chat_msg = ChatMsg { |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
457 |
nick: client.nick.clone(), |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
458 |
msg: echo.join(" "), |
13521 | 459 |
}; |
14676
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
460 |
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
|
461 |
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
|
462 |
} |
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
463 |
Delegate(nick) => match room_control.change_master(nick) { |
15487 | 464 |
Ok(ChangeMasterResult { |
465 |
old_master_id, |
|
466 |
new_master_id, |
|
467 |
}) => { |
|
468 |
if let Some(master_id) = old_master_id { |
|
469 |
response.add( |
|
470 |
ClientFlags( |
|
471 |
remove_flags(&[Flags::RoomMaster]), |
|
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
472 |
vec![room_control.server().client(master_id).nick.clone()], |
15487 | 473 |
) |
474 |
.send_all() |
|
475 |
.in_room(room_id), |
|
476 |
); |
|
477 |
} |
|
478 |
response.add( |
|
479 |
ClientFlags( |
|
480 |
add_flags(&[Flags::RoomMaster]), |
|
15523
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15522
diff
changeset
|
481 |
vec![room_control.server().client(new_master_id).nick.clone()], |
15487 | 482 |
) |
483 |
.send_all() |
|
484 |
.in_room(room_id), |
|
485 |
); |
|
486 |
} |
|
487 |
Err(ChangeMasterError::NoAccess) => { |
|
15482 | 488 |
response.warn("You're not the room master or a server admin!") |
15487 | 489 |
} |
490 |
Err(ChangeMasterError::AlreadyMaster) => { |
|
491 |
response.warn("You're already the room master.") |
|
14784 | 492 |
} |
15487 | 493 |
Err(ChangeMasterError::NoClient) => response.warn("Player is not online."), |
494 |
Err(ChangeMasterError::ClientNotInRoom) => { |
|
495 |
response.warn("The player is not in your room.") |
|
496 |
} |
|
497 |
}, |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
498 |
_ => warn!("Unimplemented!"), |
12147 | 499 |
} |
500 |
} |