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 Microsoft.Extensions.Hosting;
|
2023-08-22 10:44:05 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Data;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Rest.Core;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Services;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <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-22 10:44:05 +03:00
|
|
|
private readonly ILogger<GuildDataService> _logger;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
// https://github.com/dotnet/aspnetcore/issues/39139
|
|
|
|
public GuildDataService(
|
2023-09-30 18:36:55 +03:00
|
|
|
IHostApplicationLifetime lifetime, ILogger<GuildDataService> logger)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-08-22 10:44:05 +03:00
|
|
|
_logger = logger;
|
2023-07-09 16:32:14 +03:00
|
|
|
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-22 10:44:05 +03:00
|
|
|
public async Task SaveAsync(CancellationToken ct)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var tasks = new List<Task>();
|
2023-09-12 16:28:46 +03:00
|
|
|
var datas = _datas.Values.ToArray();
|
|
|
|
foreach (var data in datas)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-08-22 10:46:57 +03:00
|
|
|
await using var settingsStream = File.Create(data.SettingsPath);
|
2023-07-18 15:25:02 +03:00
|
|
|
tasks.Add(JsonSerializer.SerializeAsync(settingsStream, data.Settings, cancellationToken: ct));
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-08-22 10:46:57 +03:00
|
|
|
await using var eventsStream = File.Create(data.ScheduledEventsPath);
|
2023-07-09 16:32:14 +03:00
|
|
|
tasks.Add(JsonSerializer.SerializeAsync(eventsStream, data.ScheduledEvents, cancellationToken: ct));
|
|
|
|
|
2023-09-12 16:28:46 +03:00
|
|
|
var memberDatas = data.MemberData.Values.ToArray();
|
|
|
|
foreach (var memberData in memberDatas)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-08-22 10:46:57 +03:00
|
|
|
await using var memberDataStream = File.Create($"{data.MemberDataPath}/{memberData.Id}.json");
|
2023-07-09 16:32:14 +03:00
|
|
|
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-09-22 15:33:14 +03:00
|
|
|
var path = $"GuildData/{guildId}";
|
|
|
|
var memberDataPath = $"{path}/MemberData";
|
|
|
|
var settingsPath = $"{path}/Settings.json";
|
|
|
|
var scheduledEventsPath = $"{path}/ScheduledEvents.json";
|
|
|
|
|
|
|
|
MigrateGuildData(guildId, path);
|
|
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
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
|
2023-08-22 10:44:05 +03:00
|
|
|
= await JsonSerializer.DeserializeAsync<Dictionary<ulong, ScheduledEventData>>(
|
2023-07-09 16:32:14 +03:00
|
|
|
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-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-08-22 10:44:05 +03:00
|
|
|
events ?? new Dictionary<ulong, ScheduledEventData>(), scheduledEventsPath,
|
2023-07-09 16:32:14 +03:00
|
|
|
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-09-22 15:33:14 +03:00
|
|
|
private void MigrateGuildData(Snowflake guildId, string newPath)
|
|
|
|
{
|
|
|
|
var oldPath = $"{guildId}";
|
|
|
|
|
|
|
|
if (Directory.Exists(oldPath))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory($"{newPath}/..");
|
|
|
|
Directory.Move(oldPath, newPath);
|
|
|
|
|
|
|
|
_logger.LogInformation("Moved guild data to separate folder: \"{OldPath}\" -> \"{NewPath}\"", oldPath, newPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ICollection<Snowflake> GetGuildIds()
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return _datas.Keys;
|
|
|
|
}
|
2023-10-04 15:58:56 +03:00
|
|
|
|
|
|
|
public bool UnloadGuildData(Snowflake id)
|
|
|
|
{
|
|
|
|
return _datas.TryRemove(id, out _);
|
|
|
|
}
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|