Skip to content

Commit

Permalink
Merge branch 'feature/http_id_retriever' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehanari committed Jul 21, 2024
2 parents d628a25 + 1593d46 commit 1e45ea9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions castledice-matchmaker/DataSenders/CancelationResultSender.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using castledice_events_logic.ServerToClient;
using castledice_riptide_dto_adapters.Extensions;
using NLog;
using Riptide;

namespace castledice_matchmaker.DataSenders;

public class CancelationResultSender : ICancelationResultSender
{
private readonly IMessageSenderById _messageSender;
private readonly Logger _logger = LogManager.GetCurrentClassLogger();

public CancelationResultSender(IMessageSenderById messageSender)
{
Expand All @@ -15,6 +17,7 @@ public CancelationResultSender(IMessageSenderById messageSender)

public void SendCancelationResult(int playerId, bool isCanceled)
{
_logger.Info($"Sending cancelation result for player with id {playerId}");
var dto = new CancelGameResultDTO(isCanceled, playerId);
var message = Message.Create(MessageSendMode.Reliable, (ushort)ServerToClientMessageType.CancelGame);
message.AddCancelGameResultDTO(dto);
Expand Down
7 changes: 6 additions & 1 deletion castledice-matchmaker/HttpIdRetriever.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Net.Http.Headers;
using castledice_matchmaker.HttpUtilities;
using Newtonsoft.Json.Linq;
using NLog;

namespace castledice_matchmaker;

public class HttpIdRetriever : IIdRetriever
{
private readonly string _authServiceUrl;
private readonly IHttpMessageSender _messageSender;
private readonly Logger _logger = LogManager.GetCurrentClassLogger();

public HttpIdRetriever(string authServiceUrl, IHttpMessageSender messageSender)
{
Expand All @@ -17,12 +19,15 @@ public HttpIdRetriever(string authServiceUrl, IHttpMessageSender messageSender)

public async Task<int> RetrievePlayerIdAsync(string playerToken)
{
_logger.Info($"Retrieving player id for token: {playerToken}");
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, _authServiceUrl + "/me");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", playerToken);
using var response = await _messageSender.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return GetIdFromJson(responseBody);
var id = GetIdFromJson(responseBody);
_logger.Info($"Retrieved player id: {id}");
return id;
}

private int GetIdFromJson(string json)
Expand Down
2 changes: 1 addition & 1 deletion castledice-matchmaker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void Main(string[] args)
new DuelModeQueue()
},
new MatchSender(serverWrapper),
new HttpIdRetriever(authBackendConnectionOptions.Url, httpClientWrapper), //TODO: This must be replaced with an actual id retriever
new HttpIdRetriever(authBackendConnectionOptions.Url, httpClientWrapper),
new CancelationResultSender(serverWrapper)
);

Expand Down
17 changes: 17 additions & 0 deletions castledice-matchmaker/QueuesController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using castledice_events_logic.ClientToServer;
using castledice_matchmaker.DataSenders;
using castledice_matchmaker.Matches;
using castledice_matchmaker.Queues;
using NLog;

namespace castledice_matchmaker;

Expand All @@ -10,6 +12,7 @@ public class QueuesController : IRequestGameDTOAccepter, ICancelGameDTOAccepter
private readonly ICancelationResultSender _cancelationResultSender;
private readonly IMatchSender _matchSender;
private readonly IIdRetriever _idRetriever;
private readonly Logger _logger = LogManager.GetCurrentClassLogger();

public QueuesController(List<IGameModeQueue> queues, IMatchSender matchSender, IIdRetriever idRetriever, ICancelationResultSender cancelationResultSender)
{
Expand All @@ -21,20 +24,34 @@ public QueuesController(List<IGameModeQueue> queues, IMatchSender matchSender, I

public async Task AcceptRequestGameDTOAsync(RequestGameDTO dto)
{
_logger.Info($"Received request game DTO with verification key: {dto.VerificationKey}");
var playerId = await _idRetriever.RetrievePlayerIdAsync(dto.VerificationKey);
var duelQueue = _queues[0]; //TODO: At the moment the only possible game mode is duel, however in the future there will be more and this logic will have to be rewritten.
duelQueue.EnqueuePlayer(playerId);
var availableMatches = duelQueue.GetMatches();
foreach (var match in availableMatches)
{
if (match is DuelMatch duelMatch)
{
_logger.Info($"Sending duel match to players: {duelMatch.FirstPlayerId} and {duelMatch.SecondPlayerId}");
}
_matchSender.SendMatch(match);
}
}

public async Task AcceptCancelGameDTOAsync(CancelGameDTO dto)
{
_logger.Info($"Received cancel game DTO with verification key: {dto.VerificationKey}");
var playerId = await _idRetriever.RetrievePlayerIdAsync(dto.VerificationKey);
var isPlayerRemoved = _queues.Any(queue => queue.RemovePlayer(playerId));
if (isPlayerRemoved)
{
_logger.Info($"Player with id {playerId} has been removed from all queues");
}
else
{
_logger.Info($"Player with id {playerId} has not been found in any queue");
}
_cancelationResultSender.SendCancelationResult(playerId, isPlayerRemoved);
}
}

0 comments on commit 1e45ea9

Please sign in to comment.