Skip to content

Commit

Permalink
Minor refactor to handling of level republishes (#313)
Browse files Browse the repository at this point in the history
Closes #310
  • Loading branch information
jvyden authored Jan 1, 2024
2 parents 02db089 + 359c813 commit d118856
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
37 changes: 22 additions & 15 deletions Refresh.GameServer/Database/GameDatabaseContext.Levels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit d118856

Please sign in to comment.