provide all the info for a some integration client to use

who->players
add teams endpoint
This commit is contained in:
Aaron Kimbre 2024-04-21 21:56:10 -05:00
parent 55a52b6cc0
commit ed7b33d8ab
3 changed files with 53 additions and 9 deletions

View File

@ -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();
}

View File

@ -1,7 +1,8 @@
#include "PlayerContainer.h"
#include "dNetCommon.h"
#include <iostream>
#include <algorithm>
#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<uint32_t>(Game::config->GetValue("max_number_of_best_friends")).value_or(m_MaxNumberOfBestFriends);

View File

@ -7,6 +7,9 @@
#include "dServer.h"
#include <unordered_map>
#include <nlohmann/json.hpp>
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<TeamData*> GetAllTeams() { return mTeams;};
private:
LWOOBJID m_TeamIDCounter = 0;