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