Separate "thank you" messages from general ads (#25867)

* Separated "thank you" messages from general ads

* Moved MessagePackPrototype to shared, cleaned up AdvertiseComponent, and actually killed AdvertisementsPackPrototype.
+More suggests changes.

* Rename PackPrototypeID to Pack in both components.
This commit is contained in:
Tayrtahn
2024-03-28 02:46:26 -04:00
committed by GitHub
parent 29f34d8423
commit 794a447bb7
72 changed files with 603 additions and 470 deletions

View File

@@ -1,42 +0,0 @@
using Content.Server.Advertisements;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Advertise
{
[RegisterComponent, Access(typeof(AdvertiseSystem))]
public sealed partial class AdvertiseComponent : Component
{
/// <summary>
/// Minimum time in seconds to wait before saying a new ad, in seconds. Has to be larger than or equal to 1.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("minWait")]
public int MinimumWait { get; private set; } = 8 * 60;
/// <summary>
/// Maximum time in seconds to wait before saying a new ad, in seconds. Has to be larger than or equal
/// to <see cref="MinimumWait"/>
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("maxWait")]
public int MaximumWait { get; private set; } = 10 * 60;
/// <summary>
/// The identifier for the advertisements pack prototype.
/// </summary>
[DataField("pack", customTypeSerializer:typeof(PrototypeIdSerializer<AdvertisementsPackPrototype>), required: true)]
public string PackPrototypeId { get; private set; } = string.Empty;
/// <summary>
/// The next time an advertisement will be said.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public TimeSpan NextAdvertisementTime { get; set; } = TimeSpan.Zero;
/// <summary>
/// Whether the entity will say advertisements or not.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool Enabled { get; set; } = true;
}
}

View File

@@ -1,154 +0,0 @@
using Content.Server.Advertisements;
using Content.Server.Chat.Systems;
using Content.Server.Power.Components;
using Content.Shared.VendingMachines;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Advertise
{
public sealed class AdvertiseSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly ChatSystem _chat = default!;
/// <summary>
/// The maximum amount of time between checking if advertisements should be displayed
/// </summary>
private readonly TimeSpan _maximumNextCheckDuration = TimeSpan.FromSeconds(15);
/// <summary>
/// The next time the game will check if advertisements should be displayed
/// </summary>
private TimeSpan _nextCheckTime = TimeSpan.MaxValue;
public override void Initialize()
{
SubscribeLocalEvent<AdvertiseComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<AdvertiseComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<ApcPowerReceiverComponent, AdvertiseEnableChangeAttemptEvent>(OnPowerReceiverEnableChangeAttempt);
SubscribeLocalEvent<VendingMachineComponent, AdvertiseEnableChangeAttemptEvent>(OnVendingEnableChangeAttempt);
// The component inits will lower this.
_nextCheckTime = TimeSpan.MaxValue;
}
private void OnComponentInit(EntityUid uid, AdvertiseComponent advertise, ComponentInit args)
{
RefreshTimer(uid, advertise);
}
private void OnPowerChanged(EntityUid uid, AdvertiseComponent advertise, ref PowerChangedEvent args)
{
SetEnabled(uid, args.Powered, advertise);
}
public void RefreshTimer(EntityUid uid, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (!advertise.Enabled)
return;
var minDuration = Math.Max(1, advertise.MinimumWait);
var maxDuration = Math.Max(minDuration, advertise.MaximumWait);
var waitDuration = TimeSpan.FromSeconds(_random.Next(minDuration, maxDuration));
var nextTime = _gameTiming.CurTime + waitDuration;
advertise.NextAdvertisementTime = nextTime;
_nextCheckTime = MathHelper.Min(nextTime, _nextCheckTime);
}
public void SayAdvertisement(EntityUid uid, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (_prototypeManager.TryIndex(advertise.PackPrototypeId, out AdvertisementsPackPrototype? advertisements))
_chat.TrySendInGameICMessage(uid, Loc.GetString(_random.Pick(advertisements.Advertisements)), InGameICChatType.Speak, true);
}
public void SayThankYou(EntityUid uid, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (_prototypeManager.TryIndex(advertise.PackPrototypeId, out AdvertisementsPackPrototype? advertisements))
{
_chat.TrySendInGameICMessage(uid, Loc.GetString(_random.Pick(advertisements.ThankYous), ("name", Name(uid))), InGameICChatType.Speak, true);
}
}
public void SetEnabled(EntityUid uid, bool enable, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (advertise.Enabled == enable)
return;
var attemptEvent = new AdvertiseEnableChangeAttemptEvent(enable);
RaiseLocalEvent(uid, attemptEvent);
if (attemptEvent.Cancelled)
return;
advertise.Enabled = enable;
RefreshTimer(uid, advertise);
}
private static void OnPowerReceiverEnableChangeAttempt(EntityUid uid, ApcPowerReceiverComponent component, AdvertiseEnableChangeAttemptEvent args)
{
if(args.Enabling && !component.Powered)
args.Cancel();
}
private static void OnVendingEnableChangeAttempt(EntityUid uid, VendingMachineComponent component, AdvertiseEnableChangeAttemptEvent args)
{
if(args.Enabling && component.Broken)
args.Cancel();
}
public override void Update(float frameTime)
{
var curTime = _gameTiming.CurTime;
if (_nextCheckTime > curTime)
return;
_nextCheckTime = curTime + _maximumNextCheckDuration;
var query = EntityQueryEnumerator<AdvertiseComponent>();
while (query.MoveNext(out var uid, out var advert))
{
if (!advert.Enabled)
continue;
// If this isn't advertising yet
if (advert.NextAdvertisementTime > curTime)
{
_nextCheckTime = MathHelper.Min(advert.NextAdvertisementTime, _nextCheckTime);
continue;
}
SayAdvertisement(uid, advert);
RefreshTimer(uid, advert);
}
}
}
public sealed class AdvertiseEnableChangeAttemptEvent : CancellableEntityEventArgs
{
public bool Enabling { get; }
public AdvertiseEnableChangeAttemptEvent(bool enabling)
{
Enabling = enabling;
}
}
}

View File

@@ -0,0 +1,45 @@
using Content.Server.Advertise.EntitySystems;
using Content.Shared.Advertise;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Advertise.Components;
/// <summary>
/// Makes this entity periodically advertise by speaking a randomly selected
/// message from a specified MessagePack into local chat.
/// </summary>
[RegisterComponent, Access(typeof(AdvertiseSystem))]
public sealed partial class AdvertiseComponent : Component
{
/// <summary>
/// Minimum time in seconds to wait before saying a new ad, in seconds. Has to be larger than or equal to 1.
/// </summary>
[DataField]
public int MinimumWait { get; private set; } = 8 * 60;
/// <summary>
/// Maximum time in seconds to wait before saying a new ad, in seconds. Has to be larger than or equal
/// to <see cref="MinimumWait"/>
/// </summary>
[DataField]
public int MaximumWait { get; private set; } = 10 * 60;
/// <summary>
/// The identifier for the advertisements pack prototype.
/// </summary>
[DataField(required: true)]
public ProtoId<MessagePackPrototype> Pack { get; private set; }
/// <summary>
/// The next time an advertisement will be said.
/// </summary>
[DataField]
public TimeSpan NextAdvertisementTime { get; set; } = TimeSpan.Zero;
/// <summary>
/// Whether the entity will say advertisements or not.
/// </summary>
[DataField]
public bool Enabled { get; set; } = true;
}

View File

@@ -0,0 +1,36 @@
using Content.Shared.Advertise;
using Robust.Shared.Prototypes;
namespace Content.Server.Advertise.Components;
/// <summary>
/// Causes the entity to speak using the Chat system when its ActivatableUI is closed, optionally
/// requiring that a Flag be set as well.
/// </summary>
[RegisterComponent, Access(typeof(SpeakOnUIClosedSystem))]
public sealed partial class SpeakOnUIClosedComponent : Component
{
/// <summary>
/// The identifier for the message pack prototype containing messages to be spoken by this entity.
/// </summary>
[DataField(required: true)]
public ProtoId<MessagePackPrototype> Pack { get; private set; }
/// <summary>
/// Is this component active? If false, no messages will be spoken.
/// </summary>
[DataField]
public bool Enabled = true;
/// <summary>
/// Should messages be spoken only if the <see cref="Flag"/> is set (true), or every time the UI is closed (false)?
/// </summary>
[DataField]
public bool RequireFlag = true;
/// <summary>
/// State variable only used if <see cref="RequireFlag"/> is true. Set with <see cref="SpeakOnUIClosedSystem.TrySetFlag"/>.
/// </summary>
[DataField]
public bool Flag;
}

View File

@@ -0,0 +1,137 @@
using Content.Server.Advertise.Components;
using Content.Server.Chat.Systems;
using Content.Server.Power.Components;
using Content.Shared.VendingMachines;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Advertise.EntitySystems;
public sealed class AdvertiseSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly ChatSystem _chat = default!;
/// <summary>
/// The maximum amount of time between checking if advertisements should be displayed
/// </summary>
private readonly TimeSpan _maximumNextCheckDuration = TimeSpan.FromSeconds(15);
/// <summary>
/// The next time the game will check if advertisements should be displayed
/// </summary>
private TimeSpan _nextCheckTime = TimeSpan.MaxValue;
public override void Initialize()
{
SubscribeLocalEvent<AdvertiseComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<AdvertiseComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<ApcPowerReceiverComponent, AdvertiseEnableChangeAttemptEvent>(OnPowerReceiverEnableChangeAttempt);
SubscribeLocalEvent<VendingMachineComponent, AdvertiseEnableChangeAttemptEvent>(OnVendingEnableChangeAttempt);
// The component inits will lower this.
_nextCheckTime = TimeSpan.MaxValue;
}
private void OnMapInit(EntityUid uid, AdvertiseComponent advertise, MapInitEvent args)
{
RefreshTimer(uid, advertise);
}
private void OnPowerChanged(EntityUid uid, AdvertiseComponent advertise, ref PowerChangedEvent args)
{
SetEnabled(uid, args.Powered, advertise);
}
public void RefreshTimer(EntityUid uid, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (!advertise.Enabled)
return;
var minDuration = Math.Max(1, advertise.MinimumWait);
var maxDuration = Math.Max(minDuration, advertise.MaximumWait);
var waitDuration = TimeSpan.FromSeconds(_random.Next(minDuration, maxDuration));
var nextTime = _gameTiming.CurTime + waitDuration;
advertise.NextAdvertisementTime = nextTime;
_nextCheckTime = MathHelper.Min(nextTime, _nextCheckTime);
}
public void SayAdvertisement(EntityUid uid, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (_prototypeManager.TryIndex(advertise.Pack, out var advertisements))
_chat.TrySendInGameICMessage(uid, Loc.GetString(_random.Pick(advertisements.Messages)), InGameICChatType.Speak, hideChat: true);
}
public void SetEnabled(EntityUid uid, bool enable, AdvertiseComponent? advertise = null)
{
if (!Resolve(uid, ref advertise))
return;
if (advertise.Enabled == enable)
return;
var attemptEvent = new AdvertiseEnableChangeAttemptEvent(enable);
RaiseLocalEvent(uid, attemptEvent);
if (attemptEvent.Cancelled)
return;
advertise.Enabled = enable;
RefreshTimer(uid, advertise);
}
private static void OnPowerReceiverEnableChangeAttempt(EntityUid uid, ApcPowerReceiverComponent component, AdvertiseEnableChangeAttemptEvent args)
{
if (args.Enabling && !component.Powered)
args.Cancel();
}
private static void OnVendingEnableChangeAttempt(EntityUid uid, VendingMachineComponent component, AdvertiseEnableChangeAttemptEvent args)
{
if (args.Enabling && component.Broken)
args.Cancel();
}
public override void Update(float frameTime)
{
var curTime = _gameTiming.CurTime;
if (_nextCheckTime > curTime)
return;
_nextCheckTime = curTime + _maximumNextCheckDuration;
var query = EntityQueryEnumerator<AdvertiseComponent>();
while (query.MoveNext(out var uid, out var advert))
{
if (!advert.Enabled)
continue;
// If this isn't advertising yet
if (advert.NextAdvertisementTime > curTime)
{
_nextCheckTime = MathHelper.Min(advert.NextAdvertisementTime, _nextCheckTime);
continue;
}
SayAdvertisement(uid, advert);
RefreshTimer(uid, advert);
}
}
}
public sealed class AdvertiseEnableChangeAttemptEvent(bool enabling) : CancellableEntityEventArgs
{
public bool Enabling { get; } = enabling;
}

View File

@@ -0,0 +1,58 @@
using Content.Server.Advertise.Components;
using Content.Server.Chat.Systems;
using Content.Server.UserInterface;
using Content.Shared.Advertise;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Advertise;
public sealed partial class SpeakOnUIClosedSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly ChatSystem _chat = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpeakOnUIClosedComponent, BoundUIClosedEvent>(OnBoundUIClosed);
}
private void OnBoundUIClosed(Entity<SpeakOnUIClosedComponent> entity, ref BoundUIClosedEvent args)
{
if (!TryComp(entity, out ActivatableUIComponent? activatable) || !args.UiKey.Equals(activatable.Key))
return;
if (entity.Comp.RequireFlag && !entity.Comp.Flag)
return;
TrySpeak((entity, entity.Comp));
}
public bool TrySpeak(Entity<SpeakOnUIClosedComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp))
return false;
if (!entity.Comp.Enabled)
return false;
if (!_prototypeManager.TryIndex(entity.Comp.Pack, out MessagePackPrototype? messagePack))
return false;
var message = Loc.GetString(_random.Pick(messagePack.Messages), ("name", Name(entity)));
_chat.TrySendInGameICMessage(entity, message, InGameICChatType.Speak, true);
entity.Comp.Flag = false;
return true;
}
public bool TrySetFlag(Entity<SpeakOnUIClosedComponent?> entity, bool value = true)
{
if (!Resolve(entity, ref entity.Comp))
return false;
entity.Comp.Flag = value;
return true;
}
}

View File

@@ -1,18 +0,0 @@
using Robust.Shared.Prototypes;
namespace Content.Server.Advertisements
{
[Serializable, Prototype("advertisementsPack")]
public sealed partial class AdvertisementsPackPrototype : IPrototype
{
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;
[DataField("advertisements")]
public List<string> Advertisements { get; private set; } = new();
[DataField("thankyous")]
public List<string> ThankYous { get; private set; } = new();
}
}

View File

@@ -1,12 +1,11 @@
using System.Linq;
using System.Numerics;
using Content.Server.Advertise;
using Content.Server.Advertise.Components;
using Content.Server.Cargo.Systems;
using Content.Server.Chat.Systems;
using Content.Server.Emp;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.UserInterface;
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Actions;
@@ -25,7 +24,6 @@ using Robust.Shared.Audio;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.VendingMachines
{
@@ -39,7 +37,7 @@ namespace Content.Server.VendingMachines
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly AdvertiseSystem _advertise = default!;
[Dependency] private readonly SpeakOnUIClosedSystem _speakOnUIClosed = default!;
public override void Initialize()
{
@@ -58,7 +56,6 @@ namespace Content.Server.VendingMachines
Subs.BuiEvents<VendingMachineComponent>(VendingMachineUiKey.Key, subs =>
{
subs.Event<BoundUIOpenedEvent>(OnBoundUIOpened);
subs.Event<BoundUIClosedEvent>(OnBoundUIClosed);
subs.Event<VendingMachineEjectMessage>(OnInventoryEjectMessage);
});
@@ -114,16 +111,6 @@ namespace Content.Server.VendingMachines
UpdateVendingMachineInterfaceState(uid, component);
}
private void OnBoundUIClosed(EntityUid uid, VendingMachineComponent component, BoundUIClosedEvent args)
{
// Only vendors that advertise will send message after dispensing
if (component.ShouldSayThankYou && TryComp<AdvertiseComponent>(uid, out var advertise))
{
_advertise.SayThankYou(uid, advertise);
component.ShouldSayThankYou = false;
}
}
private void UpdateVendingMachineInterfaceState(EntityUid uid, VendingMachineComponent component)
{
var state = new VendingMachineInterfaceState(GetAllInventory(uid, component));
@@ -294,7 +281,10 @@ namespace Content.Server.VendingMachines
vendComponent.Ejecting = true;
vendComponent.NextItemToEject = entry.ID;
vendComponent.ThrowNextItem = throwItem;
vendComponent.ShouldSayThankYou = true;
if (TryComp(uid, out SpeakOnUIClosedComponent? speakComponent))
_speakOnUIClosed.TrySetFlag((uid, speakComponent));
entry.Amount--;
UpdateVendingMachineInterfaceState(uid, vendComponent);
TryUpdateVisualState(uid, vendComponent);