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