CatStation/Content.Server/GameTicking/Rules/SecretRuleSystem.cs
Princess Cheeseballs 2c6ffecf62
Move GameTicker to Shared (#45834)
* Scope creep my ass

* why was this shit not pushed????

* gone...

* fix that

* review plus buildd

* asfsafsafsafsafas

* grug

* burber

* I'll fix this later

* shrimply die

* also this

* it's snerf or snuthin

* assaads

* round snend

* aaaa

* fix this shit for real

* actually, don't start GameRules until MapInit now that I think aboot it.

* forgot u

* don't actually need this part nullable

* Deffer comp removal

* one liner

* do that too

* chungus

* Honestly makes more sense here, plus WHY are we doing all that shit on "GameRuleAdded" rather than GameRuleStarted(?)

* deferred removal :V

* aaaaaaaaaaaaaaa

* whhop

* Whoop

* better

* landmine that I placed for myself then forgot about lmao

* cleanup

* FUCKING `!`

* gotovo

* amogus

* please don't check by metadata I beg

* waawsasfdfsasfa

* sfafsasfafsas

* fdsaasffsasfa

* code

* fix

* fasasffasfasfsad

* chungaas

* I may be stupid

* gra

* graaaaaaaaaaaaaaaaa

* comment

* gra

* clearing is not predicted and would require major refactors, so don't cache on client.

* lame shit :P

* last holes

* nukeopsfsafsasafsfafsa

* why  was that shit public bruh

* bubrbgwahrghfaka

* dynamic

* revert change that did nothing lmao
2026-09-16 19:34:19 +00:00

166 lines
5.6 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.GameTicking.Rules.Components;
using Content.Shared.GameTicking.Components;
using Content.Shared.Random;
using Content.Shared.CCVar;
using Content.Shared.Database;
using Content.Shared.GameTicking;
using Content.Shared.GameTicking.Prototypes;
using Content.Shared.GameTicking.Rules;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Configuration;
using Robust.Shared.Utility;
namespace Content.Server.GameTicking.Rules;
public sealed partial class SecretRuleSystem : GameRuleSystem<SecretRuleComponent>
{
[Dependency] private IRobustRandom _random = default!;
[Dependency] private IConfigurationManager _configurationManager = default!;
[Dependency] private IAdminLogManager _adminLogger = default!;
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.");
foreach (var rule in preset.Rules)
{
if (GameTicker.IsIgnored(rule))
continue;
Entity<GameRuleComponent>? 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);
if (ruleEnt == null)
continue;
component.AdditionalGameRules.Add(ruleEnt.Value);
}
}
// TODO: We PROBABLY SHOULD NOT BE DOING THIS as the only time Secret ends naturally is end of round which already cleans up these rules.
// TODO: IN ADDITION We should end secret once it spawns its rules so we don't tick it :V or pause it?
protected override void Ended(Entity<SecretRuleComponent> rule, ref GameRuleEndedEvent args)
{
base.Ended(rule, ref args);
foreach (var gameRule in rule.Comp.AdditionalGameRules)
{
GameTicker.EndGameRule(gameRule);
}
}
private bool TryPickPreset(ProtoId<WeightedRandomPrototype> weights, [NotNullWhen(true)] out GamePresetPrototype? preset)
{
var options = ProtoMan.Index(weights).Weights.ShallowClone();
var players = GameTicker.ReadyPlayerCount();
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 (!ProtoMan.TryIndex(key, out selectedPreset))
Log.Error($"Invalid preset {selectedPreset} in secret rule weights: {weights}");
options.Remove(key);
sum -= weight;
break;
}
if (CanPick(selectedPreset, players))
{
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 = ProtoMan.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();
foreach (var id in protos)
{
if (!ProtoMan.TryIndex(id, out var selectedPreset))
Log.Error($"Invalid preset {selectedPreset} in secret rule weights: {id}");
if (CanPick(selectedPreset, players))
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)
{
if (selected == null)
return false;
return players >= GameTicker.GetMinimumPlayerCount(selected);
}
}