From c500292f8bd55588015c78f6b02ab4c364668776 Mon Sep 17 00:00:00 2001 From: jvyden Date: Fri, 3 Nov 2023 16:49:31 -0400 Subject: [PATCH] fixup for dumb bad code not clearing score events when levels are deleted --- .../Database/GameDatabaseContext.Levels.cs | 12 ++++++++++-- .../Database/GameDatabaseProvider.cs | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs b/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs index 282c2921..6cd25907 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs @@ -115,10 +115,10 @@ public void DeleteLevel(GameLevel level) { this._realm.Write(() => { - IQueryable events = this._realm.All() + IQueryable levelEvents = this._realm.All() .Where(e => e._StoredDataType == (int)EventDataType.Level && e.StoredSequentialId == level.LevelId); - this._realm.RemoveRange(events); + this._realm.RemoveRange(levelEvents); #region This is so terrible it needs to be hidden away @@ -138,6 +138,14 @@ public void DeleteLevel(GameLevel level) this._realm.RemoveRange(uniquePlayLevelRelations); IQueryable scores = this._realm.All().Where(r => r.Level == level); + + foreach (GameSubmittedScore score in scores) + { + IQueryable scoreEvents = this._realm.All() + .Where(e => e._StoredDataType == (int)EventDataType.Score && e.StoredObjectId == score.ScoreId); + this._realm.RemoveRange(scoreEvents); + } + this._realm.RemoveRange(scores); #endregion diff --git a/Refresh.GameServer/Database/GameDatabaseProvider.cs b/Refresh.GameServer/Database/GameDatabaseProvider.cs index 6fc65a10..a9eca2b9 100644 --- a/Refresh.GameServer/Database/GameDatabaseProvider.cs +++ b/Refresh.GameServer/Database/GameDatabaseProvider.cs @@ -32,7 +32,7 @@ protected GameDatabaseProvider(IDateTimeProvider time) this._time = time; } - protected override ulong SchemaVersion => 95; + protected override ulong SchemaVersion => 96; protected override string Filename => "refreshGameServer.realm"; @@ -233,6 +233,7 @@ protected override void Migrate(Migration migration, ulong oldVersion) // IQueryable? oldEvents = migration.OldRealm.DynamicApi.All("Event"); IQueryable? newEvents = migration.NewRealm.All(); + List eventsToNuke = new(); for (int i = 0; i < newEvents.Count(); i++) { // dynamic oldEvent = oldEvents.ElementAt(i); @@ -246,6 +247,19 @@ protected override void Migrate(Migration migration, ulong oldVersion) // Converts events to use millisecond timestamps if (oldVersion < 33 && newEvent.Timestamp < 1000000000000) newEvent.Timestamp *= 1000; + + // fixup for dumb bad code not clearing score events when levels are deleted + if (oldVersion < 96 && newEvent.StoredDataType == EventDataType.Score) + { + GameSubmittedScore? score = migration.NewRealm.All().FirstOrDefault(s => s.ScoreId == newEvent.StoredObjectId); + if(score == null) eventsToNuke.Add(newEvent); + } + } + + // realm won't let you use an IEnumerable in RemoveRange. too bad! + foreach (Event eventToNuke in eventsToNuke) + { + migration.NewRealm.Remove(eventToNuke); } // IQueryable? oldTokens = migration.OldRealm.DynamicApi.All("Token"); @@ -306,7 +320,7 @@ protected override void Migrate(Migration migration, ulong oldVersion) } } - //Remove all scores with a null level, as in version 92 we started tracking story leaderboards differently + // Remove all scores with a null level, as in version 92 we started tracking story leaderboards differently if (oldVersion < 92) migration.NewRealm.RemoveRange(migration.NewRealm.All().Where(s => s.Level == null)); IQueryable? oldScores = migration.OldRealm.DynamicApi.All("GameSubmittedScore");