2021-03-22 12:09:25 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-11-27 19:31:48 -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-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
|
|
|
|
{
|
|
|
|
|
public class ResetStats : Command
|
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
|
|
|
|
|
|
|
|
|
public ResetStats(CommandConfiguration config, ITranslationLookup translationLookup,
|
|
|
|
|
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
|
|
|
|
Name = "resetstats";
|
|
|
|
|
Description = translationLookup["PLUGINS_STATS_COMMANDS_RESET_DESC"];
|
|
|
|
|
Alias = "rs";
|
|
|
|
|
Permission = EFClient.Permission.User;
|
|
|
|
|
RequiresTarget = false;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
AllowImpersonation = true;
|
2020-12-16 14:11:30 -05:00
|
|
|
|
|
|
|
|
|
_contextFactory = contextFactory;
|
2020-01-31 21:15:07 -05:00
|
|
|
|
}
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
public override async Task ExecuteAsync(GameEvent gameEvent)
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-12-16 14:11:30 -05:00
|
|
|
|
if (gameEvent.Origin.ClientNumber >= 0)
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-12-16 14:11:30 -05:00
|
|
|
|
var serverId = Helpers.StatManager.GetIdForServer(gameEvent.Owner);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
2020-12-16 14:11:30 -05:00
|
|
|
|
var clientStats = await context.Set<EFClientStatistics>()
|
|
|
|
|
.Where(s => s.ClientId == gameEvent.Origin.ClientId)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.Where(s => s.ServerId == serverId)
|
2020-12-16 14:11:30 -05:00
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
// want to prevent resetting stats before they've gotten any kills
|
|
|
|
|
if (clientStats != null)
|
|
|
|
|
{
|
|
|
|
|
clientStats.Deaths = 0;
|
|
|
|
|
clientStats.Kills = 0;
|
|
|
|
|
clientStats.SPM = 0.0;
|
|
|
|
|
clientStats.Skill = 0.0;
|
|
|
|
|
clientStats.TimePlayed = 0;
|
|
|
|
|
// todo: make this more dynamic
|
|
|
|
|
clientStats.EloRating = 200.0;
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
|
|
|
|
// reset the cached version
|
2020-12-16 14:11:30 -05:00
|
|
|
|
Plugin.Manager.ResetStats(gameEvent.Origin);
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
gameEvent.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_RESET_SUCCESS"]);
|
2018-02-10 01:26:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-16 14:11:30 -05:00
|
|
|
|
gameEvent.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_RESET_FAIL"]);
|
2018-02-10 01:26:38 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|