Skip to content

Commit

Permalink
Track rule reading in database, don't show popup locally (#7278)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSmugleaf authored Mar 26, 2022
1 parent 19d8824 commit ca0fb3c
Show file tree
Hide file tree
Showing 13 changed files with 2,302 additions and 53 deletions.
9 changes: 0 additions & 9 deletions Content.Client/Info/RulesAndInfoWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.IoC;
using Robust.Shared.Localization;

namespace Content.Client.Info
{
Expand Down Expand Up @@ -62,12 +60,5 @@ private void AddSection(Info info, string title, string path, bool markup = fals
info.InfoContainer.AddChild(new InfoSection(title,
_resourceManager.ContentFileReadAllText($"/Server Info/{path}"), markup));
}

protected override void Opened()
{
base.Opened();

_rulesManager.SaveLastReadTime();
}
}
}
41 changes: 14 additions & 27 deletions Content.Client/Info/RulesManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Globalization;
using Content.Client.Lobby;
using Content.Client.Viewport;
using Content.Shared.CCVar;
Expand All @@ -8,28 +6,33 @@
using Robust.Client.State;
using Robust.Client.UserInterface;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Network;
using Robust.Shared.Utility;

namespace Content.Client.Info;

public sealed class RulesManager : SharedRulesManager
{
[Dependency] private readonly IResourceManager _resource = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
[Dependency] private readonly IStateManager _stateManager = default!;
[Dependency] private readonly IClientConsoleHost _consoleHost = default!;
[Dependency] private readonly INetManager _netManager = default!;

private bool _shouldShowRules;

public void Initialize()
{
_netManager.RegisterNetMessage<ShouldShowRulesPopupMessage>(OnShouldShowRules);
_netManager.RegisterNetMessage<ShowRulesPopupMessage>(OnShowRulesPopupMessage);
_netManager.RegisterNetMessage<RulesAcceptedMessage>();
_stateManager.OnStateChanged += OnStateChanged;
}

private void OnShouldShowRules(ShouldShowRulesPopupMessage message)
{
_shouldShowRules = true;
}

private void OnShowRulesPopupMessage(ShowRulesPopupMessage message)
{
ShowRules(message.PopupTime);
Expand All @@ -39,30 +42,13 @@ private void OnStateChanged(StateChangedEventArgs args)
{
if (args.NewState is not (GameScreen or LobbyState))
return;
_stateManager.OnStateChanged -= OnStateChanged;

var path = new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}");
var showRules = true;
if (_resource.UserData.TryReadAllText(path, out var lastReadTimeText)
&& DateTime.TryParse(lastReadTimeText, null, DateTimeStyles.AssumeUniversal, out var lastReadTime))
showRules = lastReadTime < DateTime.UtcNow - TimeSpan.FromDays(60);
else
SaveLastReadTime();

if (!showRules)
if (!_shouldShowRules)
return;

ShowRules(_configManager.GetCVar(CCVars.RulesWaitTime));
}
_shouldShowRules = false;

/// <summary>
/// Ran when the user opens ("read") the rules, stores the new ID to disk.
/// </summary>
public void SaveLastReadTime()
{
using var sw = _resource.UserData.OpenWriteText(new ResourcePath($"/rules_last_seen_{_configManager.GetCVar(CCVars.ServerId)}"));

sw.Write(DateTime.UtcNow.ToUniversalTime());
ShowRules(_configManager.GetCVar(CCVars.RulesWaitTime));
}

private void ShowRules(float time)
Expand All @@ -83,6 +69,7 @@ private void OnQuitPressed()

private void OnAcceptPressed()
{
SaveLastReadTime();
var message = _netManager.CreateNetMessage<RulesAcceptedMessage>();
_netManager.ClientSendMessage(message);
}
}
Loading

0 comments on commit ca0fb3c

Please sign in to comment.