2025-08-06 19:00:30 -05:00

101 lines
3.7 KiB
C++

// Copyright (C) 2007-2021 EQ2Emulator Development Team, GPL v3
#pragma once
#include <sstream>
#include <string>
#include <map>
#include <arpa/inet.h>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "net.hpp"
#include "lworld.hpp"
extern ClientList client_list;
extern LWorldList world_list;
extern NetConnection net;
/**
* Handles web requests for login server status information.
* Generates JSON response containing login status, uptime, client count, and world count.
* @param req HTTP request object containing the incoming request
* @param res HTTP response object to populate with JSON status data
*/
void NetConnection::Web_loginhandle_status(const http::request<http::string_body>& req, http::response<http::string_body>& res)
{
// Set response content type to JSON
res.set(http::field::content_type, "application/json");
boost::property_tree::ptree pt;
// Build status information tree
pt.put("web_status", "online");
pt.put("login_status", net.login_running ? "online" : "offline");
pt.put("login_uptime", (getCurrentTimestamp() - net.login_uptime));
// Convert uptime to human-readable format
auto [days, hours, minutes, seconds] = convertTimestampDuration((getCurrentTimestamp() - net.login_uptime));
std::string uptime_str("Days: " + std::to_string(days) + ", " + "Hours: " + std::to_string(hours) + ", " + "Minutes: " + std::to_string(minutes) + ", " + "Seconds: " + std::to_string(seconds));
pt.put("login_uptime_string", uptime_str);
pt.put("world_count", world_list.GetCount(ConType::World));
pt.put("client_count", net.numclients);
// Serialize to JSON and set response body
std::ostringstream oss;
boost::property_tree::write_json(oss, pt);
std::string json = oss.str();
res.body() = json;
res.prepare_payload();
}
/**
* Handles web requests for world server list information.
* Delegates to LWorldList::PopulateWorldList to generate the response.
* @param req HTTP request object containing the incoming request
* @param res HTTP response object to be populated with world server data
*/
void NetConnection::Web_loginhandle_worlds(const http::request<http::string_body>& req, http::response<http::string_body>& res)
{
world_list.PopulateWorldList(res);
}
/**
* Populates HTTP response with JSON data containing all active world servers.
* Iterates through world map and builds JSON array of world server information
* including ID, name, status, player count, and IP address.
* @param res HTTP response object to populate with world server JSON data
*/
void LWorldList::PopulateWorldList(http::response<http::string_body>& res)
{
struct in_addr in;
res.set(http::field::content_type, "application/json");
boost::property_tree::ptree maintree;
std::ostringstream oss;
// Iterate through all worlds in the world map
std::map<int32,LWorld*>::iterator map_list;
for (map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
LWorld* world = map_list->second;
in.s_addr = world->GetIP();
// Only include World type servers in the response
if (world->GetType() == World) {
boost::property_tree::ptree pt;
pt.put("id", world->GetID());
pt.put("world_name", world->GetName());
pt.put("status", (world->GetStatus() == 1) ? "online" : "offline");
pt.put("num_players", world->GetPlayerNum());
pt.put("ip_addr", inet_ntoa(in)); // Convert IP to string format
maintree.push_back(std::make_pair("", pt));
}
}
// Build final JSON structure and serialize
boost::property_tree::ptree result;
result.add_child("WorldServers", maintree);
boost::property_tree::write_json(oss, result);
std::string json = oss.str();
res.body() = json;
res.prepare_payload();
}