2018-04-08 17:50:58 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Models;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public ViewStatsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
|
|
|
|
|
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;
|
|
|
|
|
Arguments = new CommandArgument[]
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
|
|
|
|
new CommandArgument()
|
|
|
|
|
{
|
|
|
|
|
Name = "player",
|
|
|
|
|
Required = false
|
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
};
|
2020-08-01 10:58:23 -04:00
|
|
|
|
|
|
|
|
|
_config = config;
|
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
|
|
|
|
|
2020-08-01 10:58:23 -04:00
|
|
|
|
private readonly CommandConfiguration _config;
|
|
|
|
|
|
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;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
EFClientStatistics pStats;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
long serverId = StatManager.GetIdForServer(E.Owner);
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
|
|
|
|
|
if (E.Target != null)
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-04-25 20:01:26 -04:00
|
|
|
|
int performanceRanking = await Plugin.Manager.GetClientOverallRanking(E.Target.ClientId);
|
2020-01-31 21:15:07 -05:00
|
|
|
|
string performanceRankingString = performanceRanking == 0 ? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"] : $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} #{performanceRanking}";
|
2018-10-15 20:51:04 -04:00
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
if (E.Owner.GetClientsAsList().Any(_client => _client.Equals(E.Target)))
|
|
|
|
|
{
|
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
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
pStats = (await context.Set<EFClientStatistics>().FirstAsync(c => c.ServerId == serverId && c.ClientId == E.Target.ClientId));
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
statLine = $"^5{pStats.Kills} ^7{_translationLookup["PLUGINS_STATS_TEXT_KILLS"]} | ^5{pStats.Deaths} ^7{_translationLookup["PLUGINS_STATS_TEXT_DEATHS"]} | ^5{pStats.KDR} ^7KDR | ^5{pStats.Performance} ^7{_translationLookup["PLUGINS_STATS_COMMANDS_PERFORMANCE"].ToUpper()} | {performanceRankingString}";
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-25 20:01:26 -04:00
|
|
|
|
int performanceRanking = await Plugin.Manager.GetClientOverallRanking(E.Origin.ClientId);
|
2020-01-31 21:15:07 -05:00
|
|
|
|
string performanceRankingString = performanceRanking == 0 ? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"] : $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} #{performanceRanking}";
|
2019-09-09 18:40:04 -04:00
|
|
|
|
|
|
|
|
|
if (E.Owner.GetClientsAsList().Any(_client => _client.Equals(E.Origin)))
|
|
|
|
|
{
|
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
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
pStats = (await context.Set<EFClientStatistics>().FirstAsync(c => c.ServerId == serverId && c.ClientId == E.Origin.ClientId));
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2020-01-31 21:15:07 -05:00
|
|
|
|
statLine = $"^5{pStats.Kills} ^7{_translationLookup["PLUGINS_STATS_TEXT_KILLS"]} | ^5{pStats.Deaths} ^7{_translationLookup["PLUGINS_STATS_TEXT_DEATHS"]} | ^5{pStats.KDR} ^7KDR | ^5{pStats.Performance} ^7{_translationLookup["PLUGINS_STATS_COMMANDS_PERFORMANCE"].ToUpper()} | {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
|
|
|
|
{
|
|
|
|
|
string 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|