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