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