13427
|
1 |
use std::{iter};
|
13421
|
2 |
use server::{
|
13427
|
3 |
coretypes::{TeamInfo, GameCfg, GameCfg::*},
|
13421
|
4 |
client::{ClientId, HWClient}
|
|
5 |
};
|
|
6 |
|
13424
|
7 |
const MAX_HEDGEHOGS_IN_ROOM: u8 = 48;
|
13124
|
8 |
pub type RoomId = usize;
|
|
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 |
|
13432
|
57 |
fn client_teams_impl(teams: &Vec<(ClientId, TeamInfo)>, client_id: ClientId)
|
|
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 |
|
13124
|
110 |
pub struct HWRoom {
|
|
111 |
pub id: RoomId,
|
13421
|
112 |
pub master_id: Option<ClientId>,
|
13124
|
113 |
pub name: String,
|
|
114 |
pub password: Option<String>,
|
|
115 |
pub protocol_number: u32,
|
13421
|
116 |
|
|
117 |
pub players_number: u32,
|
13424
|
118 |
pub default_hedgehog_number: u8,
|
|
119 |
pub team_limit: u8,
|
13124
|
120 |
pub ready_players_number: u8,
|
13424
|
121 |
pub teams: Vec<(ClientId, TeamInfo)>,
|
13427
|
122 |
config: RoomConfig,
|
13428
|
123 |
pub game_info: Option<GameInfo>
|
13124
|
124 |
}
|
|
125 |
|
|
126 |
impl HWRoom {
|
|
127 |
pub fn new(id: RoomId) -> HWRoom {
|
|
128 |
HWRoom {
|
|
129 |
id,
|
13421
|
130 |
master_id: None,
|
13124
|
131 |
name: String::new(),
|
|
132 |
password: None,
|
|
133 |
protocol_number: 0,
|
13421
|
134 |
players_number: 0,
|
13424
|
135 |
default_hedgehog_number: 4,
|
|
136 |
team_limit: 8,
|
13124
|
137 |
ready_players_number: 0,
|
13424
|
138 |
teams: Vec::new(),
|
13427
|
139 |
config: RoomConfig::new(),
|
13424
|
140 |
game_info: None
|
13124
|
141 |
}
|
|
142 |
}
|
13421
|
143 |
|
13424
|
144 |
pub fn hedgehogs_number(&self) -> u8 {
|
|
145 |
self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
|
|
146 |
}
|
|
147 |
|
|
148 |
pub fn addable_hedgehogs(&self) -> u8 {
|
|
149 |
MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
|
|
150 |
}
|
|
151 |
|
|
152 |
pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
|
|
153 |
team.color = iter::repeat(()).enumerate()
|
|
154 |
.map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
|
|
155 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
|
|
156 |
.unwrap_or(0u8);
|
|
157 |
team.hedgehogs_number = if self.teams.is_empty() {
|
|
158 |
self.default_hedgehog_number
|
|
159 |
} else {
|
|
160 |
self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
|
|
161 |
};
|
|
162 |
self.teams.push((owner_id, team));
|
|
163 |
&self.teams.last().unwrap().1
|
|
164 |
}
|
|
165 |
|
|
166 |
pub fn remove_team(&mut self, name: &str) {
|
|
167 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
|
|
168 |
self.teams.remove(index);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
|
|
173 |
where F: Fn(&TeamInfo) -> bool {
|
|
174 |
self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
|
|
175 |
}
|
|
176 |
|
|
177 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
|
|
178 |
where F: Fn(&TeamInfo) -> bool {
|
|
179 |
self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
|
|
180 |
}
|
|
181 |
|
|
182 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
|
13432
|
183 |
client_teams_impl(&self.teams, client_id)
|
13424
|
184 |
}
|
|
185 |
|
13428
|
186 |
pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
|
|
187 |
self.teams.iter().enumerate()
|
|
188 |
.filter(move |(_, (id, _))| *id == client_id)
|
|
189 |
.map(|(i, _)| i as u8).collect()
|
|
190 |
}
|
|
191 |
|
13424
|
192 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
|
|
193 |
self.teams.iter().find(|(_, t)| t.name == team_name)
|
|
194 |
.map(|(id, t)| (*id, &t.name[..]))
|
|
195 |
}
|
|
196 |
|
|
197 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
|
|
198 |
self.client_teams(owner_id).nth(0).map(|t| t.color)
|
|
199 |
}
|
|
200 |
|
13428
|
201 |
pub fn has_multiple_clans(&self) -> bool {
|
|
202 |
self.teams.iter().min_by_key(|(_, t)| t.color) !=
|
|
203 |
self.teams.iter().max_by_key(|(_, t)| t.color)
|
|
204 |
}
|
|
205 |
|
13427
|
206 |
pub fn set_config(&mut self, cfg: GameCfg) {
|
|
207 |
let c = &mut self.config;
|
|
208 |
match cfg {
|
|
209 |
FeatureSize(s) => c.feature_size = s,
|
|
210 |
MapType(t) => c.map_type = t,
|
|
211 |
MapGenerator(g) => c.map_generator = g,
|
|
212 |
MazeSize(s) => c.maze_size = s,
|
|
213 |
Seed(s) => c.seed = s,
|
|
214 |
Template(t) => c.template = t,
|
|
215 |
|
|
216 |
Ammo(n, s) => c.ammo = Ammo {name: n, settings: s},
|
|
217 |
Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
|
|
218 |
Script(s) => c.script = s,
|
|
219 |
Theme(t) => c.theme = t,
|
|
220 |
DrawnMap(m) => c.drawn_map = Some(m)
|
|
221 |
};
|
|
222 |
}
|
|
223 |
|
13432
|
224 |
pub fn start_round(&mut self) {
|
|
225 |
if self.game_info.is_none() {
|
|
226 |
self.game_info = Some(GameInfo::new(
|
|
227 |
self.teams.clone(), self.config.clone()));
|
|
228 |
}
|
|
229 |
}
|
|
230 |
|
13421
|
231 |
pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
|
|
232 |
let flags = "-".to_string();
|
13427
|
233 |
let c = &self.config;
|
13421
|
234 |
vec![
|
|
235 |
flags,
|
|
236 |
self.name.clone(),
|
|
237 |
self.players_number.to_string(),
|
|
238 |
self.teams.len().to_string(),
|
13427
|
239 |
master.map_or("[]", |c| &c.nick).to_string(),
|
|
240 |
c.map_type.to_string(),
|
|
241 |
c.script.to_string(),
|
|
242 |
c.scheme.name.to_string(),
|
|
243 |
c.ammo.name.to_string()
|
13421
|
244 |
]
|
|
245 |
}
|
13424
|
246 |
|
13427
|
247 |
pub fn map_config(&self) -> Vec<String> {
|
13432
|
248 |
match self.game_info {
|
|
249 |
Some(ref info) => map_config_from(&info.config),
|
|
250 |
None => map_config_from(&self.config)
|
|
251 |
}
|
13427
|
252 |
}
|
|
253 |
|
|
254 |
pub fn game_config(&self) -> Vec<GameCfg> {
|
13432
|
255 |
match self.game_info {
|
|
256 |
Some(ref info) => game_config_from(&info.config),
|
|
257 |
None => game_config_from(&self.config)
|
13427
|
258 |
}
|
|
259 |
}
|
|
260 |
|
13424
|
261 |
pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
|
|
262 |
let mut info = vec![
|
|
263 |
team.name.clone(),
|
|
264 |
team.grave.clone(),
|
|
265 |
team.fort.clone(),
|
|
266 |
team.voice_pack.clone(),
|
|
267 |
team.flag.clone(),
|
|
268 |
owner.nick.clone(),
|
|
269 |
team.difficulty.to_string()];
|
|
270 |
let hogs = team.hedgehogs.iter().flat_map(|h|
|
|
271 |
iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
|
|
272 |
info.extend(hogs);
|
|
273 |
info
|
|
274 |
}
|
13124
|
275 |
} |