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