diff --git a/dChatServer/ChatHttpApi.cpp b/dChatServer/ChatHttpApi.cpp index 3f779add..624dd988 100644 --- a/dChatServer/ChatHttpApi.cpp +++ b/dChatServer/ChatHttpApi.cpp @@ -40,13 +40,36 @@ void ChatHttpApi::Listen(const uint32_t port) { Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true); }); - m_APIServer.Get("/who", [](const httplib::Request& req, httplib::Response& res) { - json data; + m_APIServer.Get("/players", [](const httplib::Request& req, httplib::Response& res) { + auto data = json::array(); for (auto& [playerID, playerData ]: Game::playerContainer.GetAllPlayers()){ if (!playerData) continue; - auto map = std::to_string(playerData.zoneID.GetMapID()); - if (!data.contains(map)) data[map] = json::array(); - data[map].push_back(playerData.playerName); + data.push_back(playerData.to_json()); + } + res.set_content(data.dump(), "application/json"); + if (data.empty()) res.status = 204; + }); + + m_APIServer.Get("/teams", [](const httplib::Request& req, httplib::Response& res) { + 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); } res.set_content(data.dump(), "application/json"); if (data.empty()) res.status = 204; @@ -55,8 +78,6 @@ void ChatHttpApi::Listen(const uint32_t port) { m_APIServer.listen("0.0.0.0", port); }; - - void ChatHttpApi::Stop(){ m_APIServer.stop(); } \ No newline at end of file diff --git a/dChatServer/PlayerContainer.cpp b/dChatServer/PlayerContainer.cpp index 17e2cd1a..443f38a0 100644 --- a/dChatServer/PlayerContainer.cpp +++ b/dChatServer/PlayerContainer.cpp @@ -1,7 +1,8 @@ -#include "PlayerContainer.h" -#include "dNetCommon.h" #include #include + +#include "PlayerContainer.h" +#include "dNetCommon.h" #include "Game.h" #include "Logger.h" #include "ChatPacketHandler.h" @@ -13,6 +14,22 @@ #include "dConfig.h" #include "eChatMessageType.h" + +const json PlayerData::to_json() const { + json data; + data["id"] = this->playerID; + data["name"] = this->playerName; + data["gm_level"] = this->gmLevel; + data["muted"] = this->GetIsMuted(); + + json zoneID; + zoneID["map_id"] = std::to_string(this->zoneID.GetMapID()); + zoneID["instance_id"] = std::to_string(this->zoneID.GetInstanceID()); + zoneID["clone_id"] = std::to_string(this->zoneID.GetCloneID()); + data["zone_id"] = zoneID; + return data; +} + void PlayerContainer::Initialize() { m_MaxNumberOfBestFriends = GeneralUtils::TryParse(Game::config->GetValue("max_number_of_best_friends")).value_or(m_MaxNumberOfBestFriends); diff --git a/dChatServer/PlayerContainer.h b/dChatServer/PlayerContainer.h index 9a17f927..c9f5e3bf 100644 --- a/dChatServer/PlayerContainer.h +++ b/dChatServer/PlayerContainer.h @@ -7,6 +7,9 @@ #include "dServer.h" #include +#include +using json = nlohmann::json; + enum class eGameMasterLevel : uint8_t; struct IgnoreData { @@ -36,6 +39,8 @@ struct PlayerData { return muteExpire == 1 || muteExpire > time(NULL); } + const json to_json() const; + SystemAddress sysAddr{}; LWOZONEID zoneID{}; LWOOBJID playerID = LWOOBJID_EMPTY; @@ -88,6 +93,7 @@ public: LWOOBJID GetId(const std::u16string& playerName); uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; } uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; } + const std::vector GetAllTeams() { return mTeams;}; private: LWOOBJID m_TeamIDCounter = 0;