1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

implement metaservice v2

This commit is contained in:
RaidMax
2022-03-23 08:43:57 -05:00
parent cde42c78ff
commit bae415c81b
21 changed files with 919 additions and 120 deletions

View File

@ -1,39 +0,0 @@
using System.Threading.Tasks;
using Data.Models;
using Data.Models.Client;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Commands
{
public class AddClientTagCommand : Command
{
private readonly IMetaService _metaService;
public AddClientTagCommand(CommandConfiguration config, ITranslationLookup layout, IMetaService metaService) :
base(config, layout)
{
Name = "addclienttag";
Description = layout["COMMANDS_ADD_CLIENT_TAG_DESC"];
Alias = "act";
Permission = EFClient.Permission.Owner;
RequiresTarget = false;
Arguments = new[]
{
new CommandArgument
{
Name = _translationLookup["COMMANDS_ARGUMENT_TAG"],
Required = true
}
};
_metaService = metaService;
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
await _metaService.AddPersistentMeta(EFMeta.ClientTagName, gameEvent.Data);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_ADD_CLIENT_TAG_SUCCESS"].FormatExt(gameEvent.Data));
}
}
}

View File

@ -1,32 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using Data.Models;
using Data.Models.Client;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Commands
{
public class ListClientTags : Command
{
private readonly IMetaService _metaService;
public ListClientTags(CommandConfiguration config, ITranslationLookup layout, IMetaService metaService) : base(
config, layout)
{
Name = "listclienttags";
Description = layout["COMMANDS_LIST_CLIENT_TAGS_DESC"];
Alias = "lct";
Permission = EFClient.Permission.Owner;
RequiresTarget = false;
_metaService = metaService;
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
var tags = await _metaService.GetPersistentMeta(EFMeta.ClientTagName);
gameEvent.Origin.Tell(tags.Select(tag => tag.Value));
}
}
}

View File

@ -1137,10 +1137,10 @@ namespace SharedLibraryCore.Commands
/// </summary>
public class SetGravatarCommand : Command
{
private readonly IMetaService _metaService;
private readonly IMetaServiceV2 _metaService;
public SetGravatarCommand(CommandConfiguration config, ITranslationLookup translationLookup,
IMetaService metaService) : base(config, translationLookup)
IMetaServiceV2 metaService) : base(config, translationLookup)
{
Name = "setgravatar";
Description = _translationLookup["COMMANDS_GRAVATAR_DESC"];
@ -1166,7 +1166,8 @@ namespace SharedLibraryCore.Commands
var gravatarEmail = string.Concat(md5
.ComputeHash(E.Data.ToLower().Select(d => Convert.ToByte(d)).ToArray())
.Select(h => h.ToString("x2")));
await _metaService.AddPersistentMeta("GravatarEmail", gravatarEmail, E.Origin);
await _metaService.SetPersistentMeta("GravatarEmail", gravatarEmail, E.Origin.ClientId,
E.Owner.Manager.CancellationToken);
}
E.Origin.Tell(_translationLookup["COMMANDS_GRAVATAR_SUCCESS_NEW"]);

View File

@ -1,39 +0,0 @@
using System.Threading.Tasks;
using Data.Models;
using Data.Models.Client;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Commands
{
public class RemoveClientTag : Command
{
private readonly IMetaService _metaService;
public RemoveClientTag(CommandConfiguration config, ITranslationLookup layout, IMetaService metaService) : base(
config, layout)
{
Name = "removeclienttag";
Description = layout["COMMANDS_REMOVE_CLIENT_TAG_DESC"];
Alias = "rct";
Permission = EFClient.Permission.Owner;
RequiresTarget = false;
Arguments = new[]
{
new CommandArgument
{
Name = _translationLookup["COMMANDS_ARGUMENT_TAG"],
Required = true
}
};
_metaService = metaService;
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
await _metaService.RemovePersistentMeta(EFMeta.ClientTagName, gameEvent.Data);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_REMOVE_CLIENT_TAG_SUCCESS"].FormatExt(gameEvent.Data));
}
}
}

View File

@ -1,51 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using Data.Models;
using Data.Models.Client;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Commands
{
public class SetClientTagCommand : Command
{
private readonly IMetaService _metaService;
public SetClientTagCommand(CommandConfiguration config, ITranslationLookup layout, IMetaService metaService) :
base(config, layout)
{
Name = "setclienttag";
Description = layout["COMMANDS_SET_CLIENT_TAG_DESC"];
Alias = "sct";
Permission = EFClient.Permission.Owner;
RequiresTarget = true;
Arguments = new[]
{
new CommandArgument
{
Name = _translationLookup["COMMANDS_ARGUMENT_TAG"],
Required = true
}
};
_metaService = metaService;
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
var availableTags = await _metaService.GetPersistentMeta(EFMeta.ClientTagName);
var matchingTag = availableTags.FirstOrDefault(tag => tag.Value == gameEvent.Data);
if (matchingTag == null)
{
gameEvent.Origin.Tell(_translationLookup["COMMANDS_SET_CLIENT_TAG_FAIL"].FormatExt(gameEvent.Data));
return;
}
gameEvent.Target.Tag = matchingTag.Value;
await _metaService.AddPersistentMeta(EFMeta.ClientTag, string.Empty, gameEvent.Target, matchingTag);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_SET_CLIENT_TAG_SUCCESS"].FormatExt(matchingTag.Value));
}
}
}

View File

@ -1,41 +0,0 @@
using System.Threading.Tasks;
using Data.Models;
using Data.Models.Client;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Commands
{
public class UnsetClientTagCommand : Command
{
private readonly IMetaService _metaService;
public UnsetClientTagCommand(CommandConfiguration config, ITranslationLookup layout, IMetaService metaService) :
base(config, layout)
{
Name = "unsetclienttag";
Description = layout["COMMANDS_UNSET_CLIENT_TAG_DESC"];
Alias = "uct";
Permission = EFClient.Permission.Owner;
RequiresTarget = true;
Arguments = new[]
{
new CommandArgument
{
Name = _translationLookup["COMMANDS_ARGUMENT_TAG"],
Required = true
}
};
_metaService = metaService;
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
gameEvent.Target.Tag = null;
await _metaService.RemovePersistentMeta(EFMeta.ClientTag, gameEvent.Target);
gameEvent.Origin.Tell(_translationLookup["COMMANDS_UNSET_CLIENT_TAG_SUCCESS"]);
}
}
}

View File

@ -0,0 +1,9 @@
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore.Dtos;
public class LookupValue<TObject> : ILookupValue<TObject>
{
public int Id { get; set; }
public TObject Value { get; set; }
}

View File

@ -0,0 +1,7 @@
namespace SharedLibraryCore.Interfaces;
public interface ILookupValue<TObject>
{
int Id { get; }
TObject Value { get; }
}

View File

@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Data.Models;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.QueryHelper;
namespace SharedLibraryCore.Interfaces;
public interface IMetaServiceV2
{
#region PER_CLIENT
/// <summary>
/// adds or updates meta key and value to the database as simple string
/// </summary>
/// <param name="metaKey">key of meta data</param>
/// <param name="metaValue">value of the meta data</param>
/// <param name="clientId">id of the client to save the meta for</param>
/// <param name="token"></param>
/// <returns></returns>
Task SetPersistentMeta(string metaKey, string metaValue, int clientId, CancellationToken token = default);
/// <summary>
/// add or update meta key and value to the database as serialized object type
/// </summary>
/// <param name="metaKey"></param>
/// <param name="metaValue"></param>
/// <param name="clientId"></param>
/// <param name="token"></param>
/// <typeparam name="T">type of object being serialized</typeparam>
/// <returns></returns>
Task SetPersistentMetaValue<T>(string metaKey, T metaValue, int clientId, CancellationToken token = default)
where T : class;
/// <summary>
/// Sets meta key to a linked lookup key and id
/// </summary>
/// <param name="metaKey">Key for the client meta</param>
/// <param name="lookupKey">Key of the global lookup meta</param>
/// <param name="lookupId">Id in the list of lookup values</param>
/// <param name="clientId">id of the client</param>
/// <param name="token"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
Task SetPersistentMetaForLookupKey(string metaKey, string lookupKey, int lookupId, int clientId,
CancellationToken token = default);
/// <summary>
/// increments meta value and persists to the database
/// <remarks>if the meta value does not already exist it will be set to the increment amount</remarks>
/// <remarks>the assumption is made that the existing value is <see cref="int"/></remarks>
/// </summary>
/// <param name="metaKey">key of meta data</param>
/// <param name="incrementAmount">value to increment by</param>
/// <param name="clientId">id of the client to save the meta for</param>
/// <param name="token"></param>
/// <returns></returns>
Task IncrementPersistentMeta(string metaKey, int incrementAmount, int clientId,
CancellationToken token = default);
/// <summary>
/// decrements meta value and persists to the database
/// <remarks>if the meta value does not already exist it will be set to the decrement amount</remarks>
/// <remarks>the assumption is made that the existing value is <see cref="int"/></remarks>
/// </summary>
/// <param name="metaKey">key of meta data</param>
/// <param name="decrementAmount">value to increment by</param>
/// <param name="clientId">id of the client to save the meta for</param>
/// <param name="token"></param>
/// <returns></returns>
Task DecrementPersistentMeta(string metaKey, int decrementAmount, int clientId,
CancellationToken token = default);
/// <summary>
/// retrieves meta entry
/// </summary>
/// <param name="metaKey"></param>
/// <param name="clientId"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<EFMeta> GetPersistentMeta(string metaKey, int clientId, CancellationToken token = default);
/// <summary>
/// retrieves meta value as deserialized object
/// </summary>
/// <param name="metaKey"></param>
/// <param name="clientId"></param>
/// <param name="token"></param>
/// <typeparam name="T">object type to deserialize into</typeparam>
/// <returns></returns>
Task<T> GetPersistentMetaValue<T>(string metaKey, int clientId, CancellationToken token = default)
where T : class;
/// <summary>
/// retrieves meta entry by with associated lookup value as string
/// </summary>
/// <param name="metaKey"></param>
/// <param name="lookupKey"></param>
/// <param name="clientId"></param>
/// <param name="token"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
Task<EFMeta> GetPersistentMetaByLookup(string metaKey, string lookupKey, int clientId,
CancellationToken token = default);
/// <summary>
/// removes meta key with given value
/// </summary>
/// <param name="metaKey">key of meta data</param>
/// <param name="clientId">client to delete the meta for</param>
/// <param name="token"></param>
/// <returns></returns>
Task RemovePersistentMeta(string metaKey, int clientId, CancellationToken token = default);
#endregion
#region GLOBAL
/// <summary>
/// adds or updates meta key and value to the database
/// </summary>
/// <param name="metaKey">key of meta data</param>
/// <param name="metaValue">value of the meta data</param>
/// <param name="token"></param>
/// <returns></returns>
Task SetPersistentMeta(string metaKey, string metaValue, CancellationToken token = default);
/// <summary>
/// serializes and sets (create or update) meta key and value
/// </summary>
/// <param name="metaKey"></param>
/// <param name="metaValue"></param>
/// <param name="token"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
Task SetPersistentMetaValue<T>(string metaKey, T metaValue, CancellationToken token = default) where T : class;
/// <summary>
/// removes meta key with given value
/// </summary>
/// <param name="metaKey">key of the meta data</param>
/// <param name="metaValue">value of the meta data</param>
/// <param name="token"></param>
/// <returns></returns>
Task RemovePersistentMeta(string metaKey, CancellationToken token = default);
/// <summary>
/// retrieves collection of meta for given key
/// </summary>
/// <param name="metaKey">key to retrieve values for</param>
/// <param name="token"></param>
/// <returns></returns>
Task<EFMeta> GetPersistentMeta(string metaKey, CancellationToken token = default);
/// <summary>
/// returns value of meta key if it exists
/// </summary>
/// <param name="metaKey"></param>
/// <param name="token"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
Task<T> GetPersistentMetaValue<T>(string metaKey, CancellationToken token = default) where T : class;
#endregion
/// <summary>
/// adds a meta task to the runtime meta list
/// </summary>
/// <param name="metaKey">type of meta</param>
/// <param name="metaAction">action to perform</param>
void AddRuntimeMeta<T, TReturn>(MetaType metaKey,
Func<T, CancellationToken, Task<IEnumerable<TReturn>>> metaAction)
where TReturn : IClientMeta where T : PaginationRequest;
/// <summary>
/// retrieves all the runtime meta information for given client idea
/// </summary>
/// <param name="request">request information</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<IClientMeta>> GetRuntimeMeta(ClientPaginationRequest request, CancellationToken token = default);
/// <summary>
/// retrieves all the runtime of provided type
/// </summary>
/// <param name="request">>request information</param>
/// <param name="metaType">type of meta to retreive</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<T>> GetRuntimeMeta<T>(ClientPaginationRequest request, MetaType metaType, CancellationToken token = default)
where T : IClientMeta;
}

View File

@ -124,8 +124,8 @@ namespace SharedLibraryCore.Database.Models
[NotMapped]
public string Tag
{
get => GetAdditionalProperty<string>(EFMeta.ClientTag);
set => SetAdditionalProperty(EFMeta.ClientTag, value);
get => GetAdditionalProperty<string>(EFMeta.ClientTagV2);
set => SetAdditionalProperty(EFMeta.ClientTagV2, value);
}
[NotMapped]