Add variables to prevent voting of various kinds (#5361)

* Add a way to disable voting for maximum authoritarianism

* Allow disabling specific vote types via cvars

* Migrate standard-vote-to-cvar dictionary to being static
This commit is contained in:
20kdc
2021-11-23 17:03:04 +00:00
committed by GitHub
parent 781a083fcf
commit a2b7982c37
7 changed files with 84 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ using Content.Server.Afk;
using Content.Server.Chat.Managers;
using Content.Server.Maps;
using Content.Shared.Administration;
using Content.Shared.CCVar;
using Content.Shared.Voting;
using Robust.Server.Player;
using Robust.Shared.Configuration;
@@ -45,6 +46,7 @@ namespace Content.Server.Voting.Managers
private readonly Dictionary<StandardVoteType, TimeSpan> _standardVoteTimeout = new();
private readonly Dictionary<NetUserId, TimeSpan> _voteTimeout = new();
private readonly HashSet<IPlayerSession> _playerCanCallVoteDirty = new();
private readonly StandardVoteType[] _standardVoteTypeValues = Enum.GetValues<StandardVoteType>();
public void Initialize()
{
@@ -53,6 +55,17 @@ namespace Content.Server.Voting.Managers
_playerManager.PlayerStatusChanged += PlayerManagerOnPlayerStatusChanged;
_adminMgr.OnPermsChanged += AdminPermsChanged;
_cfg.OnValueChanged(CCVars.VoteEnabled, value => {
DirtyCanCallVoteAll();
});
foreach (var kvp in _voteTypesToEnableCVars)
{
_cfg.OnValueChanged(kvp.Value, value => {
DirtyCanCallVoteAll();
});
}
}
private void AdminPermsChanged(AdminPermsChangedEventArgs obj)
@@ -262,9 +275,21 @@ namespace Content.Server.Voting.Managers
msg.CanCall = CanCallVote(player, null, out var isAdmin, out var timeSpan);
msg.WhenCanCallVote = timeSpan;
msg.VotesUnavailable = isAdmin
? Array.Empty<(StandardVoteType, TimeSpan)>()
: _standardVoteTimeout.Select(kv => (kv.Key, kv.Value)).ToArray();
if (isAdmin)
{
msg.VotesUnavailable = Array.Empty<(StandardVoteType, TimeSpan)>();
}
else
{
var votesUnavailable = new List<(StandardVoteType, TimeSpan)>();
foreach (var v in _standardVoteTypeValues)
{
if (CanCallVote(player, v, out var _isAdmin, out var typeTimeSpan))
continue;
votesUnavailable.Add((v, typeTimeSpan));
}
msg.VotesUnavailable = votesUnavailable.ToArray();
}
_netManager.ServerSendMessage(msg, player.ConnectedClient);
}
@@ -285,13 +310,20 @@ namespace Content.Server.Voting.Managers
return true;
}
// If voting is disabled, block votes.
if (!_cfg.GetCVar(CCVars.VoteEnabled))
return false;
// Specific standard vote types can be disabled with cvars.
if ((voteType != null) && _voteTypesToEnableCVars.TryGetValue(voteType.Value, out var cvar) && !_cfg.GetCVar(cvar))
return false;
// Cannot start vote if vote is already active (as non-admin).
if (_votes.Count != 0)
return false;
// Standard vote on timeout, no calling.
// Ghosts I understand you're dead but stop spamming the restart vote bloody hell.
if (voteType != null && _standardVoteTimeout.ContainsKey(voteType.Value))
if (voteType != null && _standardVoteTimeout.TryGetValue(voteType.Value, out timeSpan))
return false;
return !_voteTimeout.TryGetValue(initiator.UserId, out timeSpan);