From 0c07ca8ef6f2781cff630e27277ab66967cab305 Mon Sep 17 00:00:00 2001 From: Jessica James Date: Sun, 2 Feb 2020 23:39:12 -0600 Subject: [PATCH] Added `RenX::Server::activePlayers()` Adjusted ServerList code to use activePlayers where appropriate Removed legacy in-game server list code --- src/Plugins/RenX/RenX.Core/RenX_Server.cpp | 15 ++ src/Plugins/RenX/RenX.Core/RenX_Server.h | 8 ++ .../RenX/RenX.ServerList/RenX_ServerList.cpp | 132 ++---------------- .../RenX/RenX.ServerList/RenX_ServerList.h | 6 +- 4 files changed, 38 insertions(+), 123 deletions(-) diff --git a/src/Plugins/RenX/RenX.Core/RenX_Server.cpp b/src/Plugins/RenX/RenX.Core/RenX_Server.cpp index 18b0650..5aeac97 100644 --- a/src/Plugins/RenX/RenX.Core/RenX_Server.cpp +++ b/src/Plugins/RenX/RenX.Core/RenX_Server.cpp @@ -394,6 +394,21 @@ size_t RenX::Server::getBotCount() const //return RenX::Server::bot_count; } +std::vector RenX::Server::activePlayers(bool includeBots) const { + std::vector result; + result.reserve(players.size()); + + // Build list of active players + for (auto& player : players) { + // Filter teamless players and bots (if applicable) + if (player.team != TeamType::None && (includeBots || !player.isBot)) { + result.push_back(&player); + } + } + + return result; +} + RenX::PlayerInfo *RenX::Server::getPlayer(int id) const { for (auto node = this->players.begin(); node != this->players.end(); ++node) diff --git a/src/Plugins/RenX/RenX.Core/RenX_Server.h b/src/Plugins/RenX/RenX.Core/RenX_Server.h index a127555..23a186c 100644 --- a/src/Plugins/RenX/RenX.Core/RenX_Server.h +++ b/src/Plugins/RenX/RenX.Core/RenX_Server.h @@ -348,6 +348,14 @@ namespace RenX */ size_t getBotCount() const; + /** + * @brief Builds a list of all active players (i.e: players who have a team) + * + * @param includeBots Specifies whether or not to include bots in the returned list + * @return List of active players + */ + std::vector activePlayers(bool includeBots = true) const; + /** * @brief Fetches a player's data based on their ID number. * diff --git a/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.cpp b/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.cpp index 45e8fa3..60a7abb 100644 --- a/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.cpp +++ b/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.cpp @@ -150,7 +150,6 @@ bool RenX_ServerListPlugin::initialize() RenX_ServerListPlugin::server_list_page_name = this->config.get("ServersPageName"_jrs, "servers.jsp"_jrs); RenX_ServerListPlugin::server_list_long_page_name = this->config.get("HumanServersPageName"_jrs, "servers_long.jsp"_jrs); RenX_ServerListPlugin::server_page_name = this->config.get("ServerPageName"_jrs, "server.jsp"_jrs); - RenX_ServerListPlugin::game_server_list_page_name = this->config.get("ServersGamePageName"_jrs, "browser.jsp"_jrs); /** Initialize content */ Jupiter::HTTP::Server &server = getHTTPServer(); @@ -179,14 +178,6 @@ bool RenX_ServerListPlugin::initialize() content->free_result = true; server.hook(RenX_ServerListPlugin::web_hostname, RenX_ServerListPlugin::web_path, content); - // Game server list page - content = new Jupiter::HTTP::Server::Content(RenX_ServerListPlugin::game_server_list_page_name, handle_game_server_list_page); - content->language = &Jupiter::HTTP::Content::Language::ENGLISH; - content->type = &Jupiter::HTTP::Content::Type::Text::HTML; - content->charset = &Jupiter::HTTP::Content::Type::Text::Charset::ASCII; - content->free_result = false; - server.hook(RenX_ServerListPlugin::web_hostname, RenX_ServerListPlugin::web_path, content); - this->updateServerList(); return true; } @@ -197,7 +188,6 @@ RenX_ServerListPlugin::~RenX_ServerListPlugin() server.remove(RenX_ServerListPlugin::web_hostname, RenX_ServerListPlugin::web_path, RenX_ServerListPlugin::server_list_page_name); server.remove(RenX_ServerListPlugin::web_hostname, RenX_ServerListPlugin::web_path, RenX_ServerListPlugin::server_list_long_page_name); server.remove(RenX_ServerListPlugin::web_hostname, RenX_ServerListPlugin::web_path, RenX_ServerListPlugin::server_page_name); - server.remove(RenX_ServerListPlugin::web_hostname, RenX_ServerListPlugin::web_path, RenX_ServerListPlugin::game_server_list_page_name); } Jupiter::ReadableString *RenX_ServerListPlugin::getServerListJSON() @@ -205,11 +195,6 @@ Jupiter::ReadableString *RenX_ServerListPlugin::getServerListJSON() return std::addressof(RenX_ServerListPlugin::server_list_json); } -Jupiter::ReadableString *RenX_ServerListPlugin::getServerListGame() -{ - return std::addressof(RenX_ServerListPlugin::server_list_game); -} - constexpr const char *json_bool_as_cstring(bool in) { return in ? "true" : "false"; @@ -227,7 +212,7 @@ Jupiter::StringS server_as_json(const RenX::Server &server) server_name.size(), server_name.ptr(), server_map.size(), server_map.ptr(), server.getBotCount(), - server.players.size() - server.getBotCount(), + server.activePlayers(false).size(), server_version.size(), server_version.ptr(), server.getMineLimit(), json_bool_as_cstring(server.isSteamRequired()), @@ -251,64 +236,6 @@ Jupiter::StringS server_as_json(const RenX::Server &server) return server_json_block; } -Jupiter::StringS server_as_game(const RenX::Server &server) -{ - Jupiter::String server_game_block(256); - - Jupiter::String server_name = sanitize_game(server.getName()); - Jupiter::String server_map = sanitize_game(server.getMap().name); - Jupiter::String server_version = sanitize_game(server.getGameVersion()); - Jupiter::String server_levels; - - RenX::Map *map; - - if (server.maps.size() != 0) - { - map = server.maps.get(0); - server_levels = sanitize_game(RenX::formatGUID(*map)); - server_levels += '='; - server_levels += sanitize_game(map->name); - - for (size_t index = 1; index != server.maps.size(); ++index) - { - map = server.maps.get(index); - - server_levels += ';'; - server_levels += sanitize_game(RenX::formatGUID(*map)); - server_levels += '='; - server_levels += sanitize_game(map->name); - } - } - - server_game_block.format("\n<@>%.*s~%.*s~%u~%s~%.*s~" "%d;%d;%d;%s;%d;%d;%d;%s;%s;%s;%.*s;%s" "~%u~%d~%s~%s~%.*s", - server_name.size(), server_name.ptr(), - server.getSocketHostname().size(), server.getSocketHostname().c_str(), - server.getPort(), - json_bool_as_cstring(server.isPassworded()), - server_map.size(), server_map.ptr(), - //START OPTIONS - server.getPlayerLimit(), - server.getVehicleLimit(), - server.getMineLimit(), - json_bool_as_cstring(server.isCratesEnabled()), - server.getGameType(), - server.getTeamMode(), - server.getTimeLimit(), - json_bool_as_cstring(server.isPrivateMessagingEnabled()), - json_bool_as_cstring(server.isPrivateMessageTeamOnly()), - json_bool_as_cstring(server.isSteamRequired()), - server_version.size(), server_version.ptr(), - json_bool_as_cstring(server.isBotsEnabled()), - //END OPTIONS - server.players.size() - server.getBotCount(), - server.getPlayerLimit(), - json_bool_as_cstring(server.isRanked()), - json_bool_as_cstring(server.isMatchInProgress()), - server_levels.size(), server_levels.ptr()); - - return server_game_block; -} - Jupiter::StringS server_as_long_json(const RenX::Server &server) { Jupiter::String server_json_block(128); @@ -316,6 +243,7 @@ Jupiter::StringS server_as_long_json(const RenX::Server &server) Jupiter::String server_name = jsonify(server.getName()); Jupiter::String server_map = jsonify(server.getMap().name); Jupiter::String server_version = jsonify(server.getGameVersion()); + std::vector activePlayers = server.activePlayers(false); server_json_block.format(R"json({ "Name": "%.*s", @@ -343,7 +271,7 @@ Jupiter::StringS server_as_long_json(const RenX::Server &server) server_name.size(), server_name.ptr(), server_map.size(), server_map.ptr(), server.getBotCount(), - server.players.size() - server.getBotCount(), + activePlayers.size(), server_version.size(), server_version.ptr(), server.getMineLimit(), @@ -406,33 +334,24 @@ Jupiter::StringS server_as_long_json(const RenX::Server &server) } // Player List - if (server.players.size() != 0 && server.players.size() != server.getBotCount()) + if (activePlayers.size() != 0) { server_json_block += ",\n\t\t\"PlayerList\": ["_jrs; - auto node = server.players.begin(); - - while (node != server.players.end()) - { - if (node->isBot == false) - { - server_json_block += "\n\t\t\t{\n\t\t\t\t\"Name\": \""_jrs; - server_json_block += jsonify(node->name); - server_json_block += "\"\n\t\t\t}"_jrs; - - ++node; - break; - } + auto node = activePlayers.begin(); - ++node; - } + // Add first player to JSON + server_json_block += "\n\t\t\t{\n\t\t\t\t\"Name\": \""_jrs; + server_json_block += jsonify((*node)->name); + server_json_block += "\"\n\t\t\t}"_jrs; + ++node; - while (node != server.players.end()) + // Add remaining players to JSON + while (node != activePlayers.end()) { server_json_block += ",\n\t\t\t{\n\t\t\t\t\"Name\": \""_jrs; - server_json_block += jsonify(node->name); + server_json_block += jsonify((*node)->name); server_json_block += "\"\n\t\t\t}"_jrs; - ++node; } @@ -464,21 +383,6 @@ void RenX_ServerListPlugin::addServerToServerList(RenX::Server &server) RenX_ServerListPlugin::server_list_json += ']'; } - // append to server_list_game - - if (RenX_ServerListPlugin::server_list_game.isEmpty()) - { - RenX_ServerListPlugin::server_list_game = server_list_game_header; - RenX_ServerListPlugin::server_list_game += server_as_game(server); - RenX_ServerListPlugin::server_list_game += server_list_game_footer; - } - else - { - RenX_ServerListPlugin::server_list_game.truncate(server_list_game_footer.size()); // remove trailing "" - RenX_ServerListPlugin::server_list_game += server_as_game(server); - RenX_ServerListPlugin::server_list_game += server_list_game_footer; - } - // add to individual listing server_json_block = '{'; @@ -576,7 +480,6 @@ void RenX_ServerListPlugin::updateServerList() // regenerate server_list_json and server_list_Game RenX_ServerListPlugin::server_list_json = '['; - RenX_ServerListPlugin::server_list_game = server_list_game_header; while (index != servers.size()) { @@ -584,7 +487,6 @@ void RenX_ServerListPlugin::updateServerList() if (server->isConnected() && server->isFullyConnected()) { RenX_ServerListPlugin::server_list_json += server_as_json(*server); - RenX_ServerListPlugin::server_list_game += server_as_game(*server); ++index; break; @@ -598,14 +500,11 @@ void RenX_ServerListPlugin::updateServerList() { RenX_ServerListPlugin::server_list_json += ','; RenX_ServerListPlugin::server_list_json += server_as_json(*server); - - RenX_ServerListPlugin::server_list_game += server_as_game(*server); } ++index; } RenX_ServerListPlugin::server_list_json += ']'; - RenX_ServerListPlugin::server_list_game += server_list_game_footer; } void RenX_ServerListPlugin::RenX_OnServerFullyConnected(RenX::Server &server) @@ -722,11 +621,6 @@ Jupiter::ReadableString *handle_server_page(const Jupiter::ReadableString &query return new Jupiter::ReferenceString(server->varData[pluginInstance.getName()].get("j"_jrs)); } -Jupiter::ReadableString *handle_game_server_list_page(const Jupiter::ReadableString &query_string) -{ - return pluginInstance.getServerListGame(); -} - extern "C" JUPITER_EXPORT Jupiter::Plugin *getPlugin() { return &pluginInstance; diff --git a/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.h b/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.h index e9d5903..bea983c 100644 --- a/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.h +++ b/src/Plugins/RenX/RenX.ServerList/RenX_ServerList.h @@ -27,7 +27,6 @@ class RenX_ServerListPlugin : public RenX::Plugin { public: // RenX_ServerListPlugin Jupiter::ReadableString *getServerListJSON(); - Jupiter::ReadableString *getServerListGame(); void addServerToServerList(RenX::Server &server); void updateServerList(); @@ -43,13 +42,12 @@ public: // RenX::Plugin void RenX_OnMapLoad(RenX::Server &server, const Jupiter::ReadableString &map) override; private: - Jupiter::StringS server_list_json, server_list_game; - Jupiter::StringS web_hostname, web_path, server_list_page_name, server_list_long_page_name, server_page_name, game_server_list_page_name; + Jupiter::StringS server_list_json; + Jupiter::StringS web_hostname, web_path, server_list_page_name, server_list_long_page_name, server_page_name; }; Jupiter::ReadableString *handle_server_list_page(const Jupiter::ReadableString &); Jupiter::ReadableString *handle_server_list_long_page(const Jupiter::ReadableString &); Jupiter::ReadableString *handle_server_page(const Jupiter::ReadableString &query_string); -Jupiter::ReadableString *handle_game_server_list_page(const Jupiter::ReadableString &); #endif // _RENX_SERVERLIST_H_HEADER