Add ghost role raffles (#26629)

* Add ghost role raffles

* GRR: Fix dialogue sizing, fix merge

* GRR: Add raffle deciders (winner picker)

* GRR: Make settings prototype based with option to override

* GRR: Use Raffles folder and namespace

* GRR: DataFieldify and TimeSpanify

* GRR: Don't actually DataFieldify HashSet<ICommonSession>s

* GRR: add GetGhostRoleCount() + docs

* update engine on branch

* Ghost role raffles: docs, fix window size, cleanup, etc

* GRR: Admin UI

* GRR: Admin UI: Display initial/max/ext of selected raffle settings proto

* GRR: Make a ton of roles raffled
This commit is contained in:
no
2024-05-07 03:48:16 +02:00
committed by GitHub
parent 6685146a1e
commit 630a7a78ed
44 changed files with 1138 additions and 51 deletions

View File

@@ -5,7 +5,7 @@
Text="{Loc 'ghost-roles-window-request-role-button'}"
StyleClasses="OpenRight"
HorizontalAlignment="Left"
SetWidth="150"/>
SetWidth="300"/>
<Button Name="FollowButton"
Access="Public"
Text="{Loc 'ghost-roles-window-follow-role-button'}"

View File

@@ -1,9 +1,72 @@
using Robust.Client.AutoGenerated;
using Content.Shared.Ghost.Roles;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles;
[GenerateTypedNameReferences]
public sealed partial class GhostRoleEntryButtons : BoxContainer
{
[Dependency] private readonly IGameTiming _timing = default!;
private readonly GhostRoleKind _ghostRoleKind;
private readonly uint _playerCount;
private readonly TimeSpan _raffleEndTime = TimeSpan.MinValue;
public GhostRoleEntryButtons(GhostRoleInfo ghostRoleInfo)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_ghostRoleKind = ghostRoleInfo.Kind;
if (IsActiveRaffle(_ghostRoleKind))
{
_playerCount = ghostRoleInfo.RafflePlayerCount;
_raffleEndTime = ghostRoleInfo.RaffleEndTime;
}
UpdateRequestButton();
}
private void UpdateRequestButton()
{
var messageId = _ghostRoleKind switch
{
GhostRoleKind.FirstComeFirstServe => "ghost-roles-window-request-role-button",
GhostRoleKind.RaffleReady => "ghost-roles-window-join-raffle-button",
GhostRoleKind.RaffleInProgress => "ghost-roles-window-raffle-in-progress-button",
GhostRoleKind.RaffleJoined => "ghost-roles-window-leave-raffle-button",
_ => throw new ArgumentOutOfRangeException(nameof(_ghostRoleKind),
$"Unknown {nameof(GhostRoleKind)} '{_ghostRoleKind}'")
};
if (IsActiveRaffle(_ghostRoleKind))
{
var timeLeft = _timing.CurTime <= _raffleEndTime
? _raffleEndTime - _timing.CurTime
: TimeSpan.Zero;
var timeString = $"{timeLeft.Minutes:0}:{timeLeft.Seconds:00}";
RequestButton.Text = Loc.GetString(messageId, ("time", timeString), ("players", _playerCount));
}
else
{
RequestButton.Text = Loc.GetString(messageId);
}
}
private static bool IsActiveRaffle(GhostRoleKind kind)
{
return kind is GhostRoleKind.RaffleInProgress or GhostRoleKind.RaffleJoined;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (IsActiveRaffle(_ghostRoleKind))
{
UpdateRequestButton();
}
}
}

View File

@@ -26,7 +26,7 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
foreach (var role in roles)
{
var button = new GhostRoleEntryButtons();
var button = new GhostRoleEntryButtons(role);
button.RequestButton.OnPressed += _ => OnRoleSelected?.Invoke(role);
button.FollowButton.OnPressed += _ => OnRoleFollow?.Invoke(role);

View File

@@ -20,13 +20,24 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
{
_window = new GhostRolesWindow();
_window.OnRoleRequested += info =>
_window.OnRoleRequestButtonClicked += info =>
{
if (_windowRules != null)
_windowRules.Close();
_windowRules?.Close();
if (info.Kind == GhostRoleKind.RaffleJoined)
{
SendMessage(new LeaveGhostRoleRaffleMessage(info.Identifier));
return;
}
_windowRules = new GhostRoleRulesWindow(info.Rules, _ =>
{
SendMessage(new GhostRoleTakeoverRequestMessage(info.Identifier));
SendMessage(new RequestGhostRoleMessage(info.Identifier));
// if raffle role, close rules window on request, otherwise do
// old behavior of waiting for the server to close it
if (info.Kind != GhostRoleKind.FirstComeFirstServe)
_windowRules?.Close();
});
_windowRulesId = info.Identifier;
_windowRules.OnClose += () =>
@@ -38,7 +49,7 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
_window.OnRoleFollow += info =>
{
SendMessage(new GhostRoleFollowRequestMessage(info.Identifier));
SendMessage(new FollowGhostRoleMessage(info.Identifier));
};
_window.OnClose += () =>
@@ -64,7 +75,8 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
{
base.HandleState(state);
if (state is not GhostRolesEuiState ghostState) return;
if (state is not GhostRolesEuiState ghostState)
return;
_window.ClearEntries();
var entityManager = IoCManager.Resolve<IEntityManager>();

View File

@@ -1,7 +1,7 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc 'ghost-roles-window-title'}"
MinSize="450 400"
SetSize="400 500">
MinSize="490 400"
SetSize="490 500">
<Label Name="NoRolesMessage"
Text="{Loc 'ghost-roles-window-no-roles-available-label'}"
VerticalAlignment="Top" />

View File

@@ -9,7 +9,7 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
[GenerateTypedNameReferences]
public sealed partial class GhostRolesWindow : DefaultWindow
{
public event Action<GhostRoleInfo>? OnRoleRequested;
public event Action<GhostRoleInfo>? OnRoleRequestButtonClicked;
public event Action<GhostRoleInfo>? OnRoleFollow;
public void ClearEntries()
@@ -23,7 +23,7 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
NoRolesMessage.Visible = false;
var entry = new GhostRolesEntry(name, description, hasAccess, reason, roles, spriteSystem);
entry.OnRoleSelected += OnRoleRequested;
entry.OnRoleSelected += OnRoleRequestButtonClicked;
entry.OnRoleFollow += OnRoleFollow;
EntryContainer.AddChild(entry);
}

View File

@@ -1,4 +1,5 @@
using Content.Client.Eui;
using Content.Server.Ghost.Roles.Raffles;
using Content.Shared.Eui;
using Content.Shared.Ghost.Roles;
using JetBrains.Annotations;
@@ -41,7 +42,7 @@ public sealed class MakeGhostRoleEui : BaseEui
_window.OpenCentered();
}
private void OnMake(NetEntity entity, string name, string description, string rules, bool makeSentient)
private void OnMake(NetEntity entity, string name, string description, string rules, bool makeSentient, GhostRoleRaffleSettings? raffleSettings)
{
var session = _playerManager.LocalSession;
if (session == null)
@@ -49,12 +50,22 @@ public sealed class MakeGhostRoleEui : BaseEui
return;
}
var command = raffleSettings is not null ? "makeghostroleraffled" : "makeghostrole";
var makeGhostRoleCommand =
$"makeghostrole " +
$"{command} " +
$"\"{CommandParsing.Escape(entity.ToString())}\" " +
$"\"{CommandParsing.Escape(name)}\" " +
$"\"{CommandParsing.Escape(description)}\" " +
$"\"{CommandParsing.Escape(rules)}\"";
$"\"{CommandParsing.Escape(description)}\" ";
if (raffleSettings is not null)
{
makeGhostRoleCommand += $"{raffleSettings.InitialDuration} " +
$"{raffleSettings.JoinExtendsDurationBy} " +
$"{raffleSettings.MaxDuration} ";
}
makeGhostRoleCommand += $"\"{CommandParsing.Escape(rules)}\"";
_consoleHost.ExecuteCommand(session, makeGhostRoleCommand);

View File

@@ -22,6 +22,24 @@
<Label Name="MakeSentientLabel" Text="Make Sentient" />
<CheckBox Name="MakeSentientCheckbox" />
</BoxContainer>
<BoxContainer Orientation="Horizontal">
<Label Name="RaffleLabel" Text="Raffle Role?" />
<OptionButton Name="RaffleButton" />
</BoxContainer>
<BoxContainer Name="RaffleCustomSettingsContainer" Orientation="Vertical" Visible="False">
<BoxContainer Orientation="Horizontal">
<Label Name="RaffleInitialDurationLabel" Text="Initial Duration (s)" />
<SpinBox Name="RaffleInitialDuration" HorizontalExpand="True" />
</BoxContainer>
<BoxContainer Orientation="Horizontal">
<Label Name="RaffleJoinExtendsDurationByLabel" Text="Joins Extend By (s)" />
<SpinBox Name="RaffleJoinExtendsDurationBy" HorizontalExpand="True" />
</BoxContainer>
<BoxContainer Orientation="Horizontal">
<Label Name="RaffleMaxDurationLabel" Text="Max Duration (s)" />
<SpinBox Name="RaffleMaxDuration" HorizontalExpand="True" />
</BoxContainer>
</BoxContainer>
<BoxContainer Orientation="Horizontal">
<Button Name="MakeButton" Text="Make" />
</BoxContainer>

View File

@@ -1,7 +1,12 @@
using System.Numerics;
using System.Linq;
using System.Numerics;
using Content.Server.Ghost.Roles.Raffles;
using Content.Shared.Ghost.Roles.Raffles;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
@@ -9,10 +14,20 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
[GenerateTypedNameReferences]
public sealed partial class MakeGhostRoleWindow : DefaultWindow
{
public delegate void MakeRole(NetEntity uid, string name, string description, string rules, bool makeSentient);
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private readonly List<GhostRoleRaffleSettingsPrototype> _rafflePrototypes = [];
private const int RaffleDontRaffleId = -1;
private const int RaffleCustomRaffleId = -2;
private int _raffleSettingId = RaffleDontRaffleId;
private NetEntity? Entity { get; set; }
public event MakeRole? OnMake;
public MakeGhostRoleWindow()
{
IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);
MakeSentientLabel.MinSize = new Vector2(150, 0);
@@ -23,13 +38,87 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
RoleDescription.MinSize = new Vector2(300, 0);
RoleRulesLabel.MinSize = new Vector2(150, 0);
RoleRules.MinSize = new Vector2(300, 0);
RaffleLabel.MinSize = new Vector2(150, 0);
RaffleButton.MinSize = new Vector2(300, 0);
RaffleInitialDurationLabel.MinSize = new Vector2(150, 0);
RaffleInitialDuration.MinSize = new Vector2(300, 0);
RaffleJoinExtendsDurationByLabel.MinSize = new Vector2(150, 0);
RaffleJoinExtendsDurationBy.MinSize = new Vector2(270, 0);
RaffleMaxDurationLabel.MinSize = new Vector2(150, 0);
RaffleMaxDuration.MinSize = new Vector2(270, 0);
MakeButton.OnPressed += OnPressed;
RaffleInitialDuration.OverrideValue(30);
RaffleJoinExtendsDurationBy.OverrideValue(5);
RaffleMaxDuration.OverrideValue(60);
RaffleInitialDuration.SetButtons(new List<int> { -30, -10 }, new List<int> { 10, 30 });
RaffleJoinExtendsDurationBy.SetButtons(new List<int> { -10, -5 }, new List<int> { 5, 10 });
RaffleMaxDuration.SetButtons(new List<int> { -30, -10 }, new List<int> { 10, 30 });
RaffleInitialDuration.IsValid = duration => duration > 0;
RaffleJoinExtendsDurationBy.IsValid = duration => duration >= 0;
RaffleMaxDuration.IsValid = duration => duration > 0;
RaffleInitialDuration.ValueChanged += OnRaffleDurationChanged;
RaffleJoinExtendsDurationBy.ValueChanged += OnRaffleDurationChanged;
RaffleMaxDuration.ValueChanged += OnRaffleDurationChanged;
RaffleButton.AddItem("Don't raffle", RaffleDontRaffleId);
RaffleButton.AddItem("Custom settings", RaffleCustomRaffleId);
var raffleProtos =
_prototypeManager.EnumeratePrototypes<GhostRoleRaffleSettingsPrototype>();
var idx = 0;
foreach (var raffleProto in raffleProtos)
{
_rafflePrototypes.Add(raffleProto);
var s = raffleProto.Settings;
var label =
$"{raffleProto.ID} (initial {s.InitialDuration}s, max {s.MaxDuration}s, join adds {s.JoinExtendsDurationBy}s)";
RaffleButton.AddItem(label, idx++);
}
MakeButton.OnPressed += OnMakeButtonPressed;
RaffleButton.OnItemSelected += OnRaffleButtonItemSelected;
}
private NetEntity? Entity { get; set; }
private void OnRaffleDurationChanged(ValueChangedEventArgs args)
{
ValidateRaffleDurations();
}
public event MakeRole? OnMake;
private void ValidateRaffleDurations()
{
if (RaffleInitialDuration.Value > RaffleMaxDuration.Value)
{
MakeButton.Disabled = true;
MakeButton.ToolTip = "The initial duration must not exceed the maximum duration.";
}
else
{
MakeButton.Disabled = false;
MakeButton.ToolTip = null;
}
}
private void OnRaffleButtonItemSelected(OptionButton.ItemSelectedEventArgs args)
{
_raffleSettingId = args.Id;
args.Button.SelectId(args.Id);
if (args.Id != RaffleCustomRaffleId)
{
RaffleCustomSettingsContainer.Visible = false;
MakeButton.ToolTip = null;
MakeButton.Disabled = false;
}
else
{
RaffleCustomSettingsContainer.Visible = true;
ValidateRaffleDurations();
}
}
public void SetEntity(IEntityManager entManager, NetEntity entity)
{
@@ -38,14 +127,32 @@ namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
RoleEntity.Text = $"{entity}";
}
private void OnPressed(ButtonEventArgs args)
private void OnMakeButtonPressed(ButtonEventArgs args)
{
if (Entity == null)
{
return;
}
OnMake?.Invoke(Entity.Value, RoleName.Text, RoleDescription.Text, RoleRules.Text, MakeSentientCheckbox.Pressed);
GhostRoleRaffleSettings? raffleSettings = null;
if (_raffleSettingId == RaffleCustomRaffleId)
{
raffleSettings = new GhostRoleRaffleSettings()
{
InitialDuration = (uint) RaffleInitialDuration.Value,
JoinExtendsDurationBy = (uint) RaffleJoinExtendsDurationBy.Value,
MaxDuration = (uint) RaffleMaxDuration.Value
};
}
else if (_raffleSettingId != RaffleDontRaffleId)
{
raffleSettings = _rafflePrototypes[_raffleSettingId].Settings;
}
OnMake?.Invoke(Entity.Value, RoleName.Text, RoleDescription.Text, RoleRules.Text, MakeSentientCheckbox.Pressed, raffleSettings);
}
public delegate void MakeRole(NetEntity uid, string name, string description, string rules, bool makeSentient, GhostRoleRaffleSettings? settings);
}
}