mirror of
				https://github.com/DarkflameUniverse/DarkflameServer.git
				synced 2025-11-04 06:32:00 +00:00 
			
		
		
		
	WIP
Only works on linux, get rekt
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
set(DCHATSERVER_SOURCES
 | 
			
		||||
	"ChatHttpApi.cpp"
 | 
			
		||||
	"ChatIgnoreList.cpp"
 | 
			
		||||
	"ChatPacketHandler.cpp"
 | 
			
		||||
	"PlayerContainer.cpp"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										62
									
								
								dChatServer/ChatHttpApi.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								dChatServer/ChatHttpApi.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <nlohmann/json.hpp>
 | 
			
		||||
 | 
			
		||||
#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
									
								
								dChatServer/ChatHttpApi.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								dChatServer/ChatHttpApi.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
#include "httplib.h"
 | 
			
		||||
 | 
			
		||||
namespace ChatHttpApi {
 | 
			
		||||
	void Listen(const uint32_t port);
 | 
			
		||||
	void Stop();
 | 
			
		||||
};
 | 
			
		||||
@@ -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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user