author | alfadur <mail@none> |
Tue, 05 Feb 2019 00:48:40 +0300 | |
changeset 14696 | dfe652c53470 |
parent 14692 | 455865ccd36c |
child 14697 | 9377ee00f1f1 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
14478 | 3 |
use super::common::rnd_reply; |
13666 | 4 |
use crate::{ |
14478 | 5 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage::*}, |
13666 | 6 |
server::{ |
14478 | 7 |
actions::{Action, Action::*}, |
14395 | 8 |
core::HWServer, |
14478 | 9 |
coretypes::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM}, |
13666 | 10 |
room::{HWRoom, RoomFlags}, |
11 |
}, |
|
14478 | 12 |
utils::is_name_illegal, |
13416 | 13 |
}; |
14478 | 14 |
use base64::{decode, encode}; |
13810 | 15 |
use log::*; |
14478 | 16 |
use std::mem::swap; |
13423 | 17 |
|
18 |
#[derive(Clone)] |
|
19 |
struct ByMsg<'a> { |
|
14478 | 20 |
messages: &'a [u8], |
13423 | 21 |
} |
22 |
||
14478 | 23 |
impl<'a> Iterator for ByMsg<'a> { |
24 |
type Item = &'a [u8]; |
|
13423 | 25 |
|
26 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
27 |
if let Some(size) = self.messages.get(0) { |
|
28 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
29 |
self.messages = next; |
|
30 |
Some(msg) |
|
31 |
} else { |
|
32 |
None |
|
33 |
} |
|
34 |
} |
|
35 |
} |
|
36 |
||
13500 | 37 |
fn by_msg(source: &[u8]) -> ByMsg { |
14478 | 38 |
ByMsg { messages: source } |
13423 | 39 |
} |
40 |
||
41 |
const VALID_MESSAGES: &[u8] = |
|
42 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
43 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
44 |
||
13429 | 45 |
#[cfg(canhazslicepatterns)] |
13423 | 46 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13424 | 47 |
match msg { |
14478 | 48 |
[size, typ, body..] => { |
49 |
VALID_MESSAGES.contains(typ) |
|
50 |
&& match body { |
|
51 |
[1...MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => { |
|
52 |
team_indices.contains(team) |
|
53 |
} |
|
54 |
_ => *typ != b'h', |
|
55 |
} |
|
56 |
} |
|
57 |
_ => false, |
|
13423 | 58 |
} |
59 |
} |
|
60 |
||
13666 | 61 |
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool { |
13429 | 62 |
if let Some(typ) = msg.get(1) { |
63 |
VALID_MESSAGES.contains(typ) |
|
64 |
} else { |
|
65 |
false |
|
66 |
} |
|
67 |
} |
|
68 |
||
13423 | 69 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13429 | 70 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13423 | 71 |
} |
12147 | 72 |
|
13443 | 73 |
fn is_msg_timed(msg: &[u8]) -> bool { |
14478 | 74 |
msg.get(1) |
75 |
.filter(|t| !NON_TIMED_MESSAGES.contains(t)) |
|
76 |
.is_some() |
|
13443 | 77 |
} |
78 |
||
13460 | 79 |
fn voting_description(kind: &VoteType) -> String { |
14478 | 80 |
format!( |
81 |
"New voting started: {}", |
|
82 |
match kind { |
|
83 |
VoteType::Kick(nick) => format!("kick {}", nick), |
|
84 |
VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()), |
|
85 |
VoteType::Pause => "pause".to_string(), |
|
86 |
VoteType::NewSeed => "new seed".to_string(), |
|
87 |
VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number), |
|
88 |
} |
|
89 |
) |
|
13460 | 90 |
} |
91 |
||
13494 | 92 |
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags { |
13666 | 93 |
use crate::protocol::messages::HWProtocolMessage::*; |
13494 | 94 |
match msg { |
95 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
96 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
97 |
ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, |
|
14478 | 98 |
_ => RoomFlags::empty(), |
13494 | 99 |
} |
100 |
} |
|
101 |
||
14478 | 102 |
pub fn handle( |
103 |
server: &mut HWServer, |
|
104 |
client_id: ClientId, |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
105 |
response: &mut super::Response, |
14478 | 106 |
room_id: RoomId, |
107 |
message: HWProtocolMessage, |
|
108 |
) { |
|
13666 | 109 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 110 |
match message { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
111 |
Part(msg) => { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
112 |
let lobby_id = server.lobby_id; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
113 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
114 |
let msg = match msg { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
115 |
Some(s) => format!("part: {}", s), |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
116 |
None => "part".to_string() |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
117 |
}; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
118 |
super::common::exit_room(client, room, response, &msg); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
119 |
client.room_id = Some(lobby_id); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
120 |
} |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
121 |
}, |
13416 | 122 |
Chat(msg) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
123 |
let client = &mut server.clients[client_id]; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
124 |
response.add(ChatMsg {nick: client.nick.clone(), msg}.send_all().in_room(room_id)); |
13416 | 125 |
}, |
13447 | 126 |
Fix => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
127 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
128 |
if client.is_admin() { room.set_is_fixed(true) } |
13447 | 129 |
} |
130 |
} |
|
131 |
Unfix => { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
132 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
133 |
if client.is_admin() { room.set_is_fixed(false) } |
13447 | 134 |
} |
135 |
} |
|
136 |
Greeting(text) => { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
137 |
if let (clienr, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
138 |
if clienr.is_admin() || clienr.is_master() && !room.is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
139 |
room.greeting = text |
13447 | 140 |
} |
141 |
} |
|
142 |
} |
|
13416 | 143 |
RoomName(new_name) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
144 |
if is_name_illegal(&new_name) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
145 |
response.add(Warning("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
146 |
} else if server.rooms[room_id].is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
147 |
response.add(Warning("Access denied.".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
148 |
} else if server.has_room(&new_name) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
149 |
response.add(Warning("A room with the same name already exists.".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
150 |
} else { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
151 |
let mut old_name = new_name.clone(); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
152 |
let client = &server.clients[client_id]; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
153 |
let room = &mut server.rooms[room_id]; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
154 |
swap(&mut room.name, &mut old_name); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
155 |
let update_msg = RoomUpdated(old_name, room.info(Some(client))); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
156 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
157 |
}; |
13419 | 158 |
}, |
159 |
ToggleReady => { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
160 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
161 |
let flags = if client.is_ready() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
162 |
room.ready_players_number -= 1; |
13419 | 163 |
"-r" |
164 |
} else { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
165 |
room.ready_players_number += 1; |
13419 | 166 |
"+r" |
167 |
}; |
|
13775 | 168 |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
169 |
let msg = if client.protocol_number < 38 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
170 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
13775 | 171 |
} else { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
172 |
ClientFlags(flags.to_string(), vec![client.nick.clone()]) |
13775 | 173 |
}; |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
174 |
response.add(msg.send_all().in_room(room.id)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
175 |
if room.is_fixed() && room.ready_players_number == room.players_number { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
176 |
//StartRoomGame(r.id) |
13447 | 177 |
} |
13775 | 178 |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
179 |
client.set_is_ready(!client.is_ready()); |
13666 | 180 |
} |
13416 | 181 |
} |
13422 | 182 |
AddTeam(info) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
183 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
184 |
if room.teams.len() >= room.team_limit as usize { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
185 |
response.add(Warning("Too many teams!".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
186 |
} else if room.addable_hedgehogs() == 0 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
187 |
response.add(Warning("Too many hedgehogs!".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
188 |
} else if room.find_team(|t| t.name == info.name) != None { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
189 |
response.add(Warning("There's already a team with same name in the list.".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
190 |
} else if room.game_info.is_some() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
191 |
response.add(Warning("Joining not possible: Round is in progress.".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
192 |
} else if room.is_team_add_restricted() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
193 |
response.add(Warning("This room currently does not allow adding new teams.".to_string()).send_self()); |
13419 | 194 |
} else { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
195 |
let team = room.add_team(client.id, *info, client.protocol_number < 42); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
196 |
client.teams_in_game += 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
197 |
client.clan = Some(team.color); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
198 |
response.add(TeamAccepted(team.name.clone()) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
199 |
.send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
200 |
response.add(TeamAdd(HWRoom::team_info(&client, team)) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
201 |
.send_all().in_room(room_id).but_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
202 |
response.add(TeamColor(team.name.clone(), team.color) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
203 |
.send_all().in_room(room_id)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
204 |
response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
205 |
.send_all().in_room(room_id)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
206 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
207 |
let update_msg = RoomUpdated(room.name.clone(), room.info(Some(client))); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
208 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
13419 | 209 |
} |
210 |
} |
|
211 |
}, |
|
212 |
RemoveTeam(name) => { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
213 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
214 |
match room.find_team_owner(&name) { |
13419 | 215 |
None => |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
216 |
response.add(Warning("Error: The team you tried to remove does not exist.".to_string()).send_self()), |
13419 | 217 |
Some((id, _)) if id != client_id => |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
218 |
response.add(Warning("You can't remove a team you don't own.".to_string()).send_self()), |
13419 | 219 |
Some((_, name)) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
220 |
client.teams_in_game -= 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
221 |
client.clan = room.find_team_color(client.id); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
222 |
super::common::remove_teams(room, vec![name.to_string()], client.is_in_game(), response); |
13419 | 223 |
} |
224 |
} |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
225 |
} |
13419 | 226 |
}, |
227 |
SetHedgehogsNumber(team_name, number) => { |
|
13666 | 228 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13419 | 229 |
let addable_hedgehogs = r.addable_hedgehogs(); |
13666 | 230 |
let actions = if let Some((_, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
13486 | 231 |
if !c.is_master() { |
13419 | 232 |
vec![ProtocolError("You're not the room master!".to_string())] |
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
233 |
} else if number < 1 || number > MAX_HEDGEHOGS_PER_TEAM |
13419 | 234 |
|| number > addable_hedgehogs + team.hedgehogs_number { |
235 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
236 |
.send_self().action()] |
|
237 |
} else { |
|
238 |
team.hedgehogs_number = number; |
|
239 |
vec![HedgehogsNumber(team.name.clone(), number) |
|
240 |
.send_all().in_room(room_id).but_self().action()] |
|
241 |
} |
|
242 |
} else { |
|
243 |
vec![(Warn("No such team.".to_string()))] |
|
13666 | 244 |
}; |
245 |
server.react(client_id, actions); |
|
246 |
} |
|
13419 | 247 |
}, |
248 |
SetTeamColor(team_name, color) => { |
|
13666 | 249 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
250 |
let mut owner_id = None; |
|
251 |
let actions = if let Some((owner, team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13486 | 252 |
if !c.is_master() { |
13419 | 253 |
vec![ProtocolError("You're not the room master!".to_string())] |
254 |
} else if false { |
|
255 |
Vec::new() |
|
256 |
} else { |
|
257 |
owner_id = Some(owner); |
|
258 |
team.color = color; |
|
259 |
vec![TeamColor(team.name.clone(), color) |
|
260 |
.send_all().in_room(room_id).but_self().action()] |
|
261 |
} |
|
262 |
} else { |
|
263 |
vec![(Warn("No such team.".to_string()))] |
|
13666 | 264 |
}; |
13419 | 265 |
|
13666 | 266 |
if let Some(id) = owner_id { |
267 |
server.clients[id].clan = Some(color); |
|
268 |
} |
|
13419 | 269 |
|
13666 | 270 |
server.react(client_id, actions); |
271 |
}; |
|
13422 | 272 |
}, |
273 |
Cfg(cfg) => { |
|
13666 | 274 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
275 |
let actions = if r.is_fixed() { |
|
13447 | 276 |
vec![Warn("Access denied.".to_string())] |
13486 | 277 |
} else if !c.is_master() { |
13422 | 278 |
vec![ProtocolError("You're not the room master!".to_string())] |
279 |
} else { |
|
13775 | 280 |
let cfg = match cfg { |
281 |
GameCfg::Scheme(name, mut values) => { |
|
282 |
if c.protocol_number == 49 && values.len() >= 2 { |
|
283 |
let mut s = "X".repeat(50); |
|
284 |
s.push_str(&values.pop().unwrap()); |
|
285 |
values.push(s); |
|
286 |
} |
|
287 |
GameCfg::Scheme(name, values) |
|
288 |
} |
|
289 |
cfg => cfg |
|
290 |
}; |
|
291 |
||
13439 | 292 |
let v = vec![cfg.to_server_msg() |
293 |
.send_all().in_room(r.id).but_self().action()]; |
|
294 |
r.set_config(cfg); |
|
295 |
v |
|
13666 | 296 |
}; |
297 |
server.react(client_id, actions); |
|
298 |
} |
|
13419 | 299 |
} |
13528 | 300 |
Save(name, location) => { |
301 |
let actions = vec![server_chat(format!("Room config saved as {}", name)) |
|
302 |
.send_all().in_room(room_id).action()]; |
|
303 |
server.rooms[room_id].save_config(name, location); |
|
304 |
server.react(client_id, actions); |
|
305 |
} |
|
13529 | 306 |
SaveRoom(filename) => { |
13666 | 307 |
if server.clients[client_id].is_admin() { |
308 |
let actions = match server.rooms[room_id].get_saves() { |
|
14413 | 309 |
Ok(text) => match server.io.write_file(&filename, &text) { |
13529 | 310 |
Ok(_) => vec![server_chat("Room configs saved successfully.".to_string()) |
311 |
.send_self().action()], |
|
312 |
Err(e) => { |
|
313 |
warn!("Error while writing the config file \"{}\": {}", filename, e); |
|
314 |
vec![Warn("Unable to save the room configs.".to_string())] |
|
315 |
} |
|
316 |
} |
|
317 |
Err(e) => { |
|
318 |
warn!("Error while serializing the room configs: {}", e); |
|
319 |
vec![Warn("Unable to serialize the room configs.".to_string())] |
|
320 |
} |
|
13666 | 321 |
}; |
322 |
server.react(client_id, actions); |
|
323 |
} |
|
13529 | 324 |
} |
325 |
LoadRoom(filename) => { |
|
13666 | 326 |
if server.clients[client_id].is_admin() { |
14413 | 327 |
let actions = match server.io.read_file(&filename) { |
13529 | 328 |
Ok(text) => match server.rooms[room_id].set_saves(&text) { |
329 |
Ok(_) => vec![server_chat("Room configs loaded successfully.".to_string()) |
|
330 |
.send_self().action()], |
|
331 |
Err(e) => { |
|
332 |
warn!("Error while deserializing the room configs: {}", e); |
|
333 |
vec![Warn("Unable to deserialize the room configs.".to_string())] |
|
334 |
} |
|
335 |
} |
|
336 |
Err(e) => { |
|
337 |
warn!("Error while reading the config file \"{}\": {}", filename, e); |
|
338 |
vec![Warn("Unable to load the room configs.".to_string())] |
|
339 |
} |
|
13666 | 340 |
}; |
341 |
server.react(client_id, actions); |
|
342 |
} |
|
13529 | 343 |
} |
13528 | 344 |
Delete(name) => { |
345 |
let actions = if !server.rooms[room_id].delete_config(&name) { |
|
346 |
vec![Warn(format!("Save doesn't exist: {}", name))] |
|
347 |
} else { |
|
348 |
vec![server_chat(format!("Room config {} has been deleted", name)) |
|
349 |
.send_all().in_room(room_id).action()] |
|
350 |
}; |
|
351 |
server.react(client_id, actions); |
|
352 |
} |
|
13450 | 353 |
CallVote(None) => { |
354 |
server.react(client_id, vec![ |
|
13521 | 355 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
13450 | 356 |
.send_self().action()]) |
357 |
} |
|
358 |
CallVote(Some(kind)) => { |
|
13492 | 359 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13450 | 360 |
let error = match &kind { |
361 |
VoteType::Kick(nick) => { |
|
362 |
if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() { |
|
363 |
None |
|
364 |
} else { |
|
13521 | 365 |
Some("/callvote kick: No such user!".to_string()) |
13450 | 366 |
} |
367 |
}, |
|
368 |
VoteType::Map(None) => { |
|
13521 | 369 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
370 |
if names.is_empty() { |
|
371 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
372 |
} else { |
|
373 |
Some(format!("Available maps: {}", names.join(", "))) |
|
374 |
} |
|
13450 | 375 |
}, |
376 |
VoteType::Map(Some(name)) => { |
|
13521 | 377 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
13530 | 378 |
None |
13521 | 379 |
} else { |
13530 | 380 |
Some("/callvote map: No such map!".to_string()) |
13521 | 381 |
} |
13450 | 382 |
}, |
383 |
VoteType::Pause => { |
|
384 |
if is_in_game { |
|
385 |
None |
|
386 |
} else { |
|
13521 | 387 |
Some("/callvote pause: No game in progress!".to_string()) |
13450 | 388 |
} |
389 |
}, |
|
390 |
VoteType::NewSeed => { |
|
391 |
None |
|
392 |
}, |
|
393 |
VoteType::HedgehogsPerTeam(number) => { |
|
394 |
match number { |
|
13709
e335daaa77a9
Add hogs per team named constant that absolutely no one asked for
alfadur
parents:
13666
diff
changeset
|
395 |
1...MAX_HEDGEHOGS_PER_TEAM => None, |
13521 | 396 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()) |
13450 | 397 |
} |
398 |
}, |
|
399 |
}; |
|
400 |
match error { |
|
401 |
None => { |
|
13460 | 402 |
let msg = voting_description(&kind); |
13450 | 403 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13520 | 404 |
server.rooms[room_id].voting = Some(voting); |
13450 | 405 |
server.react(client_id, vec![ |
13521 | 406 |
server_chat(msg).send_all().in_room(room_id).action(), |
13450 | 407 |
AddVote{ vote: true, is_forced: false}]); |
408 |
} |
|
409 |
Some(msg) => { |
|
410 |
server.react(client_id, vec![ |
|
411 |
server_chat(msg).send_self().action()]) |
|
412 |
} |
|
413 |
} |
|
414 |
} |
|
415 |
Vote(vote) => { |
|
13520 | 416 |
server.react(client_id, vec![AddVote{ vote, is_forced: false }]); |
13450 | 417 |
} |
418 |
ForceVote(vote) => { |
|
13520 | 419 |
let is_forced = server.clients[client_id].is_admin(); |
420 |
server.react(client_id, vec![AddVote{ vote, is_forced }]); |
|
13450 | 421 |
} |
13494 | 422 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
423 |
if server.clients[client_id].is_master() { |
|
424 |
server.rooms[room_id].flags.toggle(room_message_flag(&message)); |
|
425 |
} |
|
426 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
427 |
} |
|
13423 | 428 |
StartGame => { |
13520 | 429 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13423 | 430 |
} |
431 |
EngineMessage(em) => { |
|
432 |
let mut actions = Vec::new(); |
|
433 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
434 |
if c.teams_in_game > 0 { |
|
435 |
let decoding = decode(&em[..]).unwrap(); |
|
436 |
let messages = by_msg(&decoding); |
|
13443 | 437 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
438 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
439 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
440 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13423 | 441 |
|
442 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
443 |
if !em_response.is_empty() { |
|
13428 | 444 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13423 | 445 |
.send_all().in_room(r.id).but_self().action()); |
446 |
} |
|
13427 | 447 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
448 |
if let Some(ref mut info) = r.game_info { |
|
13429 | 449 |
if !em_log.is_empty() { |
13428 | 450 |
info.msg_log.push(em_log); |
451 |
} |
|
13443 | 452 |
if let Some(msg) = sync_msg { |
453 |
info.sync_msg = msg; |
|
13427 | 454 |
} |
455 |
} |
|
13423 | 456 |
} |
457 |
} |
|
458 |
server.react(client_id, actions) |
|
459 |
} |
|
460 |
RoundFinished => { |
|
461 |
let mut actions = Vec::new(); |
|
462 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13486 | 463 |
if c.is_in_game() { |
464 |
c.set_is_in_game(false); |
|
13423 | 465 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
466 |
send_all().in_room(r.id).action()); |
|
13426 | 467 |
if r.game_info.is_some() { |
468 |
for team in r.client_teams(c.id) { |
|
469 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
470 |
} |
|
13423 | 471 |
} |
472 |
} |
|
473 |
} |
|
474 |
server.react(client_id, actions) |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
475 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
476 |
Rnd(v) => { |
13492 | 477 |
let result = rnd_reply(&v); |
478 |
let mut echo = vec!["/rnd".to_string()]; |
|
479 |
echo.extend(v.into_iter()); |
|
480 |
let chat_msg = ChatMsg { |
|
481 |
nick: server.clients[client_id].nick.clone(), |
|
482 |
msg: echo.join(" ") |
|
483 |
}; |
|
484 |
server.react(client_id, vec![ |
|
485 |
chat_msg.send_all().in_room(room_id).action(), |
|
486 |
result.send_all().in_room(room_id).action()]) |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
487 |
}, |
13419 | 488 |
_ => warn!("Unimplemented!") |
12147 | 489 |
} |
490 |
} |