Skip to content

Commit

Permalink
Dive in by level fix (#603)
Browse files Browse the repository at this point in the history
Closes #599
  • Loading branch information
jvyden authored Aug 1, 2024
2 parents db0ef76 + f506671 commit 18402e1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Refresh.GameServer/Services/MatchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Response ExecuteMethod(string methodStr, SerializedRoomData roomData, Dat
return response;

// If there's no response body, then we need to make our own using the status code
SerializedStatusCodeMatchResponse status = new((int)response.StatusCode);
SerializedStatusCodeMatchResponse status = new(response.StatusCode);

List<object> responseData = [status];

Expand Down
23 changes: 7 additions & 16 deletions Refresh.GameServer/Types/Matching/MatchMethods/FindRoomMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
using Bunkum.Core.Responses;
using Bunkum.Listener.Protocol;
using MongoDB.Bson;
using NotEnoughLogs;
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Configuration;
using Refresh.GameServer.Database;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.Matching.Responses;
using Refresh.GameServer.Types.UserData;

Expand All @@ -20,6 +16,8 @@ public class FindRoomMethod : IMatchMethod

public Response Execute(DataContext dataContext, SerializedRoomData body, GameServerConfig gameServerConfig)
{
SerializedStatusCodeMatchResponse status = new(OK);

GameRoom? usersRoom = dataContext.Match.RoomAccessor.GetRoomByUser(dataContext.User!, dataContext.Platform, dataContext.Game);
if (usersRoom == null) return BadRequest; // user should already have a room.

Expand Down Expand Up @@ -66,8 +64,8 @@ public Response Execute(DataContext dataContext, SerializedRoomData body, GameSe

IEnumerable<GameRoom> rooms = allRooms.Where(r =>
// If the level id isn't specified, or is 0, then we don't want to try to match against level IDs, else only match the user to people who are playing that level
(userLevelIds.Count == 0 || r.LevelType != RoomSlotType.Online || userLevelIds.Contains(r.LevelId)) &&
(developerLevelIds.Count == 0 || r.LevelType != RoomSlotType.Story || developerLevelIds.Contains(r.LevelId)) &&
(userLevelIds.Count == 0 || (r.LevelType == RoomSlotType.Online && userLevelIds.Contains(r.LevelId))) &&
(developerLevelIds.Count == 0 || (r.LevelType == RoomSlotType.Story && developerLevelIds.Contains(r.LevelId))) &&
// Make sure that we don't try to match the player into a full room, or a room which won't fit the user's current room
usersRoom.PlayerIds.Count + r.PlayerIds.Count <= 4)
// Shuffle the rooms around before sorting, this is because the selection is based on a weighted average towards the top of the range,
Expand Down Expand Up @@ -126,7 +124,8 @@ public Response Execute(DataContext dataContext, SerializedRoomData body, GameSe
}

// Return a 404 status code if there's no rooms to match them to
return new Response(new List<object> { new SerializedStatusCodeMatchResponse(404), }, ContentType.Json);
status.StatusCode = NotFound;
return new Response(new List<object> {status}, ContentType.Json, status.StatusCode);
}

// If the user has a forced match and we found a room
Expand Down Expand Up @@ -174,14 +173,6 @@ public Response Execute(DataContext dataContext, SerializedRoomData body, GameSe
roomMatch.Players.Add(new SerializedRoomPlayer(roomUser.Username, 1));
}

SerializedStatusCodeMatchResponse status = new(200);

List<object> response =
[
status,
roomMatch,
];

return new Response(response, ContentType.Json);
return new Response(new List<object> {status, roomMatch}, ContentType.Json, status.StatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Bunkum.Core.Responses;

namespace Refresh.GameServer.Types.Matching.Responses;

[JsonObject(MemberSerialization.OptIn)]
public class SerializedStatusCodeMatchResponse
public class SerializedStatusCodeMatchResponse: IHasResponseCode
{
[ExcludeFromCodeCoverage]
public SerializedStatusCodeMatchResponse() {}

public SerializedStatusCodeMatchResponse(int statusCode)
public SerializedStatusCodeMatchResponse(HttpStatusCode statusCode)
{
this.StatusCode = statusCode;
}

[JsonProperty] public int StatusCode { get; set; }
[JsonProperty] public HttpStatusCode StatusCode { get; set; }
}
4 changes: 2 additions & 2 deletions Refresh.GameServer/Types/Matching/SerializedRoomData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class SerializedRoomData
// ReSharper disable InconsistentNaming
// LBP has two of the same thing sometimes, have both properties to handle both cases
[JsonProperty("Slot")]
private List<int>? _Slot { get; set; }
public List<int>? _Slot { private get; set; }

[JsonProperty("Slots")]
private List<List<int>>? _Slots { get; set; }
public List<List<int>>? _Slots { private get; set; }
// ReSharper restore InconsistentNaming

[JsonIgnore]
Expand Down
54 changes: 50 additions & 4 deletions RefreshTests.GameServer/Tests/Matching/MatchingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public void DoesntMatchIfNoRooms()

//Make sure the only result is a 404 object
Assert.That(responseObjects, Has.Count.EqualTo(1));
Assert.That(response.StatusCode, Is.EqualTo(OK));
Assert.That(responseObjects[0].StatusCode, Is.EqualTo(404));
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
Assert.That(responseObjects[0].StatusCode, Is.EqualTo(NotFound));
}

[Test]
Expand Down Expand Up @@ -185,8 +185,8 @@ public void StrictNatCantJoinStrict()

//Make sure the only result is a 404 object
Assert.That(responseObjects, Has.Count.EqualTo(1));
Assert.That(response.StatusCode, Is.EqualTo(OK));
Assert.That(responseObjects[0].StatusCode, Is.EqualTo(404));
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
Assert.That(responseObjects[0].StatusCode, Is.EqualTo(NotFound));
}

[Test]
Expand Down Expand Up @@ -469,4 +469,50 @@ public void PlayersCanLeaveAndSplitIntoNewRoom()
}

}

[Test]
public void DoesntMatchIfLookingForLevelWhenPod()
{
using TestContext context = this.GetServer(false);
MatchService match = new(Logger);
match.Initialize();

SerializedRoomData roomData = new()
{
Mood = (byte)RoomMood.AllowingAll, // Tells their rooms that they can be matched with each other
NatType = new List<NatType>
{
NatType.Open,
},
_Slot =
[
(int)RoomSlotType.Pod,
0,
],
};

// Setup rooms
GameUser user1 = context.CreateUser();
GameUser user2 = context.CreateUser();

Token token1 = context.CreateToken(user1);
Token token2 = context.CreateToken(user2);

match.ExecuteMethod("CreateRoom", roomData, context.GetDataContext(token1), context.Server.Value.GameServerConfig);
match.ExecuteMethod("CreateRoom", roomData, context.GetDataContext(token2), context.Server.Value.GameServerConfig);

// Tell user2 to try to find a room
Response response = match.ExecuteMethod("FindBestRoom", new SerializedRoomData
{
NatType = new List<NatType> {
NatType.Open,
},
_Slot =
[
(int)RoomSlotType.Online,
1337,
],
}, context.GetDataContext(token2), context.Server.Value.GameServerConfig);
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}
}

0 comments on commit 18402e1

Please sign in to comment.