2023-07-09 16:32:14 +03:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Text.Json;
|
2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Boyfriend.Data;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
using Remora.Rest.Core;
|
|
|
|
|
|
|
|
namespace Boyfriend.Services;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles saving, loading, initializing and providing <see cref="GuildData" />.
|
|
|
|
/// </summary>
|
2023-08-02 23:51:16 +03:00
|
|
|
public sealed class GuildDataService : IHostedService
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
|
2023-08-02 23:51:16 +03:00
|
|
|
private readonly IDiscordRestGuildAPI _guildApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
// https://github.com/dotnet/aspnetcore/issues/39139
|
|
|
|
public GuildDataService(
|
2023-08-02 23:51:16 +03:00
|
|
|
IHostApplicationLifetime lifetime, IDiscordRestGuildAPI guildApi)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_guildApi = guildApi;
|
|
|
|
lifetime.ApplicationStopping.Register(ApplicationStopping);
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public Task StartAsync(CancellationToken ct)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public Task StopAsync(CancellationToken ct)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
private void ApplicationStopping()
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
SaveAsync(CancellationToken.None).GetAwaiter().GetResult();
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
private async Task SaveAsync(CancellationToken ct)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var tasks = new List<Task>();
|
2023-08-02 23:51:16 +03:00
|
|
|
foreach (var data in _datas.Values)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
await using var settingsStream = File.OpenWrite(data.SettingsPath);
|
|
|
|
tasks.Add(JsonSerializer.SerializeAsync(settingsStream, data.Settings, cancellationToken: ct));
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
await using var eventsStream = File.OpenWrite(data.ScheduledEventsPath);
|
|
|
|
tasks.Add(JsonSerializer.SerializeAsync(eventsStream, data.ScheduledEvents, cancellationToken: ct));
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
foreach (var memberData in data.MemberData.Values)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
await using var memberDataStream = File.OpenWrite($"{data.MemberDataPath}/{memberData.Id}.json");
|
|
|
|
tasks.Add(JsonSerializer.SerializeAsync(memberDataStream, memberData, cancellationToken: ct));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<GuildData> GetData(Snowflake guildId, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return _datas.TryGetValue(guildId, out var data) ? data : await InitializeData(guildId, ct);
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
private async Task<GuildData> InitializeData(Snowflake guildId, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var idString = $"{guildId}";
|
|
|
|
var memberDataPath = $"{guildId}/MemberData";
|
2023-07-18 15:25:02 +03:00
|
|
|
var settingsPath = $"{guildId}/Settings.json";
|
2023-07-09 16:32:14 +03:00
|
|
|
var scheduledEventsPath = $"{guildId}/ScheduledEvents.json";
|
2023-08-04 16:52:54 +03:00
|
|
|
Directory.CreateDirectory(idString);
|
2023-08-02 23:51:16 +03:00
|
|
|
|
|
|
|
if (!File.Exists(settingsPath))
|
|
|
|
{
|
|
|
|
await File.WriteAllTextAsync(settingsPath, "{}", ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!File.Exists(scheduledEventsPath))
|
|
|
|
{
|
|
|
|
await File.WriteAllTextAsync(scheduledEventsPath, "{}", ct);
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
await using var settingsStream = File.OpenRead(settingsPath);
|
|
|
|
var jsonSettings
|
|
|
|
= JsonNode.Parse(settingsStream);
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
await using var eventsStream = File.OpenRead(scheduledEventsPath);
|
|
|
|
var events
|
|
|
|
= JsonSerializer.DeserializeAsync<Dictionary<ulong, ScheduledEventData>>(
|
|
|
|
eventsStream, cancellationToken: ct);
|
|
|
|
|
|
|
|
var memberData = new Dictionary<ulong, MemberData>();
|
2023-08-04 16:52:54 +03:00
|
|
|
foreach (var dataFileInfo in Directory.CreateDirectory(memberDataPath).GetFiles())
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-08-04 16:52:54 +03:00
|
|
|
await using var dataStream = dataFileInfo.OpenRead();
|
2023-07-09 16:32:14 +03:00
|
|
|
var data = await JsonSerializer.DeserializeAsync<MemberData>(dataStream, cancellationToken: ct);
|
2023-08-02 23:51:16 +03:00
|
|
|
if (data is null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:25:02 +03:00
|
|
|
var memberResult = await _guildApi.GetGuildMemberAsync(guildId, data.Id.ToSnowflake(), ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
if (memberResult.IsSuccess)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
data.Roles = memberResult.Entity.Roles.ToList().ConvertAll(r => r.Value);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
memberData.Add(data.Id, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
var finalData = new GuildData(
|
2023-07-18 15:25:02 +03:00
|
|
|
jsonSettings ?? new JsonObject(), settingsPath,
|
2023-07-09 16:32:14 +03:00
|
|
|
await events ?? new Dictionary<ulong, ScheduledEventData>(), scheduledEventsPath,
|
|
|
|
memberData, memberDataPath);
|
2023-08-04 16:52:54 +03:00
|
|
|
|
|
|
|
_datas.TryAdd(guildId, finalData);
|
2023-08-02 23:51:16 +03:00
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
return finalData;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<JsonNode> GetSettings(Snowflake guildId, CancellationToken ct = default)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return (await GetData(guildId, ct)).Settings;
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public async Task<MemberData> GetMemberData(Snowflake guildId, Snowflake userId, CancellationToken ct = default)
|
|
|
|
{
|
2023-08-04 16:52:54 +03:00
|
|
|
return (await GetData(guildId, ct)).GetOrCreateMemberData(userId);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public ICollection<Snowflake> GetGuildIds()
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return _datas.Keys;
|
|
|
|
}
|
|
|
|
}
|