diff --git a/SVSim.EmulatedEntrypoint/Controllers/ArenaTwoPickBattleController.cs b/SVSim.EmulatedEntrypoint/Controllers/ArenaTwoPickBattleController.cs index d326da1..c2f2322 100644 --- a/SVSim.EmulatedEntrypoint/Controllers/ArenaTwoPickBattleController.cs +++ b/SVSim.EmulatedEntrypoint/Controllers/ArenaTwoPickBattleController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using SVSim.BattleNode.Bridge; using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.ArenaTwoPick; using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick; using SVSim.EmulatedEntrypoint.Services; @@ -9,13 +10,25 @@ namespace SVSim.EmulatedEntrypoint.Controllers; public class ArenaTwoPickBattleController : SVSimController { private readonly IArenaTwoPickService _svc; - public ArenaTwoPickBattleController(IArenaTwoPickService svc) => _svc = svc; + private readonly IMatchingBridge _matching; + + public ArenaTwoPickBattleController(IArenaTwoPickService svc, IMatchingBridge matching) + { + _svc = svc; + _matching = matching; + } [HttpPost("do_matching")] public IActionResult DoMatching([FromBody] DoMatchingRequest req) { - if (!TryGetViewerId(out _)) return Unauthorized(); - return Ok(new DoMatchingResponseDto()); + if (!TryGetViewerId(out var vid)) return Unauthorized(); + var match = _matching.RegisterPendingBattle(vid); + return Ok(new DoMatchingResponseDto + { + MatchingState = 3004, + BattleId = match.BattleId, + NodeServerUrl = match.NodeServerUrl, + }); } [HttpPost("finish")] diff --git a/SVSim.EmulatedEntrypoint/Models/Dtos/Responses/ArenaTwoPick/DoMatchingResponseDto.cs b/SVSim.EmulatedEntrypoint/Models/Dtos/Responses/ArenaTwoPick/DoMatchingResponseDto.cs index 0a14d50..7c3411f 100644 --- a/SVSim.EmulatedEntrypoint/Models/Dtos/Responses/ArenaTwoPick/DoMatchingResponseDto.cs +++ b/SVSim.EmulatedEntrypoint/Models/Dtos/Responses/ArenaTwoPick/DoMatchingResponseDto.cs @@ -5,23 +5,26 @@ using SVSim.EmulatedEntrypoint.Models.Dtos.Common; namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.ArenaTwoPick; [MessagePackObject] -public class DoMatchingResponseDto +public sealed class DoMatchingResponseDto { + [JsonPropertyName("matching_state")] [Key("matching_state")] + public int MatchingState { get; set; } = 3004; // SUCCEEDED + [JsonPropertyName("timeout_period")] [JsonConverter(typeof(StringifiedIntConverter))] [Key("timeout_period")] - public int TimeoutPeriod { get; set; } = 50; + public int TimeoutPeriod { get; set; } = 30; [JsonPropertyName("retry_period")] [JsonConverter(typeof(StringifiedIntConverter))] [Key("retry_period")] public int RetryPeriod { get; set; } = 3; + [JsonPropertyName("battle_id")] [Key("battle_id")] + public string BattleId { get; set; } = ""; + [JsonPropertyName("node_server_url")] [Key("node_server_url")] public string NodeServerUrl { get; set; } = ""; [JsonPropertyName("room_param")] [Key("room_param")] public string RoomParam { get; set; } = ""; - [JsonPropertyName("matching_state")] [Key("matching_state")] - public int MatchingState { get; set; } = 3002; - [JsonPropertyName("mission_parameter")] [Key("mission_parameter")] public Dictionary MissionParameter { get; set; } = new() { diff --git a/SVSim.UnitTests/Controllers/ArenaTwoPickBattleControllerTests.cs b/SVSim.UnitTests/Controllers/ArenaTwoPickBattleControllerTests.cs index 40582b4..33c4ea8 100644 --- a/SVSim.UnitTests/Controllers/ArenaTwoPickBattleControllerTests.cs +++ b/SVSim.UnitTests/Controllers/ArenaTwoPickBattleControllerTests.cs @@ -1,5 +1,6 @@ using System.Net; using System.Net.Http.Json; +using System.Text.Json; using SVSim.UnitTests.Infrastructure; namespace SVSim.UnitTests.Controllers; @@ -7,7 +8,7 @@ namespace SVSim.UnitTests.Controllers; public class ArenaTwoPickBattleControllerTests { [Test] - public async Task DoMatching_returns_perpetual_searching_with_empty_node_url() + public async Task DoMatching_AuthenticatedViewer_Returns3004WithBattleIdAndNodeUrl() { using var factory = new SVSimTestFactory(); var viewerId = await factory.SeedViewerAsync(); @@ -20,8 +21,13 @@ public class ArenaTwoPickBattleControllerTests Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.OK)); var body = await resp.Content.ReadAsStringAsync(); - StringAssert.Contains("\"matching_state\":3002", body); - StringAssert.Contains("\"node_server_url\":\"\"", body); - StringAssert.DoesNotContain("\"battle_id\"", body); + using var doc = JsonDocument.Parse(body); + var root = doc.RootElement; + + Assert.That(root.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004)); + var battleId = root.GetProperty("battle_id").GetString(); + Assert.That(battleId, Is.Not.Null.And.Not.Empty); + var nodeUrl = root.GetProperty("node_server_url").GetString(); + Assert.That(nodeUrl, Does.StartWith("ws://")); } }