add json stuff

This commit is contained in:
David Markowitz 2025-01-02 20:14:07 -08:00
parent 9e16e01b8d
commit 631980af3a
5 changed files with 124 additions and 112 deletions

View File

@ -18,109 +18,78 @@ namespace {
const char* json_content_type = "Content-Type: application/json\r\n"; const char* json_content_type = "Content-Type: application/json\r\n";
} }
struct HttpReply {
uint32_t status = 404;
std::string message = "{\"error\":\"Not Found\"}";
};
void HandleRequests(mg_connection* connection, int request, void* request_data) { void HandleRequests(mg_connection* connection, int request, void* request_data) {
if (request == MG_EV_HTTP_MSG) { if (request == MG_EV_HTTP_MSG) {
HttpReply reply;
const mg_http_message* const http_msg = static_cast<mg_http_message*>(request_data); const mg_http_message* const http_msg = static_cast<mg_http_message*>(request_data);
if (!http_msg) { if (!http_msg) {
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid Request\"}"); reply.status = 400;
return; reply.message = "{\"error\":\"Invalid Request\"}";
} } else {
// Handle Post requests
// Handle Post requests if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) {
if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) {
// handle announcements
if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) {
auto data = GeneralUtils::TryParse<json>(http_msg->body.buf); auto data = GeneralUtils::TryParse<json>(http_msg->body.buf);
if (!data) { if (!data) {
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid JSON\"}"); reply.status = 400;
return; reply.message = "{\"error\":\"Invalid JSON\"}";
} } else if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) {
auto& jsonBuffer = data.value();
// handle announcements
if (!data.value().contains("title")) { if (!jsonBuffer.contains("title")) {
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Missing paramater: title\"}"); reply.status = 400;
return; reply.message = "{\"error\":\"Missing paramater: title\"}";
} } else if (!jsonBuffer.contains("message")) {
std::string title = data.value()["title"]; reply.status = 400;
if (!data.value().contains("message")) { reply.message = "{\"error\":\"Missing paramater: message\"}";
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Missing paramater: message\"}"); } else {
return; std::string title = jsonBuffer["title"];
} std::string message = jsonBuffer["message"];
std::string message = data.value()["message"];
// build and send the packet to all world servers // build and send the packet to all world servers
{ CBITSTREAM;
CBITSTREAM; BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::GM_ANNOUNCE);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::GM_ANNOUNCE); bitStream.Write<uint32_t>(title.size());
bitStream.Write<uint32_t>(title.size()); bitStream.Write(title);
bitStream.Write(title); bitStream.Write<uint32_t>(message.size());
bitStream.Write<uint32_t>(message.size()); bitStream.Write(message);
bitStream.Write(message); SEND_PACKET_BROADCAST;
Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
}
mg_http_reply(connection, 200, json_content_type, "{\"status\":\"Announcement Sent\"}"); reply.status = 200;
return; reply.message = "{\"status\":\"Announcement Sent\"}";
}
// Handle GET Requests
} else if (mg_strcmp(http_msg->method, mg_str("GET")) == 0) {
// Get All Online players
if (mg_match(http_msg->uri, mg_str((root_path + "players").c_str()), NULL)) {
auto data = json::array();
for (auto& [playerID, playerData] : Game::playerContainer.GetAllPlayers()) {
if (!playerData) continue;
data.push_back(playerData.to_json());
}
if (data.empty()) {
mg_http_reply(connection, 200, json_content_type, "{\"error\":\"No Players Online\"}");
} else {
mg_http_reply(connection, 200, json_content_type, data.dump().c_str());
}
return;
} else if (mg_match(http_msg->uri, mg_str((root_path + "teams").c_str()), NULL)) {
// Get Teams
auto data = json::array();
for (auto& teamData : Game::playerContainer.GetAllTeams()) {
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;
for (auto& member : teamData->memberIDs) {
auto& playerData = Game::playerContainer.GetPlayerData(member);
if (!playerData) continue;
members.push_back(playerData.to_json());
} }
toInsert["members"] = members;
data.push_back(toInsert);
} }
// Handle GET Requests
} else if (mg_strcmp(http_msg->method, mg_str("GET")) == 0) {
// Get All Online players
if (mg_match(http_msg->uri, mg_str((root_path + "players").c_str()), NULL)) {
const json data = Game::playerContainer;
if (data.empty()) { reply.status = 200;
mg_http_reply(connection, 200, json_content_type, "{\"error\":\"No Teams Online\"}"); reply.message = data.empty() ? "{\"error\":\"No Players Online\"}" : data.dump();
} else { } else if (mg_match(http_msg->uri, mg_str((root_path + "teams").c_str()), NULL)) {
mg_http_reply(connection, 200, json_content_type, data.dump().c_str()); // Get Teams
const json data = Game::playerContainer.GetTeamComtainer();
reply.status = 200;
reply.message = data.empty() ? "{\"error\":\"No Teams Online\"}" : data.dump();
} }
return;
} }
} }
// If it hasn't been handled then reply 404 Not Found LOG_DEBUG("Replying with status %d: %s", reply.status, reply.message.c_str());
mg_http_reply(connection, 404, json_content_type, "{\"error\":\"Not Found\"}"); mg_http_reply(connection, reply.status, json_content_type, reply.message.c_str());
} }
} }
ChatWebAPI::ChatWebAPI() { ChatWebAPI::ChatWebAPI() {
if (Game::logger->GetLogDebugStatements()) mg_log_set(MG_LL_DEBUG); mg_log_set(MG_LL_NONE);
mg_mgr_init(&mgr); // Initialize event manager mg_mgr_init(&mgr); // Initialize event manager
} }
@ -130,12 +99,17 @@ ChatWebAPI::~ChatWebAPI() {
void ChatWebAPI::Listen() { void ChatWebAPI::Listen() {
// make listen address // make listen address
const std::string& listen_ip = Game::config->GetValue("web_server_listen_ip"); std::string listen_ip = Game::config->GetValue("web_server_listen_ip");
const std::string& listen_port = Game::config->GetValue("wed_server_listen_port"); if (listen_ip == "localhost") listen_ip = "127.0.0.1";
const std::string& listen_port = Game::config->GetValue("web_server_listen_port");
const std::string& listen_address = "http://" + listen_ip + ":" + listen_port; const std::string& listen_address = "http://" + listen_ip + ":" + listen_port;
LOG("Starting web server on %s", listen_address.c_str()); LOG("Starting web server on %s", listen_address.c_str());
mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL); // Create HTTP listener // Create HTTP listener
if (!mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL)) {
LOG("Failed to create web server listener");
}
} }
void ChatWebAPI::ReceiveRequests() { void ChatWebAPI::ReceiveRequests() {

View File

@ -15,18 +15,44 @@
using json = nlohmann::json; using json = nlohmann::json;
const json PlayerData::to_json() const { void to_json(json& data, const PlayerData& playerData) {
json data; data["id"] = playerData.playerID;
data["id"] = this->playerID; data["name"] = playerData.playerName;
data["name"] = this->playerName; data["gm_level"] = playerData.gmLevel;
data["gm_level"] = this->gmLevel; data["muted"] = playerData.GetIsMuted();
data["muted"] = this->GetIsMuted();
json& zoneID = data["zone_id"]; auto& zoneID = data["zone_id"];
zoneID["map_id"] = std::to_string(this->zoneID.GetMapID()); zoneID["map_id"] = std::to_string(playerData.zoneID.GetMapID());
zoneID["instance_id"] = std::to_string(this->zoneID.GetInstanceID()); zoneID["instance_id"] = std::to_string(playerData.zoneID.GetInstanceID());
zoneID["clone_id"] = std::to_string(this->zoneID.GetCloneID()); zoneID["clone_id"] = std::to_string(playerData.zoneID.GetCloneID());
return data; }
void to_json(json& data, const PlayerContainer& playerContainer) {
data = playerContainer.GetAllPlayers();
}
void to_json(json& data, const TeamContainer& teamContainer) {
for (auto& teamData : Game::playerContainer.GetTeams()) {
if (!teamData) continue;
data.push_back(*teamData);
}
}
void to_json(json& data, const TeamData& teamData) {
data["id"] = teamData.teamID;
data["loot_flag"] = teamData.lootFlag;
data["local"] = teamData.local;
auto& leader = Game::playerContainer.GetPlayerData(teamData.leaderID);
data["leader"] = leader.playerName;
auto& members = data["members"];
for (auto& member : teamData.memberIDs) {
auto& playerData = Game::playerContainer.GetPlayerData(member);
if (!playerData) continue;
members.push_back(playerData);
}
} }
void PlayerContainer::Initialize() { void PlayerContainer::Initialize() {
@ -232,7 +258,7 @@ TeamData* PlayerContainer::CreateTeam(LWOOBJID leader, bool local) {
team->leaderID = leader; team->leaderID = leader;
team->local = local; team->local = local;
mTeams.push_back(team); GetTeamsMut().push_back(team);
AddMember(team, leader); AddMember(team, leader);
@ -240,7 +266,7 @@ TeamData* PlayerContainer::CreateTeam(LWOOBJID leader, bool local) {
} }
TeamData* PlayerContainer::GetTeam(LWOOBJID playerID) { TeamData* PlayerContainer::GetTeam(LWOOBJID playerID) {
for (auto* team : mTeams) { for (auto* team : GetTeams()) {
if (std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID) == team->memberIDs.end()) continue; if (std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID) == team->memberIDs.end()) continue;
return team; return team;
@ -348,9 +374,9 @@ void PlayerContainer::PromoteMember(TeamData* team, LWOOBJID newLeader) {
} }
void PlayerContainer::DisbandTeam(TeamData* team) { void PlayerContainer::DisbandTeam(TeamData* team) {
const auto index = std::find(mTeams.begin(), mTeams.end(), team); const auto index = std::find(GetTeams().begin(), GetTeams().end(), team);
if (index == mTeams.end()) return; if (index == GetTeams().end()) return;
for (const auto memberId : team->memberIDs) { for (const auto memberId : team->memberIDs) {
const auto& otherMember = GetPlayerData(memberId); const auto& otherMember = GetPlayerData(memberId);
@ -365,15 +391,15 @@ void PlayerContainer::DisbandTeam(TeamData* team) {
UpdateTeamsOnWorld(team, true); UpdateTeamsOnWorld(team, true);
mTeams.erase(index); GetTeamsMut().erase(index);
delete team; delete team;
} }
void PlayerContainer::TeamStatusUpdate(TeamData* team) { void PlayerContainer::TeamStatusUpdate(TeamData* team) {
const auto index = std::find(mTeams.begin(), mTeams.end(), team); const auto index = std::find(GetTeams().begin(), GetTeams().end(), team);
if (index == mTeams.end()) return; if (index == GetTeams().end()) return;
const auto& leader = GetPlayerData(team->leaderID); const auto& leader = GetPlayerData(team->leaderID);
@ -460,5 +486,5 @@ void PlayerContainer::Shutdown() {
Database::Get()->UpdateActivityLog(id, eActivityType::PlayerLoggedOut, playerData.zoneID.GetMapID()); Database::Get()->UpdateActivityLog(id, eActivityType::PlayerLoggedOut, playerData.zoneID.GetMapID());
m_Players.erase(m_Players.begin()); m_Players.erase(m_Players.begin());
} }
for (auto* team : mTeams) if (team) delete team; for (auto* team : GetTeams()) if (team) delete team;
} }

View File

@ -10,6 +10,12 @@
enum class eGameMasterLevel : uint8_t; enum class eGameMasterLevel : uint8_t;
struct TeamData;
struct TeamContainer {
std::vector<TeamData*> mTeams;
};
struct IgnoreData { struct IgnoreData {
IgnoreData(const std::string& name, const LWOOBJID& id) : playerName{ name }, playerId{ id } {} IgnoreData(const std::string& name, const LWOOBJID& id) : playerName{ name }, playerId{ id } {}
inline bool operator==(const std::string& other) const noexcept { inline bool operator==(const std::string& other) const noexcept {
@ -37,8 +43,6 @@ struct PlayerData {
return muteExpire == 1 || muteExpire > time(NULL); return muteExpire == 1 || muteExpire > time(NULL);
} }
const nlohmann::json to_json() const;
SystemAddress sysAddr{}; SystemAddress sysAddr{};
LWOZONEID zoneID{}; LWOZONEID zoneID{};
LWOOBJID playerID = LWOOBJID_EMPTY; LWOOBJID playerID = LWOOBJID_EMPTY;
@ -51,6 +55,11 @@ struct PlayerData {
bool isFTP = false; bool isFTP = false;
}; };
void to_json(nlohmann::json& data, const PlayerData& playerData);
void to_json(nlohmann::json& data, const PlayerContainer& playerContainer);
void to_json(nlohmann::json& data, const TeamContainer& teamData);
void to_json(nlohmann::json& data, const TeamData& teamData);
struct TeamData { struct TeamData {
TeamData(); TeamData();
LWOOBJID teamID = LWOOBJID_EMPTY; // Internal use LWOOBJID teamID = LWOOBJID_EMPTY; // Internal use
@ -78,7 +87,7 @@ public:
PlayerData& GetPlayerDataMutable(const std::string& playerName); PlayerData& GetPlayerDataMutable(const std::string& playerName);
uint32_t GetPlayerCount() { return m_PlayerCount; }; uint32_t GetPlayerCount() { return m_PlayerCount; };
uint32_t GetSimCount() { return m_SimCount; }; uint32_t GetSimCount() { return m_SimCount; };
const std::map<LWOOBJID, PlayerData>& GetAllPlayers() { return m_Players; }; const std::map<LWOOBJID, PlayerData>& GetAllPlayers() const { return m_Players; };
TeamData* CreateLocalTeam(std::vector<LWOOBJID> members); TeamData* CreateLocalTeam(std::vector<LWOOBJID> members);
TeamData* CreateTeam(LWOOBJID leader, bool local = false); TeamData* CreateTeam(LWOOBJID leader, bool local = false);
@ -93,7 +102,9 @@ public:
LWOOBJID GetId(const std::u16string& playerName); LWOOBJID GetId(const std::u16string& playerName);
uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; } uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; }
uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; } uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; }
const std::vector<TeamData*>& GetAllTeams() { return mTeams;}; const TeamContainer& GetTeamComtainer() { return teamContainer; }
std::vector<TeamData*>& GetTeamsMut() { return teamContainer.mTeams; };
const std::vector<TeamData*>& GetTeams() { return GetTeamsMut(); };
void Update(const float deltaTime); void Update(const float deltaTime);
bool PlayerBeingRemoved(const LWOOBJID playerID) { return m_PlayersToRemove.contains(playerID); } bool PlayerBeingRemoved(const LWOOBJID playerID) { return m_PlayersToRemove.contains(playerID); }
@ -101,7 +112,7 @@ public:
private: private:
LWOOBJID m_TeamIDCounter = 0; LWOOBJID m_TeamIDCounter = 0;
std::map<LWOOBJID, PlayerData> m_Players; std::map<LWOOBJID, PlayerData> m_Players;
std::vector<TeamData*> mTeams; TeamContainer teamContainer{};
std::unordered_map<LWOOBJID, std::u16string> m_Names; std::unordered_map<LWOOBJID, std::u16string> m_Names;
std::map<LWOOBJID, float> m_PlayersToRemove; std::map<LWOOBJID, float> m_PlayersToRemove;
uint32_t m_MaxNumberOfBestFriends = 5; uint32_t m_MaxNumberOfBestFriends = 5;

View File

@ -130,7 +130,8 @@ components:
type: boolean type: boolean
example: false example: false
leader: leader:
$ref: "#/components/schemas/Player" type: string
example: thisisatestname
members: members:
type: array type: array
items: items:

View File

@ -10,4 +10,4 @@ max_number_of_friends=50
web_server_enabled=0 web_server_enabled=0
web_server_listen_ip=127.0.0.1 web_server_listen_ip=127.0.0.1
wed_server_listen_port=2005 web_server_listen_port=2005