Separate game rule enabling and game rule starting (#6168)

This commit is contained in:
mirrorcult
2022-02-15 20:06:28 -07:00
committed by GitHub
parent e427381be6
commit 3abc7a443e
13 changed files with 125 additions and 52 deletions

View File

@@ -8,35 +8,80 @@ namespace Content.Server.GameTicking
public partial class GameTicker
{
// No duplicates.
[ViewVariables] private readonly HashSet<GameRulePrototype> _gameRules = new();
public IEnumerable<GameRulePrototype> ActiveGameRules => _gameRules;
[ViewVariables] private readonly HashSet<GameRulePrototype> _addedGameRules = new();
public IEnumerable<GameRulePrototype> AddedGameRules => _addedGameRules;
[ViewVariables] private readonly HashSet<GameRulePrototype> _startedGameRules = new();
public IEnumerable<GameRulePrototype> StartedGameRules => _startedGameRules;
/// <summary>
/// Game rules can be 'started' separately from being added. 'Starting' them usually
/// happens at round start while they can be added and removed before then.
/// </summary>
public void StartGameRule(GameRulePrototype rule)
{
if (!GameRuleAdded(rule))
AddGameRule(rule);
if (_startedGameRules.Add(rule))
RaiseLocalEvent(new GameRuleStartedEvent(rule));
}
/// <summary>
/// Ends a game rule.
/// This always includes removing it (removing it from added game rules) so that behavior
/// is not separate from this.
/// </summary>
/// <param name="rule"></param>
public void EndGameRule(GameRulePrototype rule)
{
if (!GameRuleAdded(rule))
return;
_addedGameRules.Remove(rule);
if (GameRuleStarted(rule))
_startedGameRules.Remove(rule);
RaiseLocalEvent(new GameRuleEndedEvent(rule));
}
/// <summary>
/// Adds a game rule to the list, but does not
/// start it yet, instead waiting until roundstart.
/// </summary>
public bool AddGameRule(GameRulePrototype rule)
{
if (!_gameRules.Add(rule))
if (!_addedGameRules.Add(rule))
return false;
RaiseLocalEvent(new GameRuleAddedEvent(rule));
return true;
}
public bool RemoveGameRule(GameRulePrototype rule)
public bool GameRuleAdded(GameRulePrototype rule)
{
if (!_gameRules.Remove(rule))
return false;
RaiseLocalEvent(new GameRuleRemovedEvent(rule));
return true;
return _addedGameRules.Contains(rule);
}
public bool HasGameRule(GameRulePrototype rule)
public bool GameRuleAdded(string rule)
{
return _gameRules.Contains(rule);
foreach (var ruleProto in _addedGameRules)
{
if (ruleProto.ID.Equals(rule))
return true;
}
return false;
}
public bool HasGameRule(string rule)
public bool GameRuleStarted(GameRulePrototype rule)
{
foreach (var ruleProto in _gameRules)
return _startedGameRules.Contains(rule);
}
public bool GameRuleStarted(string rule)
{
foreach (var ruleProto in _startedGameRules)
{
if (ruleProto.ID.Equals(rule))
return true;
@@ -47,14 +92,17 @@ namespace Content.Server.GameTicking
public void ClearGameRules()
{
foreach (var rule in _gameRules.ToArray())
foreach (var rule in _addedGameRules.ToArray())
{
RemoveGameRule(rule);
EndGameRule(rule);
}
}
}
public class GameRuleAddedEvent
/// <summary>
/// Raised broadcast when a game rule is selected, but not started yet.
/// </summary>
public sealed class GameRuleAddedEvent
{
public GameRulePrototype Rule { get; }
@@ -64,11 +112,21 @@ namespace Content.Server.GameTicking
}
}
public class GameRuleRemovedEvent
public sealed class GameRuleStartedEvent
{
public GameRulePrototype Rule { get; }
public GameRuleRemovedEvent(GameRulePrototype rule)
public GameRuleStartedEvent(GameRulePrototype rule)
{
Rule = rule;
}
}
public sealed class GameRuleEndedEvent
{
public GameRulePrototype Rule { get; }
public GameRuleEndedEvent(GameRulePrototype rule)
{
Rule = rule;
}