public async Task HandleMessageAsync(IPlayer player, IReceivedMessage message) { var castedMessage = (FindGameMessage)message; if (player.GameSessionGUID != Guid.Empty) { await collections.RemovePlayer(player); collections.AddPlayer(player); } var expectedGame = new ExpectedTicTacToe(castedMessage.Size); player.SetAsSearchingForGame(expectedGame); try { var opponent = collections.FindPlayerSearchingForGame(player); var session = sessionFactory.Create(player, opponent, expectedGame); collections.AddSession(session); logger.LogInformation("Created new game session."); await messageSender.SendMessageAsync(player.Socket, new GameFoundMessage(true)); await messageSender.SendMessageAsync(opponent.Socket, new GameFoundMessage(false)); } catch (InvalidOperationException) { } }
public async Task HandleMessageAsync(IPlayer player, IReceivedMessage message) { var findChessGameMessage = (FindChessGameMessage)message; if (player.GameSessionGUID != Guid.Empty) { await collections.RemovePlayer(player); collections.AddPlayer(player); } var expectedGame = new ExpectedChess(); player.SetAsSearchingForGame(expectedGame); try { var opponent = collections.FindPlayerSearchingForGame(player); ChessGameSession session = (ChessGameSession)sessionFactory .Create(player, opponent, expectedGame); collections.AddSession(session); logger.LogInformation("Created new chess game session."); await messageSender.SendMessageAsync(player.Socket, new GameFoundMessage(true)); await messageSender.SendMessageAsync(opponent.Socket, new GameFoundMessage(false)); var piecesAndMovesMessage = new ChessPiecesAndMovesMessage() { AvailableMoves = session.GetAvailableMoves(), Pieces = session.GetAvailablePieces() }; await messageSender.SendMessageAsync(player.Socket, piecesAndMovesMessage); await messageSender.SendMessageAsync(opponent.Socket, piecesAndMovesMessage); } catch (InvalidOperationException) { } }