Delta-v/Content.Server/GameTicking/Rules/SecretRuleSystem.cs

202 lines
7.4 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Presets;
using Content.Server.GameTicking.Rules.Components;
using Content.Shared._DV.CCVars; // DeltaV
using Content.Shared.GameTicking.Components;
using Content.Shared.Random;
using Content.Shared.CCVar;
using Content.Shared.Database;
using Robust.Server.Player; // DeltaV
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Configuration;
using Robust.Shared.Utility;
namespace Content.Server.GameTicking.Rules;
public sealed class SecretRuleSystem : GameRuleSystem<SecretRuleComponent>
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPlayerManager _player = default!; // DeltaV
[Dependency] private readonly GameTicker _ticker = default!; // begin Imp
// Dictionary that contains the minimum round number for certain preset
// prototypes to be allowed to roll again
private static Dictionary<ProtoId<GamePresetPrototype>, int> _nextRoundAllowed = new(); // end Imp
private string _ruleCompName = default!;
public override void Initialize()
{
base.Initialize();
_ruleCompName = Factory.GetComponentName<GameRuleComponent>();
}
protected override void Added(EntityUid uid, SecretRuleComponent component, GameRuleComponent gameRule, GameRuleAddedEvent args)
{
base.Added(uid, component, gameRule, args);
var weights = _configurationManager.GetCVar(CCVars.SecretWeightPrototype);
if (!TryPickPreset(weights, out var preset))
{
Log.Error($"{ToPrettyString(uid)} failed to pick any preset. Removing rule.");
Del(uid);
return;
}
Log.Info($"Selected {preset.ID} as the secret preset.");
_adminLogger.Add(LogType.EventStarted, $"Selected {preset.ID} as the secret preset.");
if (_configurationManager.GetCVar(DCCVars.EnableBacktoBack) == true) // DeltaV
{
if (preset.Cooldown > 0) // Begin Imp
{
_nextRoundAllowed[preset.ID] = _ticker.RoundId + preset.Cooldown + 1;
Log.Info($"{preset.ID} is now on cooldown until {_nextRoundAllowed[preset.ID]}");
} // End Imp
} // DeltaV
foreach (var rule in preset.Rules)
{
EntityUid ruleEnt;
// if we're pre-round (i.e. will only be added)
// then just add rules. if we're added in the middle of the round (or at any other point really)
// then we want to start them as well
if (GameTicker.RunLevel <= GameRunLevel.InRound)
ruleEnt = GameTicker.AddGameRule(rule);
else
GameTicker.StartGameRule(rule, out ruleEnt);
component.AdditionalGameRules.Add(ruleEnt);
}
}
protected override void Ended(EntityUid uid, SecretRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
{
base.Ended(uid, component, gameRule, args);
foreach (var rule in component.AdditionalGameRules)
{
GameTicker.EndGameRule(rule);
}
}
private bool TryPickPreset(ProtoId<WeightedRandomPrototype> weights, [NotNullWhen(true)] out GamePresetPrototype? preset)
{
var options = _prototypeManager.Index(weights).Weights.ShallowClone();
var players = GameTicker.ReadyPlayerCount();
var totalPlayers = _player.PlayerCount; //DeltaV
GamePresetPrototype? selectedPreset = null;
var sum = options.Values.Sum();
while (options.Count > 0)
{
var accumulated = 0f;
var rand = _random.NextFloat(sum);
foreach (var (key, weight) in options)
{
accumulated += weight;
if (accumulated < rand)
continue;
if (!_prototypeManager.TryIndex(key, out selectedPreset))
Log.Error($"Invalid preset {selectedPreset} in secret rule weights: {weights}");
options.Remove(key);
sum -= weight;
break;
}
if (CanPick(selectedPreset, players, totalPlayers)) // DeltaV - total playercount
{
preset = selectedPreset;
return true;
}
if (selectedPreset != null)
Log.Info($"Excluding {selectedPreset.ID} from secret preset selection.");
}
preset = null;
return false;
}
public bool CanPickAny()
{
var secretPresetId = _configurationManager.GetCVar(CCVars.SecretWeightPrototype);
return CanPickAny(secretPresetId);
}
/// <summary>
/// Can any of the given presets be picked, taking into account the currently available player count?
/// </summary>
public bool CanPickAny(ProtoId<WeightedRandomPrototype> weightedPresets)
{
var ids = _prototypeManager.Index(weightedPresets).Weights.Keys
.Select(x => new ProtoId<GamePresetPrototype>(x));
return CanPickAny(ids);
}
/// <summary>
/// Can any of the given presets be picked, taking into account the currently available player count?
/// </summary>
public bool CanPickAny(IEnumerable<ProtoId<GamePresetPrototype>> protos)
{
var players = GameTicker.ReadyPlayerCount();
var totalPlayers = _player.PlayerCount; //DeltaV
foreach (var id in protos)
{
if (!_prototypeManager.TryIndex(id, out var selectedPreset))
Log.Error($"Invalid preset {selectedPreset} in secret rule weights: {id}");
if (CanPick(selectedPreset, players, totalPlayers)) // DeltaV - total playercount
return true;
}
return false;
}
/// <summary>
/// Can the given preset be picked, taking into account the currently available player count?
/// </summary>
private bool CanPick([NotNullWhen(true)] GamePresetPrototype? selected, int players, int totalPlayers) // DeltaV - respect total playercount
{
if (selected == null)
return false;
foreach (var ruleId in selected.Rules)
{
if (!_prototypeManager.TryIndex(ruleId, out EntityPrototype? rule)
|| !rule.TryGetComponent(_ruleCompName, out GameRuleComponent? ruleComp))
{
Log.Error($"Encountered invalid rule {ruleId} in preset {selected.ID}");
return false;
}
if (ruleComp.MinPlayers > players && ruleComp.CancelPresetOnTooFewPlayers)
return false;
if (ruleComp.MinTotalPlayers > totalPlayers) return false; // DeltaV
}
if (_configurationManager.GetCVar(DCCVars.EnableBacktoBack) == true) // DeltaV
{
if (_nextRoundAllowed.ContainsKey(selected.ID) && _nextRoundAllowed[selected.ID] > _ticker.RoundId) // Begin Imp
{
Log.Info($"Skipping preset {selected.ID} (Not available until round {_nextRoundAllowed[selected.ID]}");
return false;
} // End Imp
} // DeltaV
return true;
}
}