mirror of
https://github.com/alterware/master-server.git
synced 2025-09-04 07:17:26 +00:00
maint(stats_handler): refactor code (#94)
This commit is contained in:
@@ -41,7 +41,7 @@ void elimination_handler::run_frame()
|
||||
if (server.game == game_type::t7 && server.protocol < T7_PROTOCOL)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
console::log("Removing T7 server '%s' because they are using an outdated protocol (%i)", context.get_address().to_string().data(), server.protocol);
|
||||
console::log("Removing T7 server '%s' because they are using an outdated protocol (%i)", context.get_address().to_string().c_str(), server.protocol);
|
||||
#endif
|
||||
context.remove();
|
||||
return;
|
||||
@@ -50,7 +50,7 @@ void elimination_handler::run_frame()
|
||||
++server_count[server.game][context.get_address().to_string(false)];
|
||||
if (server_count[server.game][context.get_address().to_string(false)] > MAX_SERVERS_PER_GAME)
|
||||
{
|
||||
console::log("Removing server '%s' because it exceeds MAX_SERVERS_PER_GAME", context.get_address().to_string().data());
|
||||
console::log("Removing server '%s' because it exceeds MAX_SERVERS_PER_GAME", context.get_address().to_string().c_str());
|
||||
context.remove();
|
||||
return;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ void elimination_handler::run_frame()
|
||||
{
|
||||
if (const auto pos = name.find(entry); pos != std::string::npos)
|
||||
{
|
||||
console::log("Removing server '%s' (%s) because it contains a bad name", server.name.data(), context.get_address().to_string().data());
|
||||
console::log("Removing server '%s' (%s) because it contains a bad name", server.name.c_str(), context.get_address().to_string().c_str());
|
||||
context.remove();
|
||||
return;
|
||||
}
|
||||
|
@@ -154,5 +154,5 @@ void getbots_command::handle_command(const network::address& target, const std::
|
||||
}
|
||||
|
||||
this->get_server().send(target, "getbotsResponse", stream.str());
|
||||
console::log("Sent bot names: %s", target.to_string().data());
|
||||
console::log("Sent bot names to: %s", target.to_string().c_str());
|
||||
}
|
||||
|
@@ -36,9 +36,9 @@ void getservers_command::handle_command(const network::address& target, const st
|
||||
|
||||
const auto& game = params[0];
|
||||
|
||||
const auto* p = params[1].data();
|
||||
const auto* p = params[1].c_str();
|
||||
char* end;
|
||||
const auto protocol = strtol(params[1].data(), &end, 10);
|
||||
const auto protocol = strtol(params[1].c_str(), &end, 10);
|
||||
if (p == end)
|
||||
{
|
||||
throw execution_exception("Invalid protocol");
|
||||
@@ -92,5 +92,5 @@ void getservers_command::handle_command(const network::address& target, const st
|
||||
}
|
||||
}
|
||||
|
||||
console::log("Sent %zu servers in %zu parts for game %s:\t%s", prepared_servers.size(), packet_count, game.data(), target.to_string().data());
|
||||
console::log("Sent %zu servers in %zu parts for game %s:\t%s", prepared_servers.size(), packet_count, game.c_str(), target.to_string().c_str());
|
||||
}
|
||||
|
@@ -35,22 +35,22 @@ void info_response_command::handle_command(const network::address& target, const
|
||||
throw execution_exception{"Invalid challenge"};
|
||||
}
|
||||
|
||||
const auto player_count = atoi(info.get("clients").data());
|
||||
const auto bot_count = atoi(info.get("bots").data());
|
||||
const auto player_count = atoi(info.get("clients").c_str());
|
||||
const auto bot_count = atoi(info.get("bots").c_str());
|
||||
auto real_player_count = player_count - bot_count;
|
||||
real_player_count = std::clamp(real_player_count, 0, 18);
|
||||
|
||||
server.registered = true;
|
||||
server.game = game_type;
|
||||
server.state = game_server::state::can_ping;
|
||||
server.protocol = atoi(info.get("protocol").data());
|
||||
server.protocol = atoi(info.get("protocol").c_str());
|
||||
server.clients = static_cast<unsigned int>(real_player_count);
|
||||
server.name = info.get("hostname");
|
||||
server.heartbeat = std::chrono::high_resolution_clock::now();
|
||||
server.info_string = std::move(info);
|
||||
|
||||
console::log("Server registered for game %s (%i):\t%s\t- %s", game.data(), server.protocol,
|
||||
address.to_string().data(), server.name.data());
|
||||
console::log("Server registered for game %s (%i):\t%s\t- %s", game.c_str(), server.protocol,
|
||||
address.to_string().c_str(), server.name.c_str());
|
||||
});
|
||||
|
||||
if (!found)
|
||||
|
@@ -33,7 +33,7 @@ void kill_list::add_to_kill_list(const kill_list_entry& add)
|
||||
const auto existing_entry = entries.find(add.ip_address_);
|
||||
if (existing_entry == entries.end() || existing_entry->second.reason_ != add.reason_)
|
||||
{
|
||||
console::info("Added %s to kill list (reason: %s)", add.ip_address_.data(), add.reason_.data());
|
||||
console::info("Added %s to kill list (reason: %s)", add.ip_address_.c_str(), add.reason_.c_str());
|
||||
entries[add.ip_address_] = add;
|
||||
return true;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ void kill_list::add_to_kill_list(const kill_list_entry& add)
|
||||
|
||||
if (!any_change)
|
||||
{
|
||||
console::info("%s already in kill list, doing nothing", add.ip_address_.data());
|
||||
console::info("%s already in kill list, doing nothing", add.ip_address_.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ void kill_list::remove_from_kill_list(const std::string& remove)
|
||||
{
|
||||
if (entries.erase(remove))
|
||||
{
|
||||
console::info("Removed %s from kill list", remove.data());
|
||||
console::info("Removed %s from kill list", remove.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ void kill_list::remove_from_kill_list(const std::string& remove)
|
||||
|
||||
if (!any_change)
|
||||
{
|
||||
console::info("%s not in kill list, doing nothing", remove.data());
|
||||
console::info("%s not in kill list, doing nothing", remove.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
#include "statistics_handler.hpp"
|
||||
#include "../console.hpp"
|
||||
|
||||
#include <utils/env.hpp>
|
||||
#include <utils/io.hpp>
|
||||
#include <utils/string.hpp>
|
||||
|
||||
@@ -37,7 +38,14 @@ namespace
|
||||
root.Accept(root_writer);
|
||||
|
||||
std::string root_data(root_buffer.GetString(), root_buffer.GetLength());
|
||||
utils::io::write_file("/var/www/server.alterware.dev/html/stats.json", root_data);
|
||||
const auto location = utils::env::get_value("AW_STATS_LOCATION");
|
||||
if (location.empty())
|
||||
{
|
||||
console::error("The environment variable 'AW_STATS_LOCATION' is not set. Please set 'AW_STATS_LOCATION' to specify the save location for 'stats.json'\n");
|
||||
return;
|
||||
}
|
||||
|
||||
utils::io::write_file(location, root_data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,12 +81,12 @@ void statistics_handler::run_frame()
|
||||
|
||||
for (const auto& game_servers : servers)
|
||||
{
|
||||
console::log("%s (%d):", resolve_game_type_name(game_servers.first).data(),
|
||||
console::log("%s (%d):", resolve_game_type_name(game_servers.first).c_str(),
|
||||
static_cast<uint32_t>(game_servers.second.size()));
|
||||
|
||||
for (const auto& server : game_servers.second)
|
||||
{
|
||||
console::log("\t%s\t%s", server.second.to_string().data(), server.first.data());
|
||||
console::log("\t%s\t%s", server.second.to_string().c_str(), server.first.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user