Vote type delay, code comments.

Added doc comments to server side voting API.
There is now a 4 minute delay between creating votes of the same type.
Shuffled some code around.
Made a StandardVoteType enum instead of string IDs.
This commit is contained in:
Pieter-Jan Briers
2021-07-21 19:03:10 +02:00
parent 1187185b89
commit e9af56c7c3
13 changed files with 348 additions and 49 deletions

View File

@@ -7,7 +7,6 @@ using System.Linq;
using Content.Server.Administration;
using Content.Server.Administration.Managers;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking;
using Content.Shared.Administration;
using Content.Shared.Collections;
using Content.Shared.Voting;
@@ -39,6 +38,7 @@ namespace Content.Server.Voting.Managers
private readonly Dictionary<int, VoteReg> _votes = new();
private readonly Dictionary<int, VoteHandle> _voteHandles = new();
private readonly Dictionary<StandardVoteType, TimeSpan> _standardVoteTimeout = new();
private readonly Dictionary<NetUserId, TimeSpan> _voteTimeout = new();
private readonly HashSet<IPlayerSession> _playerCanCallVoteDirty = new();
@@ -146,6 +146,21 @@ namespace Content.Server.Voting.Managers
DirtyCanCallVote(session);
}
// Handle standard vote timeouts.
var stdTimeoutRemQueue = new RemQueue<StandardVoteType>();
foreach (var (type, timeout) in _standardVoteTimeout)
{
if (timeout < _timing.RealTime)
stdTimeoutRemQueue.Add(type);
}
foreach (var type in stdTimeoutRemQueue)
{
_standardVoteTimeout.Remove(type);
DirtyCanCallVoteAll();
}
// Handle dirty canCallVotes.
foreach (var dirtyPlayer in _playerCanCallVoteDirty)
{
@@ -240,26 +255,47 @@ namespace Content.Server.Voting.Managers
private void SendUpdateCanCallVote(IPlayerSession player)
{
var msg = _netManager.CreateNetMessage<MsgVoteCanCall>();
msg.CanCall = CanCallVote(player);
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();
_netManager.ServerSendMessage(msg, player.ConnectedClient);
}
public bool CanCallVote(IPlayerSession player)
private bool CanCallVote(
IPlayerSession initiator,
StandardVoteType? voteType,
out bool isAdmin,
out TimeSpan timeSpan)
{
isAdmin = false;
timeSpan = default;
// Admins can always call votes.
if (_adminMgr.HasAdminFlag(player, AdminFlags.Admin))
if (_adminMgr.HasAdminFlag(initiator, AdminFlags.Admin))
{
isAdmin = true;
return true;
}
// Cannot start vote if vote is already active (as non-admin).
if (_votes.Count != 0)
{
return false;
}
return !_voteTimeout.ContainsKey(player.UserId);
// 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))
return false;
return !_voteTimeout.TryGetValue(initiator.UserId, out timeSpan);
}
public bool CanCallVote(IPlayerSession initiator, StandardVoteType? voteType = null)
{
return CanCallVote(initiator, voteType, out _, out _);
}
private void EndVote(VoteReg v)
@@ -484,7 +520,7 @@ namespace Content.Server.Voting.Managers
}
public IEnumerable<object> Keys => _reg.Entries.Select(c => c.Data);
public IEnumerable<int> Values => _reg.Entries.Select(c => c.Votes);
public IEnumerable<int> Values => _reg.Entries.Select(c => c.Votes);
}
}