update for database provider specific migrations

fix issues with live radar
This commit is contained in:
RaidMax
2020-11-27 21:52:52 -06:00
parent 8ef2959f63
commit 36a02b3d7b
309 changed files with 76554 additions and 1067 deletions

View File

@ -16,7 +16,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
{
class MostPlayedCommand : Command
{
public static async Task<List<string>> GetMostPlayed(Server s, ITranslationLookup translationLookup)
public static async Task<List<string>> GetMostPlayed(Server s, ITranslationLookup translationLookup, IDatabaseContextFactory contextFactory)
{
long serverId = StatManager.GetIdForServer(s);
@ -25,42 +25,39 @@ namespace IW4MAdmin.Plugins.Stats.Commands
$"^5--{translationLookup["PLUGINS_STATS_COMMANDS_MOSTPLAYED_TEXT"]}--"
};
using (var db = new DatabaseContext(true))
{
db.ChangeTracker.AutoDetectChangesEnabled = false;
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
await using var context = contextFactory.CreateContext(false);
var thirtyDaysAgo = DateTime.UtcNow.AddMonths(-1);
var thirtyDaysAgo = DateTime.UtcNow.AddMonths(-1);
var iqStats = (from stats in context.Set<EFClientStatistics>()
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,
client.TotalConnectionTime,
stats.Kills
})
.Take(5);
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
where client.Level != EFClient.Permission.Banned
where client.LastConnection >= thirtyDaysAgo
orderby stats.TimePlayed descending
select new
{
alias.Name,
client.TotalConnectionTime,
stats.Kills
})
.Take(5);
var iqList = await iqStats.ToListAsync();
var iqList = await iqStats.ToListAsync();
mostPlayed.AddRange(iqList.Select(stats => translationLookup["COMMANDS_MOST_PLAYED_FORMAT"].FormatExt(stats.Name, stats.Kills, (DateTime.UtcNow - DateTime.UtcNow.AddSeconds(-stats.TotalConnectionTime)).HumanizeForCurrentCulture())));
}
mostPlayed.AddRange(iqList.Select(stats => translationLookup["COMMANDS_MOST_PLAYED_FORMAT"].FormatExt(stats.Name, stats.Kills, (DateTime.UtcNow - DateTime.UtcNow.AddSeconds(-stats.TotalConnectionTime)).HumanizeForCurrentCulture())));
return mostPlayed;
}
private readonly CommandConfiguration _config;
private readonly IDatabaseContextFactory _contextFactory;
public MostPlayedCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
public MostPlayedCommand(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
{
Name = "mostplayed";
Description = translationLookup["PLUGINS_STATS_COMMANDS_MOSTPLAYED_DESC"];
@ -69,11 +66,12 @@ namespace IW4MAdmin.Plugins.Stats.Commands
RequiresTarget = false;
_config = config;
_contextFactory = contextFactory;
}
public override async Task ExecuteAsync(GameEvent E)
{
var topStats = await GetMostPlayed(E.Owner, _translationLookup);
var topStats = await GetMostPlayed(E.Owner, _translationLookup, _contextFactory);
if (!E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
{
foreach (var stat in topStats)

View File

@ -12,14 +12,17 @@ namespace IW4MAdmin.Plugins.Stats.Commands
{
public class ResetStats : Command
{
public ResetStats(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
private readonly IDatabaseContextFactory _contextFactory;
public ResetStats(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
{
Name = "resetstats";
Description = translationLookup["PLUGINS_STATS_COMMANDS_RESET_DESC"];
Alias = "rs";
Permission = EFClient.Permission.User;
RequiresTarget = false;
//AllowImpersonation = true;
AllowImpersonation = true;
}
public override async Task ExecuteAsync(GameEvent E)
@ -30,27 +33,24 @@ namespace IW4MAdmin.Plugins.Stats.Commands
long serverId = Helpers.StatManager.GetIdForServer(E.Owner);
EFClientStatistics clientStats;
using (var ctx = new DatabaseContext(disableTracking: true))
{
clientStats = await ctx.Set<EFClientStatistics>()
.Where(s => s.ClientId == E.Origin.ClientId)
.Where(s => s.ServerId == serverId)
.FirstAsync();
await using var context = _contextFactory.CreateContext();
clientStats = await context.Set<EFClientStatistics>()
.Where(s => s.ClientId == E.Origin.ClientId)
.Where(s => s.ServerId == serverId)
.FirstAsync();
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;
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;
// reset the cached version
Plugin.Manager.ResetStats(E.Origin);
// reset the cached version
Plugin.Manager.ResetStats(E.Origin);
// fixme: this doesn't work properly when another context exists
await ctx.SaveChangesAsync();
}
await context.SaveChangesAsync();
E.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_RESET_SUCCESS"]);
}

View File

@ -16,7 +16,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
{
class TopStats : Command
{
public static async Task<List<string>> GetTopStats(Server s, ITranslationLookup translationLookup)
public static async Task<List<string>> GetTopStats(Server s, ITranslationLookup translationLookup, IDatabaseContextFactory contextFactory)
{
long serverId = StatManager.GetIdForServer(s);
var topStatsText = new List<string>()
@ -24,36 +24,34 @@ namespace IW4MAdmin.Plugins.Stats.Commands
$"^5--{translationLookup["PLUGINS_STATS_COMMANDS_TOP_TEXT"]}--"
};
using (var db = new DatabaseContext(true))
{
var fifteenDaysAgo = DateTime.UtcNow.AddDays(-15);
int minPlayTime = Plugin.Config.Configuration().TopPlayersMinPlayTime;
await using var context = contextFactory.CreateContext(false);
var fifteenDaysAgo = DateTime.UtcNow.AddDays(-15);
int minPlayTime = Plugin.Config.Configuration().TopPlayersMinPlayTime;
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
where stats.TimePlayed >= minPlayTime
where client.Level != EFClient.Permission.Banned
where client.LastConnection >= fifteenDaysAgo
orderby (stats.EloRating + stats.Skill) / 2.0d descending
select new
{
stats.KDR,
stats.Performance,
alias.Name
})
.Take(5);
var iqStats = (from stats in context.Set<EFClientStatistics>()
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 stats.TimePlayed >= minPlayTime
where client.Level != EFClient.Permission.Banned
where client.LastConnection >= fifteenDaysAgo
orderby (stats.EloRating + stats.Skill) / 2.0d descending
select new
{
stats.KDR,
stats.Performance,
alias.Name
})
.Take(5);
var statsList = (await iqStats.ToListAsync())
.Select(stats => $"^3{stats.Name}^7 - ^5{stats.KDR} ^7{translationLookup["PLUGINS_STATS_TEXT_KDR"]} | ^5{stats.Performance} ^7{translationLookup["PLUGINS_STATS_COMMANDS_PERFORMANCE"]}");
var statsList = (await iqStats.ToListAsync())
.Select(stats => $"^3{stats.Name}^7 - ^5{stats.KDR} ^7{translationLookup["PLUGINS_STATS_TEXT_KDR"]} | ^5{stats.Performance} ^7{translationLookup["PLUGINS_STATS_COMMANDS_PERFORMANCE"]}");
topStatsText.AddRange(statsList);
}
topStatsText.AddRange(statsList);
// no one qualified
// no one qualified
if (topStatsText.Count == 1)
{
topStatsText = new List<string>()
@ -66,8 +64,10 @@ namespace IW4MAdmin.Plugins.Stats.Commands
}
private readonly CommandConfiguration _config;
private readonly IDatabaseContextFactory _contextFactory;
public TopStats(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
public TopStats(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
{
Name = "topstats";
Description = translationLookup["PLUGINS_STATS_COMMANDS_TOP_DESC"];
@ -76,11 +76,12 @@ namespace IW4MAdmin.Plugins.Stats.Commands
RequiresTarget = false;
_config = config;
_contextFactory = contextFactory;
}
public override async Task ExecuteAsync(GameEvent E)
{
var topStats = await GetTopStats(E.Owner, _translationLookup);
var topStats = await GetTopStats(E.Owner, _translationLookup, _contextFactory);
if (!E.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
{
foreach (var stat in topStats)

View File

@ -14,7 +14,10 @@ namespace IW4MAdmin.Plugins.Stats.Commands
{
public class ViewStatsCommand : Command
{
public ViewStatsCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, translationLookup)
private readonly IDatabaseContextFactory _contextFactory;
public ViewStatsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory) : base(config, translationLookup)
{
Name = "stats";
@ -32,6 +35,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
};
_config = config;
_contextFactory = contextFactory;
}
private readonly CommandConfiguration _config;
@ -66,10 +70,8 @@ namespace IW4MAdmin.Plugins.Stats.Commands
else
{
using (var ctx = new DatabaseContext(true))
{
pStats = (await ctx.Set<EFClientStatistics>().FirstAsync(c => c.ServerId == serverId && c.ClientId == E.Target.ClientId));
}
await using var context = _contextFactory.CreateContext(false);
pStats = (await context.Set<EFClientStatistics>().FirstAsync(c => c.ServerId == serverId && c.ClientId == E.Target.ClientId));
}
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}";
}
@ -86,10 +88,8 @@ namespace IW4MAdmin.Plugins.Stats.Commands
else
{
using (var ctx = new DatabaseContext(true))
{
pStats = (await ctx.Set<EFClientStatistics>().FirstAsync(c => c.ServerId == serverId && c.ClientId == E.Origin.ClientId));
}
await using var context = _contextFactory.CreateContext(false);
pStats = (await context.Set<EFClientStatistics>().FirstAsync(c => c.ServerId == serverId && c.ClientId == E.Origin.ClientId));
}
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}";
}