155 lines
5.6 KiB
C#
155 lines
5.6 KiB
C#
using ClanTagRankCommands;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Data.Models;
|
|
//using Data.Models.Client;
|
|
using SharedLibraryCore.Interfaces;
|
|
using SharedLibraryCore;
|
|
using SharedLibraryCore.Commands;
|
|
using SharedLibraryCore.Configuration;
|
|
using SharedLibraryCore.Dtos;
|
|
using System.Threading.Tasks;
|
|
using static System.Enum;
|
|
using Microsoft.Extensions.Logging;
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading;
|
|
using SharedLibraryCore.Database.Models;
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
using System;
|
|
|
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
|
|
using Serilog.Core;
|
|
using Data.Models.Client;
|
|
|
|
namespace ClanTagRankCommands;
|
|
|
|
public class DataManager
|
|
{
|
|
public DataManager(IMetaServiceV2 metaService, ILogger<Plugin> logger)
|
|
{
|
|
_logger = logger;
|
|
_metaService = metaService;
|
|
}
|
|
|
|
private readonly IMetaServiceV2 _metaService;
|
|
private readonly ILogger _logger;
|
|
|
|
public async Task<DonatorState> ReadPersistentData(int? ClientId, IManager manager)
|
|
{
|
|
var token = manager.CancellationToken;
|
|
var availableTags = await _metaService.GetPersistentMetaValue<List<LookupValue<string>>>(EFMeta.ClientTagNameV2, token);
|
|
var matchingTag = availableTags.FirstOrDefault(tag => tag.Value == "Donator".Trim());
|
|
_logger.LogInformation("Reading persistent data for {0}", ClientId);
|
|
DonatorState clientDonatorState = DonatorState.User;
|
|
if(ClientId is not null)
|
|
{
|
|
|
|
var existingtag = await _metaService.GetPersistentMetaByLookup("ClientTagV2", "ClientTagNameV2", (int)ClientId,
|
|
token);//GetAdditionalProperty<string>(EFMeta.ClientTagNameV2);
|
|
|
|
|
|
if (existingtag is null || existingtag.Value.Contains("Guest"))
|
|
{
|
|
_logger.LogInformation("No existing tag found for {0}", ClientId);
|
|
return clientDonatorState;
|
|
}
|
|
else if (existingtag.Value.Contains("Donator"))
|
|
{
|
|
_logger.LogInformation("Existing tag: {0}", existingtag.Value);
|
|
clientDonatorState = DonatorState.Donator;
|
|
}
|
|
else if (existingtag is not null && !existingtag.Value.Contains("Guest"))
|
|
{
|
|
_logger.LogInformation("Existing tag: {0}", existingtag.Value);
|
|
clientDonatorState = DonatorState.Other;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//var clientDonatorState = client.ToPartialClient().Tag ??
|
|
//Parse<DonatorState>(value: client.ToPartialClient().Tag ?? nameof(DonatorState.User));
|
|
|
|
return clientDonatorState;
|
|
}
|
|
//public async Task<DonatorStateMeta> GetCurrentMuteState(EFClient client)
|
|
//{
|
|
// try
|
|
// {
|
|
// //await _onMuteAction.WaitAsync();
|
|
// var ClientDonatorMeta = await ReadPersistentDataV2(client);
|
|
// if (ClientDonatorMeta is not null) return ClientDonatorMeta;
|
|
|
|
// // Return null if the client doesn't have old or new meta.
|
|
// var DonatorState = await ReadPersistentDataV1(client);
|
|
// ClientDonatorMeta = new DonatorStateMeta
|
|
// {
|
|
// //Reason = muteState is null ? string.Empty : _translationLookup["PLUGINS_MUTE_MIGRATED"],
|
|
// //Expiration = muteState switch
|
|
// //{
|
|
// // null => DateTime.UtcNow,
|
|
// // MuteState.Muted => null,
|
|
// // _ => DateTime.UtcNow
|
|
// //},
|
|
// DonatorState = muteState ?? DonatorState.User,
|
|
// CommandExecuted = true
|
|
// };
|
|
|
|
// // Migrate old mute meta, else, client has no state, so set a generic one, but don't write it to database.
|
|
// if (DonatorState is not null)
|
|
// {
|
|
// ClientDonatorMeta.CommandExecuted = false;
|
|
// await WritePersistentData(client, ClientDonatorMeta);
|
|
// //await CreatePenalty(muteState.Value, Utilities.IW4MAdminClient(), client, ClientDonatorMeta.Expiration,
|
|
// // ClientDonatorMeta.Reason);
|
|
// }
|
|
// else
|
|
// {
|
|
// client.SetAdditionalProperty(Plugin.MuteKey, ClientDonatorMeta);
|
|
// }
|
|
|
|
// return ClientDonatorMeta;
|
|
// }
|
|
// finally
|
|
// {
|
|
// //if (_onMuteAction.CurrentCount == 0) _onMuteAction.Release();
|
|
// }
|
|
//}
|
|
//private async Task<DonatorStateMeta?> ReadPersistentDataV2(EFClient client)
|
|
//{
|
|
// // Get meta from client
|
|
// var clientDonatorMeta = client.GetAdditionalProperty<DonatorStateMeta>(Plugin.DonatorKey);
|
|
// if (clientDonatorMeta is not null) return clientDonatorMeta;
|
|
|
|
// // Get meta from database and store in client if exists
|
|
// clientDonatorMeta = await _metaService.GetPersistentMetaValue<DonatorStateMeta>(Plugin.DonatorKey, client.ClientId);
|
|
// if (clientDonatorMeta is not null) client.SetAdditionalProperty(Plugin.DonatorKey, clientDonatorMeta);
|
|
|
|
// return clientDonatorMeta;
|
|
//}
|
|
|
|
|
|
//public async Task WritePersistentData(EFClient client, DonatorState state)
|
|
//{
|
|
// await _metaService.SetPersistentMeta(Plugin.DonatorKey, state.ToString(), client.ClientId);
|
|
// client.SetAdditionalProperty(Plugin.DonatorKey, state);
|
|
//}
|
|
}
|
|
|
|
public class DonatorStateMeta
|
|
{
|
|
//public string Reason { get; set; } = string.Empty;
|
|
//public DateTime? Expiration { get; set; }
|
|
public DonatorState DonatorState { get; set; }
|
|
[JsonIgnore] public bool CommandExecuted { get; set; }
|
|
}
|
|
public enum DonatorState
|
|
{
|
|
User,
|
|
Donator,
|
|
Other
|
|
} |