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