Cleanup TipsSystem, add TippyOnTrigger (#41477)

* tippy on trigger and refactor

* optional parameter for command
This commit is contained in:
slarticodefast
2025-11-18 19:09:49 +01:00
committed by GitHub
parent 9bab0a3287
commit e3c0e01934
9 changed files with 376 additions and 177 deletions

View File

@@ -4,10 +4,7 @@ using Content.Shared.CCVar;
using Content.Shared.Chat;
using Content.Shared.Dataset;
using Content.Shared.Tips;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
@@ -15,10 +12,7 @@ using Robust.Shared.Timing;
namespace Content.Server.Tips;
/// <summary>
/// Handles periodically displaying gameplay tips to all players ingame.
/// </summary>
public sealed class TipsSystem : EntitySystem
public sealed class TipsSystem : SharedTipsSystem
{
[Dependency] private readonly IChatManager _chat = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
@@ -26,8 +20,6 @@ public sealed class TipsSystem : EntitySystem
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly GameTicker _ticker = default!;
[Dependency] private readonly IConsoleHost _conHost = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
private bool _tipsEnabled;
private float _tipTimeOutOfRound;
@@ -35,16 +27,6 @@ public sealed class TipsSystem : EntitySystem
private string _tipsDataset = "";
private float _tipTippyChance;
/// <summary>
/// Always adds this time to a speech message. This is so really short message stay around for a bit.
/// </summary>
private const float SpeechBuffer = 3f;
/// <summary>
/// Expected reading speed.
/// </summary>
private const float Wpm = 180f;
[ViewVariables(VVAccess.ReadWrite)]
private TimeSpan _nextTipTime = TimeSpan.Zero;
@@ -53,110 +35,45 @@ public sealed class TipsSystem : EntitySystem
base.Initialize();
SubscribeLocalEvent<GameRunLevelChangedEvent>(OnGameRunLevelChanged);
Subs.CVar(_cfg, CCVars.TipFrequencyOutOfRound, SetOutOfRound, true);
Subs.CVar(_cfg, CCVars.TipFrequencyInRound, SetInRound, true);
Subs.CVar(_cfg, CCVars.TipsEnabled, SetEnabled, true);
Subs.CVar(_cfg, CCVars.TipsDataset, SetDataset, true);
Subs.CVar(_cfg, CCVars.TipsTippyChance, SetTippyChance, true);
Subs.CVar(_cfg, CCVars.TipFrequencyOutOfRound, value => _tipTimeOutOfRound = value, true);
Subs.CVar(_cfg, CCVars.TipFrequencyInRound, value => _tipTimeInRound = value, true);
Subs.CVar(_cfg, CCVars.TipsDataset, value => _tipsDataset = value, true);
Subs.CVar(_cfg, CCVars.TipsTippyChance, value => _tipTippyChance = value, true);
RecalculateNextTipTime();
_conHost.RegisterCommand("tippy", Loc.GetString("cmd-tippy-desc"), Loc.GetString("cmd-tippy-help"), SendTippy, SendTippyHelper);
_conHost.RegisterCommand("tip", Loc.GetString("cmd-tip-desc"), "tip", SendTip);
}
private CompletionResult SendTippyHelper(IConsoleShell shell, string[] args)
private void OnGameRunLevelChanged(GameRunLevelChangedEvent ev)
{
return args.Length switch
// reset for lobby -> inround
// reset for inround -> post but not post -> lobby
if (ev.New == GameRunLevel.InRound || ev.Old == GameRunLevel.InRound)
{
1 => CompletionResult.FromHintOptions(
CompletionHelper.SessionNames(players: _playerManager),
Loc.GetString("cmd-tippy-auto-1")),
2 => CompletionResult.FromHint(Loc.GetString("cmd-tippy-auto-2")),
3 => CompletionResult.FromHintOptions(
CompletionHelper.PrototypeIdsLimited<EntityPrototype>(args[2], _prototype),
Loc.GetString("cmd-tippy-auto-3")),
4 => CompletionResult.FromHint(Loc.GetString("cmd-tippy-auto-4")),
5 => CompletionResult.FromHint(Loc.GetString("cmd-tippy-auto-5")),
6 => CompletionResult.FromHint(Loc.GetString("cmd-tippy-auto-6")),
_ => CompletionResult.Empty
};
RecalculateNextTipTime();
}
}
private void SendTip(IConsoleShell shell, string argstr, string[] args)
private void SetEnabled(bool value)
{
AnnounceRandomTip();
RecalculateNextTipTime();
_tipsEnabled = value;
if (_nextTipTime != TimeSpan.Zero)
RecalculateNextTipTime();
}
private void SendTippy(IConsoleShell shell, string argstr, string[] args)
public override void RecalculateNextTipTime()
{
if (args.Length < 2)
if (_ticker.RunLevel == GameRunLevel.InRound)
{
shell.WriteLine(Loc.GetString("cmd-tippy-help"));
return;
_nextTipTime = _timing.CurTime + TimeSpan.FromSeconds(_tipTimeInRound);
}
ActorComponent? actor = null;
if (args[0] != "all")
{
ICommonSession? session;
if (args.Length > 0)
{
// Get player entity
if (!_playerManager.TryGetSessionByUsername(args[0], out session))
{
shell.WriteLine(Loc.GetString("cmd-tippy-error-no-user"));
return;
}
}
else
{
session = shell.Player;
}
if (session?.AttachedEntity is not { } user)
{
shell.WriteLine(Loc.GetString("cmd-tippy-error-no-user"));
return;
}
if (!TryComp(user, out actor))
{
shell.WriteError(Loc.GetString("cmd-tippy-error-no-user"));
return;
}
}
var ev = new TippyEvent(args[1]);
if (args.Length > 2)
{
ev.Proto = args[2];
if (!_prototype.HasIndex<EntityPrototype>(args[2]))
{
shell.WriteError(Loc.GetString("cmd-tippy-error-no-prototype", ("proto", args[2])));
return;
}
}
if (args.Length > 3)
ev.SpeakTime = float.Parse(args[3]);
else
ev.SpeakTime = GetSpeechTime(ev.Msg);
if (args.Length > 4)
ev.SlideTime = float.Parse(args[4]);
if (args.Length > 5)
ev.WaddleInterval = float.Parse(args[5]);
if (actor != null)
RaiseNetworkEvent(ev, actor.PlayerSession);
else
RaiseNetworkEvent(ev);
{
_nextTipTime = _timing.CurTime + TimeSpan.FromSeconds(_tipTimeOutOfRound);
}
}
public override void Update(float frameTime)
{
base.Update(frameTime);
@@ -171,41 +88,30 @@ public sealed class TipsSystem : EntitySystem
}
}
private void SetOutOfRound(float value)
public override void SendTippy(
string message,
EntProtoId? prototype = null,
float speakTime = 5f,
float slideTime = 3f,
float waddleInterval = 0.5f)
{
_tipTimeOutOfRound = value;
var ev = new TippyEvent(message, prototype, speakTime, slideTime, waddleInterval);
RaiseNetworkEvent(ev);
}
private void SetInRound(float value)
public override void SendTippy(
ICommonSession session,
string message,
EntProtoId? prototype = null,
float speakTime = 5f,
float slideTime = 3f,
float waddleInterval = 0.5f)
{
_tipTimeInRound = value;
var ev = new TippyEvent(message, prototype, speakTime, slideTime, waddleInterval);
RaiseNetworkEvent(ev, session);
}
private void SetEnabled(bool value)
{
_tipsEnabled = value;
if (_nextTipTime != TimeSpan.Zero)
RecalculateNextTipTime();
}
private void SetDataset(string value)
{
_tipsDataset = value;
}
private void SetTippyChance(float value)
{
_tipTippyChance = value;
}
public static float GetSpeechTime(string text)
{
var wordCount = (float)text.Split().Length;
return SpeechBuffer + wordCount * (60f / Wpm);
}
private void AnnounceRandomTip()
public override void AnnounceRandomTip()
{
if (!_prototype.TryIndex<LocalizedDatasetPrototype>(_tipsDataset, out var tips))
return;
@@ -215,35 +121,20 @@ public sealed class TipsSystem : EntitySystem
if (_random.Prob(_tipTippyChance))
{
var ev = new TippyEvent(msg);
ev.SpeakTime = GetSpeechTime(msg);
RaiseNetworkEvent(ev);
} else
{
_chat.ChatMessageToManyFiltered(Filter.Broadcast(), ChatChannel.OOC, tip, msg,
EntityUid.Invalid, false, false, Color.MediumPurple);
}
}
private void RecalculateNextTipTime()
{
if (_ticker.RunLevel == GameRunLevel.InRound)
{
_nextTipTime = _timing.CurTime + TimeSpan.FromSeconds(_tipTimeInRound);
var speakTime = GetSpeechTime(msg);
SendTippy(msg, speakTime: speakTime);
}
else
{
_nextTipTime = _timing.CurTime + TimeSpan.FromSeconds(_tipTimeOutOfRound);
}
}
private void OnGameRunLevelChanged(GameRunLevelChangedEvent ev)
{
// reset for lobby -> inround
// reset for inround -> post but not post -> lobby
if (ev.New == GameRunLevel.InRound || ev.Old == GameRunLevel.InRound)
{
RecalculateNextTipTime();
_chat.ChatMessageToManyFiltered(
Filter.Broadcast(),
ChatChannel.OOC,
tip,
msg,
EntityUid.Invalid,
false,
false,
Color.MediumPurple);
}
}
}