2025-01-02 06:45:53 +00:00
|
|
|
#include "ChatWebAPI.h"
|
2025-01-02 22:11:45 +00:00
|
|
|
|
2025-01-02 06:45:53 +00:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "json.hpp"
|
2025-01-02 22:11:45 +00:00
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "MessageType/Chat.h"
|
|
|
|
#include "dServer.h"
|
|
|
|
#include "dConfig.h"
|
|
|
|
#include "PlayerContainer.h"
|
2025-01-02 23:04:07 +00:00
|
|
|
#include "GeneralUtils.h"
|
2025-01-02 22:11:45 +00:00
|
|
|
|
2025-01-02 22:48:31 +00:00
|
|
|
void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, void* request_data) {
|
|
|
|
if (request == MG_EV_HTTP_MSG) {
|
|
|
|
struct mg_http_message* http_msg = static_cast<struct mg_http_message*>(request_data);
|
|
|
|
if (!http_msg) {
|
|
|
|
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid Request\"}");
|
2025-01-02 22:11:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2025-01-02 06:45:53 +00:00
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
// Handle Post requests
|
2025-01-02 22:48:31 +00:00
|
|
|
if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) {
|
2025-01-02 22:11:45 +00:00
|
|
|
// handle announcements
|
2025-01-02 22:48:31 +00:00
|
|
|
if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) {
|
2025-01-02 23:04:07 +00:00
|
|
|
auto data = GeneralUtils::TryParse<json>(http_msg->body.buf);
|
2025-01-02 22:11:45 +00:00
|
|
|
if (!data) {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid JSON\"}");
|
2025-01-02 22:11:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.value().contains("title")) {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Missing paramater: title\"}");
|
2025-01-02 22:11:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string title = data.value()["title"];
|
|
|
|
if (!data.value().contains("message")) {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Missing paramater: message\"}");
|
2025-01-02 22:11:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string message = data.value()["message"];
|
2025-01-02 22:48:31 +00:00
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
// build and send the packet to all world servers
|
2025-01-02 22:48:31 +00:00
|
|
|
{
|
|
|
|
CBITSTREAM;
|
|
|
|
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::GM_ANNOUNCE);
|
|
|
|
bitStream.Write<uint32_t>(title.size());
|
|
|
|
bitStream.Write(title);
|
|
|
|
bitStream.Write<uint32_t>(message.size());
|
|
|
|
bitStream.Write(message);
|
|
|
|
Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
mg_http_reply(connection, 200, json_content_type, "{\"status\":\"Announcement Sent\"}");
|
|
|
|
return;
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
2025-01-02 22:48:31 +00:00
|
|
|
// Handle GET Requests
|
|
|
|
} else if (mg_strcmp(http_msg->method, mg_str("GET")) == 0) {
|
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
// Get All Online players
|
2025-01-02 22:48:31 +00:00
|
|
|
if (mg_match(http_msg->uri, mg_str((root_path + "players").c_str()), NULL)) {
|
2025-01-02 22:11:45 +00:00
|
|
|
auto data = json::array();
|
2025-01-02 22:48:31 +00:00
|
|
|
for (auto& [playerID, playerData] : Game::playerContainer.GetAllPlayers()) {
|
2025-01-02 22:11:45 +00:00
|
|
|
if (!playerData) continue;
|
|
|
|
data.push_back(playerData.to_json());
|
|
|
|
}
|
|
|
|
if (data.empty()) {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 200, json_content_type, "{\"error\":\"No Players Online\"}");
|
2025-01-02 22:11:45 +00:00
|
|
|
} else {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 200, json_content_type, data.dump().c_str());
|
2025-01-02 22:11:45 +00:00
|
|
|
}
|
2025-01-02 22:48:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (mg_match(http_msg->uri, mg_str((root_path + "teams").c_str()), NULL)) {
|
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
// Get Teams
|
|
|
|
auto data = json::array();
|
2025-01-02 22:48:31 +00:00
|
|
|
for (auto& teamData : Game::playerContainer.GetAllTeams()) {
|
2025-01-02 22:11:45 +00:00
|
|
|
if (!teamData) continue;
|
|
|
|
json toInsert;
|
|
|
|
toInsert["id"] = teamData->teamID;
|
|
|
|
toInsert["loot_flag"] = teamData->lootFlag;
|
|
|
|
toInsert["local"] = teamData->local;
|
|
|
|
|
|
|
|
auto& leader = Game::playerContainer.GetPlayerData(teamData->leaderID);
|
|
|
|
toInsert["leader"] = leader.to_json();
|
|
|
|
|
|
|
|
json members;
|
2025-01-02 22:48:31 +00:00
|
|
|
for (auto& member : teamData->memberIDs) {
|
2025-01-02 22:11:45 +00:00
|
|
|
auto& playerData = Game::playerContainer.GetPlayerData(member);
|
2025-01-02 22:48:31 +00:00
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
if (!playerData) continue;
|
|
|
|
members.push_back(playerData.to_json());
|
|
|
|
}
|
|
|
|
toInsert["members"] = members;
|
|
|
|
data.push_back(toInsert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.empty()) {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 200, json_content_type, "{\"error\":\"No Teams Online\"}");
|
2025-01-02 22:11:45 +00:00
|
|
|
} else {
|
2025-01-02 22:48:31 +00:00
|
|
|
mg_http_reply(connection, 200, json_content_type, data.dump().c_str());
|
2025-01-02 22:11:45 +00:00
|
|
|
}
|
2025-01-02 22:48:31 +00:00
|
|
|
return;
|
|
|
|
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-02 22:48:31 +00:00
|
|
|
|
|
|
|
// If it hasn't been handled then reply 404 Not Found
|
|
|
|
mg_http_reply(connection, 404, json_content_type, "{\"error\":\"Not Found\"}");
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
ChatWebAPI::ChatWebAPI() {
|
|
|
|
if (Game::logger->GetLogDebugStatements()) mg_log_set(MG_LL_DEBUG);
|
2025-01-02 22:35:27 +00:00
|
|
|
mg_mgr_init(&mgr); // Initialize event manager
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatWebAPI::~ChatWebAPI() {
|
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWebAPI::Listen() {
|
2025-01-02 22:11:45 +00:00
|
|
|
// make listen address
|
2025-01-03 02:03:44 +00:00
|
|
|
const std::string& listen_ip = Game::config->GetValue("web_server_listen_ip");
|
|
|
|
const std::string& listen_port = Game::config->GetValue("wed_server_listen_port");
|
|
|
|
const std::string& listen_address = "http://" + listen_ip + ":" + listen_port;
|
2025-01-02 22:28:34 +00:00
|
|
|
LOG("Starting web server on %s", listen_address.c_str());
|
2025-01-02 06:45:53 +00:00
|
|
|
|
2025-01-02 22:35:27 +00:00
|
|
|
mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL); // Create HTTP listener
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
void ChatWebAPI::ReceiveRequests() {
|
|
|
|
mg_mgr_poll(&mgr, 15);
|
|
|
|
}
|