Пример #1
0
        private void switchTurn()
        {
            m_CurrentPlayer = m_CurrentPlayer == ePiece.White ? ePiece.Black : ePiece.White;

            if (PlayerTurnChanged != null)
            {
                PlayerTurnChanged.Invoke();
            }
        }
Пример #2
0
    private void HandleSocketIoMessage(JArray array)
    {
        switch ((string)array[0])
        {
        case "playerJoined":
        {
            var player = Player.GetPlayer(array[1]["player"]);
            PlayerJoined?.Invoke(player);
            break;
        }

        case "playerLeft":
        {
            var player = Player.GetPlayerById((string)array[1]["user"]);
            PlayerLeft?.Invoke(player);
            break;
        }

        case "gameIsStarting":
        {
            GameIsStarting?.Invoke();
            break;
        }

        case "gameLobbyTimer":
        {
            var remainingSeconds = (int)array[1]["remainingSeconds"];
            GameLobbyTimer?.Invoke(remainingSeconds);
            break;
        }

        case "gameStarted":
        {
            var firstPlayer = Player.GetPlayerById(array[1]["firstPlayer"].ToString());

            GameStarted?.Invoke(firstPlayer);
            break;
        }

        case "playerRolledDice":
        {
            var player = Player.GetPlayerById(array[1]["user"].ToString());
            var dice   = ((JArray)array[1]["dice"]).Select(d => (int)d).ToArray();
            PlayerRolledDice?.Invoke(player, dice);
            break;
        }

        case "playerTurnChanged":
        {
            var player = Player.GetPlayerById(array[1]["user"].ToString());
            PlayerTurnChanged?.Invoke(player);
            break;
        }

        case "playerPlaysAgain":
        {
            var player = Player.GetPlayerById(array[1]["user"].ToString());
            PlayerPlaysAgain?.Invoke(player);
            break;
        }

        case "playerMoved":
        {
            var player   = Player.GetPlayerById(array[1]["user"].ToString());
            var location = (int)array[1]["location"];
            PlayerMoved?.Invoke(player, location);
            break;
        }

        case "playerBalanceChanged":
        {
            var player  = Player.GetPlayerById(array[1]["user"].ToString());
            var balance = (int)array[1]["balance"];
            PlayerBalanceChanged?.Invoke(player, balance);
            break;
        }

        case "playerSteppedOnChance":
        {
            var player = Player.GetPlayerById(array[1]["user"].ToString());
            var text   = array[1]["card"].ToString();
            PlayerSteppedOnChance?.Invoke(player, text);
            break;
        }

        case "playerSteppedOnCommunityChest":
        {
            var player = Player.GetPlayerById(array[1]["user"].ToString());
            var text   = array[1]["card"].ToString();
            PlayerSteppedOnCommunityChest?.Invoke(player, text);
            break;
        }

        case "propertyOwnerChanged":
        {
            var propertyIndex = (int)array[1]["propertyIndex"];
            var ownerId       = array[1]["ownerId"].ToString();
            PropertyOwnerChanged?.Invoke(propertyIndex, ownerId);
            break;
        }
        }
    }
Пример #3
0
    private void Start()
    {
        StartCoroutine(Upload("socket.io/?EIO=3&transport=polling", async(response, error) =>
        {
            if (error != null)
            {
                return;
            }

            string sid = (string)response["sid"];

            _websocket = new WebSocket(APIWrapper.WS_PROTOCOL + APIWrapper.URL + "socket.io/?EIO=3&transport=websocket&sid=" + sid);

            _websocket.OnOpen += () =>
            {
                _closed = false;
                Debug.Log("Connection open!");
                _websocket.SendText("2probe");
            };

            _websocket.OnError += (e) =>
            {
                Debug.Log("Error! " + e);
            };

            _websocket.OnClose += (e) =>
            {
                Debug.Log("Connection closed!");
                if (!_closed)
                {
                    Start();
                }
            };

            _websocket.OnMessage += (bytes) =>
            {
                if (_closed)
                {
                    return;
                }

                try
                {
                    var message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

                    if (message == "3probe")
                    {
                        _websocket.SendText("5");
                        return;
                    }

                    if (message.Length <= 3 || message.Substring(0, 2) != "42")
                    {
                        return;
                    }

                    message   = message.Substring(2);
                    var array = JArray.Parse(message);

                    Debug.Log(array[0]);

                    switch ((string)array[0])
                    {
                    case "playerJoined":
                        {
                            var player = Player.GetPlayer(array[1]["player"]);
                            GetCurrentGame().Players.Add(player);
                            PlayerJoined?.Invoke(player);
                            break;
                        }

                    case "playerLeft":
                        {
                            foreach (var player in GetCurrentGame().Players.ToArray())
                            {
                                if (player.UserId != (string)array[1]["user"])
                                {
                                    continue;
                                }

                                GetCurrentGame().Players.Remove(player);
                                PlayerLeft?.Invoke(player);
                            }
                            break;
                        }

                    case "gameStarted":
                        {
                            var firstPlayer = Player.GetPlayerById(array[1]["firstPlayer"].ToString());
                            GameManager.Instance.Game.UpdateCurrentPlayer(firstPlayer);
                            GameManager.Instance.Game.SetRunning();
                            GameStarted?.Invoke();
                            break;
                        }

                    case "playerRolledDice":
                        {
                            var player = Player.GetPlayerById(array[1]["user"].ToString());
                            var dice   = ((JArray)array[1]["dice"]).Select(d => (int)d).ToArray();

                            PlayerRolledDice?.Invoke(player, dice);
                            break;
                        }

                    case "playerTurnChanged":
                        {
                            var player = Player.GetPlayerById(array[1]["user"].ToString());
                            GameManager.Instance.Game.UpdateCurrentPlayer(player);

                            Debug.Log(player.UserId);

                            PlayerTurnChanged?.Invoke(player);
                            break;
                        }

                    case "playerPlaysAgain":
                        {
                            var player = Player.GetPlayerById(array[1]["user"].ToString());

                            PlayerPlaysAgain?.Invoke(player);
                            break;
                        }

                    case "playerMoved":
                        {
                            var player   = Player.GetPlayerById(array[1]["user"].ToString());
                            var location = (int)array[1]["location"];

                            PlayerMoved?.Invoke(player, location);
                            break;
                        }

                    case "playerBalanceChanged":
                        {
                            var player  = Player.GetPlayerById(array[1]["user"].ToString());
                            var balance = (int)array[1]["balance"];

                            player.Balance = balance;

                            PlayerBalanceChanged?.Invoke(player, balance);
                            break;
                        }

                    case "playerSteppedOnChance":
                        {
                            var player = Player.GetPlayerById(array[1]["user"].ToString());
                            var text   = array[1]["card"].ToString();

                            PlayerSteppedOnChance?.Invoke(player, text);
                            break;
                        }

                    case "playerSteppedOnCommunityChest":
                        {
                            var player = Player.GetPlayerById(array[1]["user"].ToString());
                            var text   = array[1]["card"].ToString();

                            PlayerSteppedOnCommunityChest?.Invoke(player, text);
                            break;
                        }

                    case "propertyOwnerChanged":
                        {
                            var propertyIndex = (int)array[1]["propertyIndex"];
                            var ownerId       = array[1]["ownerId"].ToString();

                            var property     = GameManager.Instance.Game.GetPropertyByIndex(propertyIndex);
                            property.OwnerId = ownerId;

                            PropertyOwnerChanged?.Invoke(propertyIndex, ownerId);
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.Log(e.Message);
                }
            };

            // Keep sending messages every 15s
            InvokeRepeating(nameof(SendWebSocketMessage), 0.0f, 5.0f);

            // Waiting for messages
            await _websocket.Connect();
        }));
    }