From 6070e81c36f0e6496dbb76d8c1ae532682b368ef Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Mon, 1 Jan 2024 21:17:37 -0800 Subject: [PATCH] MatchService + GameUser: Move force matches to GameUser This is in preparation of #282 --- .../Database/GameDatabaseContext.Users.cs | 16 ++++++++++++++ .../Database/GameDatabaseProvider.cs | 2 +- .../Game/Handshake/AuthenticationEndpoints.cs | 3 +++ Refresh.GameServer/Services/CommandService.cs | 4 ++-- Refresh.GameServer/Services/MatchService.cs | 21 ------------------- .../Matching/MatchMethods/FindRoomMethod.cs | 4 ++-- Refresh.GameServer/Types/UserData/GameUser.cs | 5 +++++ 7 files changed, 29 insertions(+), 26 deletions(-) diff --git a/Refresh.GameServer/Database/GameDatabaseContext.Users.cs b/Refresh.GameServer/Database/GameDatabaseContext.Users.cs index 0f3ba503..6caf5e17 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.Users.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.Users.cs @@ -303,6 +303,22 @@ public void SetUserGriefReportRedirection(GameUser user, bool value) user.RedirectGriefReportsToPhotos = value; }); } + + public void ClearForceMatch(GameUser user) + { + this._realm.Write(() => + { + user.ForceMatch = null; + }); + } + + public void SetForceMatch(GameUser user, GameUser target) + { + this._realm.Write(() => + { + user.ForceMatch = target.ForceMatch; + }); + } #if DEBUG public void ForceUserTokenGame(Token token, TokenGame game) diff --git a/Refresh.GameServer/Database/GameDatabaseProvider.cs b/Refresh.GameServer/Database/GameDatabaseProvider.cs index 01bfbac8..3ab46dc5 100644 --- a/Refresh.GameServer/Database/GameDatabaseProvider.cs +++ b/Refresh.GameServer/Database/GameDatabaseProvider.cs @@ -32,7 +32,7 @@ protected GameDatabaseProvider(IDateTimeProvider time) this._time = time; } - protected override ulong SchemaVersion => 102; + protected override ulong SchemaVersion => 103; protected override string Filename => "refreshGameServer.realm"; diff --git a/Refresh.GameServer/Endpoints/Game/Handshake/AuthenticationEndpoints.cs b/Refresh.GameServer/Endpoints/Game/Handshake/AuthenticationEndpoints.cs index bbf604eb..0a7b6989 100644 --- a/Refresh.GameServer/Endpoints/Game/Handshake/AuthenticationEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/Handshake/AuthenticationEndpoints.cs @@ -157,6 +157,9 @@ public class AuthenticationEndpoints : EndpointGroup Token token = database.GenerateTokenForUser(user, TokenType.Game, game.Value, platform.Value, GameDatabaseContext.GameTokenExpirySeconds); // 4 hours + //Clear the user's force match + database.ClearForceMatch(user); + if (game == TokenGame.LittleBigPlanetPSP) { return new TicketLoginResponse diff --git a/Refresh.GameServer/Services/CommandService.cs b/Refresh.GameServer/Services/CommandService.cs index 45da0a1c..e5406017 100644 --- a/Refresh.GameServer/Services/CommandService.cs +++ b/Refresh.GameServer/Services/CommandService.cs @@ -94,14 +94,14 @@ public void HandleCommand(CommandInvocation command, GameDatabaseContext databas if (target != null) { - this._match.SetForceMatch(user.UserId, target.UserId); + database.SetForceMatch(user, target); } break; } case "clearforcematch": { - this._match.ClearForceMatch(user.UserId); + database.ClearForceMatch(user); break; } case "griefphotoson": diff --git a/Refresh.GameServer/Services/MatchService.cs b/Refresh.GameServer/Services/MatchService.cs index 3a0a0dad..2036f703 100644 --- a/Refresh.GameServer/Services/MatchService.cs +++ b/Refresh.GameServer/Services/MatchService.cs @@ -19,7 +19,6 @@ public partial class MatchService(Logger logger) : EndpointService(logger) private FrozenSet _matchMethods = null!; // initialized in Initialize() public IRoomAccessor RoomAccessor { get; private set; } = null!; //initialized in Initialize() - private readonly Dictionary _forceMatches = new(); public GameRoom GetOrCreateRoomByPlayer(GameUser player, TokenPlatform platform, TokenGame game, NatType natType) { @@ -120,24 +119,4 @@ public Response ExecuteMethod(string methodStr, SerializedRoomData roomData, Gam return method.Execute(this, this.Logger, database, user, token, roomData); } - - public void SetForceMatch(ObjectId user, ObjectId target) - { - this._forceMatches[user] = target; - } - - public ObjectId? GetForceMatch(ObjectId user) - { - if (this._forceMatches.TryGetValue(user, out ObjectId target)) - { - return target; - } - - return null; - } - - public void ClearForceMatch(ObjectId user) - { - this._forceMatches.Remove(user); - } } \ No newline at end of file diff --git a/Refresh.GameServer/Types/Matching/MatchMethods/FindRoomMethod.cs b/Refresh.GameServer/Types/Matching/MatchMethods/FindRoomMethod.cs index 3c6753e5..c4c0a5f5 100644 --- a/Refresh.GameServer/Types/Matching/MatchMethods/FindRoomMethod.cs +++ b/Refresh.GameServer/Types/Matching/MatchMethods/FindRoomMethod.cs @@ -50,7 +50,7 @@ public Response Execute(MatchService service, Logger logger, GameDatabaseContext rooms = rooms.Where(r => r.NatType == NatType.Open).ToList(); } - ObjectId? forceMatch = service.GetForceMatch(user.UserId); + ObjectId? forceMatch = user.ForceMatch; //If the user has a forced match if (forceMatch != null) @@ -68,7 +68,7 @@ public Response Execute(MatchService service, Logger logger, GameDatabaseContext if (forceMatch != null) { //Clear the user's force match - service.ClearForceMatch(user.UserId); + database.ClearForceMatch(user); } GameRoom room = rooms[Random.Shared.Next(0, rooms.Count)]; diff --git a/Refresh.GameServer/Types/UserData/GameUser.cs b/Refresh.GameServer/Types/UserData/GameUser.cs index 89df0b04..03877fdf 100644 --- a/Refresh.GameServer/Types/UserData/GameUser.cs +++ b/Refresh.GameServer/Types/UserData/GameUser.cs @@ -22,6 +22,11 @@ public partial class GameUser : IRealmObject, IRateLimitUser public bool ShouldResetPassword { get; set; } public string IconHash { get; set; } = "0"; + + /// + /// The force match of the user, cleared on login + /// + public ObjectId? ForceMatch { get; set; } /// /// The , except only for PSP clients.