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