-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.cpp
201 lines (168 loc) · 4.53 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "server.hpp"
#include <cmath>
/* Client class functions */
Client::Client(const uint32_t id, uWS::WebSocket<uWS::SERVER> ws, World * const wrld, SocketInfo * si)
: nick(),
pixupdlimit(0, 1),
chatlimit(CLIENT_CHAT_RATELIMIT),
ws(ws),
wrld(wrld),
penalty(0),
handledelete(true),
rank(1),
stealthadmin(false),
suspicious(si->origin != "https://owoppa.netlify.com"),
compressionEnabled(false),
pos({0, 0, 0, 0, 0, 0}),
lastclr({0, 0, 0}),
id(id),
si(si),
mute(false),
chathtml(false){
std::cout << "(" << wrld->name << "/" << si->ip << ") New client! ID: " << id << std::endl;
uv_timer_init(uv_default_loop(), &idletimeout_hdl);
idletimeout_hdl.data = this;
//uv_timer_start(&idletimeout_hdl, (uv_timer_cb) &Client::idle_timeout, 300000, 1200000);
uint8_t msg[5] = {SET_ID};
memcpy(&msg[1], (char *)&id, sizeof(id));
ws.send((const char *)&msg, sizeof(msg), uWS::BINARY);
}
Client::~Client() {
/* std::cout << "Client deleted! ID: " << id << std::endl; */
}
bool Client::can_edit() {
return pixupdlimit.can_spend();
}
void Client::get_chunk(const int32_t x, const int32_t y) const {
wrld->send_chunk(ws, x, y);
}
void Client::put_px(const int32_t x, const int32_t y, const RGB clr) {
if(is_admin() || can_edit()){
uint32_t distx = (x >> 4) - (pos.x >> 8); distx *= distx;
uint32_t disty = (y >> 4) - (pos.y >> 8); disty *= disty;
const uint32_t dist = sqrt(distx + disty);
const uint32_t clrdist = ColourDistance(lastclr, clr);
lastclr = clr;
wrld->put_px(x, y, clr, rank);
updated();
}
}
void Client::teleport(const int32_t x, const int32_t y) {
uint8_t msg[9] = {TELEPORT};
memcpy(&msg[1], (char *)&x, sizeof(x));
memcpy(&msg[5], (char *)&y, sizeof(y));
ws.send((const char *)&msg, sizeof(msg), uWS::BINARY);
pos.x = (x << 4) + 8;
pos.y = (y << 4) + 8;
wrld->upd_cli(this);
}
void Client::move(const pinfo_t& newpos) {
pos = newpos;
wrld->upd_cli(this);
updated();
}
const pinfo_t * Client::get_pos() { /* Hmmm... */
return &pos;
}
bool Client::can_chat() {
return is_admin() || chatlimit.can_spend();
}
void Client::chat(const std::string& msg) {
if (!mute) {
if(chathtml) {
wrld->broadcast(get_nick() + ": " + msg);
} else {
wrld->broadcast(get_nick() + ": " + msg);
}
}
}
void Client::tell(const std::string& msg) {
ws.send(msg.c_str(), msg.size(), uWS::TEXT);
}
void Client::updated() {
uv_timer_again(&idletimeout_hdl);
}
void Client::idle_timeout(uv_timer_t * const t) {
//Client * const cl = (Client *)t->data;
/* Maybe there should be a proper kick function */
//cl->tell("Server: Kicked for inactivity.");
//cl->safedelete(true);
}
void Client::safedelete(const bool close) {
if(handledelete){
handledelete = false;
wrld->rm_cli(this);
uv_timer_stop(&idletimeout_hdl);
uv_close((uv_handle_t *)&idletimeout_hdl, (uv_close_cb)([](uv_handle_t * const t){
delete (Client *)t->data;
}));
if(close){
ws.close();
}
}
}
void Client::promote(uint8_t newrank, uint16_t prate) {
rank = newrank;
if (rank == ADMIN) {
tell("Server: You are now an admin. Do /help for a list of commands.");
} else if (rank == MODERATOR) {
tell("Server: You are now a moderator.");
set_pbucket(prate, 2);
} else if (rank == USER) {
set_pbucket(prate, 4);
} else {
set_pbucket(0, 1);
}
uint8_t msg[2] = {PERMISSIONS, rank};
ws.send((const char *)&msg, sizeof(msg), uWS::BINARY);
}
void Client::enableHtmlChat() {
chathtml = true;
//tell("Server: Promoted to HTML chat");
}
bool Client::warn() {
if(!is_admin() && ++penalty > CLIENT_MAX_WARN_LEVEL){
safedelete(true);
return true;
}
return false;
}
bool Client::is_mod() const {
return rank == MODERATOR;
}
bool Client::is_admin() const {
return rank == ADMIN;
}
uWS::WebSocket<uWS::SERVER> Client::get_ws() const {
return ws;
}
std::string Client::get_nick() const {
if (nick.size()) {
return nick;
}
std::string e(is_mod() && !stealthadmin ? "" : is_admin() && !stealthadmin ? "" : "");
e += std::to_string(id);
return e;
}
World * Client::get_world() const {
return wrld;
}
uint16_t Client::get_penalty() const {
return penalty;
}
uint8_t Client::get_rank() const {
return rank;
}
void Client::set_stealth(bool new_state) {
stealthadmin = new_state;
}
void Client::set_nick(const std::string & name) {
nick = name;
}
void Client::set_pbucket(uint16_t rate, uint16_t per) {
pixupdlimit.set(rate, per);
uint8_t msg[5] = {SET_PQUOTA};
memcpy(&msg[1], (char *)&rate, sizeof(rate));
memcpy(&msg[3], (char *)&per, sizeof(per));
ws.send((const char *)&msg, sizeof(msg), uWS::BINARY);
}