Skip to content

Commit

Permalink
Start tracking when users are publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Sep 2, 2023
1 parent b42d456 commit d647439
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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 @@ -8,6 +8,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.DataTypes.Request;
using Refresh.GameServer.Endpoints.Game.DataTypes.Response;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.UserData;

Expand Down Expand Up @@ -44,7 +45,7 @@ private static bool VerifyLevel(GameLevelRequest body, GameUser user, LoggerCont

[GameEndpoint("startPublish", ContentType.Xml, Method.Post)]
[NullStatusCode(BadRequest)]
public SerializedLevelResources? StartPublish(RequestContext context, GameUser user, GameDatabaseContext database, GameLevelRequest body, IDataStore dataStore, LoggerContainer<BunkumContext> logger)
public SerializedLevelResources? StartPublish(RequestContext context, GameUser user, GameDatabaseContext database, GameLevelRequest body, PublishTrackingService publishTracking, IDataStore dataStore, LoggerContainer<BunkumContext> logger)
{
//If verifying the request fails, return null
if (!VerifyLevel(body, user, logger)) return null;
Expand All @@ -58,14 +59,17 @@ private static bool VerifyLevel(GameLevelRequest body, GameUser user, LoggerCont

if (hashes.Any(hash => hash.Length != 40)) return null;

//Mark the user as publishing
publishTracking.StartTracking(user.UserId);

return new SerializedLevelResources
{
Resources = hashes.Where(r => !dataStore.ExistsInStore(r)).ToArray(),
};
}

[GameEndpoint("publish", ContentType.Xml, Method.Post)]
public Response PublishLevel(RequestContext context, GameUser user, Token token, GameDatabaseContext database, GameLevelRequest body, IDataStore dataStore, LoggerContainer<BunkumContext> logger)
public Response PublishLevel(RequestContext context, GameUser user, Token token, GameDatabaseContext database, GameLevelRequest body, PublishTrackingService publishTracking, IDataStore dataStore, LoggerContainer<BunkumContext> logger)
{
//If verifying the request fails, return null
if (!VerifyLevel(body, user, logger)) return BadRequest;
Expand Down Expand Up @@ -93,6 +97,9 @@ public Response PublishLevel(RequestContext context, GameUser user, Token token,
database.AddPublishFailNotification("You may not republish another user's level.", level, user);
return BadRequest;
}

//Mark the user as no longer publishing
publishTracking.StopTracking(user.UserId);

level.Publisher = user;

Expand Down
1 change: 1 addition & 0 deletions Refresh.GameServer/RefreshGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ protected virtual void SetupServices()
this._server.AddRateLimitService(new RateLimitSettings(60, 400, 30, "global"));
this._server.AddService<CategoryService>();
this._server.AddService<FriendStorageService>();
this._server.AddService<PublishTrackingService>();
this._server.AddService<MatchService>();
this._server.AddService<ImportService>();
this._server.AddService<DocumentationService>();
Expand Down
35 changes: 35 additions & 0 deletions Refresh.GameServer/Services/PublishTrackingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Bunkum.HttpServer;
using Bunkum.HttpServer.Services;
using MongoDB.Bson;
using NotEnoughLogs;

namespace Refresh.GameServer.Services;

public class PublishTrackingService : EndpointService
{
public PublishTrackingService(LoggerContainer<BunkumContext> logger) : base(logger) {}

private readonly HashSet<ObjectId> _usersPublishing = new();

/// <summary>
/// Start tracking the user, eg. they started publishing
/// </summary>
/// <param name="id"></param>
public void StartTracking(ObjectId id)
{
//Unconditionally add the user to the set
_ = this._usersPublishing.Add(id);
}

/// <summary>
/// Stop tracking the user, eg. they stopped publishing
/// </summary>
/// <param name="id"></param>
public void StopTracking(ObjectId id)
{
//Unconditionally remove the user from the set
_ = this._usersPublishing.Remove(id);
}

public bool IsPublishing(ObjectId id) => this._usersPublishing.Contains(id);
}

0 comments on commit d647439

Please sign in to comment.