46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Gameboard.ShogiUI.Sockets.Models;
|
|
using Gameboard.ShogiUI.Sockets.Repositories;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
|
using Newtonsoft.Json;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
|
{
|
|
// TODO: This doesn't need to be a socket action.
|
|
// It can be an HTTP route.
|
|
public class ListGamesHandler : IActionHandler
|
|
{
|
|
private readonly ISocketCommunicationManager communicationManager;
|
|
private readonly IGameboardRepository repository;
|
|
|
|
public ListGamesHandler(
|
|
ISocketCommunicationManager communicationManager,
|
|
IGameboardRepository repository)
|
|
{
|
|
this.communicationManager = communicationManager;
|
|
this.repository = repository;
|
|
}
|
|
|
|
public async Task Handle(string json, string userName)
|
|
{
|
|
var request = JsonConvert.DeserializeObject<ListGamesRequest>(json);
|
|
var getGamesResponse = string.IsNullOrWhiteSpace(userName)
|
|
? await repository.GetGames()
|
|
: await repository.GetGames(userName);
|
|
|
|
var games = getGamesResponse.Sessions
|
|
.OrderBy(s => s.Player1 == userName || s.Player2 == userName)
|
|
.Select(s => new Session(s).ToServiceModel()); // yuck
|
|
|
|
var response = new ListGamesResponse(ClientAction.ListGames)
|
|
{
|
|
Games = games.ToList()
|
|
};
|
|
|
|
await communicationManager.BroadcastToPlayers(response, userName);
|
|
}
|
|
}
|
|
}
|