Files
tbd-station-14/Content.Client/UserInterface/Systems/BugReport/Windows/BugReportWindow.xaml.cs
beck-thompson a8d6dbc324 Added button and manager for in game bug reports (Part 1) (#35350)
* Added the button and manager

* Minor cleanup

* Reigstered to the wrong thing!

* Unload UI

* Address the review

* First commit :)

* Some cleanup

* Added some comments and now the placehoder text goes away once you start typing

* Some cleanup and better test command

* Basic rate limiter class (Not finished)

* Cleanup

* Removed forgotten comment xD

* Whitespace removal

* Minor cleanup, cvar hours -> minutes

* More minor tweaks

* Don't cache timer and add examples to fields

* Added CCvar for time between bug reports

* Minor crash when restarting rounds fixed

* It compiled on my computer!

* Fix comment indents

* Remove unecessary async, removed magic strings, simplfied sawmill to not use post inject

* Make struct private

* Simplfiy TryGetLongHeader

* Changed list to enumerable

* URI cleanup

* Got rid of the queue, used a much better way!

* Made the comments a little better and fix some issues with them

* Added header consts

* Maximum reports per round is now an error message

* Time between reports is now in seconds

* Change ordering

* Change hotkey to O

* only update window when its open

* Split up validation

* address review

* Address a few issues

* inheritance fix

* API now doesn't keep track of requests, just uses the rate limited response from github

* Rough idea of how channels would work

* refactor: reorganized code, placed rate limiter into http-client-handler AND manager (usually only manager-one should work)

* cleanup

* Add user agent so api doesn't get mad

* Better error logs

* Cleanup

* It now throws!

* refactor: renaming, moved some methods, xml-doc cleanups

* refactor: BugReportWindow formatted to convention, enforced 1 updates only 1 per sec

* Add very basic licence info

* Fixed the issues!

* Set ccvar default to false

* make the button better

* fix test fail silly me

* Adress the review!

* refactor: cleanup of entry point code, binding server-side code with client-facing manager

* Resolve the other issues and cleanup and stuff smile :)

* not entity

* fixes

* Cleanup

* Cleanup

* forgor region

* fixes

* Split up function and more stuff

* Better unsubs yaygit add -A

* I pray...

* Revert "I pray..."

This reverts commit 9629fb4f1289c9009a03e4e4facd9ae975e6303e.

* I think I have to add it in the pr

* Revert "I think I have to add it in the pr"

This reverts commit e185b42f570fe5f0f51e0e44761d7938e22e67f7.

* Tweaks

* Minor tweak to permissions

---------

Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
2025-08-15 09:10:38 -07:00

182 lines
7.1 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Content.Client.Players.PlayTimeTracking;
using Content.Shared.BugReport;
using Content.Shared.CCVar;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Configuration;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.UserInterface.Systems.BugReport.Windows;
[GenerateTypedNameReferences]
public sealed partial class BugReportWindow : DefaultWindow
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
// TODO: Use SharedPlaytimeManager when its refactored out of job requirements
[Dependency] private readonly JobRequirementsManager _job = default!;
// This action gets invoked when the user submits a bug report.
public event Action<PlayerBugReportInformation>? OnBugReportSubmitted;
private DateTime _lastIsEnabledUpdated;
private readonly TimeSpan _isEnabledUpdateInterval = TimeSpan.FromSeconds(1);
// These are NOT always up to date. If someone disconnects and reconnects, the values will be reset.
// The only other way of getting updated values would be a message from client -> server then from server -> client.
// I don't think that is worth the added complexity.
private DateTime _lastBugReportSubmittedTime = DateTime.MinValue;
private int _amountOfBugReportsSubmitted;
private readonly ConfigurationMultiSubscriptionBuilder _configSub;
#region ccvar
private bool _enablePlayerBugReports;
private int _minimumPlaytimeBugReports;
private int _minimumTimeBetweenBugReports;
private int _maximumBugReportsPerRound;
private int _maximumBugReportTitleLength;
private int _minimumBugReportTitleLength;
private int _maximumBugReportDescriptionLength;
private int _minimumBugReportDescriptionLength;
#endregion
public BugReportWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_configSub = _cfg.SubscribeMultiple()
.OnValueChanged(CCVars.EnablePlayerBugReports, x => _enablePlayerBugReports = x, true)
.OnValueChanged(CCVars.MinimumPlaytimeInMinutesToEnableBugReports, x => _minimumPlaytimeBugReports = x, true)
.OnValueChanged(CCVars.MinimumSecondsBetweenBugReports, x => _minimumTimeBetweenBugReports = x, true)
.OnValueChanged(CCVars.MaximumBugReportsPerRound, x => _maximumBugReportsPerRound = x, true)
.OnValueChanged(CCVars.MaximumBugReportTitleLength, x => _maximumBugReportTitleLength = x, true)
.OnValueChanged(CCVars.MinimumBugReportTitleLength, x => _minimumBugReportTitleLength = x, true)
.OnValueChanged(CCVars.MaximumBugReportDescriptionLength, x => _maximumBugReportDescriptionLength = x, true)
.OnValueChanged(CCVars.MinimumBugReportDescriptionLength, x => _minimumBugReportDescriptionLength = x, true);
// Hook up the events
SubmitButton.OnPressed += _ => OnSubmitButtonPressed();
BugReportTitle.OnTextChanged += _ => HandleInputChange();
BugReportDescription.OnTextChanged += _ => HandleInputChange();
OnOpen += UpdateEnabled;
HandleInputChange();
UpdateEnabled();
}
private void OnSubmitButtonPressed()
{
var report = new PlayerBugReportInformation
{
BugReportTitle = BugReportTitle.Text,
BugReportDescription = Rope.Collapse(BugReportDescription.TextRope),
};
OnBugReportSubmitted?.Invoke(report);
_lastBugReportSubmittedTime = DateTime.UtcNow;
_amountOfBugReportsSubmitted++;
BugReportTitle.Text = string.Empty;
BugReportDescription.TextRope = Rope.Leaf.Empty;
HandleInputChange();
UpdateEnabled();
}
/// <summary>
/// Deals with the user changing their input. Ensures that things that depend on what the user has inputted get updated
/// (E.g. the amount of characters they have typed)
/// </summary>
private void HandleInputChange()
{
var titleLen = BugReportTitle.Text.Length;
var descriptionLen = BugReportDescription.TextLength;
var invalidTitleLen = titleLen < _minimumBugReportTitleLength || titleLen > _maximumBugReportTitleLength;
var invalidDescriptionLen = descriptionLen < _minimumBugReportDescriptionLength || descriptionLen > _maximumBugReportDescriptionLength;
TitleCharacterCounter.Text = Loc.GetString("bug-report-window-submit-char-split", ("typed", titleLen), ("total", _maximumBugReportTitleLength));
TitleCharacterCounter.FontColorOverride = invalidTitleLen ? Color.Red : Color.Green;
DescriptionCharacterCounter.Text = Loc.GetString("bug-report-window-submit-char-split", ("typed", descriptionLen), ("total", _maximumBugReportDescriptionLength));
DescriptionCharacterCounter.FontColorOverride = invalidDescriptionLen ? Color.Red : Color.Green;
SubmitButton.Disabled = invalidTitleLen || invalidDescriptionLen;
PlaceholderCenter.Visible = descriptionLen == 0;
}
/// <summary>
/// Checks if the bug report window should be enabled for this client.
/// </summary>
private bool IsEnabled([NotNullWhen(false)] out string? errorMessage)
{
errorMessage = null;
if (!_enablePlayerBugReports)
{
errorMessage = Loc.GetString("bug-report-window-disabled-not-enabled");
return false;
}
if (TimeSpan.FromMinutes(_minimumPlaytimeBugReports) > _job.FetchOverallPlaytime())
{
errorMessage = Loc.GetString("bug-report-window-disabled-playtime");
return false;
}
if (_amountOfBugReportsSubmitted >= _maximumBugReportsPerRound)
{
errorMessage = Loc.GetString("bug-report-window-disabled-submissions", ("num", _maximumBugReportsPerRound));
return false;
}
var timeSinceLastReport = DateTime.UtcNow - _lastBugReportSubmittedTime;
var timeBetweenBugReports = TimeSpan.FromSeconds(_minimumTimeBetweenBugReports);
if (timeSinceLastReport <= timeBetweenBugReports)
{
var time = timeBetweenBugReports - timeSinceLastReport;
errorMessage = Loc.GetString("bug-report-window-disabled-cooldown", ("time", time.ToString(@"d\.hh\:mm\:ss")));
return false;
}
return true;
}
// Update the state of the window to display either the bug report window or an error explaining why you can't submit a report.
private void UpdateEnabled()
{
var isEnabled = IsEnabled(out var errorMessage);
DisabledLabel.Text = errorMessage;
DisabledLabel.Visible = !isEnabled;
BugReportContainer.Visible = isEnabled;
_lastIsEnabledUpdated = DateTime.UtcNow;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (!Visible) // Don't bother updating if no one can see the window anyway.
return;
if(DateTime.UtcNow - _lastIsEnabledUpdated > _isEnabledUpdateInterval)
UpdateEnabled();
}
public void CleanupCCvars()
{
_configSub.Dispose();
}
}