From 55a52b6cc0e488fd9425254d2c5997993ce2ca56 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Sun, 21 Apr 2024 02:09:54 -0500 Subject: [PATCH 01/12] WIP Only works on linux, get rekt --- dChatServer/CMakeLists.txt | 1 + dChatServer/ChatHttpApi.cpp | 62 +++++++++++++++++++++++++++++++++++++ dChatServer/ChatHttpApi.h | 6 ++++ dChatServer/ChatServer.cpp | 6 +++- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 dChatServer/ChatHttpApi.cpp create mode 100644 dChatServer/ChatHttpApi.h diff --git a/dChatServer/CMakeLists.txt b/dChatServer/CMakeLists.txt index c7eea041e..853345dd2 100644 --- a/dChatServer/CMakeLists.txt +++ b/dChatServer/CMakeLists.txt @@ -1,4 +1,5 @@ set(DCHATSERVER_SOURCES + "ChatHttpApi.cpp" "ChatIgnoreList.cpp" "ChatPacketHandler.cpp" "PlayerContainer.cpp" diff --git a/dChatServer/ChatHttpApi.cpp b/dChatServer/ChatHttpApi.cpp new file mode 100644 index 000000000..3f779add2 --- /dev/null +++ b/dChatServer/ChatHttpApi.cpp @@ -0,0 +1,62 @@ +#include +#include + +#include "ChatHttpApi.h" +#include "dCommonVars.h" +#include "eConnectionType.h" +#include "eChatMessageType.h" +#include "httplib.h" +#include "dServer.h" +#include "PlayerContainer.h" + +using json = nlohmann::json; + +namespace { + httplib::Server m_APIServer; +} + +void ChatHttpApi::Listen(const uint32_t port) { + m_APIServer.Post("/announce", [](const httplib::Request& req, httplib::Response& res) { + const json data = json::parse(req.body); + if (!data.contains("title")) { + res.set_content("{\"error\":\"Missing paramater: title\"}", "application/json"); + res.status = 400; + return; + } + std::string title = data["title"]; + if (!data.contains("message")) { + res.set_content("{\"error\":\"Missing paramater: message\"}", "application/json"); + res.status = 400; + return; + } + std::string message = data["message"]; + // build and send the packet to all world servers + CBITSTREAM; + BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::GM_ANNOUNCE); + bitStream.Write(title.size()); + bitStream.Write(title); + bitStream.Write(message.size()); + bitStream.Write(message); + Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true); + }); + + m_APIServer.Get("/who", [](const httplib::Request& req, httplib::Response& res) { + json data; + 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); + } + res.set_content(data.dump(), "application/json"); + if (data.empty()) res.status = 204; + }); + + m_APIServer.listen("0.0.0.0", port); +}; + + + +void ChatHttpApi::Stop(){ + m_APIServer.stop(); +} \ No newline at end of file diff --git a/dChatServer/ChatHttpApi.h b/dChatServer/ChatHttpApi.h new file mode 100644 index 000000000..d6584f356 --- /dev/null +++ b/dChatServer/ChatHttpApi.h @@ -0,0 +1,6 @@ +#include "httplib.h" + +namespace ChatHttpApi { + void Listen(const uint32_t port); + void Stop(); +}; \ No newline at end of file diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index 81b6ddef2..7b72e8ecc 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -20,6 +20,7 @@ #include "eWorldMessageType.h" #include "ChatIgnoreList.h" #include "StringifiedEnum.h" +#include "ChatHttpApi.h" #include "Game.h" #include "Server.h" @@ -122,6 +123,8 @@ int main(int argc, char** argv) { uint32_t framesSinceMasterDisconnect = 0; uint32_t framesSinceLastSQLPing = 0; + std::thread APIThread(ChatHttpApi::Listen, ourPort); + Game::logger->Flush(); // once immediately before main loop while (!Game::ShouldShutdown()) { //Check if we're still connected to master: @@ -174,7 +177,8 @@ int main(int argc, char** argv) { delete Game::server; delete Game::logger; delete Game::config; - + ChatHttpApi::Stop(); + APIThread.join(); return EXIT_SUCCESS; } From ed7b33d8aba27cce9cb71c2a2efa948febcd25b6 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Sun, 21 Apr 2024 21:56:10 -0500 Subject: [PATCH 02/12] provide all the info for a some integration client to use who->players add teams endpoint --- dChatServer/ChatHttpApi.cpp | 35 ++++++++++++++++++++++++++------- dChatServer/PlayerContainer.cpp | 21 ++++++++++++++++++-- dChatServer/PlayerContainer.h | 6 ++++++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/dChatServer/ChatHttpApi.cpp b/dChatServer/ChatHttpApi.cpp index 3f779add2..624dd988f 100644 --- a/dChatServer/ChatHttpApi.cpp +++ b/dChatServer/ChatHttpApi.cpp @@ -40,22 +40,43 @@ 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.listen("0.0.0.0", port); -}; + 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; + }); + + m_APIServer.listen("0.0.0.0", port); +}; void ChatHttpApi::Stop(){ m_APIServer.stop(); diff --git a/dChatServer/PlayerContainer.cpp b/dChatServer/PlayerContainer.cpp index 17e2cd1a8..443f38a04 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 9a17f9278..c9f5e3bf8 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 @@ class PlayerContainer { 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; From 850ad2aa1524da8960adb3dc30fc00b1594f2308 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Sun, 21 Apr 2024 22:02:25 -0500 Subject: [PATCH 03/12] nl @ eof --- dChatServer/ChatHttpApi.cpp | 2 +- dChatServer/ChatHttpApi.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dChatServer/ChatHttpApi.cpp b/dChatServer/ChatHttpApi.cpp index 624dd988f..8d652a32e 100644 --- a/dChatServer/ChatHttpApi.cpp +++ b/dChatServer/ChatHttpApi.cpp @@ -80,4 +80,4 @@ void ChatHttpApi::Listen(const uint32_t port) { void ChatHttpApi::Stop(){ m_APIServer.stop(); -} \ No newline at end of file +} diff --git a/dChatServer/ChatHttpApi.h b/dChatServer/ChatHttpApi.h index d6584f356..aafb29de8 100644 --- a/dChatServer/ChatHttpApi.h +++ b/dChatServer/ChatHttpApi.h @@ -3,4 +3,4 @@ namespace ChatHttpApi { void Listen(const uint32_t port); void Stop(); -}; \ No newline at end of file +}; From 63f9a9f9d4c682dd8f983efa8c2c5f9892bf9db7 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Tue, 23 Apr 2024 16:29:57 -0500 Subject: [PATCH 04/12] feat: add nlohmann/json lib --- CMakeLists.txt | 3 ++- cmake/FindJSON.cmake | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 cmake/FindJSON.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index aa5171822..ed1a46f17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,7 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) find_package(MariaDB) +find_package(JSON) # Create a /resServer directory make_directory(${CMAKE_BINARY_DIR}/resServer) @@ -284,7 +285,7 @@ add_subdirectory(dPhysics) add_subdirectory(dServer) # Create a list of common libraries shared between all binaries -set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "MariaDB::ConnCpp" "magic_enum") +set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "MariaDB::ConnCpp" "magic_enum" "nlohmann_json::nlohmann_json") # Add platform specific common libraries if(UNIX) diff --git a/cmake/FindJSON.cmake b/cmake/FindJSON.cmake new file mode 100644 index 000000000..2d32a4db3 --- /dev/null +++ b/cmake/FindJSON.cmake @@ -0,0 +1,21 @@ +include(FetchContent) + +message(STATUS "Fetching json...") + +FetchContent_Declare( + json + GIT_REPOSITORY https://github.com/nlohmann/json + GIT_TAG v3.11.3 +) + +FetchContent_GetProperties(json) +if(NOT json_POPULATED) + set(JSON_BuildTests OFF CACHE INTERNAL "") + FetchContent_Populate(json) + add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL) +endif() + +FetchContent_MakeAvailable(json) + +message(STATUS "json fetched and is now ready.") +set(JSON_FOUND TRUE) From 29e759f793583bb74ba5c6e41df69f486292aadd Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Wed, 24 Apr 2024 10:00:37 -0500 Subject: [PATCH 05/12] remove build test off --- cmake/FindJSON.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake/FindJSON.cmake b/cmake/FindJSON.cmake index 2d32a4db3..291ebcd35 100644 --- a/cmake/FindJSON.cmake +++ b/cmake/FindJSON.cmake @@ -10,7 +10,6 @@ FetchContent_Declare( FetchContent_GetProperties(json) if(NOT json_POPULATED) - set(JSON_BuildTests OFF CACHE INTERNAL "") FetchContent_Populate(json) add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL) endif() From 77143fc2cf7fe1875aaac2d95e17c3df31d05f64 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Thu, 25 Apr 2024 09:21:59 -0500 Subject: [PATCH 06/12] rename files use config vars to disable the api and whitelist listen IP --- dChatServer/CMakeLists.txt | 2 +- dChatServer/ChatServer.cpp | 14 ++++++++----- .../{ChatHttpApi.cpp => ChatWebApi.cpp} | 20 ++++++++++++++----- dChatServer/{ChatHttpApi.h => ChatWebApi.h} | 2 +- resources/chatconfig.ini | 7 +++++++ 5 files changed, 33 insertions(+), 12 deletions(-) rename dChatServer/{ChatHttpApi.cpp => ChatWebApi.cpp} (82%) rename dChatServer/{ChatHttpApi.h => ChatWebApi.h} (75%) diff --git a/dChatServer/CMakeLists.txt b/dChatServer/CMakeLists.txt index 853345dd2..3dc64c8a8 100644 --- a/dChatServer/CMakeLists.txt +++ b/dChatServer/CMakeLists.txt @@ -1,5 +1,5 @@ set(DCHATSERVER_SOURCES - "ChatHttpApi.cpp" + "ChatWebApi.cpp" "ChatIgnoreList.cpp" "ChatPacketHandler.cpp" "PlayerContainer.cpp" diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index 7b72e8ecc..41c9b1b0c 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -20,7 +20,7 @@ #include "eWorldMessageType.h" #include "ChatIgnoreList.h" #include "StringifiedEnum.h" -#include "ChatHttpApi.h" +#include "ChatWebApi.h" #include "Game.h" #include "Server.h" @@ -37,7 +37,7 @@ namespace Game { AssetManager* assetManager = nullptr; Game::signal_t lastSignal = 0; std::mt19937 randomEngine; - PlayerContainer playerContainer; + PlayerContainer playerContainer; } void HandlePacket(Packet* packet); @@ -123,7 +123,8 @@ int main(int argc, char** argv) { uint32_t framesSinceMasterDisconnect = 0; uint32_t framesSinceLastSQLPing = 0; - std::thread APIThread(ChatHttpApi::Listen, ourPort); + // start the web api thread + std::thread webAPIThread(ChatWebApi::Listen, ourPort); Game::logger->Flush(); // once immediately before main loop while (!Game::ShouldShutdown()) { @@ -177,8 +178,11 @@ int main(int argc, char** argv) { delete Game::server; delete Game::logger; delete Game::config; - ChatHttpApi::Stop(); - APIThread.join(); + + // rejoin the web api thread + ChatWebApi::Stop(); + webAPIThread.join(); + return EXIT_SUCCESS; } diff --git a/dChatServer/ChatHttpApi.cpp b/dChatServer/ChatWebApi.cpp similarity index 82% rename from dChatServer/ChatHttpApi.cpp rename to dChatServer/ChatWebApi.cpp index 8d652a32e..c9cebc6ad 100644 --- a/dChatServer/ChatHttpApi.cpp +++ b/dChatServer/ChatWebApi.cpp @@ -1,13 +1,14 @@ #include #include -#include "ChatHttpApi.h" +#include "ChatWebApi.h" #include "dCommonVars.h" #include "eConnectionType.h" #include "eChatMessageType.h" #include "httplib.h" #include "dServer.h" #include "PlayerContainer.h" +#include "dConfig.h" using json = nlohmann::json; @@ -15,7 +16,13 @@ namespace { httplib::Server m_APIServer; } -void ChatHttpApi::Listen(const uint32_t port) { +void ChatWebApi::Listen(const uint32_t port) { + if (Game::config->GetValue("enable_chat_web_api") != "1") { + LOG("Chat Web API is disabled"); + return; + } + LOG("Chat Web API is enabled, starting web server..."); + m_APIServer.Post("/announce", [](const httplib::Request& req, httplib::Response& res) { const json data = json::parse(req.body); if (!data.contains("title")) { @@ -75,9 +82,12 @@ void ChatHttpApi::Listen(const uint32_t port) { if (data.empty()) res.status = 204; }); - m_APIServer.listen("0.0.0.0", port); + m_APIServer.listen(Game::config->GetValue("enable_chat_web_api").c_str(), port); }; -void ChatHttpApi::Stop(){ - m_APIServer.stop(); +void ChatWebApi::Stop(){ + if (Game::config->GetValue("enable_chat_web_api") == "1") { + LOG("Stopping Chat Web API server..."); + m_APIServer.stop(); + } } diff --git a/dChatServer/ChatHttpApi.h b/dChatServer/ChatWebApi.h similarity index 75% rename from dChatServer/ChatHttpApi.h rename to dChatServer/ChatWebApi.h index aafb29de8..420a40876 100644 --- a/dChatServer/ChatHttpApi.h +++ b/dChatServer/ChatWebApi.h @@ -1,6 +1,6 @@ #include "httplib.h" -namespace ChatHttpApi { +namespace ChatWebApi { void Listen(const uint32_t port); void Stop(); }; diff --git a/resources/chatconfig.ini b/resources/chatconfig.ini index 402534ed1..ee990089c 100644 --- a/resources/chatconfig.ini +++ b/resources/chatconfig.ini @@ -6,3 +6,10 @@ max_number_of_best_friends=5 # Change the value below to what you would like this to be (50 is live accurate) # going over 50 will be allowed in some secnarios, but proper handling will require client modding max_number_of_friends=50 + +# Enable or disable the chat web API, disabled by default +# It will run on the same port the chat server is running on, defined in shardconfig.ini +enable_chat_web_api=0 + +# If that chat web api is enabled, it will only listen for connections on this ip address +chat_web_api_listen_address=localhost From bce03ca08df9a44121536a2e5ba037317cc9b40e Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Thu, 25 Apr 2024 10:25:19 -0500 Subject: [PATCH 07/12] fixed config value --- dChatServer/ChatWebApi.cpp | 2 +- resources/chatconfig.ini | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/dChatServer/ChatWebApi.cpp b/dChatServer/ChatWebApi.cpp index c9cebc6ad..0ac185d88 100644 --- a/dChatServer/ChatWebApi.cpp +++ b/dChatServer/ChatWebApi.cpp @@ -82,7 +82,7 @@ void ChatWebApi::Listen(const uint32_t port) { if (data.empty()) res.status = 204; }); - m_APIServer.listen(Game::config->GetValue("enable_chat_web_api").c_str(), port); + m_APIServer.listen(Game::config->GetValue("chat_web_api_listen_address").c_str(), port); }; void ChatWebApi::Stop(){ diff --git a/resources/chatconfig.ini b/resources/chatconfig.ini index ee990089c..da82d0b1f 100644 --- a/resources/chatconfig.ini +++ b/resources/chatconfig.ini @@ -12,4 +12,5 @@ max_number_of_friends=50 enable_chat_web_api=0 # If that chat web api is enabled, it will only listen for connections on this ip address -chat_web_api_listen_address=localhost +# 127.0.0.1 is localhost +chat_web_api_listen_address=127.0.0.1 From ff721ed49b78cb53a35fe350ef9435478051edce Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Thu, 25 Apr 2024 17:24:19 -0500 Subject: [PATCH 08/12] Add Swagger OpenAPI doc --- docs/ChatWebAPI.yaml | 149 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 docs/ChatWebAPI.yaml diff --git a/docs/ChatWebAPI.yaml b/docs/ChatWebAPI.yaml new file mode 100644 index 000000000..815ddd16e --- /dev/null +++ b/docs/ChatWebAPI.yaml @@ -0,0 +1,149 @@ +openapi: 3.0.3 +info: + title: DLU Chat Server API + description: |- + This documents the available api endpoints for the DLU Chat Server Web API + + contact: + name: DarkflameUniverse Github + url: https://github.com/DarkflameUniverse/DarkflameServer/issues + license: + name: GNU AGPL v3.0 + url: https://github.com/DarkflameUniverse/DarkflameServer/blob/main/LICENSE + version: 1.0.0 + +externalDocs: + description: Find out more about Swagger + url: http://swagger.io + +servers: + - url: http://localhost:2005/ + description: localhost + +tags: + - name: management + description: Server Management Utilities + - name: user + description: User Data Utilities + +paths: + /announce: + post: + tags: + - management + summary: Send an announcement to the game server + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Announce" + required: true + responses: + "200": + description: Successful operation + "400": + description: Missing Parameter + + /players: + get: + tags: + - user + responses: + "200": + description: Successful operation + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Player" + "204": + description: No Data + + /teams: + get: + tags: + - user + responses: + "200": + description: Successful operation + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Team" + "204": + description: No Data + +components: + schemas: + Player: + type: object + properties: + id: + type: integer + format: int64 + example: 1152921508901824000 + gm_level: + type: integer + format: uint8 + example: 0 + name: + type: string + example: thisisatestname + muted: + type: boolean + example: false + zone_id: + $ref: "#/components/schemas/ZoneID" + + ZoneID: + type: object + properties: + map_id: + type: integer + format: uint16 + example: 1200 + instance_id: + type: integer + format: uint16 + example: 2 + clone_id: + type: integer + format: uint32 + example: 0 + + Team: + type: object + properties: + id: + type: integer + format: int64 + example: 1152921508901824000 + loot_flag: + type: integer + format: uint8 + example: 1 + local: + type: boolean + example: false + leader: + $ref: "#/components/schemas/Player" + members: + type: array + items: + $ref: "#/components/schemas/Player" + + Announce: + required: + - title + - message + type: object + properties: + title: + type: string + example: A Mythran has taken Action against you! + message: + type: string + example: Check your mailbox for details! From c3ea448be0393957ed552a18410dfe4f80e9991c Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Thu, 25 Apr 2024 20:55:24 -0500 Subject: [PATCH 09/12] use reference with GetAllTeams Co-authored-by: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> --- dChatServer/PlayerContainer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dChatServer/PlayerContainer.h b/dChatServer/PlayerContainer.h index c9f5e3bf8..8a0948567 100644 --- a/dChatServer/PlayerContainer.h +++ b/dChatServer/PlayerContainer.h @@ -93,7 +93,7 @@ class PlayerContainer { LWOOBJID GetId(const std::u16string& playerName); uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; } uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; } - const std::vector GetAllTeams() { return mTeams;}; + const std::vector& GetAllTeams() { return mTeams;}; private: LWOOBJID m_TeamIDCounter = 0; From 06607c9b55cd9990e589b6e79000825d5e0b6204 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Thu, 25 Apr 2024 21:05:35 -0500 Subject: [PATCH 10/12] Update dChatServer/ChatWebApi.cpp Co-authored-by: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> --- dChatServer/ChatWebApi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dChatServer/ChatWebApi.cpp b/dChatServer/ChatWebApi.cpp index 0ac185d88..05ba4a6e9 100644 --- a/dChatServer/ChatWebApi.cpp +++ b/dChatServer/ChatWebApi.cpp @@ -66,7 +66,7 @@ void ChatWebApi::Listen(const uint32_t port) { toInsert["loot_flag"] = teamData->lootFlag; toInsert["local"] = teamData->local; - auto leader = Game::playerContainer.GetPlayerData(teamData->leaderID); + auto& leader = Game::playerContainer.GetPlayerData(teamData->leaderID); toInsert["leader"] = leader.to_json(); json members; From 9192dca4e668b60193e04e2f50262ee1d71382d1 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Thu, 25 Apr 2024 22:27:28 -0500 Subject: [PATCH 11/12] address feedback --- dChatServer/ChatWebApi.cpp | 14 ++++++-------- dChatServer/ChatWebApi.h | 2 +- dChatServer/PlayerContainer.cpp | 4 ++-- dChatServer/PlayerContainer.h | 4 +--- dCommon/json.h | 10 ++++++++++ docs/ChatWebAPI.yaml | 2 ++ 6 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 dCommon/json.h diff --git a/dChatServer/ChatWebApi.cpp b/dChatServer/ChatWebApi.cpp index 0ac185d88..4e4d79a04 100644 --- a/dChatServer/ChatWebApi.cpp +++ b/dChatServer/ChatWebApi.cpp @@ -1,7 +1,7 @@ +#include "ChatWebApi.h" + #include -#include -#include "ChatWebApi.h" #include "dCommonVars.h" #include "eConnectionType.h" #include "eChatMessageType.h" @@ -9,8 +9,8 @@ #include "dServer.h" #include "PlayerContainer.h" #include "dConfig.h" - -using json = nlohmann::json; +#include "httplib.h" +#include "json.h" namespace { httplib::Server m_APIServer; @@ -86,8 +86,6 @@ void ChatWebApi::Listen(const uint32_t port) { }; void ChatWebApi::Stop(){ - if (Game::config->GetValue("enable_chat_web_api") == "1") { - LOG("Stopping Chat Web API server..."); - m_APIServer.stop(); - } + LOG("Stopping Chat Web API server..."); + m_APIServer.stop(); } diff --git a/dChatServer/ChatWebApi.h b/dChatServer/ChatWebApi.h index 420a40876..5b3c68f88 100644 --- a/dChatServer/ChatWebApi.h +++ b/dChatServer/ChatWebApi.h @@ -1,4 +1,4 @@ -#include "httplib.h" +#include namespace ChatWebApi { void Listen(const uint32_t port); diff --git a/dChatServer/PlayerContainer.cpp b/dChatServer/PlayerContainer.cpp index 443f38a04..be1225428 100644 --- a/dChatServer/PlayerContainer.cpp +++ b/dChatServer/PlayerContainer.cpp @@ -1,7 +1,8 @@ +#include "PlayerContainer.h" + #include #include -#include "PlayerContainer.h" #include "dNetCommon.h" #include "Game.h" #include "Logger.h" @@ -14,7 +15,6 @@ #include "dConfig.h" #include "eChatMessageType.h" - const json PlayerData::to_json() const { json data; data["id"] = this->playerID; diff --git a/dChatServer/PlayerContainer.h b/dChatServer/PlayerContainer.h index 8a0948567..55fb29976 100644 --- a/dChatServer/PlayerContainer.h +++ b/dChatServer/PlayerContainer.h @@ -6,9 +6,7 @@ #include "Game.h" #include "dServer.h" #include - -#include -using json = nlohmann::json; +#include "json.h" enum class eGameMasterLevel : uint8_t; diff --git a/dCommon/json.h b/dCommon/json.h new file mode 100644 index 000000000..0ce06dc78 --- /dev/null +++ b/dCommon/json.h @@ -0,0 +1,10 @@ +#ifndef __JSON__H__ +#define __JSON__H__ + +#include "nlohmann/json.hpp" + +namespace { + using json = nlohmann::json; +} + +#endif // !__JSON__H__ \ No newline at end of file diff --git a/docs/ChatWebAPI.yaml b/docs/ChatWebAPI.yaml index 815ddd16e..9c070cbbb 100644 --- a/docs/ChatWebAPI.yaml +++ b/docs/ChatWebAPI.yaml @@ -48,6 +48,7 @@ paths: get: tags: - user + summary: Get all online Players responses: "200": description: Successful operation @@ -64,6 +65,7 @@ paths: get: tags: - user + summary: Get all active Teams responses: "200": description: Successful operation From 214626222cb055d48d29289a0f1c28775a588440 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Fri, 26 Apr 2024 06:17:54 -0500 Subject: [PATCH 12/12] address feedback --- dChatServer/ChatWebApi.cpp | 4 +++- dChatServer/PlayerContainer.cpp | 4 +++- dChatServer/PlayerContainer.h | 4 ++-- dCommon/json.h | 10 ---------- 4 files changed, 8 insertions(+), 14 deletions(-) delete mode 100644 dCommon/json.h diff --git a/dChatServer/ChatWebApi.cpp b/dChatServer/ChatWebApi.cpp index 6b864d45d..d2642f1f6 100644 --- a/dChatServer/ChatWebApi.cpp +++ b/dChatServer/ChatWebApi.cpp @@ -10,7 +10,9 @@ #include "PlayerContainer.h" #include "dConfig.h" #include "httplib.h" -#include "json.h" +#include "nlohmann/json.hpp" + +using json = nlohmann::json; namespace { httplib::Server m_APIServer; diff --git a/dChatServer/PlayerContainer.cpp b/dChatServer/PlayerContainer.cpp index be1225428..cffb5c7eb 100644 --- a/dChatServer/PlayerContainer.cpp +++ b/dChatServer/PlayerContainer.cpp @@ -13,7 +13,9 @@ #include "eConnectionType.h" #include "ChatPackets.h" #include "dConfig.h" -#include "eChatMessageType.h" +#include "eChatMessageType.h" + +using json = nlohmann::json; const json PlayerData::to_json() const { json data; diff --git a/dChatServer/PlayerContainer.h b/dChatServer/PlayerContainer.h index 55fb29976..2bd675a27 100644 --- a/dChatServer/PlayerContainer.h +++ b/dChatServer/PlayerContainer.h @@ -6,7 +6,7 @@ #include "Game.h" #include "dServer.h" #include -#include "json.h" +#include "nlohmann/json.hpp" enum class eGameMasterLevel : uint8_t; @@ -37,7 +37,7 @@ struct PlayerData { return muteExpire == 1 || muteExpire > time(NULL); } - const json to_json() const; + const nlohmann::json to_json() const; SystemAddress sysAddr{}; LWOZONEID zoneID{}; diff --git a/dCommon/json.h b/dCommon/json.h deleted file mode 100644 index 0ce06dc78..000000000 --- a/dCommon/json.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __JSON__H__ -#define __JSON__H__ - -#include "nlohmann/json.hpp" - -namespace { - using json = nlohmann::json; -} - -#endif // !__JSON__H__ \ No newline at end of file