mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System.Text.Json;
|
|
using RadicadeServer.RecNet.Enums;
|
|
|
|
namespace RadicadeServer.RecNet.VersionCheck
|
|
{
|
|
public static class GameUpdateVersionCheck
|
|
{
|
|
|
|
public const string AppVersionUpdateNotificationId = "AppVersionUpdate";
|
|
|
|
public const string AnnouncementConfigKey = "Announcement";
|
|
|
|
public const string GameUpdateType = "Game Update";
|
|
|
|
public const int UpdateStage = 1;
|
|
|
|
public static VersionCheckResult Resolve(string? announcementJson, DateTime nowUtc)
|
|
=> HasLiveGameUpdate(announcementJson, nowUtc)
|
|
? new VersionCheckResult
|
|
{
|
|
VersionStatus = VersionCheckStatus.UpdateRequired,
|
|
UpdateNotificationStage = UpdateStage
|
|
}
|
|
: new VersionCheckResult { VersionStatus = VersionCheckStatus.ValidForPlay };
|
|
|
|
|
|
public static bool HasLiveGameUpdate(string? announcementJson, DateTime nowUtc)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(announcementJson)) return false;
|
|
|
|
try
|
|
{
|
|
using var doc = JsonDocument.Parse(announcementJson);
|
|
if (doc.RootElement.ValueKind != JsonValueKind.Array) return false;
|
|
|
|
foreach (var row in doc.RootElement.EnumerateArray())
|
|
{
|
|
if (row.ValueKind != JsonValueKind.Object) continue;
|
|
if (!row.TryGetProperty("type", out var type) || type.ValueKind != JsonValueKind.String) continue;
|
|
if (!string.Equals(type.GetString(), GameUpdateType, StringComparison.OrdinalIgnoreCase)) continue;
|
|
|
|
if (row.TryGetProperty("expires_at", out var expires) && expires.ValueKind == JsonValueKind.String
|
|
&& DateTime.TryParse(expires.GetString(), out var expiry) && expiry <= nowUtc)
|
|
continue;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|