IW4M-Admin/Plugins/Stats/Commands/MostKillsCommand.cs

81 lines
3.1 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using SharedLibraryCore;
using System.Collections.Generic;
using Data.Abstractions;
using Data.Models.Client.Stats;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
using Stats.Config;
2023-02-11 22:01:28 -05:00
namespace IW4MAdmin.Plugins.Stats.Commands;
class MostKillsCommand : Command
{
2023-02-11 22:01:28 -05:00
private readonly IDatabaseContextFactory _contextFactory;
private readonly StatsConfiguration _statsConfig;
public MostKillsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory, StatsConfiguration statsConfig) : base(config, translationLookup)
{
2023-02-11 22:01:28 -05:00
Name = "mostkills";
Description = translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_DESC"];
Alias = "mk";
Permission = EFClient.Permission.User;
2023-02-11 22:01:28 -05:00
_contextFactory = contextFactory;
_statsConfig = statsConfig;
}
2023-02-11 22:01:28 -05:00
public override async Task ExecuteAsync(GameEvent gameEvent)
{
var mostKills = await GetMostKills((gameEvent.Owner as IGameServer).LegacyDatabaseId, _statsConfig,
2023-02-11 22:01:28 -05:00
_contextFactory, _translationLookup);
if (!gameEvent.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
{
await gameEvent.Origin.TellAsync(mostKills, gameEvent.Owner.Manager.CancellationToken);
}
2023-02-11 22:01:28 -05:00
else
{
2023-02-11 22:01:28 -05:00
foreach (var stat in mostKills)
{
2023-02-11 22:01:28 -05:00
await gameEvent.Owner.Broadcast(stat).WaitAsync(Utilities.DefaultCommandTimeout,
gameEvent.Owner.Manager.CancellationToken);
}
}
2023-02-11 22:01:28 -05:00
}
2023-02-11 22:01:28 -05:00
public static async Task<IEnumerable<string>> GetMostKills(long? serverId, StatsConfiguration config,
IDatabaseContextFactory contextFactory, ITranslationLookup translationLookup)
{
await using var ctx = contextFactory.CreateContext(enableTracking: false);
var dayInPast = DateTime.UtcNow.AddDays(-config.MostKillsMaxInactivityDays);
2023-02-11 22:01:28 -05:00
var iqStats = (from stats in ctx.Set<EFClientStatistics>()
join client in ctx.Clients
on stats.ClientId equals client.ClientId
join alias in ctx.Aliases
on client.CurrentAliasId equals alias.AliasId
where stats.ServerId == serverId
where client.Level != EFClient.Permission.Banned
where client.LastConnection >= dayInPast
orderby stats.Kills descending
select new
{
alias.Name,
stats.Kills
})
.Take(config.MostKillsClientLimit);
2023-02-11 22:01:28 -05:00
var iqList = await iqStats.ToListAsync();
2023-02-11 22:01:28 -05:00
return iqList.Select((stats, index) => translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_FORMAT_V2"]
.FormatExt(index + 1, stats.Name, stats.Kills))
.Prepend(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_STATS_COMMANDS_MOSTKILLS_HEADER"]);
}
2022-01-26 11:32:16 -05:00
}