-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuServer.cpp
More file actions
249 lines (213 loc) · 6.86 KB
/
MenuServer.cpp
File metadata and controls
249 lines (213 loc) · 6.86 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "MenuServer.hpp"
void MenuServer::stopListening(websocketpp::connection_hdl hdl){
server.stop_listening();
}
void MenuServer::closeMenu(websocketpp::connection_hdl){
if (engine.isOverlayActive()) {
Helpers::toggleSteamOverlay();
}
}
void MenuServer::getAllSettings(websocketpp::connection_hdl hdl){
sendMessage(hdl, WEBSOCKET_GET_ALL_SETTINGS, Settings::toJsonxxObject());
}
void MenuServer::getConfigsList(websocketpp::connection_hdl hdl){
jsonxx::Object configsList;
configsList << "files" << Settings::getFilesListAsJsonxxArray();
sendMessage(hdl, WEBSOCKET_GET_CONFIGS_LIST, configsList);
}
void MenuServer::getActiveWeapon(websocketpp::connection_hdl hdl){
if (
engine.clientState->state() != INGAME ||
client.localPlayer->m_iHealth() <= 0 ||
client.localPlayer->m_iTeamNum() < TERRORIST
) {
return;
}
int activeWeaponID = client.localPlayer->m_hActiveWeapon() & 0xfff;
BaseCombatWeapon activeWeapon = BaseCombatWeapon(client.entityList->getByID(activeWeaponID - 1));
auto activeWeaponIDI = (int)activeWeapon.m_iItemDefinitionIndex();
if (activeWeaponIDI == INVALID_ITEM_DI) {
return;
}
getActiveWeapon(activeWeaponIDI);
}
void MenuServer::getActiveWeapon(int activeWeapon){
jsonxx::Object o;
o << "activeWeapon" << activeWeapon;
sendMessageAll(WEBSOCKET_GET_ACTIVE_WEAPON, o);
}
void MenuServer::getTeam(websocketpp::connection_hdl hdl) {
if (
engine.clientState->state() != INGAME ||
client.localPlayer->m_iHealth() <= 0 ||
client.localPlayer->m_iTeamNum() < TERRORIST
) {
return;
}
getTeam(client.localPlayer->m_iTeamNum());
}
void MenuServer::getTeam(TeamNum team){
jsonxx::Object o;
o << "team" << (int)team;
sendMessageAll(WEBSOCKET_GET_TEAM, o);
}
void MenuServer::updateSettings(websocketpp::connection_hdl hdl, jsonxx::Object message){
for (auto prop : message.kv_map()) {
Settings::setValue(prop.first, prop.second);
}
}
void MenuServer::loadConfig(websocketpp::connection_hdl hdl, jsonxx::Object message){
auto fileName = message.get<jsonxx::String>("fileName");
Settings::getFromFile(fileName);
}
void MenuServer::saveConfig(websocketpp::connection_hdl hdl, jsonxx::Object message){
auto fileName = message.get<jsonxx::String>("fileName");
Settings::saveToFile(fileName + ".json");
}
void MenuServer::deleteConfig(websocketpp::connection_hdl hdl, jsonxx::Object message){
auto fileName = message.get<jsonxx::String>("fileName");
Settings::deleteFile(fileName);
}
void MenuServer::gameFullForceUpdate(websocketpp::connection_hdl){
Helpers::fullForceUpdate();
}
void MenuServer::getAllSkins(websocketpp::connection_hdl hdl){
std::fstream skinsJson("skins.json");
std::string skinsBuffer((std::istreambuf_iterator<char>(skinsJson)), std::istreambuf_iterator<char>());
jsonxx::Object skins;
skins.parse(skinsBuffer);
sendMessage(hdl, WEBSOCKET_GET_ALL_SKINS, skins);
}
void MenuServer::onMessage(websocketpp::connection_hdl hdl, Server::message_ptr msg) {
try {
jsonxx::Object data;
data.parse(msg->get_payload());
if (data.has<jsonxx::String>("type")) {
auto type = data.get<jsonxx::String>("type");
if (type == WEBSOCKET_STOP_LISTENING) {
stopListening(hdl);
return;
}
else if (type == WEBSOCKET_GET_CONFIGS_LIST) {
getConfigsList(hdl);
return;
}
else if (type == WEBSOCKET_CLOSE_MENU) {
closeMenu(hdl);
return;
}
else if (type == WEBSOCKET_GET_ACTIVE_WEAPON) {
getActiveWeapon(hdl);
return;
}
else if (type == WEBSOCKET_GAME_FULL_FORCE_UPDATE) {
gameFullForceUpdate(hdl);
return;
}
else if (type == WEBSOCKET_GET_ALL_SKINS) {
getAllSkins(hdl);
return;
}
if (data.has<jsonxx::Object>("message")) {
auto message = data.get<jsonxx::Object>("message");
if (type == WEBSOCKET_UPDATE_SETTINGS) {
updateSettings(hdl, message);
return;
}
else if (type == WEBSOCKET_LOAD_CONFIG && message.has<jsonxx::String>("fileName")) {
loadConfig(hdl, message);
getAllSettings(hdl);
return;
}
else if (type == WEBSOCKET_SAVE_CONFIG && message.has<jsonxx::String>("fileName")) {
saveConfig(hdl, message);
getConfigsList(hdl);
return;
}
else if (type == WEBSOCKET_DELETE_CONFIG && message.has<jsonxx::String>("fileName")) {
deleteConfig(hdl, message);
getConfigsList(hdl);
return;
}
}
}
}
catch (const std::exception& e) {
log("onMessage error: " + (std::string)e.what());
}
}
void MenuServer::onOpen(websocketpp::connection_hdl hdl) {
try {
connections.insert(hdl);
getAllSettings(hdl);
getAllSkins(hdl);
getActiveWeapon(hdl);
getTeam(hdl);
}
catch (const std::exception& e) {
log("onOpen error: " + (std::string)e.what());
}
}
void MenuServer::onClose(websocketpp::connection_hdl hdl) {
try {
connections.erase(hdl);
}
catch (const std::exception& e) {
log("onClose error: " + (std::string)e.what());
}
}
void MenuServer::log(const std::string text) {
std::cout << "[Websocket server]: " + text << std::endl;
}
MenuServer::MenuServer(){
try {
// Set logging settings
server.set_access_channels(websocketpp::log::alevel::none);
server.set_error_channels(websocketpp::log::alevel::none);
server.init_asio();
server.set_open_handler(bind(&MenuServer::onOpen, this, ::_1));
server.set_message_handler(bind(&MenuServer::onMessage, this, ::_1, ::_2));
server.set_close_handler(bind(&MenuServer::onClose, this, ::_1));
}
catch (const std::exception& e) {
log("constructor error: " + (std::string)e.what());
}
}
void MenuServer::start() {
try {
std::thread thSocketServer([this]() {
server.listen(WEBSOCKET_PORT);
server.start_accept();
server.run();
});
std::thread thHttpSrver([]() {
std::string command = "node ./menu/server.js port=" + std::to_string(HTTP_SERVER_PORT) + " dir=\"" + HTTP_SERVER_DIRECTORY+"\"";
system(command.c_str());
});
thSocketServer.join();
thHttpSrver.join();
}
catch (const std::exception& e) {
log("start error: " + (std::string)e.what());
}
}
void MenuServer::sendMessage(websocketpp::connection_hdl hdl, std::string type, jsonxx::Value message){
try {
jsonxx::Object req;
req << "type" << type << "message" << message;
server.send(hdl, req.json(), websocketpp::frame::opcode::text);
}
catch (websocketpp::exception const& e) {
log("sendMessage error: " + (std::string)e.what());
}
}
void MenuServer::sendMessageAll(std::string type, jsonxx::Object message){
try {
for (auto hdl : connections) {
sendMessage(hdl, type, message);
}
}
catch (websocketpp::exception const& e) {
log("sendMessageAll error: " + (std::string)e.what());
}
}