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