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