author | alfadur |
Sun, 09 Sep 2018 01:20:35 +0300 | |
changeset 13806 | 5fb40c8e5542 |
parent 13800 | e335daaa77a9 |
child 13810 | 0463a4221327 |
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, |
|
13806 | 27 |
settings: Vec<String> |
13427 | 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 }, |
|
13806 | 57 |
scheme: Scheme {name: "Default".to_string(), settings: Vec::new() }, |
13427 | 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 |
||
13806 | 183 |
pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo, preserve_color: bool) -> &TeamInfo { |
184 |
if !preserve_color { |
|
185 |
team.color = iter::repeat(()).enumerate() |
|
186 |
.map(|(i, _)| i as u8).take(u8::max_value() as usize + 1) |
|
187 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i)) |
|
188 |
.unwrap_or(0u8) |
|
189 |
}; |
|
13424 | 190 |
team.hedgehogs_number = if self.teams.is_empty() { |
191 |
self.default_hedgehog_number |
|
192 |
} else { |
|
193 |
self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs()) |
|
194 |
}; |
|
195 |
self.teams.push((owner_id, team)); |
|
196 |
&self.teams.last().unwrap().1 |
|
197 |
} |
|
198 |
||
199 |
pub fn remove_team(&mut self, name: &str) { |
|
200 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) { |
|
201 |
self.teams.remove(index); |
|
202 |
} |
|
203 |
} |
|
204 |
||
13483 | 205 |
pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> { |
206 |
let mut names = Vec::new(); |
|
207 |
let teams = match self.game_info { |
|
208 |
Some(ref mut info) => &mut info.teams_at_start, |
|
209 |
None => &mut self.teams |
|
210 |
}; |
|
211 |
||
212 |
if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM { |
|
213 |
for (_, team) in teams.iter_mut() { |
|
214 |
team.hedgehogs_number = n; |
|
215 |
names.push(team.name.clone()) |
|
216 |
}; |
|
217 |
self.default_hedgehog_number = n; |
|
218 |
} |
|
219 |
names |
|
220 |
} |
|
221 |
||
13424 | 222 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)> |
223 |
where F: Fn(&TeamInfo) -> bool { |
|
224 |
self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t)) |
|
225 |
} |
|
226 |
||
227 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo> |
|
228 |
where F: Fn(&TeamInfo) -> bool { |
|
229 |
self.teams.iter().map(|(_, t)| t).find(|t| f(*t)) |
|
230 |
} |
|
231 |
||
232 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> { |
|
13432 | 233 |
client_teams_impl(&self.teams, client_id) |
13424 | 234 |
} |
235 |
||
13428 | 236 |
pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> { |
237 |
self.teams.iter().enumerate() |
|
238 |
.filter(move |(_, (id, _))| *id == client_id) |
|
239 |
.map(|(i, _)| i as u8).collect() |
|
240 |
} |
|
241 |
||
13424 | 242 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> { |
243 |
self.teams.iter().find(|(_, t)| t.name == team_name) |
|
244 |
.map(|(id, t)| (*id, &t.name[..])) |
|
245 |
} |
|
246 |
||
247 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> { |
|
248 |
self.client_teams(owner_id).nth(0).map(|t| t.color) |
|
249 |
} |
|
250 |
||
13428 | 251 |
pub fn has_multiple_clans(&self) -> bool { |
252 |
self.teams.iter().min_by_key(|(_, t)| t.color) != |
|
253 |
self.teams.iter().max_by_key(|(_, t)| t.color) |
|
254 |
} |
|
255 |
||
13427 | 256 |
pub fn set_config(&mut self, cfg: GameCfg) { |
257 |
let c = &mut self.config; |
|
258 |
match cfg { |
|
259 |
FeatureSize(s) => c.feature_size = s, |
|
260 |
MapType(t) => c.map_type = t, |
|
261 |
MapGenerator(g) => c.map_generator = g, |
|
262 |
MazeSize(s) => c.maze_size = s, |
|
263 |
Seed(s) => c.seed = s, |
|
264 |
Template(t) => c.template = t, |
|
265 |
||
266 |
Ammo(n, s) => c.ammo = Ammo {name: n, settings: s}, |
|
267 |
Scheme(n, s) => c.scheme = Scheme {name: n, settings: s}, |
|
268 |
Script(s) => c.script = s, |
|
269 |
Theme(t) => c.theme = t, |
|
270 |
DrawnMap(m) => c.drawn_map = Some(m) |
|
271 |
}; |
|
272 |
} |
|
273 |
||
13432 | 274 |
pub fn start_round(&mut self) { |
275 |
if self.game_info.is_none() { |
|
276 |
self.game_info = Some(GameInfo::new( |
|
277 |
self.teams.clone(), self.config.clone())); |
|
278 |
} |
|
279 |
} |
|
280 |
||
13528 | 281 |
pub fn is_fixed(&self) -> bool { |
282 |
self.flags.contains(RoomFlags::FIXED) |
|
283 |
} |
|
284 |
pub fn is_join_restricted(&self) -> bool { |
|
285 |
self.flags.contains(RoomFlags::RESTRICTED_JOIN) |
|
286 |
} |
|
287 |
pub fn is_team_add_restricted(&self) -> bool { |
|
288 |
self.flags.contains(RoomFlags::RESTRICTED_TEAM_ADD) |
|
289 |
} |
|
290 |
pub fn are_unregistered_players_restricted(&self) -> bool { |
|
291 |
self.flags.contains(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS) |
|
292 |
} |
|
293 |
||
294 |
pub fn set_is_fixed(&mut self, value: bool) { |
|
295 |
self.flags.set(RoomFlags::FIXED, value) |
|
296 |
} |
|
297 |
pub fn set_join_restriction(&mut self, value: bool) { |
|
298 |
self.flags.set(RoomFlags::RESTRICTED_JOIN, value) |
|
299 |
} |
|
300 |
pub fn set_team_add_restriction(&mut self, value: bool) { |
|
301 |
self.flags.set(RoomFlags::RESTRICTED_TEAM_ADD, value) |
|
302 |
} |
|
303 |
pub fn set_unregistered_players_restriction(&mut self, value: bool) { |
|
304 |
self.flags.set(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, value) |
|
305 |
} |
|
306 |
||
307 |
fn flags_string(&self) -> String { |
|
308 |
let mut result = "-".to_string(); |
|
309 |
if self.game_info.is_some() { result += "g" } |
|
310 |
if self.password.is_some() { result += "p" } |
|
311 |
if self.is_join_restricted() { result += "j" } |
|
312 |
if self.are_unregistered_players_restricted() { |
|
313 |
result += "r" |
|
314 |
} |
|
315 |
result |
|
316 |
} |
|
317 |
||
13421 | 318 |
pub fn info(&self, master: Option<&HWClient>) -> Vec<String> { |
13427 | 319 |
let c = &self.config; |
13421 | 320 |
vec![ |
13528 | 321 |
self.flags_string(), |
13421 | 322 |
self.name.clone(), |
323 |
self.players_number.to_string(), |
|
324 |
self.teams.len().to_string(), |
|
13427 | 325 |
master.map_or("[]", |c| &c.nick).to_string(), |
326 |
c.map_type.to_string(), |
|
327 |
c.script.to_string(), |
|
328 |
c.scheme.name.to_string(), |
|
329 |
c.ammo.name.to_string() |
|
13421 | 330 |
] |
331 |
} |
|
13424 | 332 |
|
13427 | 333 |
pub fn map_config(&self) -> Vec<String> { |
13432 | 334 |
match self.game_info { |
335 |
Some(ref info) => map_config_from(&info.config), |
|
336 |
None => map_config_from(&self.config) |
|
337 |
} |
|
13427 | 338 |
} |
339 |
||
340 |
pub fn game_config(&self) -> Vec<GameCfg> { |
|
13432 | 341 |
match self.game_info { |
342 |
Some(ref info) => game_config_from(&info.config), |
|
343 |
None => game_config_from(&self.config) |
|
13427 | 344 |
} |
345 |
} |
|
346 |
||
13533 | 347 |
pub fn save_config(&mut self, name: String, location: String) { |
348 |
self.saves.insert(name, RoomSave { location, config: self.config.clone() }); |
|
349 |
} |
|
350 |
||
13532 | 351 |
pub fn load_config(&mut self, name: &str) -> Option<&str> { |
352 |
if let Some(save) = self.saves.get(name) { |
|
353 |
self.config = save.config.clone(); |
|
354 |
Some(&save.location[..]) |
|
355 |
} else { |
|
356 |
None |
|
357 |
} |
|
358 |
} |
|
359 |
||
13533 | 360 |
pub fn delete_config(&mut self, name: &str) -> bool { |
361 |
self.saves.remove(name).is_some() |
|
362 |
} |
|
363 |
||
13534 | 364 |
pub fn get_saves(&self) -> Result<String, serde_yaml::Error> { |
365 |
serde_yaml::to_string(&(&self.greeting, &self.saves)) |
|
366 |
} |
|
367 |
||
368 |
pub fn set_saves(&mut self, text: &str) -> Result<(), serde_yaml::Error> { |
|
369 |
serde_yaml::from_str::<(String, HashMap<String, RoomSave>)>(text).map(|(greeting, saves)| { |
|
370 |
self.greeting = greeting; |
|
371 |
self.saves = saves; |
|
372 |
}) |
|
373 |
} |
|
374 |
||
13424 | 375 |
pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> { |
376 |
let mut info = vec![ |
|
377 |
team.name.clone(), |
|
378 |
team.grave.clone(), |
|
379 |
team.fort.clone(), |
|
380 |
team.voice_pack.clone(), |
|
381 |
team.flag.clone(), |
|
382 |
owner.nick.clone(), |
|
383 |
team.difficulty.to_string()]; |
|
384 |
let hogs = team.hedgehogs.iter().flat_map(|h| |
|
385 |
iter::once(h.name.clone()).chain(iter::once(h.hat.clone()))); |
|
386 |
info.extend(hogs); |
|
387 |
info |
|
388 |
} |
|
13124 | 389 |
} |