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 |
|
13422
|
10 |
struct Ammo {
|
|
11 |
name: String,
|
|
12 |
settings: Option<String>
|
|
13 |
}
|
|
14 |
|
|
15 |
struct Scheme {
|
|
16 |
name: String,
|
|
17 |
settings: Option<Vec<String>>
|
|
18 |
}
|
|
19 |
|
|
20 |
struct RoomConfig {
|
|
21 |
feature_size: u32,
|
|
22 |
map_type: String,
|
|
23 |
map_generator: u32,
|
|
24 |
maze_size: u32,
|
|
25 |
seed: String,
|
|
26 |
template: u32,
|
|
27 |
|
|
28 |
ammo: Ammo,
|
|
29 |
scheme: Scheme,
|
|
30 |
script: String,
|
|
31 |
theme: String,
|
|
32 |
drawn_map: Option<String>
|
|
33 |
}
|
|
34 |
|
|
35 |
impl RoomConfig {
|
|
36 |
fn new() -> RoomConfig {
|
|
37 |
RoomConfig {
|
|
38 |
feature_size: 12,
|
|
39 |
map_type: "+rnd+".to_string(),
|
|
40 |
map_generator: 0,
|
|
41 |
maze_size: 0,
|
|
42 |
seed: "seed".to_string(),
|
|
43 |
template: 0,
|
|
44 |
|
|
45 |
ammo: Ammo {name: "Default".to_string(), settings: None },
|
|
46 |
scheme: Scheme {name: "Default".to_string(), settings: None },
|
|
47 |
script: "Normal".to_string(),
|
|
48 |
theme: "\u{1f994}".to_string(),
|
|
49 |
drawn_map: None
|
|
50 |
}
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
13423
|
54 |
pub struct GameInfo {
|
|
55 |
pub teams_in_game: u8
|
|
56 |
}
|
|
57 |
|
13119
|
58 |
pub struct HWRoom {
|
|
59 |
pub id: RoomId,
|
13416
|
60 |
pub master_id: Option<ClientId>,
|
13119
|
61 |
pub name: String,
|
|
62 |
pub password: Option<String>,
|
|
63 |
pub protocol_number: u32,
|
13416
|
64 |
|
|
65 |
pub players_number: u32,
|
13419
|
66 |
pub default_hedgehog_number: u8,
|
|
67 |
pub team_limit: u8,
|
13119
|
68 |
pub ready_players_number: u8,
|
13419
|
69 |
pub teams: Vec<(ClientId, TeamInfo)>,
|
13422
|
70 |
config: RoomConfig,
|
13423
|
71 |
pub game_info: Option<GameInfo>
|
13119
|
72 |
}
|
|
73 |
|
|
74 |
impl HWRoom {
|
|
75 |
pub fn new(id: RoomId) -> HWRoom {
|
|
76 |
HWRoom {
|
|
77 |
id,
|
13416
|
78 |
master_id: None,
|
13119
|
79 |
name: String::new(),
|
|
80 |
password: None,
|
|
81 |
protocol_number: 0,
|
13416
|
82 |
players_number: 0,
|
13419
|
83 |
default_hedgehog_number: 4,
|
|
84 |
team_limit: 8,
|
13119
|
85 |
ready_players_number: 0,
|
13419
|
86 |
teams: Vec::new(),
|
13422
|
87 |
config: RoomConfig::new(),
|
13419
|
88 |
game_info: None
|
13119
|
89 |
}
|
|
90 |
}
|
13416
|
91 |
|
13419
|
92 |
pub fn hedgehogs_number(&self) -> u8 {
|
|
93 |
self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
|
|
94 |
}
|
|
95 |
|
|
96 |
pub fn addable_hedgehogs(&self) -> u8 {
|
|
97 |
MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
|
|
98 |
}
|
|
99 |
|
|
100 |
pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
|
|
101 |
team.color = iter::repeat(()).enumerate()
|
|
102 |
.map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
|
|
103 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
|
|
104 |
.unwrap_or(0u8);
|
|
105 |
team.hedgehogs_number = if self.teams.is_empty() {
|
|
106 |
self.default_hedgehog_number
|
|
107 |
} else {
|
|
108 |
self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
|
|
109 |
};
|
|
110 |
self.teams.push((owner_id, team));
|
|
111 |
&self.teams.last().unwrap().1
|
|
112 |
}
|
|
113 |
|
|
114 |
pub fn remove_team(&mut self, name: &str) {
|
|
115 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
|
|
116 |
self.teams.remove(index);
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
|
|
121 |
where F: Fn(&TeamInfo) -> bool {
|
|
122 |
self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
|
|
123 |
}
|
|
124 |
|
|
125 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
|
|
126 |
where F: Fn(&TeamInfo) -> bool {
|
|
127 |
self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
|
|
128 |
}
|
|
129 |
|
|
130 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
|
|
131 |
self.teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
|
|
132 |
}
|
|
133 |
|
13423
|
134 |
pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
|
|
135 |
self.teams.iter().enumerate()
|
|
136 |
.filter(move |(_, (id, _))| *id == client_id)
|
|
137 |
.map(|(i, _)| i as u8).collect()
|
|
138 |
}
|
|
139 |
|
13419
|
140 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
|
|
141 |
self.teams.iter().find(|(_, t)| t.name == team_name)
|
|
142 |
.map(|(id, t)| (*id, &t.name[..]))
|
|
143 |
}
|
|
144 |
|
|
145 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
|
|
146 |
self.client_teams(owner_id).nth(0).map(|t| t.color)
|
|
147 |
}
|
|
148 |
|
13423
|
149 |
pub fn has_multiple_clans(&self) -> bool {
|
|
150 |
self.teams.iter().min_by_key(|(_, t)| t.color) !=
|
|
151 |
self.teams.iter().max_by_key(|(_, t)| t.color)
|
|
152 |
}
|
|
153 |
|
13422
|
154 |
pub fn set_config(&mut self, cfg: GameCfg) {
|
|
155 |
let c = &mut self.config;
|
|
156 |
match cfg {
|
|
157 |
FeatureSize(s) => c.feature_size = s,
|
|
158 |
MapType(t) => c.map_type = t,
|
|
159 |
MapGenerator(g) => c.map_generator = g,
|
|
160 |
MazeSize(s) => c.maze_size = s,
|
|
161 |
Seed(s) => c.seed = s,
|
|
162 |
Template(t) => c.template = t,
|
|
163 |
|
|
164 |
Ammo(n, s) => c.ammo = Ammo {name: n, settings: s},
|
|
165 |
Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
|
|
166 |
Script(s) => c.script = s,
|
|
167 |
Theme(t) => c.theme = t,
|
|
168 |
DrawnMap(m) => c.drawn_map = Some(m)
|
|
169 |
};
|
|
170 |
}
|
|
171 |
|
13416
|
172 |
pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
|
|
173 |
let flags = "-".to_string();
|
13422
|
174 |
let c = &self.config;
|
13416
|
175 |
vec![
|
|
176 |
flags,
|
|
177 |
self.name.clone(),
|
|
178 |
self.players_number.to_string(),
|
|
179 |
self.teams.len().to_string(),
|
13422
|
180 |
master.map_or("[]", |c| &c.nick).to_string(),
|
|
181 |
c.map_type.to_string(),
|
|
182 |
c.script.to_string(),
|
|
183 |
c.scheme.name.to_string(),
|
|
184 |
c.ammo.name.to_string()
|
13416
|
185 |
]
|
|
186 |
}
|
13419
|
187 |
|
13422
|
188 |
pub fn map_config(&self) -> Vec<String> {
|
|
189 |
let c = &self.config;
|
|
190 |
vec![c.feature_size.to_string(), c.map_type.to_string(),
|
|
191 |
c.map_generator.to_string(), c.maze_size.to_string(),
|
|
192 |
c.seed.to_string(), c.template.to_string()]
|
|
193 |
}
|
|
194 |
|
|
195 |
pub fn game_config(&self) -> Vec<GameCfg> {
|
|
196 |
use server::coretypes::GameCfg::*;
|
|
197 |
let c = &self.config;
|
|
198 |
let mut v = vec![
|
|
199 |
Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
|
|
200 |
Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
|
|
201 |
Script(c.script.to_string()),
|
|
202 |
Theme(c.theme.to_string())];
|
|
203 |
if let Some(ref m) = c.drawn_map {
|
|
204 |
v.push(DrawnMap(m.to_string()))
|
|
205 |
}
|
|
206 |
v
|
|
207 |
}
|
|
208 |
|
13419
|
209 |
pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
|
|
210 |
let mut info = vec![
|
|
211 |
team.name.clone(),
|
|
212 |
team.grave.clone(),
|
|
213 |
team.fort.clone(),
|
|
214 |
team.voice_pack.clone(),
|
|
215 |
team.flag.clone(),
|
|
216 |
owner.nick.clone(),
|
|
217 |
team.difficulty.to_string()];
|
|
218 |
let hogs = team.hedgehogs.iter().flat_map(|h|
|
|
219 |
iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
|
|
220 |
info.extend(hogs);
|
|
221 |
info
|
|
222 |
}
|
13119
|
223 |
} |