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