author | alfadur <mail@none> |
Sat, 11 Jan 2020 01:06:41 +0300 | |
changeset 15563 | d122b65bdf6f |
parent 15562 | 479911540e17 |
child 15570 | e1c2ca38e511 |
permissions | -rw-r--r-- |
15504 | 1 |
use super::{common::rnd_reply, strings::*}; |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
2 |
use crate::core::room::GameInfo; |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
3 |
use crate::core::server::{AddTeamError, SetTeamCountError}; |
13666 | 4 |
use crate::{ |
15095 | 5 |
core::{ |
15096 | 6 |
room::{HwRoom, RoomFlags, MAX_TEAMS_IN_ROOM}, |
15514 | 7 |
server::{ |
15547 | 8 |
ChangeMasterError, ChangeMasterResult, HwRoomControl, HwServer, LeaveRoomResult, |
9 |
ModifyTeamError, StartGameError, |
|
15514 | 10 |
}, |
15095 | 11 |
types, |
12 |
types::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM}, |
|
15096 | 13 |
}, |
14 |
protocol::messages::{ |
|
15 |
add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*, |
|
16 |
ProtocolFlags as Flags, |
|
13666 | 17 |
}, |
15504 | 18 |
utils::{is_name_illegal, to_engine_msg}, |
13416 | 19 |
}; |
14478 | 20 |
use base64::{decode, encode}; |
13810 | 21 |
use log::*; |
15047 | 22 |
use std::{cmp::min, iter::once, mem::swap}; |
13423 | 23 |
|
24 |
#[derive(Clone)] |
|
25 |
struct ByMsg<'a> { |
|
14478 | 26 |
messages: &'a [u8], |
13423 | 27 |
} |
28 |
||
14478 | 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 |
||
13500 | 43 |
fn by_msg(source: &[u8]) -> ByMsg { |
14478 | 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 |
||
15550
17ad5d43e820
update subslice pattern to the new syntax
alfadur <mail@none>
parents:
15549
diff
changeset
|
51 |
#[cfg(canhazslicepatterns)] |
13423 | 52 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13424 | 53 |
match msg { |
15552 | 54 |
[size, typ, body @ ..] => { |
14478 | 55 |
VALID_MESSAGES.contains(typ) |
56 |
&& match body { |
|
15463 | 57 |
[1..=MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => { |
14478 | 58 |
team_indices.contains(team) |
59 |
} |
|
60 |
_ => *typ != b'h', |
|
61 |
} |
|
62 |
} |
|
63 |
_ => false, |
|
13423 | 64 |
} |
15550
17ad5d43e820
update subslice pattern to the new syntax
alfadur <mail@none>
parents:
15549
diff
changeset
|
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 { |
14478 | 80 |
msg.get(1) |
81 |
.filter(|t| !NON_TIMED_MESSAGES.contains(t)) |
|
82 |
.is_some() |
|
13443 | 83 |
} |
84 |
||
13460 | 85 |
fn voting_description(kind: &VoteType) -> String { |
14478 | 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 |
) |
|
13460 | 96 |
} |
97 |
||
15096 | 98 |
fn room_message_flag(msg: &HwProtocolMessage) -> RoomFlags { |
99 |
use crate::protocol::messages::HwProtocolMessage::*; |
|
13494 | 100 |
match msg { |
101 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
102 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
15556 | 103 |
ToggleRegisteredOnly => RoomFlags::REGISTRATION_REQUIRED, |
14478 | 104 |
_ => RoomFlags::empty(), |
13494 | 105 |
} |
106 |
} |
|
107 |
||
14478 | 108 |
pub fn handle( |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
109 |
mut room_control: HwRoomControl, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
110 |
response: &mut super::Response, |
15096 | 111 |
message: HwProtocolMessage, |
14478 | 112 |
) { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
113 |
let (client, room) = room_control.get(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
114 |
let (client_id, room_id) = (client.id, room.id); |
14718 | 115 |
|
15096 | 116 |
use crate::protocol::messages::HwProtocolMessage::*; |
12147 | 117 |
match message { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
118 |
Part(msg) => { |
14718 | 119 |
let msg = match msg { |
120 |
Some(s) => format!("part: {}", s), |
|
121 |
None => "part".to_string(), |
|
122 |
}; |
|
15504 | 123 |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
124 |
let result = room_control.leave_room(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
125 |
super::common::get_room_leave_result( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
126 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
127 |
room_control.room(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
128 |
&msg, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
129 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
130 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
131 |
); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
132 |
} |
13416 | 133 |
Chat(msg) => { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
134 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
135 |
ChatMsg { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
136 |
nick: client.nick.clone(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
137 |
msg, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
138 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
139 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
140 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
141 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
142 |
} |
14809 | 143 |
TeamChat(msg) => { |
15553 | 144 |
if room.game_info.is_some() { |
14809 | 145 |
if let Some(clan_color) = room.find_team_color(client_id) { |
146 |
let engine_msg = |
|
147 |
to_engine_msg(format!("b{}]{}\x20\x20", client.nick, msg).bytes()); |
|
148 |
let team = room.clan_team_owners(clan_color).collect(); |
|
149 |
response.add(ForwardEngineMessage(vec![engine_msg]).send_many(team)) |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
13447 | 153 |
Fix => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
154 |
if let Err(_) = room_control.fix_room() { |
15514 | 155 |
response.warn(ACCESS_DENIED) |
13447 | 156 |
} |
157 |
} |
|
158 |
Unfix => { |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
159 |
if let Err(_) = room_control.unfix_room() { |
15514 | 160 |
response.warn(ACCESS_DENIED) |
13447 | 161 |
} |
162 |
} |
|
163 |
Greeting(text) => { |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
164 |
if let Err(_) = room_control.set_room_greeting(text) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
165 |
response.warn(ACCESS_DENIED) |
13447 | 166 |
} |
167 |
} |
|
14809 | 168 |
MaxTeams(count) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
169 |
use crate::core::server::SetTeamCountError; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
170 |
match room_control.set_room_max_teams(count) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
171 |
Ok(()) => {} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
172 |
Err(SetTeamCountError::NotMaster) => response.warn(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
173 |
Err(SetTeamCountError::InvalidNumber) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
174 |
response.warn("/maxteams: specify number from 2 to 8") |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
175 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
176 |
}; |
14809 | 177 |
} |
13416 | 178 |
RoomName(new_name) => { |
15540 | 179 |
use crate::core::server::ModifyRoomNameError; |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
180 |
match room_control.set_room_name(new_name) { |
15540 | 181 |
Ok(old_name) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
182 |
let (client, room) = room_control.get(); |
15540 | 183 |
super::common::get_room_update(Some(old_name), room, Some(client), response) |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
184 |
} |
15540 | 185 |
Err(ModifyRoomNameError::AccessDenied) => response.warn(ACCESS_DENIED), |
186 |
Err(ModifyRoomNameError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME), |
|
187 |
Err(ModifyRoomNameError::DuplicateName) => response.warn(ROOM_EXISTS), |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
188 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
189 |
} |
13419 | 190 |
ToggleReady => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
191 |
let flags = if room_control.toggle_ready() { |
15540 | 192 |
add_flags(&[Flags::Ready]) |
14718 | 193 |
} else { |
15540 | 194 |
remove_flags(&[Flags::Ready]) |
14718 | 195 |
}; |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
196 |
let (client, room) = room_control.get(); |
13775 | 197 |
|
14718 | 198 |
let msg = if client.protocol_number < 38 { |
199 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
|
200 |
} else { |
|
14803 | 201 |
ClientFlags(flags, vec![client.nick.clone()]) |
14718 | 202 |
}; |
15540 | 203 |
response.add(msg.send_all().in_room(room_id)); |
14712 | 204 |
|
14718 | 205 |
if room.is_fixed() && room.ready_players_number == room.players_number { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
206 |
let result = room_control.start_game(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
207 |
super::common::get_start_game_data( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
208 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
209 |
room_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
210 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
211 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
212 |
); |
13666 | 213 |
} |
13416 | 214 |
} |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
215 |
AddTeam(info) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
216 |
use crate::core::server::AddTeamError; |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
217 |
match room_control.add_team(info) { |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
218 |
Ok(team) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
219 |
response.add(TeamAccepted(team.name.clone()).send_self()); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
220 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
221 |
TeamAdd(team.to_protocol()) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
222 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
223 |
.in_room(room_id) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
224 |
.but_self(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
225 |
); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
226 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
227 |
TeamColor(team.name.clone(), team.color) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
228 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
229 |
.in_room(room_id), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
230 |
); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
231 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
232 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
233 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
234 |
.in_room(room_id), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
235 |
); |
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
236 |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
237 |
let room = room_control.room(); |
15547 | 238 |
let room_master = room.master_id.map(|id| room_control.server().client(id)); |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
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:
15540
diff
changeset
|
240 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
241 |
Err(AddTeamError::TooManyTeams) => response.warn(TOO_MANY_TEAMS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
242 |
Err(AddTeamError::TooManyHedgehogs) => response.warn(TOO_MANY_HEDGEHOGS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
243 |
Err(AddTeamError::TeamAlreadyExists) => response.warn(TEAM_EXISTS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
244 |
Err(AddTeamError::GameInProgress) => response.warn(ROUND_IN_PROGRESS), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
245 |
Err(AddTeamError::Restricted) => response.warn(TEAM_ADD_RESTRICTED), |
13419 | 246 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
247 |
} |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
248 |
RemoveTeam(name) => { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
249 |
use crate::core::server::RemoveTeamError; |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
250 |
match room_control.remove_team(&name) { |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
251 |
Ok(()) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
252 |
let (client, room) = room_control.get(); |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
253 |
|
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
254 |
let removed_teams = vec![name]; |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
255 |
super::common::get_remove_teams_data( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
256 |
room_id, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
257 |
client.is_in_game(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
258 |
removed_teams, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
259 |
response, |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
260 |
); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
261 |
|
15563 | 262 |
if let Some(0) = room.teams_in_game() { |
263 |
if let Some(result) = room_control.end_game() { |
|
264 |
super::common::get_end_game_result( |
|
265 |
room_control.server(), |
|
266 |
room_id, |
|
267 |
result, |
|
268 |
response, |
|
269 |
); |
|
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
270 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
271 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
272 |
} |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
273 |
Err(RemoveTeamError::NoTeam) => response.warn(NO_TEAM_TO_REMOVE), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
274 |
Err(RemoveTeamError::TeamNotOwned) => response.warn(TEAM_NOT_OWNED), |
15504 | 275 |
} |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
276 |
} |
13419 | 277 |
SetHedgehogsNumber(team_name, number) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
278 |
use crate::core::server::SetHedgehogsError; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
279 |
match room_control.set_team_hedgehogs_number(&team_name, number) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
280 |
Ok(()) => { |
14718 | 281 |
response.add( |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
282 |
HedgehogsNumber(team_name.clone(), number) |
14718 | 283 |
.send_all() |
284 |
.in_room(room_id) |
|
285 |
.but_self(), |
|
286 |
); |
|
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
287 |
} |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
288 |
Err(SetHedgehogsError::NotMaster) => response.error(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
289 |
Err(SetHedgehogsError::NoTeam) => response.warn(NO_TEAM), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
290 |
Err(SetHedgehogsError::InvalidNumber(previous_number)) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
291 |
response.add(HedgehogsNumber(team_name.clone(), previous_number).send_self()) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
292 |
} |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
293 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
294 |
} |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
295 |
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:
15544
diff
changeset
|
296 |
Ok(()) => response.add( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
297 |
TeamColor(team_name, color) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
298 |
.send_all() |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
299 |
.in_room(room_id) |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
300 |
.but_self(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
301 |
), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
302 |
Err(ModifyTeamError::NoTeam) => response.warn(NO_TEAM), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
303 |
Err(ModifyTeamError::NotMaster) => response.error(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
304 |
}, |
13422 | 305 |
Cfg(cfg) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
306 |
use crate::core::server::SetConfigError; |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
307 |
let msg = cfg.to_server_msg(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
308 |
match room_control.set_config(cfg) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
309 |
Ok(()) => { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
310 |
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:
15544
diff
changeset
|
311 |
} |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
312 |
Err(SetConfigError::NotMaster) => response.error(NOT_MASTER), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
313 |
Err(SetConfigError::RoomFixed) => response.warn(ACCESS_DENIED), |
13666 | 314 |
} |
13419 | 315 |
} |
13528 | 316 |
Save(name, location) => { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
317 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
318 |
server_chat(format!("Room config saved as {}", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
319 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
320 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
321 |
); |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
322 |
room_control.save_config(name, location); |
13528 | 323 |
} |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
324 |
#[cfg(feature = "official-server")] |
13529 | 325 |
SaveRoom(filename) => { |
14718 | 326 |
if client.is_admin() { |
327 |
match room.get_saves() { |
|
14802 | 328 |
Ok(contents) => response.request_io(super::IoTask::SaveRoom { |
329 |
room_id, |
|
330 |
filename, |
|
331 |
contents, |
|
332 |
}), |
|
13529 | 333 |
Err(e) => { |
334 |
warn!("Error while serializing the room configs: {}", e); |
|
15504 | 335 |
response.warn("Unable to serialize the room configs.") |
13529 | 336 |
} |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
337 |
} |
13666 | 338 |
} |
13529 | 339 |
} |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
340 |
#[cfg(feature = "official-server")] |
13529 | 341 |
LoadRoom(filename) => { |
14718 | 342 |
if client.is_admin() { |
14802 | 343 |
response.request_io(super::IoTask::LoadRoom { room_id, filename }); |
13666 | 344 |
} |
13529 | 345 |
} |
13528 | 346 |
Delete(name) => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
347 |
if !room_control.delete_config(&name) { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
348 |
response.add(Warning(format!("Save doesn't exist: {}", name)).send_self()); |
13528 | 349 |
} else { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
350 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
351 |
server_chat(format!("Room config {} has been deleted", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
352 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
353 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
354 |
); |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
355 |
} |
13528 | 356 |
} |
13450 | 357 |
CallVote(None) => { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
358 |
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:
14696
diff
changeset
|
359 |
.send_self()); |
13450 | 360 |
} |
15547 | 361 |
CallVote(Some(kind)) => { |
362 |
use crate::core::server::StartVoteError; |
|
363 |
let room_id = room_control.room().id; |
|
364 |
if super::common::check_vote( |
|
365 |
room_control.server(), |
|
366 |
room_control.room(), |
|
367 |
&kind, |
|
368 |
response, |
|
369 |
) { |
|
370 |
match room_control.start_vote(kind.clone()) { |
|
371 |
Ok(()) => { |
|
372 |
let msg = voting_description(&kind); |
|
373 |
response.add(server_chat(msg).send_all().in_room(room_id)); |
|
374 |
let vote_result = room_control.vote(types::Vote { |
|
375 |
is_pro: true, |
|
376 |
is_forced: false, |
|
377 |
}); |
|
378 |
super::common::handle_vote(room_control, vote_result, response); |
|
13450 | 379 |
} |
15547 | 380 |
Err(StartVoteError::VotingInProgress) => { |
381 |
response.add( |
|
382 |
server_chat("There is already voting in progress".to_string()) |
|
383 |
.send_self(), |
|
384 |
); |
|
13450 | 385 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
386 |
} |
13450 | 387 |
} |
15547 | 388 |
} |
389 |
Vote(vote) => { |
|
390 |
let vote_result = room_control.vote(types::Vote { |
|
391 |
is_pro: vote, |
|
392 |
is_forced: false, |
|
393 |
}); |
|
394 |
super::common::handle_vote(room_control, vote_result, response); |
|
395 |
} |
|
396 |
ForceVote(vote) => { |
|
14718 | 397 |
let is_forced = client.is_admin(); |
15547 | 398 |
let vote_result = room_control.vote(types::Vote { |
399 |
is_pro: vote, |
|
400 |
is_forced, |
|
401 |
}); |
|
402 |
super::common::handle_vote(room_control, vote_result, response); |
|
403 |
} |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
404 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
405 |
if room_control.toggle_flag(room_message_flag(&message)) { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
406 |
let (client, room) = room_control.get(); |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
407 |
super::common::get_room_update(None, room, Some(&client), response); |
13494 | 408 |
} |
409 |
} |
|
13423 | 410 |
StartGame => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
411 |
let result = room_control.start_game(); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
412 |
super::common::get_start_game_data(room_control.server(), room_id, result, response); |
13423 | 413 |
} |
15549 | 414 |
EngineMessage(em) => { |
14718 | 415 |
if client.teams_in_game > 0 { |
416 |
let decoding = decode(&em[..]).unwrap(); |
|
417 |
let messages = by_msg(&decoding); |
|
418 |
let valid = messages.filter(|m| is_msg_valid(m, &client.team_indices)); |
|
419 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
420 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| { |
|
421 |
if is_msg_empty(m) { |
|
422 |
Some(encode(m)) |
|
423 |
} else { |
|
424 |
None |
|
425 |
} |
|
426 |
}); |
|
13423 | 427 |
|
14718 | 428 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
429 |
if !em_response.is_empty() { |
|
430 |
response.add( |
|
431 |
ForwardEngineMessage(vec![em_response]) |
|
432 |
.send_all() |
|
433 |
.in_room(room.id) |
|
434 |
.but_self(), |
|
435 |
); |
|
436 |
} |
|
437 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
15549 | 438 |
|
439 |
room_control.log_engine_msg(em_log, sync_msg); |
|
13423 | 440 |
} |
15549 | 441 |
} |
13423 | 442 |
RoundFinished => { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
443 |
if let Some(team_names) = room_control.leave_game() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
444 |
let (client, room) = room_control.get(); |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
445 |
response.add( |
14803 | 446 |
ClientFlags(remove_flags(&[Flags::InGame]), vec![client.nick.clone()]) |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
447 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
448 |
.in_room(room.id), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
449 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
450 |
|
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
451 |
for team_name in team_names { |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
452 |
let msg = once(b'F').chain(team_name.bytes()); |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
453 |
response.add( |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
454 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
455 |
.send_all() |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
456 |
.in_room(room_id) |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
457 |
.but_self(), |
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
458 |
); |
13423 | 459 |
} |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
460 |
|
15563 | 461 |
if let Some(0) = room.teams_in_game() { |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
462 |
if let Some(result) = room_control.end_game() { |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
463 |
super::common::get_end_game_result( |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
464 |
room_control.server(), |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
465 |
room_id, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
466 |
result, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
467 |
response, |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
468 |
); |
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
469 |
} |
15541
b3157d218ae2
disallow mutable clients to leave the server
alfadur <mail@none>
parents:
15540
diff
changeset
|
470 |
} |
14710
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14709
diff
changeset
|
471 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
472 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
473 |
Rnd(v) => { |
13492 | 474 |
let result = rnd_reply(&v); |
475 |
let mut echo = vec!["/rnd".to_string()]; |
|
476 |
echo.extend(v.into_iter()); |
|
477 |
let chat_msg = ChatMsg { |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
478 |
nick: client.nick.clone(), |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
479 |
msg: echo.join(" "), |
13492 | 480 |
}; |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
481 |
response.add(chat_msg.send_all().in_room(room_id)); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
482 |
response.add(result.send_all().in_room(room_id)); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
483 |
} |
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
484 |
Delegate(nick) => match room_control.change_master(nick) { |
15509 | 485 |
Ok(ChangeMasterResult { |
486 |
old_master_id, |
|
487 |
new_master_id, |
|
488 |
}) => { |
|
489 |
if let Some(master_id) = old_master_id { |
|
490 |
response.add( |
|
491 |
ClientFlags( |
|
492 |
remove_flags(&[Flags::RoomMaster]), |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
493 |
vec![room_control.server().client(master_id).nick.clone()], |
15509 | 494 |
) |
495 |
.send_all() |
|
496 |
.in_room(room_id), |
|
497 |
); |
|
498 |
} |
|
499 |
response.add( |
|
500 |
ClientFlags( |
|
501 |
add_flags(&[Flags::RoomMaster]), |
|
15545
f4f6060b536c
add a separate interface for modifying room state
alfadur <mail@none>
parents:
15544
diff
changeset
|
502 |
vec![room_control.server().client(new_master_id).nick.clone()], |
15509 | 503 |
) |
504 |
.send_all() |
|
505 |
.in_room(room_id), |
|
506 |
); |
|
507 |
} |
|
508 |
Err(ChangeMasterError::NoAccess) => { |
|
15504 | 509 |
response.warn("You're not the room master or a server admin!") |
15509 | 510 |
} |
511 |
Err(ChangeMasterError::AlreadyMaster) => { |
|
512 |
response.warn("You're already the room master.") |
|
14805 | 513 |
} |
15509 | 514 |
Err(ChangeMasterError::NoClient) => response.warn("Player is not online."), |
515 |
Err(ChangeMasterError::ClientNotInRoom) => { |
|
516 |
response.warn("The player is not in your room.") |
|
517 |
} |
|
518 |
}, |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
519 |
_ => warn!("Unimplemented!"), |
12147 | 520 |
} |
521 |
} |