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