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); } public class ListGamesHandler : IListGamesHandler { private readonly ISocketConnectionManager communicationManager; private readonly IGameboardRepository repository; public ListGamesHandler( ISocketConnectionManager communicationManager, IGameboardRepository repository) { this.communicationManager = communicationManager; this.repository = repository; } public async Task Handle(ListGamesRequest _, string userName) { var sessions = await repository.ReadSessionMetadatas(); var games = sessions.Select(s => new Game(s.Name, s.Player1, s.Player2)).ToList(); var response = new ListGamesResponse(ClientAction.ListGames) { Games = games }; await communicationManager.BroadcastToPlayers(response, userName); } } }