13666
|
1 |
use crate::server::{
|
14478
|
2 |
client::HWClient,
|
|
3 |
coretypes::{ClientId, GameCfg, GameCfg::*, RoomId, TeamInfo, Voting, MAX_HEDGEHOGS_PER_TEAM},
|
13416
|
4 |
};
|
13810
|
5 |
use bitflags::*;
|
14478
|
6 |
use serde::{Deserialize, Serialize};
|
|
7 |
use serde_derive::{Deserialize, Serialize};
|
13529
|
8 |
use serde_yaml;
|
14478
|
9 |
use std::{collections::HashMap, iter};
|
13416
|
10 |
|
13511
|
11 |
const MAX_TEAMS_IN_ROOM: u8 = 8;
|
14478
|
12 |
const MAX_HEDGEHOGS_IN_ROOM: u8 = MAX_HEDGEHOGS_PER_TEAM * MAX_HEDGEHOGS_PER_TEAM;
|
13119
|
13 |
|
13529
|
14 |
#[derive(Clone, Serialize, Deserialize)]
|
13422
|
15 |
struct Ammo {
|
|
16 |
name: String,
|
14478
|
17 |
settings: Option<String>,
|
13422
|
18 |
}
|
|
19 |
|
13529
|
20 |
#[derive(Clone, Serialize, Deserialize)]
|
13422
|
21 |
struct Scheme {
|
|
22 |
name: String,
|
14478
|
23 |
settings: Vec<String>,
|
13422
|
24 |
}
|
|
25 |
|
13529
|
26 |
#[derive(Clone, Serialize, Deserialize)]
|
13422
|
27 |
struct RoomConfig {
|
|
28 |
feature_size: u32,
|
|
29 |
map_type: String,
|
|
30 |
map_generator: u32,
|
|
31 |
maze_size: u32,
|
|
32 |
seed: String,
|
|
33 |
template: u32,
|
|
34 |
|
|
35 |
ammo: Ammo,
|
|
36 |
scheme: Scheme,
|
|
37 |
script: String,
|
|
38 |
theme: String,
|
14478
|
39 |
drawn_map: Option<String>,
|
13422
|
40 |
}
|
|
41 |
|
|
42 |
impl RoomConfig {
|
|
43 |
fn new() -> RoomConfig {
|
|
44 |
RoomConfig {
|
|
45 |
feature_size: 12,
|
|
46 |
map_type: "+rnd+".to_string(),
|
|
47 |
map_generator: 0,
|
|
48 |
maze_size: 0,
|
|
49 |
seed: "seed".to_string(),
|
|
50 |
template: 0,
|
|
51 |
|
14478
|
52 |
ammo: Ammo {
|
|
53 |
name: "Default".to_string(),
|
|
54 |
settings: None,
|
|
55 |
},
|
|
56 |
scheme: Scheme {
|
|
57 |
name: "Default".to_string(),
|
|
58 |
settings: Vec::new(),
|
|
59 |
},
|
13422
|
60 |
script: "Normal".to_string(),
|
|
61 |
theme: "\u{1f994}".to_string(),
|
14478
|
62 |
drawn_map: None,
|
13422
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
14478
|
67 |
fn client_teams_impl(
|
|
68 |
teams: &[(ClientId, TeamInfo)],
|
|
69 |
client_id: ClientId,
|
|
70 |
) -> impl Iterator<Item = &TeamInfo> + Clone {
|
|
71 |
teams
|
|
72 |
.iter()
|
|
73 |
.filter(move |(id, _)| *id == client_id)
|
|
74 |
.map(|(_, t)| t)
|
13427
|
75 |
}
|
|
76 |
|
|
77 |
fn map_config_from(c: &RoomConfig) -> Vec<String> {
|
14478
|
78 |
vec![
|
|
79 |
c.feature_size.to_string(),
|
|
80 |
c.map_type.to_string(),
|
|
81 |
c.map_generator.to_string(),
|
|
82 |
c.maze_size.to_string(),
|
|
83 |
c.seed.to_string(),
|
|
84 |
c.template.to_string(),
|
|
85 |
]
|
13427
|
86 |
}
|
|
87 |
|
|
88 |
fn game_config_from(c: &RoomConfig) -> Vec<GameCfg> {
|
13666
|
89 |
use crate::server::coretypes::GameCfg::*;
|
13427
|
90 |
let mut v = vec![
|
|
91 |
Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
|
|
92 |
Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
|
|
93 |
Script(c.script.to_string()),
|
14478
|
94 |
Theme(c.theme.to_string()),
|
|
95 |
];
|
13427
|
96 |
if let Some(ref m) = c.drawn_map {
|
|
97 |
v.push(DrawnMap(m.to_string()))
|
|
98 |
}
|
|
99 |
v
|
|
100 |
}
|
|
101 |
|
13423
|
102 |
pub struct GameInfo {
|
13426
|
103 |
pub teams_in_game: u8,
|
13427
|
104 |
pub teams_at_start: Vec<(ClientId, TeamInfo)>,
|
|
105 |
pub left_teams: Vec<String>,
|
13428
|
106 |
pub msg_log: Vec<String>,
|
13443
|
107 |
pub sync_msg: Option<String>,
|
13427
|
108 |
pub is_paused: bool,
|
14478
|
109 |
config: RoomConfig,
|
13426
|
110 |
}
|
|
111 |
|
|
112 |
impl GameInfo {
|
13427
|
113 |
fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
|
13426
|
114 |
GameInfo {
|
13427
|
115 |
left_teams: Vec::new(),
|
13428
|
116 |
msg_log: Vec::new(),
|
13443
|
117 |
sync_msg: None,
|
13427
|
118 |
is_paused: false,
|
|
119 |
teams_in_game: teams.len() as u8,
|
|
120 |
teams_at_start: teams,
|
14478
|
121 |
config,
|
13426
|
122 |
}
|
|
123 |
}
|
13427
|
124 |
|
|
125 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
|
|
126 |
client_teams_impl(&self.teams_at_start, client_id)
|
|
127 |
}
|
13423
|
128 |
}
|
|
129 |
|
13529
|
130 |
#[derive(Serialize, Deserialize)]
|
13521
|
131 |
pub struct RoomSave {
|
|
132 |
pub location: String,
|
14478
|
133 |
config: RoomConfig,
|
13521
|
134 |
}
|
|
135 |
|
14478
|
136 |
bitflags! {
|
13494
|
137 |
pub struct RoomFlags: u8 {
|
|
138 |
const FIXED = 0b0000_0001;
|
|
139 |
const RESTRICTED_JOIN = 0b0000_0010;
|
|
140 |
const RESTRICTED_TEAM_ADD = 0b0000_0100;
|
|
141 |
const RESTRICTED_UNREGISTERED_PLAYERS = 0b0000_1000;
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
13119
|
145 |
pub struct HWRoom {
|
|
146 |
pub id: RoomId,
|
13416
|
147 |
pub master_id: Option<ClientId>,
|
13119
|
148 |
pub name: String,
|
|
149 |
pub password: Option<String>,
|
13494
|
150 |
pub greeting: String,
|
13486
|
151 |
pub protocol_number: u16,
|
13494
|
152 |
pub flags: RoomFlags,
|
13416
|
153 |
|
13494
|
154 |
pub players_number: u8,
|
13419
|
155 |
pub default_hedgehog_number: u8,
|
|
156 |
pub team_limit: u8,
|
13119
|
157 |
pub ready_players_number: u8,
|
13419
|
158 |
pub teams: Vec<(ClientId, TeamInfo)>,
|
13422
|
159 |
config: RoomConfig,
|
13450
|
160 |
pub voting: Option<Voting>,
|
13521
|
161 |
pub saves: HashMap<String, RoomSave>,
|
14478
|
162 |
pub game_info: Option<GameInfo>,
|
13119
|
163 |
}
|
|
164 |
|
|
165 |
impl HWRoom {
|
|
166 |
pub fn new(id: RoomId) -> HWRoom {
|
|
167 |
HWRoom {
|
|
168 |
id,
|
13416
|
169 |
master_id: None,
|
13119
|
170 |
name: String::new(),
|
|
171 |
password: None,
|
13447
|
172 |
greeting: "".to_string(),
|
13494
|
173 |
flags: RoomFlags::empty(),
|
13119
|
174 |
protocol_number: 0,
|
13416
|
175 |
players_number: 0,
|
13419
|
176 |
default_hedgehog_number: 4,
|
13511
|
177 |
team_limit: MAX_TEAMS_IN_ROOM,
|
13119
|
178 |
ready_players_number: 0,
|
13419
|
179 |
teams: Vec::new(),
|
13422
|
180 |
config: RoomConfig::new(),
|
13450
|
181 |
voting: None,
|
13521
|
182 |
saves: HashMap::new(),
|
14478
|
183 |
game_info: None,
|
13119
|
184 |
}
|
|
185 |
}
|
13416
|
186 |
|
13419
|
187 |
pub fn hedgehogs_number(&self) -> u8 {
|
|
188 |
self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
|
|
189 |
}
|
|
190 |
|
|
191 |
pub fn addable_hedgehogs(&self) -> u8 {
|
|
192 |
MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
|
|
193 |
}
|
|
194 |
|
14478
|
195 |
pub fn add_team(
|
|
196 |
&mut self,
|
|
197 |
owner_id: ClientId,
|
|
198 |
mut team: TeamInfo,
|
|
199 |
preserve_color: bool,
|
|
200 |
) -> &TeamInfo {
|
13775
|
201 |
if !preserve_color {
|
14478
|
202 |
team.color = iter::repeat(())
|
|
203 |
.enumerate()
|
|
204 |
.map(|(i, _)| i as u8)
|
|
205 |
.take(u8::max_value() as usize + 1)
|
13775
|
206 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i))
|
|
207 |
.unwrap_or(0u8)
|
|
208 |
};
|
13419
|
209 |
team.hedgehogs_number = if self.teams.is_empty() {
|
|
210 |
self.default_hedgehog_number
|
|
211 |
} else {
|
14478
|
212 |
self.teams[0]
|
|
213 |
.1
|
|
214 |
.hedgehogs_number
|
|
215 |
.min(self.addable_hedgehogs())
|
13419
|
216 |
};
|
|
217 |
self.teams.push((owner_id, team));
|
|
218 |
&self.teams.last().unwrap().1
|
|
219 |
}
|
|
220 |
|
|
221 |
pub fn remove_team(&mut self, name: &str) {
|
|
222 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
|
|
223 |
self.teams.remove(index);
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
13450
|
227 |
pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
|
|
228 |
let mut names = Vec::new();
|
|
229 |
let teams = match self.game_info {
|
|
230 |
Some(ref mut info) => &mut info.teams_at_start,
|
14478
|
231 |
None => &mut self.teams,
|
13450
|
232 |
};
|
|
233 |
|
|
234 |
if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
|
|
235 |
for (_, team) in teams.iter_mut() {
|
|
236 |
team.hedgehogs_number = n;
|
|
237 |
names.push(team.name.clone())
|
14478
|
238 |
}
|
13450
|
239 |
self.default_hedgehog_number = n;
|
|
240 |
}
|
|
241 |
names
|
|
242 |
}
|
|
243 |
|
13419
|
244 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
|
14478
|
245 |
where
|
|
246 |
F: Fn(&TeamInfo) -> bool,
|
|
247 |
{
|
|
248 |
self.teams
|
|
249 |
.iter_mut()
|
|
250 |
.find(|(_, t)| f(t))
|
|
251 |
.map(|(id, t)| (*id, t))
|
13419
|
252 |
}
|
|
253 |
|
|
254 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
|
14478
|
255 |
where
|
|
256 |
F: Fn(&TeamInfo) -> bool,
|
|
257 |
{
|
|
258 |
self.teams
|
|
259 |
.iter()
|
|
260 |
.find_map(|(_, t)| Some(t).filter(|t| f(&t)))
|
13419
|
261 |
}
|
|
262 |
|
|
263 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
|
13427
|
264 |
client_teams_impl(&self.teams, client_id)
|
13419
|
265 |
}
|
|
266 |
|
13423
|
267 |
pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
|
14478
|
268 |
self.teams
|
|
269 |
.iter()
|
|
270 |
.enumerate()
|
13423
|
271 |
.filter(move |(_, (id, _))| *id == client_id)
|
14478
|
272 |
.map(|(i, _)| i as u8)
|
|
273 |
.collect()
|
13423
|
274 |
}
|
|
275 |
|
13419
|
276 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
|
14478
|
277 |
self.teams
|
|
278 |
.iter()
|
|
279 |
.find(|(_, t)| t.name == team_name)
|
13419
|
280 |
.map(|(id, t)| (*id, &t.name[..]))
|
|
281 |
}
|
|
282 |
|
|
283 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
|
|
284 |
self.client_teams(owner_id).nth(0).map(|t| t.color)
|
|
285 |
}
|
|
286 |
|
13423
|
287 |
pub fn has_multiple_clans(&self) -> bool {
|
14478
|
288 |
self.teams.iter().min_by_key(|(_, t)| t.color)
|
|
289 |
!= self.teams.iter().max_by_key(|(_, t)| t.color)
|
13423
|
290 |
}
|
|
291 |
|
13422
|
292 |
pub fn set_config(&mut self, cfg: GameCfg) {
|
|
293 |
let c = &mut self.config;
|
|
294 |
match cfg {
|
|
295 |
FeatureSize(s) => c.feature_size = s,
|
|
296 |
MapType(t) => c.map_type = t,
|
|
297 |
MapGenerator(g) => c.map_generator = g,
|
|
298 |
MazeSize(s) => c.maze_size = s,
|
|
299 |
Seed(s) => c.seed = s,
|
|
300 |
Template(t) => c.template = t,
|
|
301 |
|
14478
|
302 |
Ammo(n, s) => {
|
|
303 |
c.ammo = Ammo {
|
|
304 |
name: n,
|
|
305 |
settings: s,
|
|
306 |
}
|
|
307 |
}
|
|
308 |
Scheme(n, s) => {
|
|
309 |
c.scheme = Scheme {
|
|
310 |
name: n,
|
|
311 |
settings: s,
|
|
312 |
}
|
|
313 |
}
|
13422
|
314 |
Script(s) => c.script = s,
|
|
315 |
Theme(t) => c.theme = t,
|
14478
|
316 |
DrawnMap(m) => c.drawn_map = Some(m),
|
13422
|
317 |
};
|
|
318 |
}
|
|
319 |
|
13427
|
320 |
pub fn start_round(&mut self) {
|
|
321 |
if self.game_info.is_none() {
|
14478
|
322 |
self.game_info = Some(GameInfo::new(self.teams.clone(), self.config.clone()));
|
13427
|
323 |
}
|
|
324 |
}
|
|
325 |
|
13494
|
326 |
pub fn is_fixed(&self) -> bool {
|
|
327 |
self.flags.contains(RoomFlags::FIXED)
|
|
328 |
}
|
|
329 |
pub fn is_join_restricted(&self) -> bool {
|
|
330 |
self.flags.contains(RoomFlags::RESTRICTED_JOIN)
|
|
331 |
}
|
|
332 |
pub fn is_team_add_restricted(&self) -> bool {
|
|
333 |
self.flags.contains(RoomFlags::RESTRICTED_TEAM_ADD)
|
|
334 |
}
|
|
335 |
pub fn are_unregistered_players_restricted(&self) -> bool {
|
14478
|
336 |
self.flags
|
|
337 |
.contains(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS)
|
13494
|
338 |
}
|
|
339 |
|
|
340 |
pub fn set_is_fixed(&mut self, value: bool) {
|
|
341 |
self.flags.set(RoomFlags::FIXED, value)
|
|
342 |
}
|
|
343 |
pub fn set_join_restriction(&mut self, value: bool) {
|
|
344 |
self.flags.set(RoomFlags::RESTRICTED_JOIN, value)
|
|
345 |
}
|
|
346 |
pub fn set_team_add_restriction(&mut self, value: bool) {
|
|
347 |
self.flags.set(RoomFlags::RESTRICTED_TEAM_ADD, value)
|
|
348 |
}
|
|
349 |
pub fn set_unregistered_players_restriction(&mut self, value: bool) {
|
14478
|
350 |
self.flags
|
|
351 |
.set(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, value)
|
13494
|
352 |
}
|
|
353 |
|
|
354 |
fn flags_string(&self) -> String {
|
|
355 |
let mut result = "-".to_string();
|
14478
|
356 |
if self.game_info.is_some() {
|
|
357 |
result += "g"
|
|
358 |
}
|
|
359 |
if self.password.is_some() {
|
|
360 |
result += "p"
|
|
361 |
}
|
|
362 |
if self.is_join_restricted() {
|
|
363 |
result += "j"
|
|
364 |
}
|
13494
|
365 |
if self.are_unregistered_players_restricted() {
|
|
366 |
result += "r"
|
|
367 |
}
|
|
368 |
result
|
|
369 |
}
|
|
370 |
|
13416
|
371 |
pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
|
13422
|
372 |
let c = &self.config;
|
13416
|
373 |
vec![
|
13494
|
374 |
self.flags_string(),
|
13416
|
375 |
self.name.clone(),
|
|
376 |
self.players_number.to_string(),
|
|
377 |
self.teams.len().to_string(),
|
13422
|
378 |
master.map_or("[]", |c| &c.nick).to_string(),
|
|
379 |
c.map_type.to_string(),
|
|
380 |
c.script.to_string(),
|
|
381 |
c.scheme.name.to_string(),
|
14478
|
382 |
c.ammo.name.to_string(),
|
13416
|
383 |
]
|
|
384 |
}
|
13419
|
385 |
|
13422
|
386 |
pub fn map_config(&self) -> Vec<String> {
|
13427
|
387 |
match self.game_info {
|
|
388 |
Some(ref info) => map_config_from(&info.config),
|
14478
|
389 |
None => map_config_from(&self.config),
|
13427
|
390 |
}
|
13422
|
391 |
}
|
|
392 |
|
|
393 |
pub fn game_config(&self) -> Vec<GameCfg> {
|
13427
|
394 |
match self.game_info {
|
|
395 |
Some(ref info) => game_config_from(&info.config),
|
14478
|
396 |
None => game_config_from(&self.config),
|
13422
|
397 |
}
|
|
398 |
}
|
|
399 |
|
13528
|
400 |
pub fn save_config(&mut self, name: String, location: String) {
|
14478
|
401 |
self.saves.insert(
|
|
402 |
name,
|
|
403 |
RoomSave {
|
|
404 |
location,
|
|
405 |
config: self.config.clone(),
|
|
406 |
},
|
|
407 |
);
|
13528
|
408 |
}
|
|
409 |
|
13521
|
410 |
pub fn load_config(&mut self, name: &str) -> Option<&str> {
|
|
411 |
if let Some(save) = self.saves.get(name) {
|
|
412 |
self.config = save.config.clone();
|
|
413 |
Some(&save.location[..])
|
|
414 |
} else {
|
|
415 |
None
|
|
416 |
}
|
|
417 |
}
|
|
418 |
|
13528
|
419 |
pub fn delete_config(&mut self, name: &str) -> bool {
|
|
420 |
self.saves.remove(name).is_some()
|
|
421 |
}
|
|
422 |
|
13529
|
423 |
pub fn get_saves(&self) -> Result<String, serde_yaml::Error> {
|
|
424 |
serde_yaml::to_string(&(&self.greeting, &self.saves))
|
|
425 |
}
|
|
426 |
|
|
427 |
pub fn set_saves(&mut self, text: &str) -> Result<(), serde_yaml::Error> {
|
14478
|
428 |
serde_yaml::from_str::<(String, HashMap<String, RoomSave>)>(text).map(
|
|
429 |
|(greeting, saves)| {
|
|
430 |
self.greeting = greeting;
|
|
431 |
self.saves = saves;
|
|
432 |
},
|
|
433 |
)
|
13529
|
434 |
}
|
|
435 |
|
13419
|
436 |
pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
|
|
437 |
let mut info = vec![
|
|
438 |
team.name.clone(),
|
|
439 |
team.grave.clone(),
|
|
440 |
team.fort.clone(),
|
|
441 |
team.voice_pack.clone(),
|
|
442 |
team.flag.clone(),
|
|
443 |
owner.nick.clone(),
|
14478
|
444 |
team.difficulty.to_string(),
|
|
445 |
];
|
|
446 |
let hogs = team
|
|
447 |
.hedgehogs
|
|
448 |
.iter()
|
|
449 |
.flat_map(|h| iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
|
13419
|
450 |
info.extend(hogs);
|
|
451 |
info
|
|
452 |
}
|
14478
|
453 |
}
|