13527
|
1 |
use std::{
|
|
2 |
iter, collections::HashMap
|
|
3 |
};
|
13416
|
4 |
use server::{
|
13478
|
5 |
coretypes::{ClientId, RoomId, TeamInfo, GameCfg, GameCfg::*, Voting},
|
|
6 |
client::{HWClient}
|
13416
|
7 |
};
|
|
8 |
|
13525
|
9 |
const MAX_HEDGEHOGS_IN_ROOM: u8 = 64;
|
|
10 |
const MAX_TEAMS_IN_ROOM: u8 = 8;
|
13119
|
11 |
|
13427
|
12 |
#[derive(Clone)]
|
13422
|
13 |
struct Ammo {
|
|
14 |
name: String,
|
|
15 |
settings: Option<String>
|
|
16 |
}
|
|
17 |
|
13427
|
18 |
#[derive(Clone)]
|
13422
|
19 |
struct Scheme {
|
|
20 |
name: String,
|
|
21 |
settings: Option<Vec<String>>
|
|
22 |
}
|
|
23 |
|
13427
|
24 |
#[derive(Clone)]
|
13422
|
25 |
struct RoomConfig {
|
|
26 |
feature_size: u32,
|
|
27 |
map_type: String,
|
|
28 |
map_generator: u32,
|
|
29 |
maze_size: u32,
|
|
30 |
seed: String,
|
|
31 |
template: u32,
|
|
32 |
|
|
33 |
ammo: Ammo,
|
|
34 |
scheme: Scheme,
|
|
35 |
script: String,
|
|
36 |
theme: String,
|
|
37 |
drawn_map: Option<String>
|
|
38 |
}
|
|
39 |
|
|
40 |
impl RoomConfig {
|
|
41 |
fn new() -> RoomConfig {
|
|
42 |
RoomConfig {
|
|
43 |
feature_size: 12,
|
|
44 |
map_type: "+rnd+".to_string(),
|
|
45 |
map_generator: 0,
|
|
46 |
maze_size: 0,
|
|
47 |
seed: "seed".to_string(),
|
|
48 |
template: 0,
|
|
49 |
|
|
50 |
ammo: Ammo {name: "Default".to_string(), settings: None },
|
|
51 |
scheme: Scheme {name: "Default".to_string(), settings: None },
|
|
52 |
script: "Normal".to_string(),
|
|
53 |
theme: "\u{1f994}".to_string(),
|
|
54 |
drawn_map: None
|
|
55 |
}
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
13524
|
59 |
fn client_teams_impl(teams: &[(ClientId, TeamInfo)], client_id: ClientId)
|
13427
|
60 |
-> impl Iterator<Item = &TeamInfo> + Clone
|
|
61 |
{
|
|
62 |
teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
|
|
63 |
}
|
|
64 |
|
|
65 |
fn map_config_from(c: &RoomConfig) -> Vec<String> {
|
|
66 |
vec![c.feature_size.to_string(), c.map_type.to_string(),
|
|
67 |
c.map_generator.to_string(), c.maze_size.to_string(),
|
|
68 |
c.seed.to_string(), c.template.to_string()]
|
|
69 |
}
|
|
70 |
|
|
71 |
fn game_config_from(c: &RoomConfig) -> Vec<GameCfg> {
|
|
72 |
use server::coretypes::GameCfg::*;
|
|
73 |
let mut v = vec![
|
|
74 |
Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
|
|
75 |
Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
|
|
76 |
Script(c.script.to_string()),
|
|
77 |
Theme(c.theme.to_string())];
|
|
78 |
if let Some(ref m) = c.drawn_map {
|
|
79 |
v.push(DrawnMap(m.to_string()))
|
|
80 |
}
|
|
81 |
v
|
|
82 |
}
|
|
83 |
|
13423
|
84 |
pub struct GameInfo {
|
13426
|
85 |
pub teams_in_game: u8,
|
13427
|
86 |
pub teams_at_start: Vec<(ClientId, TeamInfo)>,
|
|
87 |
pub left_teams: Vec<String>,
|
13428
|
88 |
pub msg_log: Vec<String>,
|
13443
|
89 |
pub sync_msg: Option<String>,
|
13427
|
90 |
pub is_paused: bool,
|
|
91 |
config: RoomConfig
|
13426
|
92 |
}
|
|
93 |
|
|
94 |
impl GameInfo {
|
13427
|
95 |
fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
|
13426
|
96 |
GameInfo {
|
13427
|
97 |
left_teams: Vec::new(),
|
13428
|
98 |
msg_log: Vec::new(),
|
13443
|
99 |
sync_msg: None,
|
13427
|
100 |
is_paused: false,
|
|
101 |
teams_in_game: teams.len() as u8,
|
|
102 |
teams_at_start: teams,
|
|
103 |
config
|
13426
|
104 |
}
|
|
105 |
}
|
13427
|
106 |
|
|
107 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
|
|
108 |
client_teams_impl(&self.teams_at_start, client_id)
|
|
109 |
}
|
13423
|
110 |
}
|
|
111 |
|
13527
|
112 |
pub struct RoomSave {
|
|
113 |
pub location: String,
|
|
114 |
config: RoomConfig
|
|
115 |
}
|
|
116 |
|
13523
|
117 |
bitflags!{
|
|
118 |
pub struct RoomFlags: u8 {
|
|
119 |
const FIXED = 0b0000_0001;
|
|
120 |
const RESTRICTED_JOIN = 0b0000_0010;
|
|
121 |
const RESTRICTED_TEAM_ADD = 0b0000_0100;
|
|
122 |
const RESTRICTED_UNREGISTERED_PLAYERS = 0b0000_1000;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
13119
|
126 |
pub struct HWRoom {
|
|
127 |
pub id: RoomId,
|
13416
|
128 |
pub master_id: Option<ClientId>,
|
13119
|
129 |
pub name: String,
|
|
130 |
pub password: Option<String>,
|
13523
|
131 |
pub greeting: String,
|
13520
|
132 |
pub protocol_number: u16,
|
13523
|
133 |
pub flags: RoomFlags,
|
13416
|
134 |
|
13523
|
135 |
pub players_number: u8,
|
13419
|
136 |
pub default_hedgehog_number: u8,
|
|
137 |
pub team_limit: u8,
|
13119
|
138 |
pub ready_players_number: u8,
|
13419
|
139 |
pub teams: Vec<(ClientId, TeamInfo)>,
|
13422
|
140 |
config: RoomConfig,
|
13478
|
141 |
pub voting: Option<Voting>,
|
13527
|
142 |
pub saves: HashMap<String, RoomSave>,
|
13423
|
143 |
pub game_info: Option<GameInfo>
|
13119
|
144 |
}
|
|
145 |
|
|
146 |
impl HWRoom {
|
|
147 |
pub fn new(id: RoomId) -> HWRoom {
|
|
148 |
HWRoom {
|
|
149 |
id,
|
13416
|
150 |
master_id: None,
|
13119
|
151 |
name: String::new(),
|
|
152 |
password: None,
|
13477
|
153 |
greeting: "".to_string(),
|
13523
|
154 |
flags: RoomFlags::empty(),
|
13119
|
155 |
protocol_number: 0,
|
13416
|
156 |
players_number: 0,
|
13419
|
157 |
default_hedgehog_number: 4,
|
13525
|
158 |
team_limit: MAX_TEAMS_IN_ROOM,
|
13119
|
159 |
ready_players_number: 0,
|
13419
|
160 |
teams: Vec::new(),
|
13422
|
161 |
config: RoomConfig::new(),
|
13478
|
162 |
voting: None,
|
13527
|
163 |
saves: HashMap::new(),
|
13419
|
164 |
game_info: None
|
13119
|
165 |
}
|
|
166 |
}
|
13416
|
167 |
|
13419
|
168 |
pub fn hedgehogs_number(&self) -> u8 {
|
|
169 |
self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
|
|
170 |
}
|
|
171 |
|
|
172 |
pub fn addable_hedgehogs(&self) -> u8 {
|
|
173 |
MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
|
|
174 |
}
|
|
175 |
|
|
176 |
pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
|
|
177 |
team.color = iter::repeat(()).enumerate()
|
|
178 |
.map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
|
|
179 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
|
|
180 |
.unwrap_or(0u8);
|
|
181 |
team.hedgehogs_number = if self.teams.is_empty() {
|
|
182 |
self.default_hedgehog_number
|
|
183 |
} else {
|
|
184 |
self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
|
|
185 |
};
|
|
186 |
self.teams.push((owner_id, team));
|
|
187 |
&self.teams.last().unwrap().1
|
|
188 |
}
|
|
189 |
|
|
190 |
pub fn remove_team(&mut self, name: &str) {
|
|
191 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
|
|
192 |
self.teams.remove(index);
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
13478
|
196 |
pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
|
|
197 |
let mut names = Vec::new();
|
|
198 |
let teams = match self.game_info {
|
|
199 |
Some(ref mut info) => &mut info.teams_at_start,
|
|
200 |
None => &mut self.teams
|
|
201 |
};
|
|
202 |
|
|
203 |
if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
|
|
204 |
for (_, team) in teams.iter_mut() {
|
|
205 |
team.hedgehogs_number = n;
|
|
206 |
names.push(team.name.clone())
|
|
207 |
};
|
|
208 |
self.default_hedgehog_number = n;
|
|
209 |
}
|
|
210 |
names
|
|
211 |
}
|
|
212 |
|
13419
|
213 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
|
|
214 |
where F: Fn(&TeamInfo) -> bool {
|
|
215 |
self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
|
|
216 |
}
|
|
217 |
|
|
218 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
|
|
219 |
where F: Fn(&TeamInfo) -> bool {
|
|
220 |
self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
|
|
221 |
}
|
|
222 |
|
|
223 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
|
13427
|
224 |
client_teams_impl(&self.teams, client_id)
|
13419
|
225 |
}
|
|
226 |
|
13423
|
227 |
pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
|
|
228 |
self.teams.iter().enumerate()
|
|
229 |
.filter(move |(_, (id, _))| *id == client_id)
|
|
230 |
.map(|(i, _)| i as u8).collect()
|
|
231 |
}
|
|
232 |
|
13419
|
233 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
|
|
234 |
self.teams.iter().find(|(_, t)| t.name == team_name)
|
|
235 |
.map(|(id, t)| (*id, &t.name[..]))
|
|
236 |
}
|
|
237 |
|
|
238 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
|
|
239 |
self.client_teams(owner_id).nth(0).map(|t| t.color)
|
|
240 |
}
|
|
241 |
|
13423
|
242 |
pub fn has_multiple_clans(&self) -> bool {
|
|
243 |
self.teams.iter().min_by_key(|(_, t)| t.color) !=
|
|
244 |
self.teams.iter().max_by_key(|(_, t)| t.color)
|
|
245 |
}
|
|
246 |
|
13422
|
247 |
pub fn set_config(&mut self, cfg: GameCfg) {
|
|
248 |
let c = &mut self.config;
|
|
249 |
match cfg {
|
|
250 |
FeatureSize(s) => c.feature_size = s,
|
|
251 |
MapType(t) => c.map_type = t,
|
|
252 |
MapGenerator(g) => c.map_generator = g,
|
|
253 |
MazeSize(s) => c.maze_size = s,
|
|
254 |
Seed(s) => c.seed = s,
|
|
255 |
Template(t) => c.template = t,
|
|
256 |
|
|
257 |
Ammo(n, s) => c.ammo = Ammo {name: n, settings: s},
|
|
258 |
Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
|
|
259 |
Script(s) => c.script = s,
|
|
260 |
Theme(t) => c.theme = t,
|
|
261 |
DrawnMap(m) => c.drawn_map = Some(m)
|
|
262 |
};
|
|
263 |
}
|
|
264 |
|
13427
|
265 |
pub fn start_round(&mut self) {
|
|
266 |
if self.game_info.is_none() {
|
|
267 |
self.game_info = Some(GameInfo::new(
|
|
268 |
self.teams.clone(), self.config.clone()));
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
13523
|
272 |
pub fn is_fixed(&self) -> bool {
|
|
273 |
self.flags.contains(RoomFlags::FIXED)
|
|
274 |
}
|
|
275 |
pub fn is_join_restricted(&self) -> bool {
|
|
276 |
self.flags.contains(RoomFlags::RESTRICTED_JOIN)
|
|
277 |
}
|
|
278 |
pub fn is_team_add_restricted(&self) -> bool {
|
|
279 |
self.flags.contains(RoomFlags::RESTRICTED_TEAM_ADD)
|
|
280 |
}
|
|
281 |
pub fn are_unregistered_players_restricted(&self) -> bool {
|
|
282 |
self.flags.contains(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS)
|
|
283 |
}
|
|
284 |
|
|
285 |
pub fn set_is_fixed(&mut self, value: bool) {
|
|
286 |
self.flags.set(RoomFlags::FIXED, value)
|
|
287 |
}
|
|
288 |
pub fn set_join_restriction(&mut self, value: bool) {
|
|
289 |
self.flags.set(RoomFlags::RESTRICTED_JOIN, value)
|
|
290 |
}
|
|
291 |
pub fn set_team_add_restriction(&mut self, value: bool) {
|
|
292 |
self.flags.set(RoomFlags::RESTRICTED_TEAM_ADD, value)
|
|
293 |
}
|
|
294 |
pub fn set_unregistered_players_restriction(&mut self, value: bool) {
|
|
295 |
self.flags.set(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, value)
|
|
296 |
}
|
|
297 |
|
|
298 |
fn flags_string(&self) -> String {
|
|
299 |
let mut result = "-".to_string();
|
|
300 |
if self.game_info.is_some() { result += "g" }
|
|
301 |
if self.password.is_some() { result += "p" }
|
|
302 |
if self.is_join_restricted() { result += "j" }
|
|
303 |
if self.are_unregistered_players_restricted() {
|
|
304 |
result += "r"
|
|
305 |
}
|
|
306 |
result
|
|
307 |
}
|
|
308 |
|
13416
|
309 |
pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
|
13422
|
310 |
let c = &self.config;
|
13416
|
311 |
vec![
|
13523
|
312 |
self.flags_string(),
|
13416
|
313 |
self.name.clone(),
|
|
314 |
self.players_number.to_string(),
|
|
315 |
self.teams.len().to_string(),
|
13422
|
316 |
master.map_or("[]", |c| &c.nick).to_string(),
|
|
317 |
c.map_type.to_string(),
|
|
318 |
c.script.to_string(),
|
|
319 |
c.scheme.name.to_string(),
|
|
320 |
c.ammo.name.to_string()
|
13416
|
321 |
]
|
|
322 |
}
|
13419
|
323 |
|
13422
|
324 |
pub fn map_config(&self) -> Vec<String> {
|
13427
|
325 |
match self.game_info {
|
|
326 |
Some(ref info) => map_config_from(&info.config),
|
|
327 |
None => map_config_from(&self.config)
|
|
328 |
}
|
13422
|
329 |
}
|
|
330 |
|
|
331 |
pub fn game_config(&self) -> Vec<GameCfg> {
|
13427
|
332 |
match self.game_info {
|
|
333 |
Some(ref info) => game_config_from(&info.config),
|
|
334 |
None => game_config_from(&self.config)
|
13422
|
335 |
}
|
|
336 |
}
|
|
337 |
|
13528
|
338 |
pub fn save_config(&mut self, name: String, location: String) {
|
|
339 |
self.saves.insert(name, RoomSave { location, config: self.config.clone() });
|
|
340 |
}
|
|
341 |
|
13527
|
342 |
pub fn load_config(&mut self, name: &str) -> Option<&str> {
|
|
343 |
if let Some(save) = self.saves.get(name) {
|
|
344 |
self.config = save.config.clone();
|
|
345 |
Some(&save.location[..])
|
|
346 |
} else {
|
|
347 |
None
|
|
348 |
}
|
|
349 |
}
|
|
350 |
|
13528
|
351 |
pub fn delete_config(&mut self, name: &str) -> bool {
|
|
352 |
self.saves.remove(name).is_some()
|
|
353 |
}
|
|
354 |
|
13419
|
355 |
pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
|
|
356 |
let mut info = vec![
|
|
357 |
team.name.clone(),
|
|
358 |
team.grave.clone(),
|
|
359 |
team.fort.clone(),
|
|
360 |
team.voice_pack.clone(),
|
|
361 |
team.flag.clone(),
|
|
362 |
owner.nick.clone(),
|
|
363 |
team.difficulty.to_string()];
|
|
364 |
let hogs = team.hedgehogs.iter().flat_map(|h|
|
|
365 |
iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
|
|
366 |
info.extend(hogs);
|
|
367 |
info
|
|
368 |
}
|
13119
|
369 |
} |