Skip to content

Commit

Permalink
Use game time struct instead of obsolete methods (#562)
Browse files Browse the repository at this point in the history
* Use GameTime struct instead of obsolete methods (WIP)

* Use GameTime for custom entry processing

* Use GameTime elsewhere

* Update src/DevilDaggersInfo.Web.Server.Domain/Repositories/CustomLeaderboardRepository.cs
  • Loading branch information
NoahStolk authored Sep 7, 2024
1 parent c108954 commit c949ffc
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Web.ApiSpec.Admin.CustomEntries;
using DevilDaggersInfo.Web.Server.Domain.Entities;

Expand Down Expand Up @@ -32,13 +32,13 @@ public static GetCustomEntryForOverview ToAdminApiOverview(this CustomEntryEntit
GemsTotal = customEntry.GemsTotal,
HomingStored = customEntry.HomingStored,
HomingEaten = customEntry.HomingEaten,
LevelUpTime2 = customEntry.LevelUpTime2.ToSecondsTime(),
LevelUpTime3 = customEntry.LevelUpTime3.ToSecondsTime(),
LevelUpTime4 = customEntry.LevelUpTime4.ToSecondsTime(),
LevelUpTime2 = GameTime.FromGameUnits(customEntry.LevelUpTime2).Seconds,
LevelUpTime3 = GameTime.FromGameUnits(customEntry.LevelUpTime3).Seconds,
LevelUpTime4 = GameTime.FromGameUnits(customEntry.LevelUpTime4).Seconds,
PlayerName = customEntry.Player.PlayerName,
SpawnsetName = customEntry.CustomLeaderboard.Spawnset.Name,
SubmitDate = customEntry.SubmitDate,
Time = customEntry.Time.ToSecondsTime(),
Time = GameTime.FromGameUnits(customEntry.Time).Seconds,
};
}

Expand All @@ -62,13 +62,13 @@ public static GetCustomEntry ToAdminApi(this CustomEntryEntity customEntry)
GemsTotal = customEntry.GemsTotal,
HomingStored = customEntry.HomingStored,
HomingEaten = customEntry.HomingEaten,
LevelUpTime2 = customEntry.LevelUpTime2.ToSecondsTime(),
LevelUpTime3 = customEntry.LevelUpTime3.ToSecondsTime(),
LevelUpTime4 = customEntry.LevelUpTime4.ToSecondsTime(),
LevelUpTime2 = GameTime.FromGameUnits(customEntry.LevelUpTime2).Seconds,
LevelUpTime3 = GameTime.FromGameUnits(customEntry.LevelUpTime3).Seconds,
LevelUpTime4 = GameTime.FromGameUnits(customEntry.LevelUpTime4).Seconds,
PlayerId = customEntry.PlayerId,
SpawnsetId = customEntry.CustomLeaderboard.SpawnsetId,
SubmitDate = customEntry.SubmitDate,
Time = customEntry.Time.ToSecondsTime(),
Time = GameTime.FromGameUnits(customEntry.Time).Seconds,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.CriteriaExpression;
using DevilDaggersInfo.Web.Server.Domain.Entities;
using DevilDaggersInfo.Web.Server.Domain.Entities.Values;
Expand Down Expand Up @@ -91,11 +91,11 @@ private static AdminApi.GetCustomLeaderboardDaggers ToAdminApiDaggers(this Custo
bool isTime = customLeaderboard.RankSorting.IsTime();
return new()
{
Bronze = isTime ? customLeaderboard.Bronze.ToSecondsTime() : customLeaderboard.Bronze,
Silver = isTime ? customLeaderboard.Silver.ToSecondsTime() : customLeaderboard.Silver,
Golden = isTime ? customLeaderboard.Golden.ToSecondsTime() : customLeaderboard.Golden,
Devil = isTime ? customLeaderboard.Devil.ToSecondsTime() : customLeaderboard.Devil,
Leviathan = isTime ? customLeaderboard.Leviathan.ToSecondsTime() : customLeaderboard.Leviathan,
Bronze = isTime ? GameTime.FromGameUnits(customLeaderboard.Bronze).Seconds : customLeaderboard.Bronze,
Silver = isTime ? GameTime.FromGameUnits(customLeaderboard.Silver).Seconds : customLeaderboard.Silver,
Golden = isTime ? GameTime.FromGameUnits(customLeaderboard.Golden).Seconds : customLeaderboard.Golden,
Devil = isTime ? GameTime.FromGameUnits(customLeaderboard.Devil).Seconds : customLeaderboard.Devil,
Leviathan = isTime ? GameTime.FromGameUnits(customLeaderboard.Leviathan).Seconds : customLeaderboard.Leviathan,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Web.ApiSpec.Admin.CustomEntries;
using DevilDaggersInfo.Web.Server.Domain.Admin.Exceptions;
using DevilDaggersInfo.Web.Server.Domain.Entities;
Expand Down Expand Up @@ -45,12 +45,12 @@ public async Task AddCustomEntryAsync(AddCustomEntry addCustomEntry)
GemsTotal = addCustomEntry.GemsTotal,
HomingStored = addCustomEntry.HomingStored,
HomingEaten = addCustomEntry.HomingEaten,
LevelUpTime2 = addCustomEntry.LevelUpTime2.To10thMilliTime(),
LevelUpTime3 = addCustomEntry.LevelUpTime3.To10thMilliTime(),
LevelUpTime4 = addCustomEntry.LevelUpTime4.To10thMilliTime(),
LevelUpTime2 = (int)GameTime.FromSeconds(addCustomEntry.LevelUpTime2).GameUnits,
LevelUpTime3 = (int)GameTime.FromSeconds(addCustomEntry.LevelUpTime3).GameUnits,
LevelUpTime4 = (int)GameTime.FromSeconds(addCustomEntry.LevelUpTime4).GameUnits,
PlayerId = addCustomEntry.PlayerId,
SubmitDate = addCustomEntry.SubmitDate,
Time = addCustomEntry.Time.To10thMilliTime(),
Time = (int)GameTime.FromSeconds(addCustomEntry.Time).GameUnits,
};
_dbContext.CustomEntries.Add(customEntry);
await _dbContext.SaveChangesAsync();
Expand Down Expand Up @@ -81,12 +81,12 @@ public async Task EditCustomEntryAsync(int id, EditCustomEntry editCustomEntry)
customEntry.GemsTotal = editCustomEntry.GemsTotal;
customEntry.HomingStored = editCustomEntry.HomingStored;
customEntry.HomingEaten = editCustomEntry.HomingEaten;
customEntry.LevelUpTime2 = editCustomEntry.LevelUpTime2.To10thMilliTime();
customEntry.LevelUpTime3 = editCustomEntry.LevelUpTime3.To10thMilliTime();
customEntry.LevelUpTime4 = editCustomEntry.LevelUpTime4.To10thMilliTime();
customEntry.LevelUpTime2 = (int)GameTime.FromSeconds(editCustomEntry.LevelUpTime2).GameUnits;
customEntry.LevelUpTime3 = (int)GameTime.FromSeconds(editCustomEntry.LevelUpTime3).GameUnits;
customEntry.LevelUpTime4 = (int)GameTime.FromSeconds(editCustomEntry.LevelUpTime4).GameUnits;
customEntry.PlayerId = editCustomEntry.PlayerId;
customEntry.SubmitDate = editCustomEntry.SubmitDate;
customEntry.Time = editCustomEntry.Time.To10thMilliTime();
customEntry.Time = (int)GameTime.FromSeconds(editCustomEntry.Time).GameUnits;
await _dbContext.SaveChangesAsync();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.CriteriaExpression;
using DevilDaggersInfo.Core.CriteriaExpression.Parts;
using DevilDaggersInfo.Core.Spawnset;
Expand Down Expand Up @@ -514,10 +514,13 @@ levelUpTime3Operator is CustomLeaderboardCriteriaOperator.Equal or CustomLeaderb
return expressionBytes;
}

/// <summary>
/// Gets the dagger value in game units if the rank sorting is time, otherwise the value as is.
/// </summary>
private static int GetDaggerValue(ApiSpec.Admin.CustomLeaderboards.CustomLeaderboardRankSorting apiRankSorting, double apiValue)
{
if (apiRankSorting.ToDomain().IsTime())
return apiValue.To10thMilliTime();
return (int)GameTime.FromSeconds(apiValue).GameUnits;

return (int)apiValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.Spawnset;
using DevilDaggersInfo.Web.ApiSpec.Admin.Spawnsets;
using DevilDaggersInfo.Web.Server.Domain.Admin.Exceptions;
Expand Down Expand Up @@ -53,13 +53,13 @@ public async Task AddSpawnsetAsync(AddSpawnset addSpawnset)
GameMode = spawnsetBinary.GameMode.ToDomain(),
SpawnVersion = spawnsetBinary.SpawnVersion,
WorldVersion = spawnsetBinary.WorldVersion,
PreLoopLength = sections.PreLoopSection.Length?.To10thMilliTime(),
PreLoopLength = sections.PreLoopSection.Length.HasValue ? (int)GameTime.FromSeconds(sections.PreLoopSection.Length.Value).GameUnits : null,
PreLoopSpawnCount = sections.PreLoopSection.SpawnCount,
LoopLength = sections.LoopSection.Length?.To10thMilliTime(),
LoopLength = sections.LoopSection.Length.HasValue ? (int)GameTime.FromSeconds(sections.LoopSection.Length.Value).GameUnits : null,
LoopSpawnCount = sections.LoopSection.SpawnCount,
HandLevel = spawnsetBinary.HandLevel.ToDomain(),
AdditionalGems = spawnsetBinary.AdditionalGems,
TimerStart = spawnsetBinary.TimerStart.To10thMilliTime(),
TimerStart = (int)GameTime.FromSeconds(spawnsetBinary.TimerStart).GameUnits,
EffectiveHandLevel = effectivePlayerSettings.HandLevel.ToDomain(),
EffectiveGemsOrHoming = effectivePlayerSettings.GemsOrHoming,
EffectiveHandMesh = effectivePlayerSettings.HandMesh.ToDomain(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Web.ApiSpec.Main.LeaderboardHistoryStatistics;
using DevilDaggersInfo.Web.Server.Domain.Models.FileSystem;
using DevilDaggersInfo.Web.Server.Domain.Models.LeaderboardHistory;
Expand Down Expand Up @@ -31,7 +31,7 @@ public List<GetLeaderboardHistoryStatistics> GetLeaderboardHistoryStatistics()
ulong deathsGlobal = current.DeathsGlobal;
ulong gemsGlobal = current.GemsGlobal;
ulong killsGlobal = current.KillsGlobal;
double timeGlobal = current.TimeGlobal.ToSecondsTime();
double timeGlobal = GameTime.FromGameUnits(current.TimeGlobal).Seconds;
double rank100 = GetTimeOr0(current, 100);
double rank10 = GetTimeOr0(current, 10);
double rank3 = GetTimeOr0(current, 3);
Expand Down Expand Up @@ -94,7 +94,7 @@ public List<GetLeaderboardHistoryStatistics> GetLeaderboardHistoryStatistics()
totalPlayersUpdated = true;
}

double currentTimeGlobal = current.TimeGlobal.ToSecondsTime();
double currentTimeGlobal = GameTime.FromGameUnits(current.TimeGlobal).Seconds;
if (Math.Abs(timeGlobal - currentTimeGlobal) > 0.0002)
{
timeGlobal = currentTimeGlobal;
Expand Down Expand Up @@ -151,6 +151,9 @@ void Add(bool daggersFiredUpdated, bool daggersHitUpdated, bool deathsUpdated, b
});

static double GetTimeOr0(LeaderboardHistory history, int rank)
=> history.Entries.Find(eh => eh.Rank == rank)?.Time.ToSecondsTime() ?? 0;
{
EntryHistory? entry = history.Entries.Find(eh => eh.Rank == rank);
return entry == null ? 0 : GameTime.FromGameUnits(entry.Time).Seconds;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.Wiki;
using DevilDaggersInfo.Web.Server.Domain.Entities;
using DevilDaggersInfo.Web.Server.Domain.Entities.Enums;
Expand Down Expand Up @@ -179,7 +179,7 @@ private List<BaseWorldRecord> GetBaseWorldRecords()
DateTime = leaderboard.DateTime,
Id = mainPlayerId ?? firstLegitPlace.Id,
Username = firstLegitPlace.Username,
Time = firstLegitPlace.Time.ToSecondsTime(),
Time = GameTime.FromGameUnits(firstLegitPlace.Time).Seconds,
Kills = firstLegitPlace.Kills,
Gems = firstLegitPlace.Gems,
DeathType = firstLegitPlace.DeathType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.CriteriaExpression;
using DevilDaggersInfo.Web.Server.Domain.Constants;
Expand Down Expand Up @@ -59,7 +60,7 @@ public UploadRequest(
PlayerId = playerId;
PlayerName = playerName;
ReplayPlayerId = replayPlayerId;
TimeInSeconds = timeInSeconds;
Time = GameTime.FromSeconds(timeInSeconds);
TimeAsBytes = timeAsBytes;
GemsCollected = gemsCollected;
EnemiesKilled = enemiesKilled;
Expand All @@ -72,9 +73,9 @@ public UploadRequest(
GemsEaten = gemsEaten;
GemsTotal = gemsTotal;
DeathType = deathType;
LevelUpTime2InSeconds = levelUpTime2InSeconds;
LevelUpTime3InSeconds = levelUpTime3InSeconds;
LevelUpTime4InSeconds = levelUpTime4InSeconds;
LevelUpTime2 = GameTime.FromSeconds(levelUpTime2InSeconds);
LevelUpTime3 = GameTime.FromSeconds(levelUpTime3InSeconds);
LevelUpTime4 = GameTime.FromSeconds(levelUpTime4InSeconds);
LevelUpTime2AsBytes = levelUpTime2AsBytes;
LevelUpTime3AsBytes = levelUpTime3AsBytes;
LevelUpTime4AsBytes = levelUpTime4AsBytes;
Expand Down Expand Up @@ -102,7 +103,7 @@ public UploadRequest(

public int ReplayPlayerId { get; }

public double TimeInSeconds { get; }
public GameTime Time { get; }

public byte[] TimeAsBytes { get; }

Expand Down Expand Up @@ -131,11 +132,11 @@ public UploadRequest(

public byte DeathType { get; }

public double LevelUpTime2InSeconds { get; }
public GameTime LevelUpTime2 { get; }

public double LevelUpTime3InSeconds { get; }
public GameTime LevelUpTime3 { get; }

public double LevelUpTime4InSeconds { get; }
public GameTime LevelUpTime4 { get; }

public byte[] LevelUpTime2AsBytes { get; }

Expand Down Expand Up @@ -219,10 +220,10 @@ public TargetCollection CreateTargetCollection()
HomingStored = GetFinalHomingValue(),
HomingEaten = HomingEaten,
DeathType = DeathType,
Time = TimeInSeconds.To10thMilliTime(),
LevelUpTime2 = LevelUpTime2InSeconds.To10thMilliTime(),
LevelUpTime3 = LevelUpTime3InSeconds.To10thMilliTime(),
LevelUpTime4 = LevelUpTime4InSeconds.To10thMilliTime(),
Time = (int)Time.GameUnits,
LevelUpTime2 = (int)LevelUpTime2.GameUnits,
LevelUpTime3 = (int)LevelUpTime3.GameUnits,
LevelUpTime4 = (int)LevelUpTime4.GameUnits,
EnemiesAlive = EnemiesAlive,
Skull1Kills = GetFinalEnemyStat(urd => urd.Skull1sKilled),
Skull2Kills = GetFinalEnemyStat(urd => urd.Skull2sKilled),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;

namespace DevilDaggersInfo.Web.Server.Domain.Models.LeaderboardStatistics;

Expand All @@ -16,7 +16,7 @@ public void Populate(List<CompressedEntry> entries, int? limit = null)
if (limit.HasValue)
entries = entries.Take(limit.Value).ToList();

Times.Populate(entries.ConvertAll(e => e.Time.ToSecondsTime()), d => d);
Times.Populate(entries.ConvertAll(e => GameTime.FromGameUnits(e.Time).Seconds), d => d);
Kills.Populate(entries.ConvertAll(e => (double)e.Kills), d => d);
Gems.Populate(entries.ConvertAll(e => (double)e.Gems), d => d);
DaggersFired.Populate(entries.ConvertAll(e => (double)e.DaggersFired), d => Math.Round(d / 100) * 100);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.CriteriaExpression;
using DevilDaggersInfo.Web.Server.Domain.Constants;
Expand Down Expand Up @@ -273,7 +274,7 @@ public async Task<GlobalCustomLeaderboard> GetGlobalCustomLeaderboardAsync(Spawn
CustomEntryEntity customEntry = customEntries[i];

GlobalCustomLeaderboardEntryData data;
if (!globalData.ContainsKey(customEntry.PlayerId))
if (!globalData.TryGetValue(customEntry.PlayerId, out (string Name, GlobalCustomLeaderboardEntryData Data) value))
{
data = new();

Expand All @@ -282,7 +283,7 @@ public async Task<GlobalCustomLeaderboard> GetGlobalCustomLeaderboardAsync(Spawn
}
else
{
data = globalData[customEntry.PlayerId].Data;
data = value.Data;
}

data.Rankings.Add(new() { Rank = i + 1, TotalPlayers = customEntries.Count });
Expand Down Expand Up @@ -399,7 +400,7 @@ private static CustomLeaderboardOverview ToOverview(CustomLeaderboardData cl)

static double GetDaggerValue(int databaseValue, CustomLeaderboardRankSorting rankSorting) => rankSorting switch
{
CustomLeaderboardRankSorting.TimeAsc or CustomLeaderboardRankSorting.TimeDesc => databaseValue.ToSecondsTime(),
CustomLeaderboardRankSorting.TimeAsc or CustomLeaderboardRankSorting.TimeDesc => GameTime.FromGameUnits(databaseValue).Seconds,
_ => databaseValue,
};
}
Expand Down Expand Up @@ -504,7 +505,7 @@ private static double GetHighscoreValue(IDaggerStatCustomEntry customEntry, Cust
{
return rankSorting switch
{
CustomLeaderboardRankSorting.TimeAsc or CustomLeaderboardRankSorting.TimeDesc => customEntry.Time.ToSecondsTime(),
CustomLeaderboardRankSorting.TimeAsc or CustomLeaderboardRankSorting.TimeDesc => GameTime.FromGameUnits(customEntry.Time).Seconds,
CustomLeaderboardRankSorting.GemsCollectedAsc or CustomLeaderboardRankSorting.GemsCollectedDesc => customEntry.GemsCollected,
CustomLeaderboardRankSorting.GemsDespawnedAsc or CustomLeaderboardRankSorting.GemsDespawnedDesc => customEntry.GemsDespawned,
CustomLeaderboardRankSorting.GemsEatenAsc or CustomLeaderboardRankSorting.GemsEatenDesc => customEntry.GemsEaten,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Web.Server.Domain.Entities;
using DevilDaggersInfo.Web.Server.Domain.Entities.Enums;
using DevilDaggersInfo.Web.Server.Domain.Models.FileSystem;
Expand Down Expand Up @@ -85,7 +85,7 @@ public PlayerHistory GetPlayerHistoryById(int id)
Gems = entry.Gems,
Kills = entry.Kills,
Rank = correctedRank,
Time = entry.Time.ToSecondsTime(),
Time = GameTime.FromGameUnits(entry.Time).Seconds,
Username = entry.Username,
});

Expand All @@ -110,7 +110,7 @@ public PlayerHistory GetPlayerHistoryById(int id)
activityHistory.Add(new()
{
DeathsIncrement = totalDeathsForActivityHistory.HasValue && timeSpan.HasValue ? (entry.DeathsTotal - totalDeathsForActivityHistory.Value) / timeSpan.Value.TotalDays : 0,
TimeIncrement = totalTimeForActivityHistory.HasValue && timeSpan.HasValue ? (entry.TimeTotal - totalTimeForActivityHistory.Value).ToSecondsTime() / timeSpan.Value.TotalDays : 0,
TimeIncrement = totalTimeForActivityHistory.HasValue && timeSpan.HasValue ? GameTime.FromGameUnits(entry.TimeTotal - totalTimeForActivityHistory.Value).Seconds / timeSpan.Value.TotalDays : 0,
DateTime = leaderboard.DateTime,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DevilDaggersInfo.Core.Common.Extensions;
using DevilDaggersInfo.Core.Common;
using DevilDaggersInfo.Core.Wiki;
using DevilDaggersInfo.Core.Wiki.Objects;
using DevilDaggersInfo.Web.Server.Domain.Models.FileSystem;
Expand Down Expand Up @@ -115,7 +115,7 @@ public void Initiate()
else
DeathsStatistics[death]++;

double seconds = entry.Time.ToSecondsTime();
double seconds = GameTime.FromGameUnits(entry.Time).Seconds;

if (seconds < currentDaggerTime)
{
Expand Down
Loading

0 comments on commit c949ffc

Please sign in to comment.