using Gameboard.ShogiUI.Sockets.Extensions; 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.Net.WebSockets; using System.Threading.Tasks; namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class ListGamesHandler : IActionHandler { private readonly IGameboardRepository repository; public ListGamesHandler( IGameboardRepository repository) { this.repository = repository; } public async Task Handle(WebSocket socket, string json, string userName) { var request = JsonConvert.DeserializeObject(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 }; var serialized = JsonConvert.SerializeObject(response); await socket.SendTextAsync(serialized); } } }