2018-05-16 00:57:37 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Models;
|
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using System.Collections.Generic;
|
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
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public static async Task<List<string>> GetMostPlayed(Server s, ITranslationLookup translationLookup)
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
long serverId = StatManager.GetIdForServer(s);
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2018-05-16 00:57:37 -04:00
|
|
|
|
List<string> mostPlayed = new List<string>()
|
|
|
|
|
{
|
2020-08-17 22:21:11 -04:00
|
|
|
|
$"^5--{translationLookup["PLUGINS_STATS_COMMANDS_MOSTPLAYED_TEXT"]}--"
|
2018-05-16 00:57:37 -04:00
|
|
|
|
};
|
|
|
|
|
|
2019-06-11 09:00:14 -04:00
|
|
|
|
using (var db = new DatabaseContext(true))
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
|
|
|
|
db.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
|
|
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
|
|
|
|
|
|
|
|
|
var thirtyDaysAgo = DateTime.UtcNow.AddMonths(-1);
|
|
|
|
|
|
|
|
|
|
var iqStats = (from stats in db.Set<EFClientStatistics>()
|
|
|
|
|
join client in db.Clients
|
|
|
|
|
on stats.ClientId equals client.ClientId
|
|
|
|
|
join alias in db.Aliases
|
|
|
|
|
on client.CurrentAliasId equals alias.AliasId
|
|
|
|
|
where stats.ServerId == serverId
|
2018-11-05 22:01:29 -05:00
|
|
|
|
where client.Level != EFClient.Permission.Banned
|
2018-05-16 00:57:37 -04:00
|
|
|
|
where client.LastConnection >= thirtyDaysAgo
|
2019-03-02 18:29:09 -05:00
|
|
|
|
orderby stats.TimePlayed descending
|
2018-05-16 00:57:37 -04:00
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
alias.Name,
|
|
|
|
|
client.TotalConnectionTime,
|
|
|
|
|
stats.Kills
|
|
|
|
|
})
|
|
|
|
|
.Take(5);
|
|
|
|
|
|
|
|
|
|
var iqList = await iqStats.ToListAsync();
|
|
|
|
|
|
2020-08-17 22:21:11 -04:00
|
|
|
|
mostPlayed.AddRange(iqList.Select(stats => translationLookup["COMMANDS_MOST_PLAYED_FORMAT"].FormatExt(stats.Name, (DateTime.UtcNow - DateTime.UtcNow.AddSeconds(-stats.TotalConnectionTime)).HumanizeForCurrentCulture())));
|
2018-05-16 00:57:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return mostPlayed;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 10:58:23 -04:00
|
|
|
|
private readonly CommandConfiguration _config;
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public MostPlayedCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
_config = config;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
}
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
|
|
|
|
public override async Task ExecuteAsync(GameEvent E)
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
var topStats = await GetMostPlayed(E.Owner, _translationLookup);
|
2020-08-01 10:58:23 -04:00
|
|
|
|
if (!E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
|
|
|
|
foreach (var stat in topStats)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
|
|
|
|
E.Origin.Tell(stat);
|
|
|
|
|
}
|
2018-05-16 00:57:37 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var stat in topStats)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
|
|
|
|
E.Owner.Broadcast(stat);
|
|
|
|
|
}
|
2018-05-16 00:57:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|