Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Only works on linux, get rekt
  • Loading branch information
aronwk-aaron committed Apr 21, 2024
1 parent 99e7349 commit 55a52b6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions dChatServer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(DCHATSERVER_SOURCES
"ChatHttpApi.cpp"
"ChatIgnoreList.cpp"
"ChatPacketHandler.cpp"
"PlayerContainer.cpp"
Expand Down
62 changes: 62 additions & 0 deletions dChatServer/ChatHttpApi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <cstdint>
#include <nlohmann/json.hpp>

Check failure on line 2 in dChatServer/ChatHttpApi.cpp

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-22.04)

nlohmann/json.hpp: No such file or directory

Check failure on line 2 in dChatServer/ChatHttpApi.cpp

View workflow job for this annotation

GitHub Actions / Build & Test (macos-13)

'nlohmann/json.hpp' file not found

#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<uint32_t>(title.size());
bitStream.Write(title);
bitStream.Write<uint32_t>(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();
}
6 changes: 6 additions & 0 deletions dChatServer/ChatHttpApi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "httplib.h"

namespace ChatHttpApi {
void Listen(const uint32_t port);
void Stop();
};
6 changes: 5 additions & 1 deletion dChatServer/ChatServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "eWorldMessageType.h"
#include "ChatIgnoreList.h"
#include "StringifiedEnum.h"
#include "ChatHttpApi.h"

#include "Game.h"
#include "Server.h"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 55a52b6

Please sign in to comment.