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:
@@ -12,11 +12,14 @@
|
||||
</BoxContainer>
|
||||
<hudUi:HighDivider />
|
||||
|
||||
<BoxContainer Margin="8 2 8 0" VerticalExpand="True" VerticalAlignment="Top" Orientation="Horizontal">
|
||||
<OptionButton Name="VoteTypeButton" HorizontalExpand="True" />
|
||||
<Control HorizontalExpand="True">
|
||||
<OptionButton Name="VoteSecondButton" Visible="False" />
|
||||
</Control>
|
||||
<BoxContainer Orientation="Vertical" Margin="8 2 8 0" VerticalExpand="True" VerticalAlignment="Top">
|
||||
<BoxContainer Orientation="Horizontal">
|
||||
<OptionButton Name="VoteTypeButton" HorizontalExpand="True" />
|
||||
<Control HorizontalExpand="True">
|
||||
<OptionButton Name="VoteSecondButton" Visible="False" />
|
||||
</Control>
|
||||
</BoxContainer>
|
||||
<Label Name="VoteTypeTimeoutLabel" Visible="False" />
|
||||
</BoxContainer>
|
||||
|
||||
<Button Margin="8 2" Name="CreateButton" Text="{Loc 'ui-vote-create-button'}" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Client.Stylesheets;
|
||||
using Content.Shared.Voting;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Console;
|
||||
@@ -9,6 +10,7 @@ using Robust.Shared.Console;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Voting.UI
|
||||
{
|
||||
@@ -16,12 +18,15 @@ namespace Content.Client.Voting.UI
|
||||
public partial class VoteCallMenu : BaseWindow
|
||||
{
|
||||
[Dependency] private readonly IClientConsoleHost _consoleHost = default!;
|
||||
[Dependency] private readonly IVoteManager _voteManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public static readonly (string name, string id, (string name, string id)[]? secondaries)[] AvailableVoteTypes =
|
||||
{
|
||||
("ui-vote-type-restart", "restart", null),
|
||||
("ui-vote-type-gamemode", "preset", null)
|
||||
};
|
||||
public static readonly (string name, StandardVoteType type, (string name, string id)[]? secondaries)[]
|
||||
AvailableVoteTypes =
|
||||
{
|
||||
("ui-vote-type-restart", StandardVoteType.Restart, null),
|
||||
("ui-vote-type-gamemode", StandardVoteType.Preset, null)
|
||||
};
|
||||
|
||||
public VoteCallMenu()
|
||||
{
|
||||
@@ -42,6 +47,33 @@ namespace Content.Client.Voting.UI
|
||||
CreateButton.OnPressed += CreatePressed;
|
||||
}
|
||||
|
||||
protected override void Opened()
|
||||
{
|
||||
base.Opened();
|
||||
|
||||
_voteManager.CanCallVoteChanged += CanCallVoteChanged;
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
|
||||
_voteManager.CanCallVoteChanged -= CanCallVoteChanged;
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
base.FrameUpdate(args);
|
||||
|
||||
UpdateVoteTimeout();
|
||||
}
|
||||
|
||||
private void CanCallVoteChanged(bool obj)
|
||||
{
|
||||
if (!obj)
|
||||
Close();
|
||||
}
|
||||
|
||||
private void CreatePressed(BaseButton.ButtonEventArgs obj)
|
||||
{
|
||||
var typeId = VoteTypeButton.SelectedId;
|
||||
@@ -62,6 +94,20 @@ namespace Content.Client.Voting.UI
|
||||
Close();
|
||||
}
|
||||
|
||||
private void UpdateVoteTimeout()
|
||||
{
|
||||
var (_, typeKey, _) = AvailableVoteTypes[VoteTypeButton.SelectedId];
|
||||
var isAvailable = _voteManager.CanCallStandardVote(typeKey, out var timeout);
|
||||
CreateButton.Disabled = !isAvailable;
|
||||
VoteTypeTimeoutLabel.Visible = !isAvailable;
|
||||
|
||||
if (!isAvailable)
|
||||
{
|
||||
var remaining = timeout - _gameTiming.RealTime;
|
||||
VoteTypeTimeoutLabel.Text = Loc.GetString("ui-vote-type-timeout", ("remaining", remaining.ToString("mm\\:ss")));
|
||||
}
|
||||
}
|
||||
|
||||
private static void VoteSecondSelected(OptionButton.ItemSelectedEventArgs obj)
|
||||
{
|
||||
obj.Button.SelectId(obj.Id);
|
||||
|
||||
@@ -19,7 +19,10 @@ namespace Content.Client.Voting
|
||||
void ClearPopupContainer();
|
||||
void SetPopupContainer(Control container);
|
||||
bool CanCallVote { get; }
|
||||
|
||||
bool CanCallStandardVote(StandardVoteType type, out TimeSpan whenCan);
|
||||
event Action<bool> CanCallVoteChanged;
|
||||
event Action CanCallStandardVotesChanged;
|
||||
}
|
||||
|
||||
public sealed class VoteManager : IVoteManager
|
||||
@@ -29,13 +32,17 @@ namespace Content.Client.Voting
|
||||
[Dependency] private readonly IClientConsoleHost _console = default!;
|
||||
[Dependency] private readonly IBaseClient _client = default!;
|
||||
|
||||
private readonly Dictionary<StandardVoteType, TimeSpan> _standardVoteTimeouts = new();
|
||||
private readonly Dictionary<int, ActiveVote> _votes = new();
|
||||
private readonly Dictionary<int, UI.VotePopup> _votePopups = new();
|
||||
private Control? _popupContainer;
|
||||
|
||||
public bool CanCallVote { get; private set; }
|
||||
|
||||
public event Action<bool>? CanCallVoteChanged;
|
||||
|
||||
public event Action? CanCallStandardVotesChanged;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_netManager.RegisterNetMessage<MsgVoteData>(ReceiveVoteData);
|
||||
@@ -54,6 +61,11 @@ namespace Content.Client.Voting
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanCallStandardVote(StandardVoteType type, out TimeSpan whenCan)
|
||||
{
|
||||
return !_standardVoteTimeouts.TryGetValue(type, out whenCan);
|
||||
}
|
||||
|
||||
public void ClearPopupContainer()
|
||||
{
|
||||
if (_popupContainer == null)
|
||||
@@ -161,11 +173,20 @@ namespace Content.Client.Voting
|
||||
|
||||
private void ReceiveVoteCanCall(MsgVoteCanCall message)
|
||||
{
|
||||
if (CanCallVote == message.CanCall)
|
||||
return;
|
||||
if (CanCallVote != message.CanCall)
|
||||
{
|
||||
// TODO: actually use the "when can call vote" time for UI display or something.
|
||||
CanCallVote = message.CanCall;
|
||||
CanCallVoteChanged?.Invoke(CanCallVote);
|
||||
}
|
||||
|
||||
CanCallVote = message.CanCall;
|
||||
CanCallVoteChanged?.Invoke(CanCallVote);
|
||||
_standardVoteTimeouts.Clear();
|
||||
foreach (var (type, time) in message.VotesUnavailable)
|
||||
{
|
||||
_standardVoteTimeouts.Add(type, _gameTiming.RealServerToLocal(time));
|
||||
}
|
||||
|
||||
CanCallStandardVotesChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void SendCastVote(int voteId, int option)
|
||||
|
||||
Reference in New Issue
Block a user