Skip to content

Commit

Permalink
fixup for dumb bad code not clearing score events when levels are del…
Browse files Browse the repository at this point in the history
…eted (#247)

:(

Closes #246
  • Loading branch information
jvyden authored Nov 3, 2023
2 parents 8ed5bcf + c500292 commit eb0651e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 10 additions & 2 deletions Refresh.GameServer/Database/GameDatabaseContext.Levels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ public void DeleteLevel(GameLevel level)
{
this._realm.Write(() =>
{
IQueryable<Event> events = this._realm.All<Event>()
IQueryable<Event> levelEvents = this._realm.All<Event>()
.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

Expand All @@ -138,6 +138,14 @@ public void DeleteLevel(GameLevel level)
this._realm.RemoveRange(uniquePlayLevelRelations);

IQueryable<GameSubmittedScore> scores = this._realm.All<GameSubmittedScore>().Where(r => r.Level == level);

foreach (GameSubmittedScore score in scores)
{
IQueryable<Event> scoreEvents = this._realm.All<Event>()
.Where(e => e._StoredDataType == (int)EventDataType.Score && e.StoredObjectId == score.ScoreId);
this._realm.RemoveRange(scoreEvents);
}

this._realm.RemoveRange(scores);

#endregion
Expand Down
18 changes: 16 additions & 2 deletions Refresh.GameServer/Database/GameDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -233,6 +233,7 @@ protected override void Migrate(Migration migration, ulong oldVersion)
// IQueryable<dynamic>? oldEvents = migration.OldRealm.DynamicApi.All("Event");
IQueryable<Event>? newEvents = migration.NewRealm.All<Event>();

List<Event> eventsToNuke = new();
for (int i = 0; i < newEvents.Count(); i++)
{
// dynamic oldEvent = oldEvents.ElementAt(i);
Expand All @@ -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<GameSubmittedScore>().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<dynamic>? oldTokens = migration.OldRealm.DynamicApi.All("Token");
Expand Down Expand Up @@ -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<GameSubmittedScore>().Where(s => s.Level == null));

IQueryable<dynamic>? oldScores = migration.OldRealm.DynamicApi.All("GameSubmittedScore");
Expand Down

0 comments on commit eb0651e

Please sign in to comment.