Exemplo n.º 1
0
    /// <summary>
    /// Reads incoming messages from the server and reacts accordingly
    /// </summary>
    /// <param name="data">Message that was received from server</param>
    private void OnIncomingData(string data)
    {
        // Splits message into tokens
        string[] tokens = data.Split('~');

        switch (tokens[0])
        {
        case "SPONG":
            receivedPingReply = true;
            break;

        case "SMOV":
            OthelloBoard.Instance.MakeMove(int.Parse(tokens[1]), int.Parse(tokens[2]), OthelloBoard.Instance.isWhiteTurn);
            break;

        case "SADDP":

            GameObject playerListObject = GameObject.Find("PlayerList");

            if (playerListObject != null)
            {
                PlayerList playerList = GameObject.Find("PlayerList").GetComponent <PlayerList>();
                playerList.AddPlayer(tokens[1], PlayerListItem.PlayerState.kReady);
            }

            GameManager.Instance.players.Add(tokens[1], PlayerListItem.PlayerState.kReady);
            break;

        case "SDELP":

            playerListObject = GameObject.Find("PlayerList");


            if (playerListObject != null)
            {
                PlayerList playerList = GameObject.Find("PlayerList").GetComponent <PlayerList>();
                playerList.DeletePlayer(tokens[1]);
            }

            GameManager.Instance.players.Remove(tokens[1]);
            break;

        case "SUPS":
            if (tokens[2] == "READY")
            {
                GameManager.Instance.players[tokens[1]] = PlayerListItem.PlayerState.kReady;
                playerListObject = GameObject.Find("PlayerList");

                if (playerListObject != null)
                {
                    PlayerList playerList = GameObject.Find("PlayerList").GetComponent <PlayerList>();
                    playerList.UpdatePlayer(tokens[1], PlayerListItem.PlayerState.kReady);
                }
            }
            else
            {
                GameManager.Instance.players[tokens[1]] = PlayerListItem.PlayerState.kBusy;

                playerListObject = GameObject.Find("PlayerList");

                if (playerListObject != null)
                {
                    PlayerList playerList = GameObject.Find("PlayerList").GetComponent <PlayerList>();
                    playerList.UpdatePlayer(tokens[1], PlayerListItem.PlayerState.kBusy);
                }
            }
            break;


        case "SREQ":
            Lobby lobby = GameObject.Find("Lobby").GetComponent <Lobby>();
            lobby.OpenPopupWindow(tokens[1]);
            break;

        case "SREQC":
            lobby = GameObject.Find("Lobby").GetComponent <Lobby>();
            lobby.closeWaitForOtherPlayer();
            lobby.closeRequestWindow();
            break;

        case "SRECOV":
            GameManager.Instance.RecoverGame(tokens[1], tokens[2] == "1", tokens[3] == "1");
            break;


        case "SSTARTG":
            GameManager.Instance.StartGame();
            break;

        case "SGAMEC":
            GameManager.Instance.EndGame();
            break;

        case "SGAMEI":
            OthelloBoard board = GameObject.Find("Board").GetComponent <OthelloBoard>();
            board.Alert(tokens[1]);
            break;

        default:
            break;
        }
    }