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