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