2018-04-08 17:50:58 -04:00
|
|
|
|
using SharedLibraryCore;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models.Client.Stats;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-10-15 20:51:04 -04:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Helpers;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.Commands;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
namespace IW4MAdmin.Plugins.Stats.Commands
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public class ViewStatsCommand : Command
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
2020-12-16 14:11:30 -05:00
|
|
|
|
|
|
|
|
|
public ViewStatsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
|
2020-11-27 22:52:52 -05:00
|
|
|
|
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
|
|
|
|
Name = "stats";
|
|
|
|
|
Description = translationLookup["PLUGINS_STATS_COMMANDS_VIEW_DESC"];
|
|
|
|
|
Alias = "xlrstats";
|
|
|
|
|
Permission = EFClient.Permission.User;
|
|
|
|
|
RequiresTarget = false;
|
2021-11-23 18:26:33 -05:00
|
|
|
|
Arguments = new []
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
new CommandArgument
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
|
|
|
|
Name = "player",
|
|
|
|
|
Required = false
|
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
};
|
2020-12-16 14:11:30 -05:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
_contextFactory = contextFactory;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
}
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public override async Task ExecuteAsync(GameEvent E)
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
string statLine;
|
2020-12-16 14:11:30 -05:00
|
|
|
|
EFClientStatistics pStats = null;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
|
|
|
|
if (E.Data.Length > 0 && E.Target == null)
|
|
|
|
|
{
|
2018-06-05 17:31:36 -04:00
|
|
|
|
E.Target = E.Owner.GetClientByName(E.Data).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (E.Target == null)
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
E.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_VIEW_FAIL"]);
|
2018-06-05 17:31:36 -04:00
|
|
|
|
}
|
2018-02-10 01:26:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
var serverId = StatManager.GetIdForServer(E.Owner);
|
2019-09-09 18:40:04 -04:00
|
|
|
|
|
2021-06-29 18:14:25 -04:00
|
|
|
|
var totalRankedPlayers = await Plugin.Manager.GetTotalRankedPlayers(serverId);
|
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// getting stats for a particular client
|
2019-09-09 18:40:04 -04:00
|
|
|
|
if (E.Target != null)
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var performanceRanking = await Plugin.Manager.GetClientOverallRanking(E.Target.ClientId, serverId);
|
2020-12-16 14:11:30 -05:00
|
|
|
|
var performanceRankingString = performanceRanking == 0
|
|
|
|
|
? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"]
|
2021-06-29 18:14:25 -04:00
|
|
|
|
: $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} #{performanceRanking}/{totalRankedPlayers}";
|
2018-10-15 20:51:04 -04:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// target is currently connected so we want their cached stats if they exist
|
|
|
|
|
if (E.Owner.GetClientsAsList().Any(client => client.Equals(E.Target)))
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
2019-10-18 14:39:21 -04:00
|
|
|
|
pStats = E.Target.GetAdditionalProperty<EFClientStatistics>(StatManager.CLIENT_STATS_KEY);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// target is not connected so we want to look up via database
|
|
|
|
|
if (pStats == null)
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
2021-11-23 18:26:33 -05:00
|
|
|
|
pStats = await context.Set<EFClientStatistics>()
|
|
|
|
|
.FirstOrDefaultAsync(c => c.ServerId == serverId && c.ClientId == E.Target.ClientId);
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
2020-12-16 14:11:30 -05:00
|
|
|
|
|
|
|
|
|
// if it's still null then they've not gotten a kill or death yet
|
|
|
|
|
statLine = pStats == null
|
|
|
|
|
? _translationLookup["PLUGINS_STATS_COMMANDS_NOTAVAILABLE"]
|
2021-11-23 18:26:33 -05:00
|
|
|
|
: _translationLookup["COMMANDS_VIEW_STATS_RESULT"].FormatExt(pStats.Kills, pStats.Deaths,
|
|
|
|
|
pStats.KDR, pStats.Performance, performanceRankingString);
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// getting self stats
|
2019-09-09 18:40:04 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var performanceRanking = await Plugin.Manager.GetClientOverallRanking(E.Origin.ClientId, serverId);
|
2020-12-16 14:11:30 -05:00
|
|
|
|
var performanceRankingString = performanceRanking == 0
|
|
|
|
|
? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"]
|
2021-06-29 18:14:25 -04:00
|
|
|
|
: $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} #{performanceRanking}/{totalRankedPlayers}";
|
2019-09-09 18:40:04 -04:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// check if current client is connected to the server
|
|
|
|
|
if (E.Owner.GetClientsAsList().Any(client => client.Equals(E.Origin)))
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
2019-10-18 14:39:21 -04:00
|
|
|
|
pStats = E.Origin.GetAdditionalProperty<EFClientStatistics>(StatManager.CLIENT_STATS_KEY);
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
2018-10-15 20:51:04 -04:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// happens if the user has not gotten a kill/death since connecting
|
|
|
|
|
if (pStats == null)
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
2020-12-16 14:11:30 -05:00
|
|
|
|
pStats = (await context.Set<EFClientStatistics>()
|
|
|
|
|
.FirstOrDefaultAsync(c => c.ServerId == serverId && c.ClientId == E.Origin.ClientId));
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2020-12-16 14:11:30 -05:00
|
|
|
|
|
|
|
|
|
// if it's still null then they've not gotten a kill or death yet
|
|
|
|
|
statLine = pStats == null
|
|
|
|
|
? _translationLookup["PLUGINS_STATS_COMMANDS_NOTAVAILABLE"]
|
2021-11-23 18:26:33 -05:00
|
|
|
|
: _translationLookup["COMMANDS_VIEW_STATS_RESULT"].FormatExt(pStats.Kills, pStats.Deaths,
|
|
|
|
|
pStats.KDR, pStats.Performance, performanceRankingString);
|
2018-02-10 01:26:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 10:58:23 -04:00
|
|
|
|
if (E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-12-16 14:11:30 -05:00
|
|
|
|
var name = E.Target == null ? E.Origin.Name : E.Target.Name;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
E.Owner.Broadcast(_translationLookup["PLUGINS_STATS_COMMANDS_VIEW_SUCCESS"].FormatExt(name));
|
2018-09-29 15:52:22 -04:00
|
|
|
|
E.Owner.Broadcast(statLine);
|
2018-02-10 01:26:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (E.Target != null)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
E.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_VIEW_SUCCESS"].FormatExt(E.Target.Name));
|
2018-09-29 15:52:22 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
E.Origin.Tell(statLine);
|
2018-02-10 01:26:38 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-16 14:11:30 -05:00
|
|
|
|
}
|