Пример #1
0
        private static void connectionHandler_OnMessageReceived(object source, string message)
        {
            var clientConnectionHandler = source as ClientConnectionHandler;
            var responseMessage         = _messageConverter.ConvertToResponseMessage(message);

            switch (responseMessage.Method)
            {
            case "player/added":
            {
                if (responseMessage.Status == MessageStatus.Failure)
                {
                    Console.WriteLine(responseMessage.Text);
                    if (responseMessage.InnerMethod.Equals("samechar"))
                    {
                        var player = responseMessage.Data.Deserialize <Player>();
                        JsonConvert.DeserializeObject <Player>(responseMessage.Data);
                        var requestMessage = new RequestMessage
                        {
                            Data = JsonConvert.SerializeObject(new Player
                                {
                                    Name = player.Name,
                                    Mark = player.Mark.Equals('X') ? 'O' : 'X'
                                }),
                            Method = "player/add"
                        };
                        clientConnectionHandler.Send(_messageConverter.ConvertToJson(requestMessage));
                    }
                }
                if (responseMessage.Status == MessageStatus.Success)
                {
                    if (!string.IsNullOrEmpty(responseMessage.Data))
                    {
                        _currentPlayer = responseMessage.Data.Deserialize <Player>();
                        Console.WriteLine("* Connected successfully *");
                    }
                    else
                    {
                        Console.WriteLine(responseMessage.Text);
                    }
                }
                break;
            }

            case "game/started":
            {
                if (responseMessage.Status == MessageStatus.Success)
                {
                    Console.Clear();
                    _currentGame = responseMessage.Data.Deserialize <Game>();
                    BuildMap(_currentGame.Map);
                    Console.WriteLine("\r\n* Game has been started *");
                    if (_currentPlayer.Mark.Equals('X'))
                    {
                        Console.WriteLine("* You start *");
                    }
                    else
                    {
                        Console.WriteLine("* Your enemy starts *");
                    }
                }
                else
                {
                    Console.WriteLine(responseMessage.Text);
                }
                break;
            }

            case "game/moved":
            {
                if (responseMessage.Status == MessageStatus.Success)
                {
                    _currentGame = responseMessage.Data.Deserialize <Game>();
                    Console.Clear();
                    if (_currentGame.Finished)
                    {
                        WhoWon();
                    }
                    else
                    {
                        BuildMap(_currentGame.Map);
                        IsYourTurn();
                    }
                }
                else
                {
                    Console.WriteLine(responseMessage.Text);
                }
                break;
            }

            default:
            {
                Console.WriteLine("* Something bad happened *");
                break;
            }
            }
        }