1 #include <atomizes.hpp>
11 #include <gpcm/client.h>
12 #include <battlefield/gamestat.h>
14 #include <webserver/client.h>
17 const Util::Url::Variables& url_variables)
20 auto it = url_variables.find(
"password");
21 if (it == url_variables.end() || it->second != g_settings[
"webserver"][
"password"].asString())
26 Json::Value json_results;
29 Json::Value json_browsing(Json::arrayValue);
30 for(std::shared_ptr<Net::Socket> client : g_browsing_server->
GetClients())
32 Json::Value json_client;
34 json_client[
"ip"] = client.get()->GetIP();
35 json_client[
"port"] = client.get()->GetPort();
37 time_t last_recieved_time = std::chrono::system_clock::to_time_t(client.get()->GetLastRecievedTime());
38 json_client[
"last_recieved_time"] = std::string(std::ctime(&last_recieved_time));
43 json_browsing.append(json_client);
45 json_results[
"browsing"] = json_browsing;
48 Json::Value json_gamestats(Json::arrayValue);
49 for(std::shared_ptr<Net::Socket> client : g_gamestats_server->
GetClients())
51 Json::Value json_client;
53 json_client[
"ip"] = client.get()->GetIP();
54 json_client[
"port"] = client.get()->GetPort();
56 time_t last_recieved_time = std::chrono::system_clock::to_time_t(client.get()->GetLastRecievedTime());
57 json_client[
"last_recieved_time"] = std::string(std::ctime(&last_recieved_time));
62 json_gamestats.append(json_client);
64 json_results[
"gamestats"] = json_gamestats;
67 Json::Value json_gpcm(Json::arrayValue);
68 for(std::shared_ptr<Net::Socket> client : g_gpcm_server->
GetClients())
70 Json::Value json_client;
71 Json::Value json_session;
73 std::shared_ptr<GPCM::Client> gpcm_client = std::dynamic_pointer_cast<GPCM::Client>(client);
79 player.SetProfileId(session.profileid);
82 json_client[
"ip"] = client.get()->GetIP();
83 json_client[
"port"] = client.get()->GetPort();
85 time_t last_recieved_time = std::chrono::system_clock::to_time_t(client.get()->GetLastRecievedTime());
86 json_client[
"last_recieved_time"] = std::string(std::ctime(&last_recieved_time));
88 json_session[
"authtoken"] = session.authtoken;
89 json_session[
"challenge"] = session.challenge;
90 json_session[
"profileid"] = session.profileid;
91 json_session[
"uniquenick"] = player.GetUniquenick();
92 json_session[
"status"] = session.status;
94 json_client[
"session"] = json_session;
99 json_gpcm.append(json_client);
101 json_results[
"gpcm"] = json_gpcm;
104 Json::Value json_gpsp(Json::arrayValue);
105 for(std::shared_ptr<Net::Socket> client : g_gpsp_server->
GetClients())
107 Json::Value json_client;
109 json_client[
"ip"] = client.get()->GetIP();
110 json_client[
"port"] = client.get()->GetPort();
112 time_t last_recieved_time = std::chrono::system_clock::to_time_t(client.get()->GetLastRecievedTime());
113 json_client[
"last_recieved_time"] = std::string(std::ctime(&last_recieved_time));
118 json_gpsp.append(json_client);
120 json_results[
"gpsp"] = json_gpsp;
123 Json::Value json_webserver(Json::arrayValue);
124 for(std::shared_ptr<Net::Socket> client : g_webserver_server->
GetClients())
126 Json::Value json_client;
128 json_client[
"ip"] = client.get()->GetIP();
129 json_client[
"port"] = client.get()->GetPort();
131 time_t last_recieved_time = std::chrono::system_clock::to_time_t(client.get()->GetLastRecievedTime());
132 json_client[
"last_recieved_time"] = std::string(std::ctime(&last_recieved_time));
137 json_webserver.append(json_client);
139 json_results[
"webserver"] = json_webserver;
141 this->
Send(json_results);
147 const Util::Url::Variables& url_variables)
149 Json::Value json_results;
151 json_results[
"result"] =
"FAIL";
154 auto it = url_variables.find(
"password");
155 if (it == url_variables.end() || it->second != g_settings[
"webserver"][
"password"].asString())
163 it = url_variables.find(
"profileid");
164 if (it == url_variables.end() || !player.SetProfileId(it->second))
169 for(std::shared_ptr<Net::Socket> client : g_gpcm_server->
GetClients())
171 std::shared_ptr<GPCM::Client> gpcm_client = std::dynamic_pointer_cast<GPCM::Client>(client);
176 if(session.profileid == player.GetProfileId())
178 gpcm_client.get()->Disconnect();
179 json_results[
"result"] =
"OK";
183 this->Send(json_results);
185 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
189 const Util::Url::Variables& url_variables)
192 auto it = url_variables.find(
"password");
193 if (it == url_variables.end() || it->second != g_settings[
"webserver"][
"password"].asString())
199 it = url_variables.find(
"message");
200 if (it == url_variables.end() || !Util::UTF8::isValid(it->second))
204 std::string message = it->second;
208 it = url_variables.find(
"profileid");
209 if (it == url_variables.end())
211 player.SetProfileId(it->second);
214 Json::Value json_results;
216 for(std::shared_ptr<Net::Socket> client : g_gpcm_server->
GetClients())
218 std::shared_ptr<GPCM::Client> gpcm_client = std::dynamic_pointer_cast<GPCM::Client>(client);
223 if(session.profileid == player.GetProfileId() || player.GetProfileId() == -1)
232 json_results[
"result"] =
"OK";
236 this->Send(json_results);
238 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
242 const Util::Url::Variables& url_variables)
245 auto it = url_variables.find(
"password");
246 if (it == url_variables.end() || it->second != g_settings[
"webserver"][
"password"].asString())
254 it = url_variables.find(
"profileid");
255 if (it == url_variables.end() || !player.SetProfileId(it->second) && player.GetProfileId() > 0)
260 Json::Value json_results;
261 Json::Value json_gsplayers(Json::arrayValue);
266 json_results[
"total"] = total;
268 for(uint32_t offset = 0; offset < total; offset += 250)
270 Battlefield::GameStatPlayers gsplayers;
273 std::this_thread::sleep_for(std::chrono::milliseconds(50));
280 if(gsplayer.IsDisabled())
287 Json::Value json_gsplayer;
289 json_gsplayer[
"id"] = gsplayer.GetId();
290 json_gsplayer[
"medals"] = player.GetMedals();
291 json_gsplayer[
"score"] = player.GetScore();
292 json_gsplayer[
"pph"] = player.GetPPH();
293 json_gsplayer[
"rank"] = player.GetRank();
295 json_gsplayers.append(json_gsplayer);
299 json_results[
"gsplayers"] = json_gsplayers;
304 this->Send(json_results);
306 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
Represents a player's statistics in a game.
void Update(const Battlefield::GameStatPlayer &gsplayer)
Update player stats based on played game.
Represents a player with extended statistics.
bool queryGameStatPlayersByProfileId(const Battlefield::Player &player, Battlefield::GameStatPlayers &gsplayers, uint32_t limit=250, uint32_t offset=0)
Queries game statistics for a player by their profile ID.
bool queryPlayerByProfileId(Battlefield::Player &player)
Queries a player by their profile ID.
bool updatePlayerStats(const Battlefield::Player &player)
Updates the statistics of a player in the database.
bool countGameStatPlayersByProfileId(const Battlefield::Player &player, uint32_t &output_total)
Counts the number of game statistic players for a player by their profile ID.
static void SendBuddyMessage(int profileid, int target_profileid, const std::string &bm, const std::string &message)
Sends a buddy message from one profile ID to another.
std::vector< std::shared_ptr< Net::Socket > > GetClients()
Get the vector of client sockets connected to this server.
void requestAPIAdminKick(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to kick a client through the API.
void requestAPIAdminMessage(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to send a message to a client through the API.
void requestAPIAdminClients(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request for admin clients through the API.
void Send(const atomizes::HTTPMessage &http_response) const
Send an HTTP response.
void _LogTransaction(const std::string &direction, const std::string &response) const
Log a transaction.
void requestAPIAdminPlayerStatsRecalc(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handles the API request to recalculate player statistics.
Represents a session with a GPCM client.