author | alfadur <mail@none> |
Tue, 05 Feb 2019 17:46:43 +0300 | |
changeset 14704 | 932ff7683653 |
parent 14697 | 9377ee00f1f1 |
child 14707 | 9f98086de1b6 |
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), |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
116 |
None => "part".to_string(), |
14696
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 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
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]; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
124 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
125 |
ChatMsg { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
126 |
nick: client.nick.clone(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
127 |
msg, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
128 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
129 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
130 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
131 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
132 |
} |
13447 | 133 |
Fix => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
134 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
135 |
if client.is_admin() { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
136 |
room.set_is_fixed(true) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
137 |
} |
13447 | 138 |
} |
139 |
} |
|
140 |
Unfix => { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
141 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
142 |
if client.is_admin() { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
143 |
room.set_is_fixed(false) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
144 |
} |
13447 | 145 |
} |
146 |
} |
|
147 |
Greeting(text) => { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
room.greeting = text |
13447 | 151 |
} |
152 |
} |
|
153 |
} |
|
13416 | 154 |
RoomName(new_name) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
155 |
if is_name_illegal(&new_name) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
156 |
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
|
157 |
} else if server.rooms[room_id].is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
158 |
response.add(Warning("Access denied.".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
159 |
} else if server.has_room(&new_name) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
160 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
161 |
Warning("A room with the same name already exists.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
162 |
); |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
163 |
} else { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
164 |
let mut old_name = new_name.clone(); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
165 |
let client = &server.clients[client_id]; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
166 |
let room = &mut server.rooms[room_id]; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
167 |
swap(&mut room.name, &mut old_name); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
168 |
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
|
169 |
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
|
170 |
}; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
171 |
} |
13419 | 172 |
ToggleReady => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
173 |
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
|
174 |
let flags = if client.is_ready() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
175 |
room.ready_players_number -= 1; |
13419 | 176 |
"-r" |
177 |
} else { |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
178 |
room.ready_players_number += 1; |
13419 | 179 |
"+r" |
180 |
}; |
|
13775 | 181 |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
182 |
let msg = if client.protocol_number < 38 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
183 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
13775 | 184 |
} else { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
185 |
ClientFlags(flags.to_string(), vec![client.nick.clone()]) |
13775 | 186 |
}; |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
187 |
response.add(msg.send_all().in_room(room.id)); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
188 |
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
|
189 |
//StartRoomGame(r.id) |
13447 | 190 |
} |
13775 | 191 |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
192 |
client.set_is_ready(!client.is_ready()); |
13666 | 193 |
} |
13416 | 194 |
} |
13422 | 195 |
AddTeam(info) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
196 |
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
|
197 |
if room.teams.len() >= room.team_limit as usize { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
198 |
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
|
199 |
} else if room.addable_hedgehogs() == 0 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
200 |
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
|
201 |
} else if room.find_team(|t| t.name == info.name) != None { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
202 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
203 |
Warning("There's already a team with same name in the list.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
204 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
205 |
); |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
206 |
} else if room.game_info.is_some() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
207 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
208 |
Warning("Joining not possible: Round is in progress.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
209 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
210 |
); |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
211 |
} else if room.is_team_add_restricted() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
212 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
213 |
Warning("This room currently does not allow adding new teams.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
214 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
215 |
); |
13419 | 216 |
} else { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
217 |
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
|
218 |
client.teams_in_game += 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
219 |
client.clan = Some(team.color); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
220 |
response.add(TeamAccepted(team.name.clone()).send_self()); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
221 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
222 |
TeamAdd(HWRoom::team_info(&client, team)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
223 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
224 |
.in_room(room_id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
225 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
226 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
227 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
228 |
TeamColor(team.name.clone(), team.color) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
229 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
230 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
231 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
232 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
233 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
234 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
235 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
236 |
); |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
237 |
|
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
238 |
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
|
239 |
response.add(update_msg.send_all().with_protocol(room.protocol_number)); |
13419 | 240 |
} |
241 |
} |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
242 |
} |
13419 | 243 |
RemoveTeam(name) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
244 |
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
|
245 |
match room.find_team_owner(&name) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
246 |
None => response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
247 |
Warning("Error: The team you tried to remove does not exist.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
248 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
249 |
), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
250 |
Some((id, _)) if id != client_id => response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
251 |
Warning("You can't remove a team you don't own.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
252 |
), |
13419 | 253 |
Some((_, name)) => { |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
254 |
client.teams_in_game -= 1; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
255 |
client.clan = room.find_team_color(client.id); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
256 |
super::common::remove_teams( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
257 |
room, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
258 |
vec![name.to_string()], |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
259 |
client.is_in_game(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
260 |
response, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
261 |
); |
13419 | 262 |
} |
263 |
} |
|
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
264 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
265 |
} |
13419 | 266 |
SetHedgehogsNumber(team_name, number) => { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
267 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
268 |
let addable_hedgehogs = room.addable_hedgehogs(); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
269 |
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:
14696
diff
changeset
|
270 |
if !client.is_master() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
271 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
272 |
} else if number < 1 |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
273 |
|| number > MAX_HEDGEHOGS_PER_TEAM |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
274 |
|| number > addable_hedgehogs + team.hedgehogs_number |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
275 |
{ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
276 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
277 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
278 |
); |
13419 | 279 |
} else { |
280 |
team.hedgehogs_number = number; |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
281 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
282 |
HedgehogsNumber(team.name.clone(), number) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
283 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
284 |
.in_room(room_id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
285 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
286 |
); |
13419 | 287 |
} |
288 |
} else { |
|
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
289 |
response.add(Warning("No such team.".to_string()).send_self()); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
290 |
} |
13666 | 291 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
292 |
} |
13419 | 293 |
SetTeamColor(team_name, color) => { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
294 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
295 |
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:
14696
diff
changeset
|
296 |
if !client.is_master() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
297 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
13419 | 298 |
} else { |
299 |
team.color = color; |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
300 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
301 |
TeamColor(team.name.clone(), color) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
302 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
303 |
.in_room(room_id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
304 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
305 |
); |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
306 |
server.clients[owner].clan = Some(color); |
13419 | 307 |
} |
308 |
} else { |
|
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
309 |
response.add(Warning("No such team.".to_string()).send_self()); |
13666 | 310 |
} |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
311 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
312 |
} |
13422 | 313 |
Cfg(cfg) => { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
314 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
315 |
if room.is_fixed() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
316 |
response.add(Warning("Access denied.".to_string()).send_self()); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
317 |
} else if !client.is_master() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
318 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
13422 | 319 |
} else { |
13775 | 320 |
let cfg = match cfg { |
321 |
GameCfg::Scheme(name, mut values) => { |
|
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
322 |
if client.protocol_number == 49 && values.len() >= 2 { |
13775 | 323 |
let mut s = "X".repeat(50); |
324 |
s.push_str(&values.pop().unwrap()); |
|
325 |
values.push(s); |
|
326 |
} |
|
327 |
GameCfg::Scheme(name, values) |
|
328 |
} |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
329 |
cfg => cfg, |
13775 | 330 |
}; |
331 |
||
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
332 |
response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self()); |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
333 |
room.set_config(cfg); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
334 |
} |
13666 | 335 |
} |
13419 | 336 |
} |
13528 | 337 |
Save(name, location) => { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
338 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
339 |
server_chat(format!("Room config saved as {}", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
340 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
341 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
342 |
); |
13528 | 343 |
server.rooms[room_id].save_config(name, location); |
344 |
} |
|
13529 | 345 |
SaveRoom(filename) => { |
13666 | 346 |
if server.clients[client_id].is_admin() { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
347 |
match server.rooms[room_id].get_saves() { |
14413 | 348 |
Ok(text) => match server.io.write_file(&filename, &text) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
349 |
Ok(_) => response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
350 |
server_chat("Room configs saved successfully.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
351 |
), |
13529 | 352 |
Err(e) => { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
353 |
warn!( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
354 |
"Error while writing the config file \"{}\": {}", |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
355 |
filename, e |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
356 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
357 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
358 |
Warning("Unable to save the room configs.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
359 |
); |
13529 | 360 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
361 |
}, |
13529 | 362 |
Err(e) => { |
363 |
warn!("Error while serializing the room configs: {}", e); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
364 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
365 |
Warning("Unable to serialize the room configs.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
366 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
367 |
) |
13529 | 368 |
} |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
369 |
} |
13666 | 370 |
} |
13529 | 371 |
} |
372 |
LoadRoom(filename) => { |
|
13666 | 373 |
if server.clients[client_id].is_admin() { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
374 |
match server.io.read_file(&filename) { |
13529 | 375 |
Ok(text) => match server.rooms[room_id].set_saves(&text) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
376 |
Ok(_) => response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
377 |
server_chat("Room configs loaded successfully.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
378 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
379 |
), |
13529 | 380 |
Err(e) => { |
381 |
warn!("Error while deserializing the room configs: {}", e); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
382 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
383 |
Warning("Unable to deserialize the room configs.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
384 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
385 |
); |
13529 | 386 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
387 |
}, |
13529 | 388 |
Err(e) => { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
389 |
warn!( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
390 |
"Error while reading the config file \"{}\": {}", |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
391 |
filename, e |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
392 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
393 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
394 |
Warning("Unable to load the room configs.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
395 |
); |
13529 | 396 |
} |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
397 |
} |
13666 | 398 |
} |
13529 | 399 |
} |
13528 | 400 |
Delete(name) => { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
401 |
if !server.rooms[room_id].delete_config(&name) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
402 |
response.add(Warning(format!("Save doesn't exist: {}", name)).send_self()); |
13528 | 403 |
} else { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
404 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
405 |
server_chat(format!("Room config {} has been deleted", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
406 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
407 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
408 |
); |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
409 |
} |
13528 | 410 |
} |
13450 | 411 |
CallVote(None) => { |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
412 |
response.add(server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
413 |
.send_self()); |
13450 | 414 |
} |
415 |
CallVote(Some(kind)) => { |
|
13492 | 416 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13450 | 417 |
let error = match &kind { |
418 |
VoteType::Kick(nick) => { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
419 |
if server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
420 |
.find_client(&nick) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
421 |
.filter(|c| c.room_id == Some(room_id)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
422 |
.is_some() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
423 |
{ |
13450 | 424 |
None |
425 |
} else { |
|
13521 | 426 |
Some("/callvote kick: No such user!".to_string()) |
13450 | 427 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
428 |
} |
13450 | 429 |
VoteType::Map(None) => { |
13521 | 430 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
431 |
if names.is_empty() { |
|
432 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
433 |
} else { |
|
434 |
Some(format!("Available maps: {}", names.join(", "))) |
|
435 |
} |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
436 |
} |
13450 | 437 |
VoteType::Map(Some(name)) => { |
13521 | 438 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
13530 | 439 |
None |
13521 | 440 |
} else { |
13530 | 441 |
Some("/callvote map: No such map!".to_string()) |
13521 | 442 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
443 |
} |
13450 | 444 |
VoteType::Pause => { |
445 |
if is_in_game { |
|
446 |
None |
|
447 |
} else { |
|
13521 | 448 |
Some("/callvote pause: No game in progress!".to_string()) |
13450 | 449 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
450 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
451 |
VoteType::NewSeed => None, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
452 |
VoteType::HedgehogsPerTeam(number) => match number { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
453 |
1...MAX_HEDGEHOGS_PER_TEAM => None, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
454 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()), |
13450 | 455 |
}, |
456 |
}; |
|
457 |
match error { |
|
458 |
None => { |
|
13460 | 459 |
let msg = voting_description(&kind); |
13450 | 460 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13520 | 461 |
server.rooms[room_id].voting = Some(voting); |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
462 |
response.add(server_chat(msg).send_all().in_room(room_id)); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
463 |
//AddVote{ vote: true, is_forced: false} |
13450 | 464 |
} |
465 |
Some(msg) => { |
|
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
466 |
response.add(server_chat(msg).send_self()); |
13450 | 467 |
} |
468 |
} |
|
469 |
} |
|
470 |
Vote(vote) => { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
471 |
server.react( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
472 |
client_id, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
473 |
vec![AddVote { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
474 |
vote, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
475 |
is_forced: false, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
476 |
}], |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
477 |
); |
13450 | 478 |
} |
479 |
ForceVote(vote) => { |
|
13520 | 480 |
let is_forced = server.clients[client_id].is_admin(); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
481 |
server.react(client_id, vec![AddVote { vote, is_forced }]); |
13450 | 482 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
483 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
13494 | 484 |
if server.clients[client_id].is_master() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
485 |
server.rooms[room_id] |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
486 |
.flags |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
487 |
.toggle(room_message_flag(&message)); |
13494 | 488 |
} |
489 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
490 |
} |
|
13423 | 491 |
StartGame => { |
13520 | 492 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13423 | 493 |
} |
494 |
EngineMessage(em) => { |
|
495 |
let mut actions = Vec::new(); |
|
496 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
497 |
if c.teams_in_game > 0 { |
|
498 |
let decoding = decode(&em[..]).unwrap(); |
|
499 |
let messages = by_msg(&decoding); |
|
13443 | 500 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
501 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
502 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
503 |
if is_msg_empty(m) { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
504 |
Some(encode(m)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
505 |
} else { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
506 |
None |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
507 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
508 |
}); |
13423 | 509 |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
510 |
let em_response = |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
511 |
encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
13423 | 512 |
if !em_response.is_empty() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
513 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
514 |
ForwardEngineMessage(vec![em_response]) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
515 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
516 |
.in_room(r.id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
517 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
518 |
); |
13423 | 519 |
} |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
520 |
let em_log = |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
521 |
encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
13427 | 522 |
if let Some(ref mut info) = r.game_info { |
13429 | 523 |
if !em_log.is_empty() { |
13428 | 524 |
info.msg_log.push(em_log); |
525 |
} |
|
13443 | 526 |
if let Some(msg) = sync_msg { |
527 |
info.sync_msg = msg; |
|
13427 | 528 |
} |
529 |
} |
|
13423 | 530 |
} |
531 |
} |
|
532 |
server.react(client_id, actions) |
|
533 |
} |
|
534 |
RoundFinished => { |
|
535 |
let mut actions = Vec::new(); |
|
536 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13486 | 537 |
if c.is_in_game() { |
538 |
c.set_is_in_game(false); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
539 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
540 |
ClientFlags("-g".to_string(), vec![c.nick.clone()]) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
541 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
542 |
.in_room(r.id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
543 |
); |
13426 | 544 |
if r.game_info.is_some() { |
545 |
for team in r.client_teams(c.id) { |
|
546 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
547 |
} |
|
13423 | 548 |
} |
549 |
} |
|
550 |
} |
|
551 |
server.react(client_id, actions) |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
552 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
553 |
Rnd(v) => { |
13492 | 554 |
let result = rnd_reply(&v); |
555 |
let mut echo = vec!["/rnd".to_string()]; |
|
556 |
echo.extend(v.into_iter()); |
|
557 |
let chat_msg = ChatMsg { |
|
558 |
nick: server.clients[client_id].nick.clone(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
559 |
msg: echo.join(" "), |
13492 | 560 |
}; |
14697
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
561 |
response.add(chat_msg.send_all().in_room(room_id)); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
562 |
response.add(result.send_all().in_room(room_id)); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
563 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14697
diff
changeset
|
564 |
_ => warn!("Unimplemented!"), |
12147 | 565 |
} |
566 |
} |