13424
|
1 |
use std::iter;
|
13421
|
2 |
use server::{
|
13424
|
3 |
coretypes::{TeamInfo, GameCfg},
|
13421
|
4 |
client::{ClientId, HWClient}
|
|
5 |
};
|
|
6 |
|
13424
|
7 |
const MAX_HEDGEHOGS_IN_ROOM: u8 = 48;
|
13124
|
8 |
pub type RoomId = usize;
|
|
9 |
|
|
10 |
pub struct HWRoom {
|
|
11 |
pub id: RoomId,
|
13421
|
12 |
pub master_id: Option<ClientId>,
|
13124
|
13 |
pub name: String,
|
|
14 |
pub password: Option<String>,
|
|
15 |
pub protocol_number: u32,
|
13421
|
16 |
|
|
17 |
pub players_number: u32,
|
13424
|
18 |
pub default_hedgehog_number: u8,
|
|
19 |
pub team_limit: u8,
|
13124
|
20 |
pub ready_players_number: u8,
|
13424
|
21 |
pub teams: Vec<(ClientId, TeamInfo)>,
|
|
22 |
pub game_info: Option<()>
|
13124
|
23 |
}
|
|
24 |
|
|
25 |
impl HWRoom {
|
|
26 |
pub fn new(id: RoomId) -> HWRoom {
|
|
27 |
HWRoom {
|
|
28 |
id,
|
13421
|
29 |
master_id: None,
|
13124
|
30 |
name: String::new(),
|
|
31 |
password: None,
|
|
32 |
protocol_number: 0,
|
13421
|
33 |
players_number: 0,
|
13424
|
34 |
default_hedgehog_number: 4,
|
|
35 |
team_limit: 8,
|
13124
|
36 |
ready_players_number: 0,
|
13424
|
37 |
teams: Vec::new(),
|
|
38 |
game_info: None
|
13124
|
39 |
}
|
|
40 |
}
|
13421
|
41 |
|
13424
|
42 |
pub fn hedgehogs_number(&self) -> u8 {
|
|
43 |
self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
|
|
44 |
}
|
|
45 |
|
|
46 |
pub fn addable_hedgehogs(&self) -> u8 {
|
|
47 |
MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
|
|
48 |
}
|
|
49 |
|
|
50 |
pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
|
|
51 |
team.color = iter::repeat(()).enumerate()
|
|
52 |
.map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
|
|
53 |
.find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
|
|
54 |
.unwrap_or(0u8);
|
|
55 |
team.hedgehogs_number = if self.teams.is_empty() {
|
|
56 |
self.default_hedgehog_number
|
|
57 |
} else {
|
|
58 |
self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
|
|
59 |
};
|
|
60 |
self.teams.push((owner_id, team));
|
|
61 |
&self.teams.last().unwrap().1
|
|
62 |
}
|
|
63 |
|
|
64 |
pub fn remove_team(&mut self, name: &str) {
|
|
65 |
if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
|
|
66 |
self.teams.remove(index);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
|
|
71 |
where F: Fn(&TeamInfo) -> bool {
|
|
72 |
self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
|
|
73 |
}
|
|
74 |
|
|
75 |
pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
|
|
76 |
where F: Fn(&TeamInfo) -> bool {
|
|
77 |
self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
|
|
78 |
}
|
|
79 |
|
|
80 |
pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
|
|
81 |
self.teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
|
|
82 |
}
|
|
83 |
|
|
84 |
pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
|
|
85 |
self.teams.iter().find(|(_, t)| t.name == team_name)
|
|
86 |
.map(|(id, t)| (*id, &t.name[..]))
|
|
87 |
}
|
|
88 |
|
|
89 |
pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
|
|
90 |
self.client_teams(owner_id).nth(0).map(|t| t.color)
|
|
91 |
}
|
|
92 |
|
13421
|
93 |
pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
|
|
94 |
let flags = "-".to_string();
|
|
95 |
vec![
|
|
96 |
flags,
|
|
97 |
self.name.clone(),
|
|
98 |
self.players_number.to_string(),
|
|
99 |
self.teams.len().to_string(),
|
|
100 |
master.map_or("?", |c| &c.nick).to_string(),
|
13424
|
101 |
"Normal".to_string(),
|
13421
|
102 |
"Default".to_string(),
|
|
103 |
"Default".to_string(),
|
|
104 |
"Default".to_string(),
|
|
105 |
]
|
|
106 |
}
|
13424
|
107 |
|
|
108 |
pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
|
|
109 |
let mut info = vec![
|
|
110 |
team.name.clone(),
|
|
111 |
team.grave.clone(),
|
|
112 |
team.fort.clone(),
|
|
113 |
team.voice_pack.clone(),
|
|
114 |
team.flag.clone(),
|
|
115 |
owner.nick.clone(),
|
|
116 |
team.difficulty.to_string()];
|
|
117 |
let hogs = team.hedgehogs.iter().flat_map(|h|
|
|
118 |
iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
|
|
119 |
info.extend(hogs);
|
|
120 |
info
|
|
121 |
}
|
13124
|
122 |
} |