Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor to handling of level republishes #313

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading