author | alfadur <mail@none> |
Sat, 11 Jan 2020 01:06:41 +0300 | |
changeset 15563 | d122b65bdf6f |
parent 15562 | 479911540e17 |
child 15574 | 0031683bfa76 |
permissions | -rw-r--r-- |
15095 | 1 |
use super::{ |
15096 | 2 |
client::HwClient, |
15095 | 3 |
types::{ |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
4 |
ClientId, GameCfg, GameCfg::*, RoomConfig, RoomId, TeamInfo, Voting, MAX_HEDGEHOGS_PER_TEAM, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
5 |
}, |
13416 | 6 |
}; |
13810 | 7 |
use bitflags::*; |
14478 | 8 |
use serde::{Deserialize, Serialize}; |
9 |
use serde_derive::{Deserialize, Serialize}; |
|
13529 | 10 |
use serde_yaml; |
14478 | 11 |
use std::{collections::HashMap, iter}; |
13416 | 12 |
|
14809 | 13 |
pub const MAX_TEAMS_IN_ROOM: u8 = 8; |
15547 | 14 |
pub const MAX_HEDGEHOGS_IN_ROOM: u8 = MAX_TEAMS_IN_ROOM * MAX_HEDGEHOGS_PER_TEAM; |
13119 | 15 |
|
15562 | 16 |
fn client_teams_impl( |
17 |
teams: &[(ClientId, TeamInfo)], |
|
18 |
client_id: ClientId, |
|
19 |
) -> impl Iterator<Item = &TeamInfo> + Clone { |
|
20 |
teams |
|
21 |
.iter() |
|
22 |
.filter(move |(id, _)| *id == client_id) |
|
23 |
.map(|(_, t)| t) |
|
24 |
} |
|
25 |
||
13423 | 26 |
pub struct GameInfo { |
15563 | 27 |
pub original_teams: Vec<(ClientId, TeamInfo)>, |
13427 | 28 |
pub left_teams: Vec<String>, |
13428 | 29 |
pub msg_log: Vec<String>, |
13443 | 30 |
pub sync_msg: Option<String>, |
13427 | 31 |
pub is_paused: bool, |
15563 | 32 |
original_config: RoomConfig, |
13426 | 33 |
} |
34 |
||
35 |
impl GameInfo { |
|
13427 | 36 |
fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo { |
13426 | 37 |
GameInfo { |
13427 | 38 |
left_teams: Vec::new(), |
13428 | 39 |
msg_log: Vec::new(), |
13443 | 40 |
sync_msg: None, |
13427 | 41 |
is_paused: false, |
15563 | 42 |
original_teams: teams, |
43 |
original_config: config, |
|
13426 | 44 |
} |
45 |
} |
|
15562 | 46 |
|
47 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone { |
|
15563 | 48 |
client_teams_impl(&self.original_teams, client_id) |
15562 | 49 |
} |
13423 | 50 |
} |
51 |
||
13529 | 52 |
#[derive(Serialize, Deserialize)] |
13521 | 53 |
pub struct RoomSave { |
54 |
pub location: String, |
|
14478 | 55 |
config: RoomConfig, |
13521 | 56 |
} |
57 |
||
14478 | 58 |
bitflags! { |
13494 | 59 |
pub struct RoomFlags: u8 { |
60 |
const FIXED = 0b0000_0001; |
|
61 |
const RESTRICTED_JOIN = 0b0000_0010; |
|
62 |
const RESTRICTED_TEAM_ADD = 0b0000_0100; |
|
15556 | 63 |
const REGISTRATION_REQUIRED = 0b0000_1000; |
13494 | 64 |
} |
65 |
} |
|
66 |
||
15096 | 67 |
pub struct HwRoom { |
13119 | 68 |
pub id: RoomId, |
13416 | 69 |
pub master_id: Option<ClientId>, |
13119 | 70 |
pub name: String, |
71 |
pub password: Option<String>, |
|
13494 | 72 |
pub greeting: String, |
13486 | 73 |
pub protocol_number: u16, |
13494 | 74 |
pub flags: RoomFlags, |
13416 | 75 |
|
13494 | 76 |
pub players_number: u8, |
13419 | 77 |
pub default_hedgehog_number: u8, |
14809 | 78 |
pub max_teams: u8, |
13119 | 79 |
pub ready_players_number: u8, |
13419 | 80 |
pub teams: Vec<(ClientId, TeamInfo)>, |
13422 | 81 |
config: RoomConfig, |
13450 | 82 |
pub voting: Option<Voting>, |
13521 | 83 |
pub saves: HashMap<String, RoomSave>, |
14478 | 84 |
pub game_info: Option<GameInfo>, |
13119 | 85 |
} |
86 |
||
15096 | 87 |
impl HwRoom { |
88 |
pub fn new(id: RoomId) -> HwRoom { |
|
89 |
HwRoom { |
|
13119 | 90 |
id, |
13416 | 91 |
master_id: None, |
13119 | 92 |
name: String::new(), |
93 |
password: None, |
|
13447 | 94 |
greeting: "".to_string(), |
13494 | 95 |
flags: RoomFlags::empty(), |
13119 | 96 |
protocol_number: 0, |
13416 | 97 |
players_number: 0, |
13419 | 98 |
default_hedgehog_number: 4, |
14809 | 99 |
max_teams: MAX_TEAMS_IN_ROOM, |
13119 | 100 |
ready_players_number: 0, |
13419 | 101 |
teams: Vec::new(), |
13422 | 102 |
config: RoomConfig::new(), |
13450 | 103 |
voting: None, |
13521 | 104 |
saves: HashMap::new(), |
14478 | 105 |
game_info: None, |
13119 | 106 |
} |
107 |
} |
|
13416 | 108 |
|
13419 | 109 |
pub fn hedgehogs_number(&self) -> u8 { |
110 |
self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum() |
|
111 |
} |
|
112 |
||
113 |
pub fn addable_hedgehogs(&self) -> u8 { |
|
114 |
MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number() |
|
115 |
} |
|
116 |
||
14478 | 117 |
pub fn add_team( |
118 |
&mut self, |
|
119 |
owner_id: ClientId, |
|
120 |
mut team: TeamInfo, |
|
121 |
preserve_color: bool, |
|
122 |
) -> &TeamInfo { |
|
13775 | 123 |
if !preserve_color { |
14478 | 124 |
team.color = iter::repeat(()) |
125 |
.enumerate() |
|
126 |
.map(|(i, _)| i as u8) |
|
127 |
.take(u8::max_value() as usize + 1) |
|
13775 | 128 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i)) |
129 |
.unwrap_or(0u8) |
|
130 |
}; |
|
13419 | 131 |
team.hedgehogs_number = if self.teams.is_empty() { |
132 |
self.default_hedgehog_number |
|
133 |
} else { |
|
14478 | 134 |
self.teams[0] |
135 |
.1 |
|
136 |
.hedgehogs_number |
|
137 |
.min(self.addable_hedgehogs()) |
|
13419 | 138 |
}; |
139 |
self.teams.push((owner_id, team)); |
|
140 |
&self.teams.last().unwrap().1 |
|
141 |
} |
|
142 |
||
15504 | 143 |
pub fn remove_team(&mut self, team_name: &str) { |
144 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == team_name) { |
|
15562 | 145 |
self.teams.remove(index); |
146 |
||
15504 | 147 |
if let Some(info) = &mut self.game_info { |
148 |
info.left_teams.push(team_name.to_string()); |
|
149 |
||
150 |
if let Some(m) = &info.sync_msg { |
|
151 |
info.msg_log.push(m.clone()); |
|
152 |
info.sync_msg = None |
|
153 |
} |
|
154 |
let remove_msg = |
|
155 |
crate::utils::to_engine_msg(iter::once(b'F').chain(team_name.bytes())); |
|
156 |
info.msg_log.push(remove_msg.clone()); |
|
157 |
} |
|
13419 | 158 |
} |
159 |
} |
|
160 |
||
13450 | 161 |
pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> { |
162 |
let mut names = Vec::new(); |
|
15562 | 163 |
let teams = match self.game_info { |
15563 | 164 |
Some(ref mut info) => &mut info.original_teams, |
15562 | 165 |
None => &mut self.teams, |
166 |
}; |
|
167 |
||
13450 | 168 |
if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM { |
169 |
for (_, team) in teams.iter_mut() { |
|
170 |
team.hedgehogs_number = n; |
|
171 |
names.push(team.name.clone()) |
|
14478 | 172 |
} |
13450 | 173 |
self.default_hedgehog_number = n; |
174 |
} |
|
175 |
names |
|
176 |
} |
|
177 |
||
15563 | 178 |
pub fn teams_in_game(&self) -> Option<u8> { |
179 |
self.game_info |
|
180 |
.as_ref() |
|
181 |
.map(|info| (info.original_teams.len() - info.left_teams.len()) as u8) |
|
182 |
} |
|
183 |
||
13419 | 184 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)> |
14478 | 185 |
where |
186 |
F: Fn(&TeamInfo) -> bool, |
|
187 |
{ |
|
188 |
self.teams |
|
189 |
.iter_mut() |
|
190 |
.find(|(_, t)| f(t)) |
|
191 |
.map(|(id, t)| (*id, t)) |
|
13419 | 192 |
} |
193 |
||
194 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo> |
|
14478 | 195 |
where |
196 |
F: Fn(&TeamInfo) -> bool, |
|
197 |
{ |
|
198 |
self.teams |
|
199 |
.iter() |
|
200 |
.find_map(|(_, t)| Some(t).filter(|t| f(&t))) |
|
13419 | 201 |
} |
202 |
||
15562 | 203 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> { |
204 |
client_teams_impl(&self.teams, client_id) |
|
13419 | 205 |
} |
206 |
||
13423 | 207 |
pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> { |
14478 | 208 |
self.teams |
209 |
.iter() |
|
210 |
.enumerate() |
|
13423 | 211 |
.filter(move |(_, (id, _))| *id == client_id) |
14478 | 212 |
.map(|(i, _)| i as u8) |
213 |
.collect() |
|
13423 | 214 |
} |
215 |
||
14809 | 216 |
pub fn clan_team_owners(&self, color: u8) -> impl Iterator<Item = ClientId> + '_ { |
217 |
self.teams |
|
218 |
.iter() |
|
219 |
.filter(move |(_, t)| t.color == color) |
|
220 |
.map(|(id, _)| *id) |
|
221 |
} |
|
222 |
||
13419 | 223 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> { |
14478 | 224 |
self.teams |
225 |
.iter() |
|
226 |
.find(|(_, t)| t.name == team_name) |
|
13419 | 227 |
.map(|(id, t)| (*id, &t.name[..])) |
228 |
} |
|
229 |
||
230 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> { |
|
231 |
self.client_teams(owner_id).nth(0).map(|t| t.color) |
|
232 |
} |
|
233 |
||
13423 | 234 |
pub fn has_multiple_clans(&self) -> bool { |
14478 | 235 |
self.teams.iter().min_by_key(|(_, t)| t.color) |
236 |
!= self.teams.iter().max_by_key(|(_, t)| t.color) |
|
13423 | 237 |
} |
238 |
||
13422 | 239 |
pub fn set_config(&mut self, cfg: GameCfg) { |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
240 |
self.config.set_config(cfg); |
13422 | 241 |
} |
242 |
||
13427 | 243 |
pub fn start_round(&mut self) { |
244 |
if self.game_info.is_none() { |
|
14478 | 245 |
self.game_info = Some(GameInfo::new(self.teams.clone(), self.config.clone())); |
13427 | 246 |
} |
247 |
} |
|
248 |
||
13494 | 249 |
pub fn is_fixed(&self) -> bool { |
250 |
self.flags.contains(RoomFlags::FIXED) |
|
251 |
} |
|
252 |
pub fn is_join_restricted(&self) -> bool { |
|
253 |
self.flags.contains(RoomFlags::RESTRICTED_JOIN) |
|
254 |
} |
|
255 |
pub fn is_team_add_restricted(&self) -> bool { |
|
256 |
self.flags.contains(RoomFlags::RESTRICTED_TEAM_ADD) |
|
257 |
} |
|
15556 | 258 |
pub fn is_registration_required(&self) -> bool { |
259 |
self.flags.contains(RoomFlags::REGISTRATION_REQUIRED) |
|
13494 | 260 |
} |
261 |
||
262 |
pub fn set_is_fixed(&mut self, value: bool) { |
|
263 |
self.flags.set(RoomFlags::FIXED, value) |
|
264 |
} |
|
265 |
pub fn set_join_restriction(&mut self, value: bool) { |
|
266 |
self.flags.set(RoomFlags::RESTRICTED_JOIN, value) |
|
267 |
} |
|
268 |
pub fn set_team_add_restriction(&mut self, value: bool) { |
|
269 |
self.flags.set(RoomFlags::RESTRICTED_TEAM_ADD, value) |
|
270 |
} |
|
271 |
pub fn set_unregistered_players_restriction(&mut self, value: bool) { |
|
15556 | 272 |
self.flags.set(RoomFlags::REGISTRATION_REQUIRED, value) |
13494 | 273 |
} |
274 |
||
275 |
fn flags_string(&self) -> String { |
|
276 |
let mut result = "-".to_string(); |
|
14478 | 277 |
if self.game_info.is_some() { |
278 |
result += "g" |
|
279 |
} |
|
280 |
if self.password.is_some() { |
|
281 |
result += "p" |
|
282 |
} |
|
283 |
if self.is_join_restricted() { |
|
284 |
result += "j" |
|
285 |
} |
|
15556 | 286 |
if self.is_registration_required() { |
13494 | 287 |
result += "r" |
288 |
} |
|
289 |
result |
|
290 |
} |
|
291 |
||
15096 | 292 |
pub fn info(&self, master: Option<&HwClient>) -> Vec<String> { |
13422 | 293 |
let c = &self.config; |
13416 | 294 |
vec![ |
13494 | 295 |
self.flags_string(), |
13416 | 296 |
self.name.clone(), |
297 |
self.players_number.to_string(), |
|
298 |
self.teams.len().to_string(), |
|
13422 | 299 |
master.map_or("[]", |c| &c.nick).to_string(), |
300 |
c.map_type.to_string(), |
|
301 |
c.script.to_string(), |
|
302 |
c.scheme.name.to_string(), |
|
14478 | 303 |
c.ammo.name.to_string(), |
13416 | 304 |
] |
305 |
} |
|
13419 | 306 |
|
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
307 |
pub fn active_config(&self) -> &RoomConfig { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
308 |
match self.game_info { |
15563 | 309 |
Some(ref info) => &info.original_config, |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
310 |
None => &self.config, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
311 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
312 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
313 |
|
13422 | 314 |
pub fn map_config(&self) -> Vec<String> { |
13427 | 315 |
match self.game_info { |
15563 | 316 |
Some(ref info) => info.original_config.to_map_config(), |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
317 |
None => self.config.to_map_config(), |
13427 | 318 |
} |
13422 | 319 |
} |
320 |
||
321 |
pub fn game_config(&self) -> Vec<GameCfg> { |
|
13427 | 322 |
match self.game_info { |
15563 | 323 |
Some(ref info) => info.original_config.to_game_config(), |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
324 |
None => self.config.to_game_config(), |
13422 | 325 |
} |
326 |
} |
|
327 |
||
13528 | 328 |
pub fn save_config(&mut self, name: String, location: String) { |
14478 | 329 |
self.saves.insert( |
330 |
name, |
|
331 |
RoomSave { |
|
332 |
location, |
|
333 |
config: self.config.clone(), |
|
334 |
}, |
|
335 |
); |
|
13528 | 336 |
} |
337 |
||
13521 | 338 |
pub fn load_config(&mut self, name: &str) -> Option<&str> { |
339 |
if let Some(save) = self.saves.get(name) { |
|
340 |
self.config = save.config.clone(); |
|
341 |
Some(&save.location[..]) |
|
342 |
} else { |
|
343 |
None |
|
344 |
} |
|
345 |
} |
|
346 |
||
13528 | 347 |
pub fn delete_config(&mut self, name: &str) -> bool { |
348 |
self.saves.remove(name).is_some() |
|
349 |
} |
|
350 |
||
13529 | 351 |
pub fn get_saves(&self) -> Result<String, serde_yaml::Error> { |
352 |
serde_yaml::to_string(&(&self.greeting, &self.saves)) |
|
353 |
} |
|
354 |
||
355 |
pub fn set_saves(&mut self, text: &str) -> Result<(), serde_yaml::Error> { |
|
14478 | 356 |
serde_yaml::from_str::<(String, HashMap<String, RoomSave>)>(text).map( |
357 |
|(greeting, saves)| { |
|
358 |
self.greeting = greeting; |
|
359 |
self.saves = saves; |
|
360 |
}, |
|
361 |
) |
|
13529 | 362 |
} |
14478 | 363 |
} |