Skip to content

Commit

Permalink
More accurate playlist timestamps (set before write), move foreach in…
Browse files Browse the repository at this point in the history
…to write block, add game-independent GetTotalLevelsInPlaylistCount method
  • Loading branch information
Toastbrot236 committed Dec 16, 2024
1 parent 061a058 commit 966641d
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Refresh.GameServer/Database/GameDatabaseContext.Playlists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public GamePlaylist CreatePlaylist(GameUser user, SerializedLbp3Playlist createI

public void CreatePlaylist(GamePlaylist createInfo)
{
DateTimeOffset now = this._time.Now;

this.Write(() =>
{
createInfo.CreationDate = now;
createInfo.LastUpdateDate = now;
this.AddSequentialObject(createInfo);
createInfo.CreationDate = this._time.Now;
createInfo.LastUpdateDate = this._time.Now;
});
}

Expand All @@ -39,24 +41,28 @@ public void CreatePlaylist(GamePlaylist createInfo)

public void UpdatePlaylist(GamePlaylist playlist, SerializedLbp1Playlist updateInfo)
{
DateTimeOffset now = this._time.Now;

this.Write(() =>
{
playlist.LastUpdateDate = now;
playlist.Name = updateInfo.Name;
playlist.Description = updateInfo.Description;
playlist.IconHash = updateInfo.Icon;
playlist.LocationX = updateInfo.Location.X;
playlist.LocationY = updateInfo.Location.Y;
playlist.LastUpdateDate = this._time.Now;
});
}

public void UpdatePlaylist(GamePlaylist playlist, SerializedLbp3Playlist updateInfo)
{
DateTimeOffset now = this._time.Now;

this.Write(() =>
{
playlist.LastUpdateDate = now;
if (updateInfo.Name != null) playlist.Name = updateInfo.Name;
if (updateInfo.Description != null) playlist.Description = updateInfo.Description;
playlist.LastUpdateDate = this._time.Now;
});
}

Expand Down Expand Up @@ -118,8 +124,8 @@ public void AddLevelToPlaylist(GameLevel level, GamePlaylist parent)
{
Level = level,
Playlist = parent,
// Setting TokenGame to LBP3 to get the true amount of levels in the playlist
Index = this.GetTotalLevelsInPlaylistCount(parent, TokenGame.LittleBigPlanet3),
// index of new relation = index of last relation + 1 = relation count (without new relation)
Index = this.GetTotalLevelsInPlaylistCount(parent),
});
});
}
Expand Down Expand Up @@ -147,13 +153,12 @@ private void DecreasePlaylistLevelIndicesAfterIndex(GamePlaylist playlist, int i
.Where(r => r.Playlist == playlist && r.Index >= index)
.AsEnumerable();


foreach(LevelPlaylistRelation relation in relations)
{
this.Write(() => {
this.Write(() => {
foreach(LevelPlaylistRelation relation in relations)
{
relation.Index--;
});
}
}
});
}

public void SetPlaylistLevelIndex(GamePlaylist playlist, GameLevel level, int newIndex)
Expand Down Expand Up @@ -198,6 +203,11 @@ public int GetTotalLevelsInPlaylistCount(GamePlaylist playlist, TokenGame game)
.FilterByGameVersion(game)
.Count();

public int GetTotalLevelsInPlaylistCount(GamePlaylist playlist) =>
this.LevelPlaylistRelations.Where(l => l.Playlist == playlist).AsEnumerable()
.Select(l => l.Level)
.Count();

public IEnumerable<GamePlaylist> GetPlaylistsInPlaylist(GamePlaylist playlist)
// TODO: When we have postgres, remove the `AsEnumerable` call for performance.
=> this.SubPlaylistRelations.Where(p => p.Playlist == playlist).AsEnumerable()
Expand Down

0 comments on commit 966641d

Please sign in to comment.