diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs b/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs index 5409aac2..0d98820a 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Levels.cs @@ -58,34 +58,41 @@ public GameLevel GetStoryLevelById(int id) return level; } - public GameLevel? UpdateLevel(GameLevel level, GameUser user) + public GameLevel? UpdateLevel(GameLevel newLevel, GameUser author) { // Verify if this level is able to be republished - GameLevel? oldSlot = this.GetLevelById(level.LevelId); - if (oldSlot == null) return null; + GameLevel? oldLevel = this.GetLevelById(newLevel.LevelId); + if (oldLevel == null) return null; - Debug.Assert(oldSlot.Publisher != null); - if (oldSlot.Publisher.UserId != user.UserId) return null; + Debug.Assert(oldLevel.Publisher != null); + if (oldLevel.Publisher.UserId != author.UserId) return null; - // All checks passed, lets move - - long oldDate = oldSlot.PublishDate; + // All checks passed, let's start by retaining some information from the old level + newLevel.Publisher = author; + newLevel.PublishDate = newLevel.PublishDate; + newLevel.UpdateDate = this._time.TimestampMilliseconds; // Set the last modified date + + // If the actual contents of the level haven't changed, extract some extra information + if (oldLevel.RootResource == newLevel.RootResource) + { + newLevel.TeamPicked = oldLevel.TeamPicked; + newLevel.GameVersion = oldLevel.GameVersion; + } + + // Now newLevel is set up to replace oldLevel. + // If information is lost here, then that's probably a bug. + // Update the level's properties in the database this._realm.Write(() => { PropertyInfo[] userProps = typeof(GameLevel).GetProperties(); foreach (PropertyInfo prop in userProps) { if (!prop.CanWrite || !prop.CanRead) continue; - prop.SetValue(oldSlot, prop.GetValue(level)); + prop.SetValue(oldLevel, prop.GetValue(newLevel)); } - - oldSlot.Publisher = user; - - oldSlot.PublishDate = oldDate; - oldSlot.UpdateDate = this._time.TimestampMilliseconds; }); - return oldSlot; + return oldLevel; } public GameLevel UpdateLevel(ApiEditLevelRequest body, GameLevel level) diff --git a/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs index 8848f1b6..630cabb5 100644 --- a/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs @@ -83,7 +83,14 @@ private static bool VerifyLevel(GameLevelRequest body, GameUser user, Logger log } [GameEndpoint("publish", ContentType.Xml, HttpMethods.Post)] - public Response PublishLevel(RequestContext context, GameUser user, Token token, GameDatabaseContext database, GameLevelRequest body, CommandService command, IDataStore dataStore, GuidCheckerService guidChecker) + public Response PublishLevel(RequestContext context, + GameUser user, + Token token, + GameDatabaseContext database, + GameLevelRequest body, + CommandService commandService, + IDataStore dataStore, + GuidCheckerService guidChecker) { //If verifying the request fails, return null if (!VerifyLevel(body, user, context.Logger, guidChecker, token.TokenGame)) return BadRequest; @@ -117,7 +124,7 @@ public Response PublishLevel(RequestContext context, GameUser user, Token token, } //Mark the user as no longer publishing - command.StopPublishing(user.UserId); + commandService.StopPublishing(user.UserId); level.Publisher = user;