author | alfadur <mail@none> |
Wed, 06 Feb 2019 22:40:38 +0300 | |
changeset 14696 | 2071da901c63 |
parent 14695 | f61ce544d436 |
child 14699 | 25c564f77b7d |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
14462 | 3 |
use super::common::rnd_reply; |
14694
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
4 |
use crate::utils::to_engine_msg; |
13671 | 5 |
use crate::{ |
14462 | 6 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage::*}, |
13671 | 7 |
server::{ |
14379 | 8 |
core::HWServer, |
14692
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
9 |
coretypes, |
14462 | 10 |
coretypes::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM}, |
13671 | 11 |
room::{HWRoom, RoomFlags}, |
12 |
}, |
|
14462 | 13 |
utils::is_name_illegal, |
13421 | 14 |
}; |
14462 | 15 |
use base64::{decode, encode}; |
13810 | 16 |
use log::*; |
14694
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
17 |
use std::iter::once; |
14462 | 18 |
use std::mem::swap; |
13428 | 19 |
|
20 |
#[derive(Clone)] |
|
21 |
struct ByMsg<'a> { |
|
14462 | 22 |
messages: &'a [u8], |
13428 | 23 |
} |
24 |
||
14462 | 25 |
impl<'a> Iterator for ByMsg<'a> { |
26 |
type Item = &'a [u8]; |
|
13428 | 27 |
|
28 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
29 |
if let Some(size) = self.messages.get(0) { |
|
30 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
31 |
self.messages = next; |
|
32 |
Some(msg) |
|
33 |
} else { |
|
34 |
None |
|
35 |
} |
|
36 |
} |
|
37 |
} |
|
38 |
||
13529 | 39 |
fn by_msg(source: &[u8]) -> ByMsg { |
14462 | 40 |
ByMsg { messages: source } |
13428 | 41 |
} |
42 |
||
43 |
const VALID_MESSAGES: &[u8] = |
|
44 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
45 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
46 |
||
13434 | 47 |
#[cfg(canhazslicepatterns)] |
13428 | 48 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13429 | 49 |
match msg { |
14462 | 50 |
[size, typ, body..] => { |
51 |
VALID_MESSAGES.contains(typ) |
|
52 |
&& match body { |
|
53 |
[1...MAX_HEDGEHOGS_PER_TEAM, team, ..] if *typ == b'h' => { |
|
54 |
team_indices.contains(team) |
|
55 |
} |
|
56 |
_ => *typ != b'h', |
|
57 |
} |
|
58 |
} |
|
59 |
_ => false, |
|
13428 | 60 |
} |
61 |
} |
|
62 |
||
13671 | 63 |
fn is_msg_valid(msg: &[u8], _team_indices: &[u8]) -> bool { |
13434 | 64 |
if let Some(typ) = msg.get(1) { |
65 |
VALID_MESSAGES.contains(typ) |
|
66 |
} else { |
|
67 |
false |
|
68 |
} |
|
69 |
} |
|
70 |
||
13428 | 71 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13434 | 72 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13428 | 73 |
} |
12152 | 74 |
|
13448 | 75 |
fn is_msg_timed(msg: &[u8]) -> bool { |
14462 | 76 |
msg.get(1) |
77 |
.filter(|t| !NON_TIMED_MESSAGES.contains(t)) |
|
78 |
.is_some() |
|
13448 | 79 |
} |
80 |
||
13485 | 81 |
fn voting_description(kind: &VoteType) -> String { |
14462 | 82 |
format!( |
83 |
"New voting started: {}", |
|
84 |
match kind { |
|
85 |
VoteType::Kick(nick) => format!("kick {}", nick), |
|
86 |
VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()), |
|
87 |
VoteType::Pause => "pause".to_string(), |
|
88 |
VoteType::NewSeed => "new seed".to_string(), |
|
89 |
VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number), |
|
90 |
} |
|
91 |
) |
|
13485 | 92 |
} |
93 |
||
13528 | 94 |
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags { |
13671 | 95 |
use crate::protocol::messages::HWProtocolMessage::*; |
13528 | 96 |
match msg { |
97 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
98 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
99 |
ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, |
|
14462 | 100 |
_ => RoomFlags::empty(), |
13528 | 101 |
} |
102 |
} |
|
103 |
||
14462 | 104 |
pub fn handle( |
105 |
server: &mut HWServer, |
|
106 |
client_id: ClientId, |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
107 |
response: &mut super::Response, |
14462 | 108 |
room_id: RoomId, |
109 |
message: HWProtocolMessage, |
|
110 |
) { |
|
13671 | 111 |
use crate::protocol::messages::HWProtocolMessage::*; |
12152 | 112 |
match message { |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
113 |
Part(msg) => { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
114 |
let lobby_id = server.lobby_id; |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
115 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
116 |
let msg = match msg { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
117 |
Some(s) => format!("part: {}", s), |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
118 |
None => "part".to_string(), |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
119 |
}; |
14695
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
120 |
super::common::exit_room(server, client_id, response, &msg); |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
121 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
122 |
} |
13421 | 123 |
Chat(msg) => { |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
124 |
let client = &mut server.clients[client_id]; |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
125 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
126 |
ChatMsg { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
127 |
nick: client.nick.clone(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
128 |
msg, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
129 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
130 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
131 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
132 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
133 |
} |
13482 | 134 |
Fix => { |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
135 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
136 |
if client.is_admin() { |
14695
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
137 |
room.set_is_fixed(true); |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
138 |
room.set_join_restriction(false); |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
139 |
room.set_team_add_restriction(false); |
f61ce544d436
Server action refactoring part N of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
140 |
room.set_unregistered_players_restriction(true); |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
141 |
} |
13482 | 142 |
} |
143 |
} |
|
144 |
Unfix => { |
|
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
145 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
146 |
if client.is_admin() { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
147 |
room.set_is_fixed(false) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
148 |
} |
13482 | 149 |
} |
150 |
} |
|
151 |
Greeting(text) => { |
|
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
152 |
if let (clienr, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
153 |
if clienr.is_admin() || clienr.is_master() && !room.is_fixed() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
154 |
room.greeting = text |
13482 | 155 |
} |
156 |
} |
|
157 |
} |
|
13421 | 158 |
RoomName(new_name) => { |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
159 |
if is_name_illegal(&new_name) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
160 |
response.add(Warning("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string()).send_self()); |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
161 |
} else if server.has_room(&new_name) { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
162 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
163 |
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:
14681
diff
changeset
|
164 |
); |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
165 |
} else { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
166 |
let room = &mut server.rooms[room_id]; |
14693
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
167 |
if room.is_fixed() || room.master_id != Some(client_id) { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
168 |
response.add(Warning("Access denied.".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
169 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
170 |
let mut old_name = new_name.clone(); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
171 |
let client = &server.clients[client_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
172 |
swap(&mut room.name, &mut old_name); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
173 |
super::common::get_room_update(Some(old_name), room, Some(&client), response); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
174 |
} |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
175 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
176 |
} |
13424 | 177 |
ToggleReady => { |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
178 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
179 |
let flags = if client.is_ready() { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
180 |
room.ready_players_number -= 1; |
13424 | 181 |
"-r" |
182 |
} else { |
|
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
183 |
room.ready_players_number += 1; |
13424 | 184 |
"+r" |
185 |
}; |
|
13806 | 186 |
|
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
187 |
let msg = if client.protocol_number < 38 { |
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
188 |
LegacyReady(client.is_ready(), vec![client.nick.clone()]) |
13806 | 189 |
} else { |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
190 |
ClientFlags(flags.to_string(), vec![client.nick.clone()]) |
13806 | 191 |
}; |
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
192 |
response.add(msg.send_all().in_room(room.id)); |
14696 | 193 |
client.set_is_ready(!client.is_ready()); |
194 |
||
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
195 |
if room.is_fixed() && room.ready_players_number == room.players_number { |
14696 | 196 |
super::common::start_game(server, room_id, response); |
13482 | 197 |
} |
13671 | 198 |
} |
13421 | 199 |
} |
13427 | 200 |
AddTeam(info) => { |
14693
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
201 |
let client = &mut server.clients[client_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
202 |
let room = &mut server.rooms[room_id]; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
203 |
if room.teams.len() >= room.team_limit as usize { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
204 |
response.add(Warning("Too many teams!".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
205 |
} else if room.addable_hedgehogs() == 0 { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
206 |
response.add(Warning("Too many hedgehogs!".to_string()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
207 |
} else if room.find_team(|t| t.name == info.name) != None { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
208 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
209 |
Warning("There's already a team with same name in the list.".to_string()) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
210 |
.send_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
211 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
212 |
} else if room.game_info.is_some() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
213 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
214 |
Warning("Joining not possible: Round is in progress.".to_string()).send_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
215 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
216 |
} else if room.is_team_add_restricted() { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
217 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
218 |
Warning("This room currently does not allow adding new teams.".to_string()) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
219 |
.send_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
220 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
221 |
} else { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
222 |
let team = room.add_team(client.id, *info, client.protocol_number < 42); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
223 |
client.teams_in_game += 1; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
224 |
client.clan = Some(team.color); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
225 |
response.add(TeamAccepted(team.name.clone()).send_self()); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
226 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
227 |
TeamAdd(HWRoom::team_info(&client, team)) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
228 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
229 |
.in_room(room_id) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
230 |
.but_self(), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
231 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
232 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
233 |
TeamColor(team.name.clone(), team.color) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
234 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
235 |
.in_room(room_id), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
236 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
237 |
response.add( |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
238 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
239 |
.send_all() |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
240 |
.in_room(room_id), |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
241 |
); |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
242 |
|
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
243 |
let room_master = if let Some(id) = room.master_id { |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
244 |
Some(&server.clients[id]) |
13424 | 245 |
} else { |
14693
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
246 |
None |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
247 |
}; |
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
248 |
super::common::get_room_update(None, room, room_master, response); |
13424 | 249 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
250 |
} |
13424 | 251 |
RemoveTeam(name) => { |
14696 | 252 |
let client = &mut server.clients[client_id]; |
253 |
let room = &mut server.rooms[room_id]; |
|
254 |
match room.find_team_owner(&name) { |
|
255 |
None => response.add( |
|
256 |
Warning("Error: The team you tried to remove does not exist.".to_string()) |
|
257 |
.send_self(), |
|
258 |
), |
|
259 |
Some((id, _)) if id != client_id => response |
|
260 |
.add(Warning("You can't remove a team you don't own.".to_string()).send_self()), |
|
261 |
Some((_, name)) => { |
|
262 |
client.teams_in_game -= 1; |
|
263 |
client.clan = room.find_team_color(client.id); |
|
264 |
super::common::remove_teams( |
|
265 |
room, |
|
266 |
vec![name.to_string()], |
|
267 |
client.is_in_game(), |
|
268 |
response, |
|
269 |
); |
|
270 |
||
271 |
match room.game_info { |
|
272 |
Some(ref info) if info.teams_in_game == 0 => { |
|
273 |
super::common::end_game(server, room_id, response) |
|
274 |
} |
|
275 |
_ => (), |
|
13424 | 276 |
} |
277 |
} |
|
14680
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
278 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
279 |
} |
13424 | 280 |
SetHedgehogsNumber(team_name, number) => { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
281 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
282 |
let addable_hedgehogs = room.addable_hedgehogs(); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
283 |
if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
284 |
if !client.is_master() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
285 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
286 |
} else if number < 1 |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
287 |
|| number > MAX_HEDGEHOGS_PER_TEAM |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
288 |
|| number > addable_hedgehogs + team.hedgehogs_number |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
289 |
{ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
290 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
291 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
292 |
); |
13424 | 293 |
} else { |
294 |
team.hedgehogs_number = number; |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
295 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
296 |
HedgehogsNumber(team.name.clone(), number) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
297 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
298 |
.in_room(room_id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
299 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
300 |
); |
13424 | 301 |
} |
302 |
} else { |
|
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
303 |
response.add(Warning("No such team.".to_string()).send_self()); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
304 |
} |
13671 | 305 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
306 |
} |
13424 | 307 |
SetTeamColor(team_name, color) => { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
308 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
309 |
if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
310 |
if !client.is_master() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
311 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
13424 | 312 |
} else { |
313 |
team.color = color; |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
314 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
315 |
TeamColor(team.name.clone(), color) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
316 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
317 |
.in_room(room_id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
318 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
319 |
); |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
320 |
server.clients[owner].clan = Some(color); |
13424 | 321 |
} |
322 |
} else { |
|
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
323 |
response.add(Warning("No such team.".to_string()).send_self()); |
13671 | 324 |
} |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
325 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
326 |
} |
13427 | 327 |
Cfg(cfg) => { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
328 |
if let (client, Some(room)) = server.client_and_room(client_id) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
329 |
if room.is_fixed() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
330 |
response.add(Warning("Access denied.".to_string()).send_self()); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
331 |
} else if !client.is_master() { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
332 |
response.add(Error("You're not the room master!".to_string()).send_self()); |
13427 | 333 |
} else { |
13806 | 334 |
let cfg = match cfg { |
335 |
GameCfg::Scheme(name, mut values) => { |
|
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
336 |
if client.protocol_number == 49 && values.len() >= 2 { |
13806 | 337 |
let mut s = "X".repeat(50); |
338 |
s.push_str(&values.pop().unwrap()); |
|
339 |
values.push(s); |
|
340 |
} |
|
341 |
GameCfg::Scheme(name, values) |
|
342 |
} |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
343 |
cfg => cfg, |
13806 | 344 |
}; |
345 |
||
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
346 |
response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self()); |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
347 |
room.set_config(cfg); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
348 |
} |
13671 | 349 |
} |
13424 | 350 |
} |
13533 | 351 |
Save(name, location) => { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
352 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
353 |
server_chat(format!("Room config saved as {}", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
354 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
355 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
356 |
); |
13533 | 357 |
server.rooms[room_id].save_config(name, location); |
358 |
} |
|
13534 | 359 |
SaveRoom(filename) => { |
13671 | 360 |
if server.clients[client_id].is_admin() { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
361 |
match server.rooms[room_id].get_saves() { |
14397 | 362 |
Ok(text) => match server.io.write_file(&filename, &text) { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
363 |
Ok(_) => response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
364 |
server_chat("Room configs saved successfully.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
365 |
), |
13534 | 366 |
Err(e) => { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
367 |
warn!( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
368 |
"Error while writing the config file \"{}\": {}", |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
369 |
filename, e |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
370 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
371 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
372 |
Warning("Unable to save the room configs.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
373 |
); |
13534 | 374 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
375 |
}, |
13534 | 376 |
Err(e) => { |
377 |
warn!("Error while serializing the room configs: {}", e); |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
378 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
379 |
Warning("Unable to serialize the room configs.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
380 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
381 |
) |
13534 | 382 |
} |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
383 |
} |
13671 | 384 |
} |
13534 | 385 |
} |
386 |
LoadRoom(filename) => { |
|
13671 | 387 |
if server.clients[client_id].is_admin() { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
388 |
match server.io.read_file(&filename) { |
13534 | 389 |
Ok(text) => match server.rooms[room_id].set_saves(&text) { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
390 |
Ok(_) => response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
391 |
server_chat("Room configs loaded successfully.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
392 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
393 |
), |
13534 | 394 |
Err(e) => { |
395 |
warn!("Error while deserializing the room configs: {}", e); |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
396 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
397 |
Warning("Unable to deserialize the room configs.".to_string()) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
398 |
.send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
399 |
); |
13534 | 400 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
401 |
}, |
13534 | 402 |
Err(e) => { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
403 |
warn!( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
404 |
"Error while reading the config file \"{}\": {}", |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
405 |
filename, e |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
406 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
407 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
408 |
Warning("Unable to load the room configs.".to_string()).send_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
409 |
); |
13534 | 410 |
} |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
411 |
} |
13671 | 412 |
} |
13534 | 413 |
} |
13533 | 414 |
Delete(name) => { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
415 |
if !server.rooms[room_id].delete_config(&name) { |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
416 |
response.add(Warning(format!("Save doesn't exist: {}", name)).send_self()); |
13533 | 417 |
} else { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
418 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
419 |
server_chat(format!("Room config {} has been deleted", name)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
420 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
421 |
.in_room(room_id), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
422 |
); |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
423 |
} |
13533 | 424 |
} |
13483 | 425 |
CallVote(None) => { |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
426 |
response.add(server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
427 |
.send_self()); |
13483 | 428 |
} |
429 |
CallVote(Some(kind)) => { |
|
13526 | 430 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13483 | 431 |
let error = match &kind { |
432 |
VoteType::Kick(nick) => { |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
433 |
if server |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
434 |
.find_client(&nick) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
435 |
.filter(|c| c.room_id == Some(room_id)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
436 |
.is_some() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
437 |
{ |
13483 | 438 |
None |
439 |
} else { |
|
13532 | 440 |
Some("/callvote kick: No such user!".to_string()) |
13483 | 441 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
442 |
} |
13483 | 443 |
VoteType::Map(None) => { |
13532 | 444 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
445 |
if names.is_empty() { |
|
446 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
447 |
} else { |
|
448 |
Some(format!("Available maps: {}", names.join(", "))) |
|
449 |
} |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
450 |
} |
13483 | 451 |
VoteType::Map(Some(name)) => { |
13532 | 452 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
13535 | 453 |
None |
13532 | 454 |
} else { |
13535 | 455 |
Some("/callvote map: No such map!".to_string()) |
13532 | 456 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
457 |
} |
13483 | 458 |
VoteType::Pause => { |
459 |
if is_in_game { |
|
460 |
None |
|
461 |
} else { |
|
13532 | 462 |
Some("/callvote pause: No game in progress!".to_string()) |
13483 | 463 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
464 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
465 |
VoteType::NewSeed => None, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
466 |
VoteType::HedgehogsPerTeam(number) => match number { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
467 |
1...MAX_HEDGEHOGS_PER_TEAM => None, |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
468 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()), |
13483 | 469 |
}, |
470 |
}; |
|
471 |
match error { |
|
472 |
None => { |
|
13485 | 473 |
let msg = voting_description(&kind); |
13483 | 474 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
475 |
let room = &mut server.rooms[room_id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
476 |
room.voting = Some(voting); |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
477 |
response.add(server_chat(msg).send_all().in_room(room_id)); |
14692
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
478 |
super::common::submit_vote( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
479 |
server, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
480 |
coretypes::Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
481 |
is_pro: true, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
482 |
is_forced: false, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
483 |
}, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
484 |
response, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
485 |
); |
13483 | 486 |
} |
487 |
Some(msg) => { |
|
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
488 |
response.add(server_chat(msg).send_self()); |
13483 | 489 |
} |
490 |
} |
|
491 |
} |
|
492 |
Vote(vote) => { |
|
14692
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
493 |
super::common::submit_vote( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
494 |
server, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
495 |
coretypes::Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
496 |
is_pro: vote, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
497 |
is_forced: false, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
498 |
}, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
499 |
response, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
500 |
); |
13483 | 501 |
} |
502 |
ForceVote(vote) => { |
|
13531 | 503 |
let is_forced = server.clients[client_id].is_admin(); |
14692
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
504 |
super::common::submit_vote( |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
505 |
server, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
506 |
coretypes::Vote { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
507 |
is_pro: vote, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
508 |
is_forced, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
509 |
}, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
510 |
response, |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14691
diff
changeset
|
511 |
); |
13483 | 512 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
513 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
514 |
let client = &server.clients[client_id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
515 |
let room = &mut server.rooms[room_id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
516 |
if client.is_master() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
517 |
room.flags.toggle(room_message_flag(&message)); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
518 |
super::common::get_room_update(None, room, Some(&client), response); |
13528 | 519 |
} |
520 |
} |
|
13428 | 521 |
StartGame => { |
14696 | 522 |
super::common::start_game(server, room_id, response); |
13428 | 523 |
} |
524 |
EngineMessage(em) => { |
|
525 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
526 |
if c.teams_in_game > 0 { |
|
527 |
let decoding = decode(&em[..]).unwrap(); |
|
528 |
let messages = by_msg(&decoding); |
|
13448 | 529 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
530 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
531 |
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:
14681
diff
changeset
|
532 |
if is_msg_empty(m) { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
533 |
Some(encode(m)) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
534 |
} else { |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
535 |
None |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
536 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
537 |
}); |
13428 | 538 |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
539 |
let em_response = |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
540 |
encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
13428 | 541 |
if !em_response.is_empty() { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
542 |
response.add( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
543 |
ForwardEngineMessage(vec![em_response]) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
544 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
545 |
.in_room(r.id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
546 |
.but_self(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
547 |
); |
13428 | 548 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
549 |
let em_log = |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
550 |
encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
13432 | 551 |
if let Some(ref mut info) = r.game_info { |
13434 | 552 |
if !em_log.is_empty() { |
13433 | 553 |
info.msg_log.push(em_log); |
554 |
} |
|
13448 | 555 |
if let Some(msg) = sync_msg { |
556 |
info.sync_msg = msg; |
|
13432 | 557 |
} |
558 |
} |
|
13428 | 559 |
} |
560 |
} |
|
561 |
} |
|
562 |
RoundFinished => { |
|
14694
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
563 |
let mut game_ended = false; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
564 |
let client = &mut server.clients[client_id]; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
565 |
if client.is_in_game() { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
566 |
let room = &mut server.rooms[room_id]; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
567 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
568 |
client.set_is_in_game(false); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
569 |
response.add( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
570 |
ClientFlags("-g".to_string(), vec![client.nick.clone()]) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
571 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
572 |
.in_room(room.id), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
573 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
574 |
let team_names: Vec<_> = room |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
575 |
.client_teams(client_id) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
576 |
.map(|t| t.name.clone()) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
577 |
.collect(); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
578 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
579 |
if let Some(ref mut info) = room.game_info { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
580 |
info.teams_in_game -= team_names.len() as u8; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
581 |
if info.teams_in_game == 0 { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
582 |
game_ended = true; |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
583 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
584 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
585 |
for team_name in team_names { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
586 |
let msg = once(b'F').chain(team_name.bytes()); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
587 |
response.add( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
588 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
589 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
590 |
.in_room(room_id) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
591 |
.but_self(), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
592 |
); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
593 |
|
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
594 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
595 |
if let Some(m) = &info.sync_msg { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
596 |
info.msg_log.push(m.clone()); |
13431 | 597 |
} |
14694
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
598 |
if info.sync_msg.is_some() { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
599 |
info.sync_msg = None |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
600 |
} |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
601 |
info.msg_log.push(remove_msg.clone()); |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
602 |
response.add( |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
603 |
ForwardEngineMessage(vec![remove_msg]) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
604 |
.send_all() |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
605 |
.in_room(room_id) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
606 |
.but_self(), |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
607 |
); |
13428 | 608 |
} |
609 |
} |
|
610 |
} |
|
14694
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
611 |
if game_ended { |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
612 |
super::common::end_game(server, room_id, response) |
aae29ba56aec
Server action refactoring part C of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
613 |
} |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
614 |
} |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
615 |
Rnd(v) => { |
13526 | 616 |
let result = rnd_reply(&v); |
617 |
let mut echo = vec!["/rnd".to_string()]; |
|
618 |
echo.extend(v.into_iter()); |
|
619 |
let chat_msg = ChatMsg { |
|
620 |
nick: server.clients[client_id].nick.clone(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
621 |
msg: echo.join(" "), |
13526 | 622 |
}; |
14681
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
623 |
response.add(chat_msg.send_all().in_room(room_id)); |
9377ee00f1f1
Server action refactoring part 7 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
624 |
response.add(result.send_all().in_room(room_id)); |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
625 |
} |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14681
diff
changeset
|
626 |
_ => warn!("Unimplemented!"), |
12152 | 627 |
} |
628 |
} |