Merge pull request #146 from diamante0018/feature/random-sv-map-rotation

[Dedicated] Add random map rotation
This commit is contained in:
Dss0 2021-11-22 22:20:33 +01:00 committed by GitHub
commit 1088e760d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 6 deletions

View File

@ -3,6 +3,7 @@
namespace Components namespace Components
{ {
SteamID Dedicated::PlayerGuids[18][2]; SteamID Dedicated::PlayerGuids[18][2];
Dvar::Var Dedicated::SVRandomMapRotation;
bool Dedicated::IsEnabled() bool Dedicated::IsEnabled()
{ {
@ -119,6 +120,41 @@ namespace Components
Game::Com_Error(code, message); Game::Com_Error(code, message);
} }
void Dedicated::RandomizeMapRotation()
{
auto rotation = Dvar::Var("sv_mapRotation").get<std::string>();
const auto tokens = Utils::String::Explode(rotation, ' ');
std::vector<std::pair<std::string, std::string>> mapRotationPair;
for (auto i = 0u; i < (tokens.size() - 1); i += 2)
{
if (i + 1 >= tokens.size()) break;
const auto& key = tokens[i];
const auto& value = tokens[i + 1];
mapRotationPair.push_back(std::make_pair(key, value));
}
const auto seed = Utils::Cryptography::Rand::GenerateInt();
std::shuffle(std::begin(mapRotationPair), std::end(mapRotationPair), std::default_random_engine(seed));
// Rebuild map rotation using the randomized key/values
rotation.clear();
for (auto j = 0u; j < mapRotationPair.size(); j++)
{
const auto& pair = mapRotationPair[j];
rotation.append(pair.first);
rotation.append(" ");
rotation.append(pair.second);
if (j != mapRotationPair.size() - 1)
rotation.append(" ");
}
Dvar::Var("sv_mapRotationCurrent").set(rotation);
}
void Dedicated::MapRotate() void Dedicated::MapRotate()
{ {
if (!Dedicated::IsEnabled() && Dvar::Var("sv_dontrotate").get<bool>()) if (!Dedicated::IsEnabled() && Dvar::Var("sv_dontrotate").get<bool>())
@ -134,9 +170,10 @@ namespace Components
} }
Logger::Print("Rotating map...\n"); Logger::Print("Rotating map...\n");
const auto mapRotation = Dvar::Var("sv_mapRotation").get<std::string>();
// if nothing, just restart // if nothing, just restart
if (Dvar::Var("sv_mapRotation").get<std::string>().empty()) if (mapRotation.empty())
{ {
Logger::Print("No rotation defined, restarting map.\n"); Logger::Print("No rotation defined, restarting map.\n");
@ -152,14 +189,23 @@ namespace Components
return; return;
} }
// first, check if the string contains nothing // First, check if the string contains nothing
if (Dvar::Var("sv_mapRotationCurrent").get<std::string>().empty()) if (Dvar::Var("sv_mapRotationCurrent").get<std::string>().empty())
{ {
Logger::Print("Current map rotation has finished, reloading...\n"); Logger::Print("Current map rotation has finished, reloading...\n");
Dvar::Var("sv_mapRotationCurrent").set(Dvar::Var("sv_mapRotation").get<const char*>());
if (Dedicated::SVRandomMapRotation.get<bool>())
{
Logger::Print("Randomizing map rotation\n");
Dedicated::RandomizeMapRotation();
}
else
{
Dvar::Var("sv_mapRotationCurrent").set(mapRotation);
}
} }
std::string rotation = Dvar::Var("sv_mapRotationCurrent").get<std::string>(); auto rotation = Dvar::Var("sv_mapRotationCurrent").get<std::string>();
auto tokens = Utils::String::Explode(rotation, ' '); auto tokens = Utils::String::Explode(rotation, ' ');
@ -346,6 +392,7 @@ namespace Components
Dvar::OnInit([]() Dvar::OnInit([]()
{ {
Dedicated::SVRandomMapRotation = Dvar::Register<bool>("sv_randomMapRotation", false, Game::dvar_flag::DVAR_FLAG_SAVED, "Randomize map rotation when true");
Dvar::Register<const char*>("sv_sayName", "^7Console", Game::dvar_flag::DVAR_FLAG_NONE, "The name to pose as for 'say' commands"); Dvar::Register<const char*>("sv_sayName", "^7Console", Game::dvar_flag::DVAR_FLAG_NONE, "The name to pose as for 'say' commands");
Dvar::Register<const char*>("sv_motd", "", Game::dvar_flag::DVAR_FLAG_NONE, "A custom message of the day for servers"); Dvar::Register<const char*>("sv_motd", "", Game::dvar_flag::DVAR_FLAG_NONE, "A custom message of the day for servers");

View File

@ -15,6 +15,9 @@ namespace Components
static void Heartbeat(); static void Heartbeat();
private: private:
static Dvar::Var SVRandomMapRotation;
static void RandomizeMapRotation();
static void MapRotate(); static void MapRotate();
static void InitDedicatedServer(); static void InitDedicatedServer();

View File

@ -42,10 +42,9 @@
#include <algorithm> #include <algorithm>
#include <limits> #include <limits>
#include <cmath> #include <cmath>
// Experimental C++17 features
#include <filesystem> #include <filesystem>
#include <optional> #include <optional>
#include <random>
#pragma warning(pop) #pragma warning(pop)