author | alfadur <mail@none> |
Sat, 04 Jan 2020 01:39:13 +0300 | |
changeset 15558 | a798e6441a36 |
parent 15556 | bb93e9642b5b |
child 15562 | 479911540e17 |
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 |
|
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, |
14478 | 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, |
14478 | 31 |
config, |
13426 | 32 |
} |
33 |
} |
|
13423 | 34 |
} |
35 |
||
13529 | 36 |
#[derive(Serialize, Deserialize)] |
13521 | 37 |
pub struct RoomSave { |
38 |
pub location: String, |
|
14478 | 39 |
config: RoomConfig, |
13521 | 40 |
} |
41 |
||
14478 | 42 |
bitflags! { |
13494 | 43 |
pub struct RoomFlags: u8 { |
44 |
const FIXED = 0b0000_0001; |
|
45 |
const RESTRICTED_JOIN = 0b0000_0010; |
|
46 |
const RESTRICTED_TEAM_ADD = 0b0000_0100; |
|
15556 | 47 |
const REGISTRATION_REQUIRED = 0b0000_1000; |
13494 | 48 |
} |
49 |
} |
|
50 |
||
15096 | 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>, |
|
13494 | 56 |
pub greeting: String, |
13486 | 57 |
pub protocol_number: u16, |
13494 | 58 |
pub flags: RoomFlags, |
13416 | 59 |
|
13494 | 60 |
pub players_number: u8, |
13419 | 61 |
pub default_hedgehog_number: u8, |
14809 | 62 |
pub max_teams: u8, |
13119 | 63 |
pub ready_players_number: u8, |
13419 | 64 |
pub teams: Vec<(ClientId, TeamInfo)>, |
13422 | 65 |
config: RoomConfig, |
13450 | 66 |
pub voting: Option<Voting>, |
13521 | 67 |
pub saves: HashMap<String, RoomSave>, |
14478 | 68 |
pub game_info: Option<GameInfo>, |
13119 | 69 |
} |
70 |
||
15096 | 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, |
|
13447 | 78 |
greeting: "".to_string(), |
13494 | 79 |
flags: RoomFlags::empty(), |
13119 | 80 |
protocol_number: 0, |
13416 | 81 |
players_number: 0, |
13419 | 82 |
default_hedgehog_number: 4, |
14809 | 83 |
max_teams: MAX_TEAMS_IN_ROOM, |
13119 | 84 |
ready_players_number: 0, |
13419 | 85 |
teams: Vec::new(), |
13422 | 86 |
config: RoomConfig::new(), |
13450 | 87 |
voting: None, |
13521 | 88 |
saves: HashMap::new(), |
14478 | 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 |
||
14478 | 101 |
pub fn add_team( |
102 |
&mut self, |
|
103 |
owner_id: ClientId, |
|
104 |
mut team: TeamInfo, |
|
105 |
preserve_color: bool, |
|
106 |
) -> &TeamInfo { |
|
13775 | 107 |
if !preserve_color { |
14478 | 108 |
team.color = iter::repeat(()) |
109 |
.enumerate() |
|
110 |
.map(|(i, _)| i as u8) |
|
111 |
.take(u8::max_value() as usize + 1) |
|
13775 | 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 { |
|
14478 | 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 |
||
15504 | 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()); |
|
15558 | 139 |
} else { |
140 |
self.teams.remove(index); |
|
15504 | 141 |
} |
13419 | 142 |
} |
143 |
} |
|
144 |
||
13450 | 145 |
pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> { |
146 |
let mut names = Vec::new(); |
|
15558 | 147 |
let teams = &mut self.teams; |
13450 | 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()) |
|
14478 | 152 |
} |
13450 | 153 |
self.default_hedgehog_number = n; |
154 |
} |
|
155 |
names |
|
156 |
} |
|
157 |
||
15558 | 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)> |
14478 | 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> |
|
14478 | 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 |
||
15558 | 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> { |
14478 | 191 |
self.teams |
192 |
.iter() |
|
193 |
.enumerate() |
|
13423 | 194 |
.filter(move |(_, (id, _))| *id == client_id) |
14478 | 195 |
.map(|(i, _)| i as u8) |
196 |
.collect() |
|
13423 | 197 |
} |
198 |
||
14809 | 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)> { |
14478 | 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 { |
14478 | 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) { |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
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() { |
|
14478 | 228 |
self.game_info = Some(GameInfo::new(self.teams.clone(), self.config.clone())); |
13427 | 229 |
} |
230 |
} |
|
231 |
||
13494 | 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 |
} |
|
15556 | 241 |
pub fn is_registration_required(&self) -> bool { |
242 |
self.flags.contains(RoomFlags::REGISTRATION_REQUIRED) |
|
13494 | 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) { |
|
15556 | 255 |
self.flags.set(RoomFlags::REGISTRATION_REQUIRED, value) |
13494 | 256 |
} |
257 |
||
258 |
fn flags_string(&self) -> String { |
|
259 |
let mut result = "-".to_string(); |
|
14478 | 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 |
} |
|
15556 | 269 |
if self.is_registration_required() { |
13494 | 270 |
result += "r" |
271 |
} |
|
272 |
result |
|
273 |
} |
|
274 |
||
15096 | 275 |
pub fn info(&self, master: Option<&HwClient>) -> Vec<String> { |
13422 | 276 |
let c = &self.config; |
13416 | 277 |
vec![ |
13494 | 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(), |
|
14478 | 286 |
c.ammo.name.to_string(), |
13416 | 287 |
] |
288 |
} |
|
13419 | 289 |
|
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
290 |
pub fn active_config(&self) -> &RoomConfig { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
291 |
match self.game_info { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
292 |
Some(ref info) => &info.config, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
293 |
None => &self.config, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
294 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
295 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
296 |
|
13422 | 297 |
pub fn map_config(&self) -> Vec<String> { |
13427 | 298 |
match self.game_info { |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
299 |
Some(ref info) => info.config.to_map_config(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
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 { |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
diff
changeset
|
306 |
Some(ref info) => info.config.to_game_config(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14478
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) { |
14478 | 312 |
self.saves.insert( |
313 |
name, |
|
314 |
RoomSave { |
|
315 |
location, |
|
316 |
config: self.config.clone(), |
|
317 |
}, |
|
318 |
); |
|
13528 | 319 |
} |
320 |
||
13521 | 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> { |
|
14478 | 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 |
} |
14478 | 346 |
} |