2020-05-05 19:49:30 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using System.Collections.Generic;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models.Client.Stats;
|
2020-05-05 19:49:30 -04:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Helpers;
|
2021-11-23 18:26:33 -05:00
|
|
|
|
using Stats.Config;
|
2020-05-05 19:49:30 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Plugins.Stats.Commands
|
|
|
|
|
{
|
|
|
|
|
class MostKillsCommand : Command
|
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
2020-08-01 10:58:23 -04:00
|
|
|
|
private readonly CommandConfiguration _config;
|
2020-05-05 19:49:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
public MostKillsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
|
|
|
|
|
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
|
2020-05-05 19:49:30 -04:00
|
|
|
|
{
|
|
|
|
|
Name = "mostkills";
|
|
|
|
|
Description = translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_DESC"];
|
|
|
|
|
Alias = "mk";
|
|
|
|
|
Permission = EFClient.Permission.User;
|
|
|
|
|
|
|
|
|
|
_contextFactory = contextFactory;
|
2020-08-01 10:58:23 -04:00
|
|
|
|
_config = config;
|
2020-05-05 19:49:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task ExecuteAsync(GameEvent E)
|
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var mostKills = await GetMostKills(StatManager.GetIdForServer(E.Owner), Plugin.Config.Configuration(),
|
|
|
|
|
_contextFactory, _translationLookup);
|
2020-08-01 10:58:23 -04:00
|
|
|
|
if (!E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
|
2020-05-05 19:49:30 -04:00
|
|
|
|
{
|
|
|
|
|
foreach (var stat in mostKills)
|
|
|
|
|
{
|
|
|
|
|
E.Origin.Tell(stat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var stat in mostKills)
|
|
|
|
|
{
|
|
|
|
|
E.Owner.Broadcast(stat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
public static async Task<IEnumerable<string>> GetMostKills(long? serverId, StatsConfiguration config,
|
|
|
|
|
IDatabaseContextFactory contextFactory, ITranslationLookup translationLookup)
|
2020-05-05 19:49:30 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
var dayInPast = DateTime.UtcNow.AddDays(-config.MostKillsMaxInactivityDays);
|
2020-05-05 19:49:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -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);
|
2020-05-05 19:49:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var iqList = await iqStats.ToListAsync();
|
2020-05-05 19:49:30 -04:00
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
return iqList.Select((stats, index) => translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_FORMAT_V2"]
|
2020-11-29 17:01:52 -05:00
|
|
|
|
.FormatExt(index + 1, stats.Name, stats.Kills))
|
|
|
|
|
.Prepend(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_STATS_COMMANDS_MOSTKILLS_HEADER"]);
|
2020-05-05 19:49:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-29 17:01:52 -05:00
|
|
|
|
}
|