2018-05-16 00:57:37 -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;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Helpers;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Plugins.Stats.Commands
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
class MostPlayedCommand : Command
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2023-04-05 11:15:10 -04:00
|
|
|
|
public static async Task<List<string>> GetMostPlayed(IGameServer gameServer, ITranslationLookup translationLookup,
|
2021-06-29 16:35:56 -04:00
|
|
|
|
IDatabaseContextFactory contextFactory)
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2023-04-05 11:15:10 -04:00
|
|
|
|
var serverId = gameServer.LegacyDatabaseId;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
var mostPlayed = new List<string>
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
$"(Color::Accent)--{translationLookup["PLUGINS_STATS_COMMANDS_MOSTPLAYED_TEXT"]}--"
|
2018-05-16 00:57:37 -04:00
|
|
|
|
};
|
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = contextFactory.CreateContext(false);
|
|
|
|
|
var thirtyDaysAgo = DateTime.UtcNow.AddMonths(-1);
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
var iqStats = (from stats in context.Set<EFClientStatistics>()
|
2021-06-29 16:35:56 -04:00
|
|
|
|
join client in context.Clients
|
|
|
|
|
on stats.ClientId equals client.ClientId
|
|
|
|
|
join alias in context.Aliases
|
|
|
|
|
on client.CurrentAliasId equals alias.AliasId
|
|
|
|
|
where stats.ServerId == serverId
|
|
|
|
|
where client.Level != EFClient.Permission.Banned
|
|
|
|
|
where client.LastConnection >= thirtyDaysAgo
|
|
|
|
|
orderby stats.TimePlayed descending
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
alias.Name,
|
|
|
|
|
stats.TimePlayed,
|
|
|
|
|
stats.Kills
|
|
|
|
|
})
|
|
|
|
|
.Take(5);
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
var iqList = await iqStats.ToListAsync();
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
2021-06-29 16:35:56 -04:00
|
|
|
|
mostPlayed.AddRange(iqList.Select((stats, index) =>
|
2021-11-23 18:26:33 -05:00
|
|
|
|
$"#{index + 1} " + translationLookup["COMMANDS_MOST_PLAYED_FORMAT_V2"].FormatExt(stats.Name, stats.Kills,
|
2021-06-29 16:35:56 -04:00
|
|
|
|
(DateTime.UtcNow - DateTime.UtcNow.AddSeconds(-stats.TimePlayed))
|
|
|
|
|
.HumanizeForCurrentCulture())));
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return mostPlayed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
2020-08-01 10:58:23 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
public MostPlayedCommand(CommandConfiguration config, ITranslationLookup translationLookup,
|
2021-06-29 16:35:56 -04:00
|
|
|
|
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
|
|
|
|
Name = "mostplayed";
|
|
|
|
|
Description = translationLookup["PLUGINS_STATS_COMMANDS_MOSTPLAYED_DESC"];
|
|
|
|
|
Alias = "mp";
|
|
|
|
|
Permission = EFClient.Permission.User;
|
|
|
|
|
RequiresTarget = false;
|
2020-08-01 10:58:23 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
_contextFactory = contextFactory;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
}
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
2022-03-23 13:09:40 -04:00
|
|
|
|
public override async Task ExecuteAsync(GameEvent gameEvent)
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2022-03-23 13:09:40 -04:00
|
|
|
|
var topStats = await GetMostPlayed(gameEvent.Owner, _translationLookup, _contextFactory);
|
|
|
|
|
if (!gameEvent.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2022-03-23 13:09:40 -04:00
|
|
|
|
await gameEvent.Origin.TellAsync(topStats, gameEvent.Owner.Manager.CancellationToken);
|
2018-05-16 00:57:37 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-09 10:56:41 -04:00
|
|
|
|
await gameEvent.Owner.BroadcastAsync(topStats);
|
2018-05-16 00:57:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|