From d647439ad6b80ef30e55b7c53bdbab5387787135 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Fri, 1 Sep 2023 23:00:17 -0700 Subject: [PATCH] Start tracking when users are publishing --- .../Endpoints/Game/Levels/PublishEndpoints.cs | 11 ++++-- Refresh.GameServer/RefreshGameServer.cs | 1 + .../Services/PublishTrackingService.cs | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 Refresh.GameServer/Services/PublishTrackingService.cs diff --git a/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs index 07d097b2..25d57c60 100644 --- a/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs @@ -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; @@ -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 logger) + public SerializedLevelResources? StartPublish(RequestContext context, GameUser user, GameDatabaseContext database, GameLevelRequest body, PublishTrackingService publishTracking, IDataStore dataStore, LoggerContainer logger) { //If verifying the request fails, return null if (!VerifyLevel(body, user, logger)) return null; @@ -58,6 +59,9 @@ 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(), @@ -65,7 +69,7 @@ private static bool VerifyLevel(GameLevelRequest body, GameUser user, LoggerCont } [GameEndpoint("publish", ContentType.Xml, Method.Post)] - public Response PublishLevel(RequestContext context, GameUser user, Token token, GameDatabaseContext database, GameLevelRequest body, IDataStore dataStore, LoggerContainer logger) + public Response PublishLevel(RequestContext context, GameUser user, Token token, GameDatabaseContext database, GameLevelRequest body, PublishTrackingService publishTracking, IDataStore dataStore, LoggerContainer logger) { //If verifying the request fails, return null if (!VerifyLevel(body, user, logger)) return BadRequest; @@ -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; diff --git a/Refresh.GameServer/RefreshGameServer.cs b/Refresh.GameServer/RefreshGameServer.cs index 15b19b47..33e871f5 100644 --- a/Refresh.GameServer/RefreshGameServer.cs +++ b/Refresh.GameServer/RefreshGameServer.cs @@ -120,6 +120,7 @@ protected virtual void SetupServices() this._server.AddRateLimitService(new RateLimitSettings(60, 400, 30, "global")); this._server.AddService(); this._server.AddService(); + this._server.AddService(); this._server.AddService(); this._server.AddService(); this._server.AddService(); diff --git a/Refresh.GameServer/Services/PublishTrackingService.cs b/Refresh.GameServer/Services/PublishTrackingService.cs new file mode 100644 index 00000000..719e0683 --- /dev/null +++ b/Refresh.GameServer/Services/PublishTrackingService.cs @@ -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 logger) : base(logger) {} + + private readonly HashSet _usersPublishing = new(); + + /// + /// Start tracking the user, eg. they started publishing + /// + /// + public void StartTracking(ObjectId id) + { + //Unconditionally add the user to the set + _ = this._usersPublishing.Add(id); + } + + /// + /// Stop tracking the user, eg. they stopped publishing + /// + /// + 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); +} \ No newline at end of file