Files
Shogi/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/ListGamesHandler.cs
2021-05-08 10:26:04 -05:00

43 lines
1.3 KiB
C#

using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System.Linq;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public interface IListGamesHandler
{
Task Handle(ListGamesRequest request, string userName);
}
// TODO: This doesn't need to be a socket action.
// It can be an HTTP route.
public class ListGamesHandler : IListGamesHandler
{
private readonly ISocketCommunicationManager communicationManager;
private readonly IGameboardRepository repository;
public ListGamesHandler(
ISocketCommunicationManager communicationManager,
IGameboardRepository repository)
{
this.communicationManager = communicationManager;
this.repository = repository;
}
public async Task Handle(ListGamesRequest _, string userName)
{
var sessions = await repository.ReadSessions();
var games = sessions.Select(s => s.ToServiceModel()); // yuck
var response = new ListGamesResponse(ClientAction.ListGames)
{
Games = games.ToList()
};
await communicationManager.BroadcastToPlayers(response, userName);
}
}
}