author | alfadur <mail@none> |
Mon, 04 Feb 2019 19:22:21 +0300 | |
changeset 14693 | 6e6632068a33 |
parent 14692 | 455865ccd36c |
child 14694 | 08a8605bafaf |
permissions | -rw-r--r-- |
13416 | 1 |
use super::{ |
14478 | 2 |
actions, |
3 |
actions::{Destination, PendingMessage}, |
|
4 |
client::HWClient, |
|
5 |
coretypes::{ClientId, RoomId}, |
|
6 |
handlers, |
|
14413 | 7 |
io::HWServerIO, |
14478 | 8 |
room::HWRoom, |
13416 | 9 |
}; |
13666 | 10 |
use crate::protocol::messages::*; |
14478 | 11 |
use crate::utils; |
12 |
use base64::encode; |
|
13810 | 13 |
use log::*; |
14478 | 14 |
use rand::{thread_rng, RngCore}; |
15 |
use slab; |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
16 |
use std::borrow::BorrowMut; |
12126 | 17 |
|
12852 | 18 |
type Slab<T> = slab::Slab<T>; |
12127 | 19 |
|
12126 | 20 |
pub struct HWServer { |
12143 | 21 |
pub clients: Slab<HWClient>, |
22 |
pub rooms: Slab<HWRoom>, |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
23 |
pub lobby_id: RoomId, |
13419 | 24 |
pub output: Vec<(Vec<ClientId>, HWServerMessage)>, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
25 |
pub removed_clients: Vec<ClientId>, |
14478 | 26 |
pub io: Box<dyn HWServerIO>, |
12126 | 27 |
} |
28 |
||
29 |
impl HWServer { |
|
14413 | 30 |
pub fn new(clients_limit: usize, rooms_limit: usize, io: Box<dyn HWServerIO>) -> HWServer { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
31 |
let rooms = Slab::with_capacity(rooms_limit); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
32 |
let clients = Slab::with_capacity(clients_limit); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
33 |
let mut server = HWServer { |
14478 | 34 |
clients, |
35 |
rooms, |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
36 |
lobby_id: 0, |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
37 |
output: vec![], |
14413 | 38 |
removed_clients: vec![], |
14478 | 39 |
io, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
40 |
}; |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
41 |
server.lobby_id = server.add_room().id; |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
42 |
server |
12126 | 43 |
} |
44 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
45 |
pub fn add_client(&mut self) -> ClientId { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
46 |
let key: ClientId; |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
47 |
{ |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
48 |
let entry = self.clients.vacant_entry(); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
49 |
key = entry.key(); |
13771 | 50 |
let mut salt = [0u8; 18]; |
51 |
thread_rng().fill_bytes(&mut salt); |
|
52 |
||
53 |
let client = HWClient::new(entry.key(), encode(&salt)); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
54 |
entry.insert(client); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
55 |
} |
14478 | 56 |
self.send( |
57 |
key, |
|
58 |
&Destination::ToSelf, |
|
59 |
HWServerMessage::Connected(utils::PROTOCOL_VERSION), |
|
60 |
); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
61 |
key |
12126 | 62 |
} |
12127 | 63 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
64 |
pub fn client_lost(&mut self, client_id: ClientId) { |
14478 | 65 |
actions::run_action( |
66 |
self, |
|
67 |
client_id, |
|
68 |
actions::Action::ByeClient("Connection reset".to_string()), |
|
69 |
); |
|
12127 | 70 |
} |
71 |
||
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
72 |
pub fn add_room(&mut self) -> &mut HWRoom { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
73 |
allocate_room(&mut self.rooms) |
12127 | 74 |
} |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
75 |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
76 |
#[inline] |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
77 |
pub fn create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
78 |
&mut self, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
79 |
creator_id: ClientId, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
80 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
81 |
password: Option<String>, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
82 |
) -> RoomId { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
83 |
create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
84 |
&mut self.clients[creator_id], |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
85 |
&mut self.rooms, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
86 |
name, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
87 |
password, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
88 |
) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
89 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
90 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
91 |
#[inline] |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
92 |
pub fn move_to_room(&mut self, client_id: ClientId, room_id: RoomId) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
93 |
move_to_room(&mut self.clients[client_id], &mut self.rooms[room_id]) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
94 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
95 |
|
14478 | 96 |
pub fn send( |
97 |
&mut self, |
|
98 |
client_id: ClientId, |
|
99 |
destination: &Destination, |
|
100 |
message: HWServerMessage, |
|
101 |
) { |
|
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
102 |
|
13416 | 103 |
} |
104 |
||
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
105 |
pub fn send_msg(&mut self, client_id: ClientId, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
106 |
self.send(client_id, &message.destination, message.message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
107 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
108 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
109 |
pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) { |
12139 | 110 |
for action in actions { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
111 |
actions::run_action(self, client_id, action); |
12138 | 112 |
} |
113 |
} |
|
13416 | 114 |
|
14478 | 115 |
pub fn lobby(&self) -> &HWRoom { |
116 |
&self.rooms[self.lobby_id] |
|
117 |
} |
|
13492 | 118 |
|
13416 | 119 |
pub fn has_room(&self, name: &str) -> bool { |
120 |
self.rooms.iter().any(|(_, r)| r.name == name) |
|
121 |
} |
|
122 |
||
123 |
pub fn find_room(&self, name: &str) -> Option<&HWRoom> { |
|
14478 | 124 |
self.rooms |
125 |
.iter() |
|
126 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 127 |
} |
128 |
||
129 |
pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HWRoom> { |
|
14478 | 130 |
self.rooms |
131 |
.iter_mut() |
|
132 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 133 |
} |
134 |
||
13450 | 135 |
pub fn find_client(&self, nick: &str) -> Option<&HWClient> { |
14478 | 136 |
self.clients |
137 |
.iter() |
|
138 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 139 |
} |
140 |
||
141 |
pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HWClient> { |
|
14478 | 142 |
self.clients |
143 |
.iter_mut() |
|
144 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 145 |
} |
146 |
||
13416 | 147 |
pub fn select_clients<F>(&self, f: F) -> Vec<ClientId> |
14478 | 148 |
where |
149 |
F: Fn(&(usize, &HWClient)) -> bool, |
|
150 |
{ |
|
151 |
self.clients.iter().filter(f).map(|(_, c)| c.id).collect() |
|
13416 | 152 |
} |
153 |
||
154 |
pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> { |
|
155 |
self.select_clients(|(_, c)| c.room_id == Some(room_id)) |
|
156 |
} |
|
157 |
||
13486 | 158 |
pub fn protocol_clients(&self, protocol: u16) -> Vec<ClientId> { |
13416 | 159 |
self.select_clients(|(_, c)| c.protocol_number == protocol) |
160 |
} |
|
161 |
||
162 |
pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
|
163 |
let room_id = self.clients[self_id].room_id; |
|
14478 | 164 |
self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id) |
13416 | 165 |
} |
166 |
||
167 |
pub fn client_and_room(&mut self, client_id: ClientId) -> (&mut HWClient, Option<&mut HWRoom>) { |
|
168 |
let c = &mut self.clients[client_id]; |
|
169 |
if let Some(room_id) = c.room_id { |
|
170 |
(c, Some(&mut self.rooms[room_id])) |
|
171 |
} else { |
|
172 |
(c, None) |
|
173 |
} |
|
174 |
} |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
175 |
|
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
176 |
pub fn room(&mut self, client_id: ClientId) -> Option<&mut HWRoom> { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
177 |
self.client_and_room(client_id).1 |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
178 |
} |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
179 |
} |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
180 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
181 |
fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
182 |
let entry = rooms.vacant_entry(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
183 |
let key = entry.key(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
184 |
let room = HWRoom::new(entry.key()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
185 |
entry.insert(room) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
186 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
187 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
188 |
fn create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
189 |
client: &mut HWClient, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
190 |
rooms: &mut Slab<HWRoom>, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
191 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
192 |
password: Option<String>, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
193 |
) -> RoomId { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
194 |
let room = allocate_room(rooms); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
195 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
196 |
room.master_id = Some(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
197 |
room.name = name; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
198 |
room.password = password; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
199 |
room.protocol_number = client.protocol_number; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
200 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
201 |
room.players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
202 |
room.ready_players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
203 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
204 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
205 |
client.set_is_master(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
206 |
client.set_is_ready(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
207 |
client.set_is_joined_mid_game(false); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
208 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
209 |
room.id |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
210 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
211 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
212 |
fn move_to_room(client: &mut HWClient, room: &mut HWRoom) { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
213 |
debug_assert!(client.room_id != Some(room.id)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
214 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
215 |
room.players_number += 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
216 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
217 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
218 |
client.set_is_joined_mid_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
219 |
client.set_is_in_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
220 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
221 |
if let Some(ref mut info) = room.game_info { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
222 |
let teams = info.client_teams(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
223 |
client.teams_in_game = teams.clone().count() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
224 |
client.clan = teams.clone().next().map(|t| t.color); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
225 |
let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
226 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
227 |
if !team_names.is_empty() { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
228 |
info.left_teams.retain(|name| !team_names.contains(&name)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
229 |
info.teams_in_game += team_names.len() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
230 |
room.teams = info |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
231 |
.teams_at_start |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
232 |
.iter() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
233 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
234 |
.cloned() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
235 |
.collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
236 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
237 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
238 |
} |