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