Voting (#3185)
* Basic voting * Rewrite lobby in XAML. Working lobby voting. * Escape menu is now XAML. * Vote menu works, custom votes, gamemode votes. * Vote timeouts & administration. Basically done now. * I will now pretend I was never planning to code voting hotkeys. * Make vote call UI a bit... funny. * Fix exception on round restart. * Fix some vote command definitions.
This commit is contained in:
committed by
GitHub
parent
db290fd91e
commit
cea87d6985
39
Content.Client/Voting/VoteCallMenu.xaml
Normal file
39
Content.Client/Voting/VoteCallMenu.xaml
Normal file
@@ -0,0 +1,39 @@
|
||||
<v:VoteCallMenu xmlns="https://spacestation14.io"
|
||||
xmlns:cuic="clr-namespace:Content.Client.UserInterface.Controls"
|
||||
xmlns:v="clr-namespace:Content.Client.Voting"
|
||||
MouseFilter="Stop" CustomMinimumSize="350 150">
|
||||
<PanelContainer StyleClasses="AngleRect" />
|
||||
<VBoxContainer>
|
||||
<HBoxContainer>
|
||||
<MarginContainer MarginLeftOverride="8" SizeFlagsHorizontal="FillExpand">
|
||||
<Label Text="{Loc 'Call Vote'}" VAlign="Center" StyleClasses="LabelHeading" />
|
||||
</MarginContainer>
|
||||
<MarginContainer MarginRightOverride="8">
|
||||
<TextureButton Name="CloseButton" StyleClasses="windowCloseButton"
|
||||
SizeFlagsVertical="ShrinkCenter" />
|
||||
</MarginContainer>
|
||||
</HBoxContainer>
|
||||
<cuic:HighDivider />
|
||||
|
||||
<MarginContainer SizeFlagsHorizontal="Fill" SizeFlagsVertical="Expand"
|
||||
MarginLeftOverride="8" MarginRightOverride="8" MarginTopOverride="2">
|
||||
<HBoxContainer>
|
||||
<OptionButton Name="VoteTypeButton" SizeFlagsHorizontal="FillExpand" />
|
||||
<Control SizeFlagsHorizontal="FillExpand">
|
||||
<OptionButton Name="VoteSecondButton" Visible="False" />
|
||||
</Control>
|
||||
</HBoxContainer>
|
||||
</MarginContainer>
|
||||
|
||||
<MarginContainer SizeFlagsHorizontal="Fill"
|
||||
MarginLeftOverride="8" MarginRightOverride="8" MarginBottomOverride="2">
|
||||
<Button Name="CreateButton" Text="{Loc 'Call Vote'}" />
|
||||
</MarginContainer>
|
||||
|
||||
<PanelContainer StyleClasses="LowDivider" />
|
||||
<MarginContainer MarginLeftOverride="12">
|
||||
<Label StyleClasses="LabelSubText" Text="{Loc 'Powered by Robust™ Anti-Tamper Technology'}" />
|
||||
</MarginContainer>
|
||||
|
||||
</VBoxContainer>
|
||||
</v:VoteCallMenu>
|
||||
111
Content.Client/Voting/VoteCallMenu.xaml.cs
Normal file
111
Content.Client/Voting/VoteCallMenu.xaml.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
#nullable enable
|
||||
using Content.Client.UserInterface.Stylesheets;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Console;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
namespace Content.Client.Voting
|
||||
{
|
||||
[GenerateTypedNameReferences]
|
||||
public partial class VoteCallMenu : BaseWindow
|
||||
{
|
||||
[Dependency] private readonly IClientConsoleHost _consoleHost = default!;
|
||||
|
||||
public static readonly (string name, string id, (string name, string id)[]? secondaries)[] AvailableVoteTypes =
|
||||
{
|
||||
("Restart round", "restart", null),
|
||||
("Next gamemode", "preset", null)
|
||||
};
|
||||
|
||||
public VoteCallMenu()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
RobustXamlLoader.Load(this);
|
||||
|
||||
Stylesheet = IoCManager.Resolve<IStylesheetManager>().SheetSpace;
|
||||
CloseButton.OnPressed += _ => Close();
|
||||
|
||||
for (var i = 0; i < AvailableVoteTypes.Length; i++)
|
||||
{
|
||||
var (text, _, _) = AvailableVoteTypes[i];
|
||||
VoteTypeButton.AddItem(Loc.GetString(text), i);
|
||||
}
|
||||
|
||||
VoteTypeButton.OnItemSelected += VoteTypeSelected;
|
||||
VoteSecondButton.OnItemSelected += VoteSecondSelected;
|
||||
CreateButton.OnPressed += CreatePressed;
|
||||
}
|
||||
|
||||
private void CreatePressed(BaseButton.ButtonEventArgs obj)
|
||||
{
|
||||
var typeId = VoteTypeButton.SelectedId;
|
||||
var (_, typeKey, secondaries) = AvailableVoteTypes[typeId];
|
||||
|
||||
if (secondaries != null)
|
||||
{
|
||||
var secondaryId = VoteSecondButton.SelectedId;
|
||||
var (_, secondKey) = secondaries[secondaryId];
|
||||
|
||||
_consoleHost.LocalShell.RemoteExecuteCommand($"createvote {typeKey} {secondKey}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_consoleHost.LocalShell.RemoteExecuteCommand($"createvote {typeKey}");
|
||||
}
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
private static void VoteSecondSelected(OptionButton.ItemSelectedEventArgs obj)
|
||||
{
|
||||
obj.Button.SelectId(obj.Id);
|
||||
}
|
||||
|
||||
private void VoteTypeSelected(OptionButton.ItemSelectedEventArgs obj)
|
||||
{
|
||||
VoteTypeButton.SelectId(obj.Id);
|
||||
|
||||
var (_, _, options) = AvailableVoteTypes[obj.Id];
|
||||
if (options == null)
|
||||
{
|
||||
VoteSecondButton.Visible = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
VoteSecondButton.Visible = true;
|
||||
VoteSecondButton.Clear();
|
||||
|
||||
for (var i = 0; i < options.Length; i++)
|
||||
{
|
||||
var (text, _) = options[i];
|
||||
VoteSecondButton.AddItem(Loc.GetString(text), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override DragMode GetDragModeFor(Vector2 relativeMousePos)
|
||||
{
|
||||
return DragMode.Move;
|
||||
}
|
||||
}
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class VoteMenuCommand : IConsoleCommand
|
||||
{
|
||||
public string Command => "votemenu";
|
||||
public string Description => "Opens the voting menu";
|
||||
public string Help => "Usage: votemenu";
|
||||
|
||||
public void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
new VoteCallMenu().OpenCentered();
|
||||
}
|
||||
}
|
||||
}
|
||||
49
Content.Client/Voting/VoteCallMenuButton.cs
Normal file
49
Content.Client/Voting/VoteCallMenuButton.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
|
||||
namespace Content.Client.Voting
|
||||
{
|
||||
/// <summary>
|
||||
/// LITERALLY just a button that opens the vote call menu.
|
||||
/// Automatically disables itself if the client cannot call votes.
|
||||
/// </summary>
|
||||
public sealed class VoteCallMenuButton : Button
|
||||
{
|
||||
[Dependency] private readonly IVoteManager _voteManager = default!;
|
||||
|
||||
public VoteCallMenuButton()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
Text = Loc.GetString("Call vote");
|
||||
OnPressed += OnOnPressed;
|
||||
}
|
||||
|
||||
private void OnOnPressed(ButtonEventArgs obj)
|
||||
{
|
||||
var menu = new VoteCallMenu();
|
||||
menu.OpenCentered();
|
||||
}
|
||||
|
||||
protected override void EnteredTree()
|
||||
{
|
||||
base.EnteredTree();
|
||||
|
||||
UpdateCanCall(_voteManager.CanCallVote);
|
||||
_voteManager.CanCallVoteChanged += UpdateCanCall;
|
||||
}
|
||||
|
||||
protected override void ExitedTree()
|
||||
{
|
||||
base.ExitedTree();
|
||||
|
||||
_voteManager.CanCallVoteChanged += UpdateCanCall;
|
||||
}
|
||||
|
||||
private void UpdateCanCall(bool canCall)
|
||||
{
|
||||
Disabled = !canCall;
|
||||
}
|
||||
}
|
||||
}
|
||||
196
Content.Client/Voting/VoteManager.cs
Normal file
196
Content.Client/Voting/VoteManager.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Shared.Network.NetMessages;
|
||||
using Robust.Client.Console;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Content.Client.Voting
|
||||
{
|
||||
public interface IVoteManager
|
||||
{
|
||||
void Initialize();
|
||||
void SendCastVote(int voteId, int option);
|
||||
void ClearPopupContainer();
|
||||
void SetPopupContainer(Control container);
|
||||
bool CanCallVote { get; }
|
||||
event Action<bool> CanCallVoteChanged;
|
||||
}
|
||||
|
||||
public sealed class VoteManager : IVoteManager
|
||||
{
|
||||
[Dependency] private readonly IClientNetManager _netManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IClientConsoleHost _console = default!;
|
||||
|
||||
private readonly Dictionary<int, ActiveVote> _votes = new();
|
||||
private readonly Dictionary<int, VotePopup> _votePopups = new();
|
||||
private Control? _popupContainer;
|
||||
|
||||
public bool CanCallVote { get; private set; }
|
||||
public event Action<bool>? CanCallVoteChanged;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_netManager.RegisterNetMessage<MsgVoteData>(MsgVoteData.NAME, ReceiveVoteData);
|
||||
_netManager.RegisterNetMessage<MsgVoteCanCall>(MsgVoteCanCall.NAME, ReceiveVoteCanCall);
|
||||
}
|
||||
|
||||
public void ClearPopupContainer()
|
||||
{
|
||||
if (_popupContainer == null)
|
||||
return;
|
||||
|
||||
if (!_popupContainer.Disposed)
|
||||
{
|
||||
foreach (var popup in _votePopups.Values)
|
||||
{
|
||||
popup.Orphan();
|
||||
}
|
||||
}
|
||||
|
||||
_votePopups.Clear();
|
||||
_popupContainer = null;
|
||||
}
|
||||
|
||||
public void SetPopupContainer(Control container)
|
||||
{
|
||||
if (_popupContainer != null)
|
||||
{
|
||||
ClearPopupContainer();
|
||||
}
|
||||
|
||||
_popupContainer = container;
|
||||
|
||||
foreach (var (vId, vote) in _votes)
|
||||
{
|
||||
var popup = new VotePopup(vote);
|
||||
|
||||
_votePopups.Add(vId, popup);
|
||||
_popupContainer.AddChild(popup);
|
||||
popup.UpdateData();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReceiveVoteData(MsgVoteData message)
|
||||
{
|
||||
var @new = false;
|
||||
var voteId = message.VoteId;
|
||||
if (!_votes.TryGetValue(voteId, out var existingVote))
|
||||
{
|
||||
if (!message.VoteActive)
|
||||
{
|
||||
// Got "vote inactive" for nonexistent vote???
|
||||
return;
|
||||
}
|
||||
|
||||
@new = true;
|
||||
|
||||
// New vote from the server.
|
||||
var vote = new ActiveVote(voteId)
|
||||
{
|
||||
Entries = message.Options
|
||||
.Select(c => new VoteEntry(c.name))
|
||||
.ToArray()
|
||||
};
|
||||
|
||||
existingVote = vote;
|
||||
_votes.Add(voteId, vote);
|
||||
}
|
||||
else if (!message.VoteActive)
|
||||
{
|
||||
// Remove gone vote.
|
||||
_votes.Remove(voteId);
|
||||
if (_votePopups.TryGetValue(voteId, out var toRemove))
|
||||
{
|
||||
toRemove.Orphan();
|
||||
_votePopups.Remove(voteId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Update vote data from incoming.
|
||||
if (message.IsYourVoteDirty)
|
||||
existingVote.OurVote = message.YourVote;
|
||||
// On the server, most of these params can't change.
|
||||
// It can't hurt to just re-set this stuff since I'm lazy and the server is sending it anyways, so...
|
||||
existingVote.Initiator = message.VoteInitiator;
|
||||
existingVote.Title = message.VoteTitle;
|
||||
existingVote.StartTime = _gameTiming.RealServerToLocal(message.StartTime);
|
||||
existingVote.EndTime = _gameTiming.RealServerToLocal(message.EndTime);
|
||||
|
||||
// Logger.Debug($"{existingVote.StartTime}, {existingVote.EndTime}, {_gameTiming.RealTime}");
|
||||
|
||||
for (var i = 0; i < message.Options.Length; i++)
|
||||
{
|
||||
existingVote.Entries[i].Votes = message.Options[i].votes;
|
||||
}
|
||||
|
||||
if (@new && _popupContainer != null)
|
||||
{
|
||||
var popup = new VotePopup(existingVote);
|
||||
|
||||
_votePopups.Add(voteId, popup);
|
||||
_popupContainer.AddChild(popup);
|
||||
}
|
||||
|
||||
if (_votePopups.TryGetValue(voteId, out var ePopup))
|
||||
{
|
||||
ePopup.UpdateData();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReceiveVoteCanCall(MsgVoteCanCall message)
|
||||
{
|
||||
if (CanCallVote == message.CanCall)
|
||||
return;
|
||||
|
||||
CanCallVote = message.CanCall;
|
||||
CanCallVoteChanged?.Invoke(CanCallVote);
|
||||
}
|
||||
|
||||
public void SendCastVote(int voteId, int option)
|
||||
{
|
||||
var data = _votes[voteId];
|
||||
// Update immediately to avoid any funny reconciliation bugs.
|
||||
// See also code in server side to avoid bulldozing this.
|
||||
data.OurVote = option;
|
||||
_console.LocalShell.RemoteExecuteCommand($"vote {voteId} {option}");
|
||||
}
|
||||
|
||||
public sealed class ActiveVote
|
||||
{
|
||||
public VoteEntry[] Entries = default!;
|
||||
|
||||
// Both of these are local RealTime (converted at NetMsg receive).
|
||||
public TimeSpan StartTime;
|
||||
public TimeSpan EndTime;
|
||||
public string Title = "";
|
||||
public string Initiator = "";
|
||||
public int? OurVote;
|
||||
public int Id;
|
||||
|
||||
public ActiveVote(int voteId)
|
||||
{
|
||||
Id = voteId;
|
||||
}
|
||||
}
|
||||
|
||||
public class VoteEntry
|
||||
{
|
||||
public string Text { get; }
|
||||
public int Votes { get; set; }
|
||||
|
||||
public VoteEntry(string text)
|
||||
{
|
||||
Text = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Content.Client/Voting/VotePopup.xaml
Normal file
19
Content.Client/Voting/VotePopup.xaml
Normal file
@@ -0,0 +1,19 @@
|
||||
<ui:Control xmlns:ui="clr-namespace:Robust.Client.UserInterface;assembly=Robust.Client"
|
||||
xmlns:uic="clr-namespace:Robust.Client.UserInterface.Controls;assembly=Robust.Client">
|
||||
<uic:PanelContainer StyleClasses="AngleRect" />
|
||||
<uic:MarginContainer MarginLeftOverride="4" MarginRightOverride="4" MarginTopOverride="4" MarginBottomOverride="4">
|
||||
<uic:VBoxContainer>
|
||||
<uic:Label Name="VoteCaller" />
|
||||
<uic:Label Name="VoteTitle" />
|
||||
|
||||
<uic:GridContainer Columns="3" Name="VoteOptionsContainer" />
|
||||
<uic:HBoxContainer>
|
||||
<uic:MarginContainer SizeFlagsHorizontal="FillExpand" MarginLeftOverride="2" MarginRightOverride="2"
|
||||
MarginTopOverride="2" MarginBottomOverride="2">
|
||||
<uic:ProgressBar Name="TimeLeftBar" MinValue="0" MaxValue="1" />
|
||||
</uic:MarginContainer>
|
||||
<uic:Label Name="TimeLeftText" />
|
||||
</uic:HBoxContainer>
|
||||
</uic:VBoxContainer>
|
||||
</uic:MarginContainer>
|
||||
</ui:Control>
|
||||
83
Content.Client/Voting/VotePopup.xaml.cs
Normal file
83
Content.Client/Voting/VotePopup.xaml.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using Content.Client.UserInterface.Stylesheets;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Voting
|
||||
{
|
||||
[GenerateTypedNameReferences]
|
||||
public partial class VotePopup : Control
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IVoteManager _voteManager = default!;
|
||||
|
||||
private readonly VoteManager.ActiveVote _vote;
|
||||
private readonly Button[] _voteButtons;
|
||||
|
||||
public VotePopup(VoteManager.ActiveVote vote)
|
||||
{
|
||||
_vote = vote;
|
||||
IoCManager.InjectDependencies(this);
|
||||
RobustXamlLoader.Load(this);
|
||||
|
||||
Stylesheet = IoCManager.Resolve<IStylesheetManager>().SheetSpace;
|
||||
|
||||
Modulate = Color.White.WithAlpha(0.75f);
|
||||
_voteButtons = new Button[vote.Entries.Length];
|
||||
var group = new ButtonGroup();
|
||||
|
||||
for (var i = 0; i < _voteButtons.Length; i++)
|
||||
{
|
||||
var button = new Button
|
||||
{
|
||||
ToggleMode = true,
|
||||
Group = group
|
||||
};
|
||||
_voteButtons[i] = button;
|
||||
VoteOptionsContainer.AddChild(button);
|
||||
var i1 = i;
|
||||
button.OnPressed += _ => _voteManager.SendCastVote(vote.Id, i1);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateData()
|
||||
{
|
||||
VoteTitle.Text = _vote.Title;
|
||||
VoteCaller.Text = Loc.GetString("{0} called a vote:", _vote.Initiator);
|
||||
|
||||
for (var i = 0; i < _voteButtons.Length; i++)
|
||||
{
|
||||
var entry = _vote.Entries[i];
|
||||
_voteButtons[i].Text = Loc.GetString("{0} ({1})", entry.Text, entry.Votes);
|
||||
|
||||
if (_vote.OurVote == i)
|
||||
_voteButtons[i].Pressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
// Logger.Debug($"{_gameTiming.ServerTime}, {_vote.StartTime}, {_vote.EndTime}");
|
||||
|
||||
var curTime = _gameTiming.RealTime;
|
||||
var timeLeft = _vote.EndTime - curTime;
|
||||
if (timeLeft < TimeSpan.Zero)
|
||||
timeLeft = TimeSpan.Zero;
|
||||
|
||||
// Round up a second.
|
||||
timeLeft = TimeSpan.FromSeconds(Math.Ceiling(timeLeft.TotalSeconds));
|
||||
|
||||
TimeLeftBar.Value = Math.Min(1, (float) ((curTime.TotalSeconds - _vote.StartTime.TotalSeconds) /
|
||||
(_vote.EndTime.TotalSeconds - _vote.StartTime.TotalSeconds)));
|
||||
|
||||
TimeLeftText.Text = $"{timeLeft:m\\:ss}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user