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