Skip to content

Commit

Permalink
Add hashed live play now
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Sep 7, 2024
1 parent 2e67288 commit 6359afb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Refresh.Common/Constants/SystemUsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public static class SystemUsers

public const string UnknownUserName = "!Unknown";
public const string UnknownUserDescription = "I'm a fake user that represents a non existent publisher for re-published levels.";

public const string HashedUserName = "!Hashed";
public const string HashedUserDescription = "I'm a fake user that represents an unknown publisher for hashed levels.";
}
7 changes: 5 additions & 2 deletions Refresh.GameServer/Endpoints/ApiV3/LevelApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Refresh.GameServer.Endpoints.ApiV3.ApiTypes.Errors;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Request;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response.Levels;
using Refresh.GameServer.Endpoints.Game.DataTypes.Response;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Extensions;
using Refresh.GameServer.Services;
Expand Down Expand Up @@ -166,12 +167,14 @@ public ApiOkResponse SetLevelAsOverrideById(RequestContext context,
[DocSummary("Marks the level hash to show in the next slot list gotten from the game")]
[DocError(typeof(ApiValidationError), ApiValidationError.HashInvalidErrorWhen)]
public ApiOkResponse SetLevelAsOverrideByHash(RequestContext context, GameDatabaseContext database, GameUser user,
LevelListOverrideService service, [DocSummary("The hash of level root resource")] string hash)
LevelListOverrideService service, PresenceService presenceService, [DocSummary("The hash of level root resource")] string hash)
{
if (!CommonPatterns.Sha1Regex().IsMatch(hash))
return ApiValidationError.HashInvalidError;

bool presenceUsed = presenceService.PlayLevel(user, GameLevelResponse.LevelIdFromHash(hash));

service.AddHashOverrideForUser(user, hash);
service.AddHashOverrideForUser(user, hash, presenceUsed);

return new ApiOkResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static GameLevelResponse FromHash(string hash, DataContext dataContext)
Location = new GameLocation(),
Handle = new SerializedUserHandle
{
Username = $"!Hashed",
Username = SystemUsers.HashedUserName,
IconHash = "0",
},
Type = GameSlotType.User.ToGameType(),
Expand Down
4 changes: 2 additions & 2 deletions Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Bunkum.Core;
using Bunkum.Core.Endpoints;
using Bunkum.Core.Endpoints.Debugging;
using Bunkum.Core.Storage;
using Bunkum.Listener.Protocol;
using Refresh.Common.Constants;
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.DataTypes.Response;
Expand Down Expand Up @@ -47,7 +47,7 @@ public class LevelEndpoints : EndpointGroup

// If we are getting the levels by a user, and that user is "!Hashed", then we pull that user's overrides
if (route == "by"
&& (context.QueryString.Get("u") == "!Hashed" || user.Username == "!Hashed")
&& (context.QueryString.Get("u") == SystemUsers.HashedUserName || user.Username == SystemUsers.HashedUserName)
&& overrideService.GetLastHashOverrideForUser(token, out string hash))
{
return new SerializedMinimalLevelList
Expand Down
4 changes: 2 additions & 2 deletions Refresh.GameServer/Services/LevelListOverrideService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ private bool UserHasLevelHashOverride(GameUser user)
public bool UserHasOverrides(GameUser user)
=> this.UserHasLevelHashOverride(user) || this.UserHasLevelIdOverrides(user);

public void AddHashOverrideForUser(GameUser user, string hash)
public void AddHashOverrideForUser(GameUser user, string hash, bool accessed = false)
{
this.Logger.LogDebug(RefreshContext.LevelListOverride, "Adding level hash override for {0}: [{1}]", user.Username, hash);

this._userIdsToLevelHash[user.UserId] = (false, hash);
this._userIdsToLevelHash[user.UserId] = (accessed, hash);
}

public bool GetLastHashOverrideForUser(Token token, out string hash)
Expand Down
2 changes: 2 additions & 0 deletions Refresh.GameServer/Services/PresenceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public bool PlayLevel(GameUser user, int levelId)
if (!this._config.PresenceEnabled || user.PresenceServerAuthToken == null)
return false;

this.Logger.LogInfo(RefreshContext.Presence, $"Sending live play now for level ID {levelId} to {user}");

HttpResponseMessage result = this._client.PostAsync($"/api/playLevel/{levelId}", new StringContent(user.PresenceServerAuthToken)).Result;

if (result.IsSuccessStatusCode)
Expand Down

0 comments on commit 6359afb

Please sign in to comment.