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