mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-27 07:59:03 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RadicadeServer.DB;
|
||||
using RadicadeServer.Extensions;
|
||||
using RadicadeServer.RecNet.Enums.Players;
|
||||
using RadicadeServer.RecNet.Notifications;
|
||||
|
||||
namespace RadicadeServer.API
|
||||
{
|
||||
[Route("api/announcement")]
|
||||
public class AnnouncementAPI : Controller
|
||||
{
|
||||
RecDBContext db;
|
||||
public AnnouncementAPI(RecDBContext context)
|
||||
{
|
||||
this.db = context;
|
||||
}
|
||||
|
||||
[HttpGet("v1/get")]
|
||||
public List<Announcement>? GetAnnouncement()
|
||||
{
|
||||
var config = db.ServerConfig.FirstOrDefault(x => x.Key == "Announcement");
|
||||
if (config == null || string.IsNullOrWhiteSpace(config.Value) || config.Value == "[]")
|
||||
return new List<Announcement>();
|
||||
|
||||
try
|
||||
{
|
||||
var announcements = config.Value.FromJson<List<Announcement>>() ?? new List<Announcement>();
|
||||
|
||||
|
||||
|
||||
var raw = System.Text.Json.JsonDocument.Parse(config.Value);
|
||||
if (raw.RootElement.ValueKind == System.Text.Json.JsonValueKind.Array)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var keep = new List<Announcement>();
|
||||
int i = 0;
|
||||
foreach (var element in raw.RootElement.EnumerateArray())
|
||||
{
|
||||
var ann = i < announcements.Count ? announcements[i] : null;
|
||||
if (ann != null)
|
||||
{
|
||||
if (element.TryGetProperty("type", out var typeProp) && typeProp.ValueKind == System.Text.Json.JsonValueKind.String &&
|
||||
string.Equals(typeProp.GetString(), "Game Update", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
string? expiresAt = null;
|
||||
if (element.TryGetProperty("expires_at", out var exp) && exp.ValueKind == System.Text.Json.JsonValueKind.String)
|
||||
expiresAt = exp.GetString();
|
||||
|
||||
if (string.IsNullOrEmpty(expiresAt) || !DateTime.TryParse(expiresAt, out var expiry) || expiry > now)
|
||||
keep.Add(ann);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
announcements = keep;
|
||||
}
|
||||
|
||||
|
||||
announcements = announcements.OrderByDescending(x => x.CreatedAt).ToList();
|
||||
foreach (var ann in announcements)
|
||||
{
|
||||
ann.IsMostRecent = false;
|
||||
|
||||
|
||||
|
||||
ann.Platform = Platforms.All;
|
||||
}
|
||||
if (announcements.Count > 0)
|
||||
announcements[0].IsMostRecent = true;
|
||||
|
||||
return announcements;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<Announcement>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user