author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 02 Jun 2019 17:19:07 +0200 | |
changeset 15095 | e13b9054316b |
parent 15075 | e935b1ad23f3 |
child 15102 | 80ff12edf5e6 |
permissions | -rw-r--r-- |
12149 | 1 |
use mio; |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
2 |
use std::{collections::HashMap, io, io::Write}; |
12149 | 3 |
|
15074 | 4 |
use self::{ |
5 |
actions::{Destination, DestinationGroup, PendingMessage}, |
|
15075 | 6 |
inanteroom::LoginResult, |
14694 | 7 |
}; |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
8 |
use crate::{ |
15074 | 9 |
core::{ |
15075 | 10 |
room::RoomSave, |
11 |
server::HwServer, |
|
12 |
types::{ClientId, GameCfg, Replay, RoomId, TeamInfo}, |
|
15074 | 13 |
}, |
14 |
protocol::messages::{ |
|
15075 | 15 |
global_chat, server_chat, HwProtocolMessage, HwProtocolMessage::EngineMessage, |
16 |
HwServerMessage, HwServerMessage::*, |
|
15074 | 17 |
}, |
14693 | 18 |
utils, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
19 |
}; |
14693 | 20 |
use base64::encode; |
13805 | 21 |
use log::*; |
14693 | 22 |
use rand::{thread_rng, RngCore}; |
13666 | 23 |
|
15074 | 24 |
mod actions; |
14457 | 25 |
mod checker; |
26 |
mod common; |
|
15075 | 27 |
mod inanteroom; |
15074 | 28 |
mod inlobby; |
15075 | 29 |
mod inroom; |
12149 | 30 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
31 |
use std::fmt::{Formatter, LowerHex}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
32 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
33 |
#[derive(PartialEq)] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
34 |
pub struct Sha1Digest([u8; 20]); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
35 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
36 |
impl Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
37 |
pub fn new(digest: [u8; 20]) -> Self { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
38 |
Self(digest) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
39 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
40 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
41 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
42 |
impl LowerHex for Sha1Digest { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
43 |
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
44 |
for byte in &self.0 { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
45 |
write!(f, "{:02x}", byte)?; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
46 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
47 |
Ok(()) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
48 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
49 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
50 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
51 |
pub struct AccountInfo { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
52 |
pub is_registered: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
53 |
pub is_admin: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
54 |
pub is_contributor: bool, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
55 |
pub server_hash: Sha1Digest, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
56 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
57 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
58 |
pub enum IoTask { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
59 |
GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
60 |
nick: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
61 |
protocol: u16, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
62 |
password_hash: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
63 |
client_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
64 |
server_salt: String, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
65 |
}, |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
66 |
GetReplay { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
67 |
id: u32, |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
68 |
}, |
14780 | 69 |
SaveRoom { |
70 |
room_id: RoomId, |
|
71 |
filename: String, |
|
72 |
contents: String, |
|
73 |
}, |
|
74 |
LoadRoom { |
|
75 |
room_id: RoomId, |
|
14781 | 76 |
filename: String, |
77 |
}, |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
78 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
79 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
80 |
pub enum IoResult { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
81 |
Account(Option<AccountInfo>), |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
82 |
Replay(Option<Replay>), |
14780 | 83 |
SaveRoom(RoomId, bool), |
14781 | 84 |
LoadRoom(RoomId, Option<String>), |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
85 |
} |
14693 | 86 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
87 |
pub struct Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
88 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
89 |
messages: Vec<PendingMessage>, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
90 |
io_tasks: Vec<IoTask>, |
14696 | 91 |
removed_clients: Vec<ClientId>, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
92 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
93 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
94 |
impl Response { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
95 |
pub fn new(client_id: ClientId) -> Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
96 |
Self { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
97 |
client_id, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
98 |
messages: vec![], |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
99 |
io_tasks: vec![], |
14696 | 100 |
removed_clients: vec![], |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
101 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
102 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
103 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
104 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
105 |
pub fn is_empty(&self) -> bool { |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
106 |
self.messages.is_empty() && self.removed_clients.is_empty() && self.io_tasks.is_empty() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
107 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
108 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
109 |
#[inline] |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
110 |
pub fn len(&self) -> usize { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
111 |
self.messages.len() |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
112 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
113 |
|
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
114 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
115 |
pub fn client_id(&self) -> ClientId { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
116 |
self.client_id |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
117 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
118 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
119 |
#[inline] |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
120 |
pub fn add(&mut self, message: PendingMessage) { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
121 |
self.messages.push(message) |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
122 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
123 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
124 |
#[inline] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
125 |
pub fn request_io(&mut self, task: IoTask) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
126 |
self.io_tasks.push(task) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
127 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
128 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
129 |
pub fn extract_messages<'a, 'b: 'a>( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
130 |
&'b mut self, |
15075 | 131 |
server: &'a HwServer, |
132 |
) -> impl Iterator<Item = (Vec<ClientId>, HwServerMessage)> + 'a { |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
133 |
let client_id = self.client_id; |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
134 |
self.messages.drain(..).map(move |m| { |
14788 | 135 |
let ids = get_recipients(server, client_id, m.destination); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
136 |
(ids, m.message) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
137 |
}) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
138 |
} |
14696 | 139 |
|
140 |
pub fn remove_client(&mut self, client_id: ClientId) { |
|
141 |
self.removed_clients.push(client_id); |
|
142 |
} |
|
143 |
||
144 |
pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ { |
|
145 |
self.removed_clients.drain(..) |
|
146 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
147 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
148 |
pub fn extract_io_tasks(&mut self) -> impl Iterator<Item = IoTask> + '_ { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
149 |
self.io_tasks.drain(..) |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
150 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
151 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
152 |
|
14687
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
153 |
impl Extend<PendingMessage> for Response { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
154 |
fn extend<T: IntoIterator<Item = PendingMessage>>(&mut self, iter: T) { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
155 |
for msg in iter { |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
156 |
self.add(msg) |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
157 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
158 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
159 |
} |
5122c584804e
Server action refactoring part A of N
alfadur <mail@none>
parents:
14673
diff
changeset
|
160 |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
161 |
fn get_recipients( |
15075 | 162 |
server: &HwServer, |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
163 |
client_id: ClientId, |
14788 | 164 |
destination: Destination, |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
165 |
) -> Vec<ClientId> { |
14788 | 166 |
match destination { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
167 |
Destination::ToSelf => vec![client_id], |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
168 |
Destination::ToId(id) => vec![id], |
14788 | 169 |
Destination::ToIds(ids) => ids, |
170 |
Destination::ToAll { group, skip_self } => { |
|
14789 | 171 |
let mut ids: Vec<_> = match group { |
172 |
DestinationGroup::All => server.all_clients().collect(), |
|
173 |
DestinationGroup::Lobby => server.lobby_clients().collect(), |
|
174 |
DestinationGroup::Protocol(proto) => server.protocol_clients(proto).collect(), |
|
175 |
DestinationGroup::Room(id) => server.room_clients(id).collect(), |
|
14788 | 176 |
}; |
177 |
||
178 |
if skip_self { |
|
179 |
if let Some(index) = ids.iter().position(|id| *id == client_id) { |
|
180 |
ids.remove(index); |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
ids |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
185 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
186 |
} |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
187 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
188 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
189 |
pub fn handle( |
15075 | 190 |
server: &mut HwServer, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
191 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
192 |
response: &mut Response, |
15075 | 193 |
message: HwProtocolMessage, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
194 |
) { |
12149 | 195 |
match message { |
15075 | 196 |
HwProtocolMessage::Ping => response.add(Pong.send_self()), |
14693 | 197 |
_ => { |
198 |
if server.anteroom.clients.contains(client_id) { |
|
15074 | 199 |
match inanteroom::handle(server, client_id, response, message) { |
14693 | 200 |
LoginResult::Unchanged => (), |
201 |
LoginResult::Complete => { |
|
202 |
if let Some(client) = server.anteroom.remove_client(client_id) { |
|
203 |
server.add_client(client_id, client); |
|
14781 | 204 |
common::join_lobby(server, response); |
14693 | 205 |
} |
206 |
} |
|
207 |
LoginResult::Exit => { |
|
208 |
server.anteroom.remove_client(client_id); |
|
14696 | 209 |
response.remove_client(client_id); |
14693 | 210 |
} |
211 |
} |
|
14796 | 212 |
} else if server.clients.contains(client_id) { |
14693 | 213 |
match message { |
15075 | 214 |
HwProtocolMessage::Quit(Some(msg)) => { |
14693 | 215 |
common::remove_client(server, response, "User quit: ".to_string() + &msg); |
216 |
} |
|
15075 | 217 |
HwProtocolMessage::Quit(None) => { |
14693 | 218 |
common::remove_client(server, response, "User quit".to_string()); |
219 |
} |
|
15075 | 220 |
HwProtocolMessage::Info(nick) => { |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
221 |
if let Some(client) = server.find_client(&nick) { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
222 |
let admin_sign = if client.is_admin() { "@" } else { "" }; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
223 |
let master_sign = if client.is_master() { "+" } else { "" }; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
224 |
let room_info = match client.room_id { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
225 |
Some(room_id) => { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
226 |
let room = &server.rooms[room_id]; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
227 |
let status = match room.game_info { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
228 |
Some(_) if client.teams_in_game == 0 => "(spectating)", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
229 |
Some(_) => "(playing)", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
230 |
None => "", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
231 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
232 |
format!( |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
233 |
"[{}{}room {}]{}", |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
234 |
admin_sign, master_sign, room.name, status |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
235 |
) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
236 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
237 |
None => format!("[{}lobby]", admin_sign), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
238 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
239 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
240 |
let info = vec![ |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
241 |
client.nick.clone(), |
14802 | 242 |
"[]".to_string(), |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
243 |
utils::protocol_version_string(client.protocol_number).to_string(), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
244 |
room_info, |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
245 |
]; |
14802 | 246 |
response.add(Info(info).send_self()) |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
247 |
} else { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
248 |
response |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
249 |
.add(server_chat("Player is not online.".to_string()).send_self()) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
250 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
251 |
} |
15075 | 252 |
HwProtocolMessage::ToggleServerRegisteredOnly => { |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
253 |
if !server.clients[client_id].is_admin() { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
254 |
response.add(Warning("Access denied.".to_string()).send_self()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
255 |
} else { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
256 |
server.set_is_registered_only(server.is_registered_only()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
257 |
let msg = if server.is_registered_only() { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
258 |
"This server no longer allows unregistered players to join." |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
259 |
} else { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
260 |
"This server now allows unregistered players to join." |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
261 |
}; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
262 |
response.add(server_chat(msg.to_string()).send_all()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
263 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
264 |
} |
15075 | 265 |
HwProtocolMessage::Global(msg) => { |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
266 |
if !server.clients[client_id].is_admin() { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
267 |
response.add(Warning("Access denied.".to_string()).send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
268 |
} else { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
269 |
response.add(global_chat(msg).send_all()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
270 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
271 |
} |
15075 | 272 |
HwProtocolMessage::SuperPower => { |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
273 |
if !server.clients[client_id].is_admin() { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
274 |
response.add(Warning("Access denied.".to_string()).send_self()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
275 |
} else { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
276 |
server.clients[client_id].set_has_super_power(true); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
277 |
response |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
278 |
.add(server_chat("Super power activated.".to_string()).send_self()) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
279 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
280 |
} |
15075 | 281 |
HwProtocolMessage::Watch(id) => { |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
282 |
#[cfg(feature = "official-server")] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
283 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
284 |
response.request_io(IoTask::GetReplay { id }) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
285 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
286 |
|
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
287 |
#[cfg(not(feature = "official-server"))] |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
288 |
{ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
289 |
response.add( |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
290 |
Warning("This server does not support replays!".to_string()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
291 |
.send_self(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
292 |
); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
293 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
294 |
} |
14693 | 295 |
_ => match server.clients[client_id].room_id { |
15074 | 296 |
None => inlobby::handle(server, client_id, response, message), |
14693 | 297 |
Some(room_id) => { |
298 |
inroom::handle(server, client_id, response, room_id, message) |
|
299 |
} |
|
300 |
}, |
|
301 |
} |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
302 |
} |
14693 | 303 |
} |
12149 | 304 |
} |
305 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
306 |
|
15075 | 307 |
pub fn handle_client_accept(server: &mut HwServer, client_id: ClientId, response: &mut Response) { |
14693 | 308 |
let mut salt = [0u8; 18]; |
309 |
thread_rng().fill_bytes(&mut salt); |
|
310 |
||
311 |
server.anteroom.add_client(client_id, encode(&salt)); |
|
312 |
||
15075 | 313 |
response.add(HwServerMessage::Connected(utils::SERVER_VERSION).send_self()); |
14693 | 314 |
} |
315 |
||
15075 | 316 |
pub fn handle_client_loss(server: &mut HwServer, client_id: ClientId, response: &mut Response) { |
14696 | 317 |
if server.anteroom.remove_client(client_id).is_none() { |
318 |
common::remove_client(server, response, "Connection reset".to_string()); |
|
319 |
} |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
320 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
321 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
322 |
pub fn handle_io_result( |
15075 | 323 |
server: &mut HwServer, |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
324 |
client_id: ClientId, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
325 |
response: &mut Response, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
326 |
io_result: IoResult, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
327 |
) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
328 |
match io_result { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
329 |
IoResult::Account(Some(info)) => { |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
330 |
if !info.is_registered && server.is_registered_only() { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
331 |
response.add( |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
332 |
Bye("This server only allows registered users to join.".to_string()) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
333 |
.send_self(), |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
334 |
); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
335 |
response.remove_client(client_id); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
336 |
} else { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
337 |
response.add(ServerAuth(format!("{:x}", info.server_hash)).send_self()); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
338 |
if let Some(client) = server.anteroom.remove_client(client_id) { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
339 |
server.add_client(client_id, client); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
340 |
let client = &mut server.clients[client_id]; |
14791 | 341 |
client.set_is_registered(info.is_registered); |
14786
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
342 |
client.set_is_admin(info.is_admin); |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
343 |
client.set_is_contributor(info.is_admin) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14785
diff
changeset
|
344 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
345 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
346 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
347 |
IoResult::Account(None) => { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
348 |
response.add(Error("Authentication failed.".to_string()).send_self()); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
349 |
response.remove_client(client_id); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
350 |
} |
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
351 |
IoResult::Replay(Some(replay)) => { |
14907 | 352 |
let protocol = server.clients[client_id].protocol_number; |
353 |
let start_msg = if protocol < 58 { |
|
354 |
RoomJoined(vec![server.clients[client_id].nick.clone()]) |
|
355 |
} else { |
|
356 |
ReplayStart |
|
357 |
}; |
|
358 |
response.add(start_msg.send_self()); |
|
359 |
||
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
360 |
common::get_room_config_impl(&replay.config, client_id, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
361 |
common::get_teams(replay.teams.iter(), client_id, response); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
362 |
response.add(RunGame.send_self()); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
363 |
response.add(ForwardEngineMessage(replay.message_log).send_self()); |
14907 | 364 |
|
365 |
if protocol < 58 { |
|
366 |
response.add(Kicked.send_self()); |
|
367 |
} |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
368 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
369 |
IoResult::Replay(None) => { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
370 |
response.add(Warning("Could't load the replay".to_string()).send_self()) |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14784
diff
changeset
|
371 |
} |
14780 | 372 |
IoResult::SaveRoom(_, true) => { |
373 |
response.add(server_chat("Room configs saved successfully.".to_string()).send_self()); |
|
374 |
} |
|
375 |
IoResult::SaveRoom(_, false) => { |
|
14781 | 376 |
response.add(Warning("Unable to save the room configs.".to_string()).send_self()); |
14780 | 377 |
} |
378 |
IoResult::LoadRoom(room_id, Some(contents)) => { |
|
379 |
if let Some(ref mut room) = server.rooms.get_mut(room_id) { |
|
380 |
match room.set_saves(&contents) { |
|
381 |
Ok(_) => response.add( |
|
14781 | 382 |
server_chat("Room configs loaded successfully.".to_string()).send_self(), |
14780 | 383 |
), |
384 |
Err(e) => { |
|
385 |
warn!("Error while deserializing the room configs: {}", e); |
|
386 |
response.add( |
|
387 |
Warning("Unable to deserialize the room configs.".to_string()) |
|
388 |
.send_self(), |
|
389 |
); |
|
390 |
} |
|
391 |
} |
|
392 |
} |
|
393 |
} |
|
14781 | 394 |
IoResult::LoadRoom(_, None) => { |
395 |
response.add(Warning("Unable to load the room configs.".to_string()).send_self()); |
|
14780 | 396 |
} |
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
397 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
398 |
} |