void Update()
    {
        if (PhotonNetwork.connected)
        {
            if (PhotonNetwork.isMasterClient && !wasMasterWhenConnected && onlineSeconds > 10f)
            {
                popups.Show($"You are now the master client! The previous master was disconnected. If you keep the game running, they can probably reconnect and resume normally.", "Continue", null, 800f);
            }
            wasMasterWhenConnected = PhotonNetwork.isMasterClient;
            numOthersWhenConnected = PhotonNetwork.otherPlayers.Length;
            roomNameWhenConnected  = PhotonNetwork.room?.Name;
        }

        if (PhotonUtil.ActuallyConnected())
        {
            onlineSeconds += Time.unscaledDeltaTime;
        }

        // TEMP TEMP
        if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.F2))
        {
            PhotonNetwork.room.IsVisible = !PhotonNetwork.room.IsVisible;
            Util.Log($"toggled room visible to {PhotonNetwork.room.IsVisible}");
        }
    }
 public override void Open()
 {
     Util.Log($"Photon state: {PhotonUtil.GetPhotonStateString()}");
     sortText.text = multiplayerSort.ToString();
     base.Open();
     StartCoroutine(OpenRoutine());
 }
    void Start()
    {
#if USE_PUN
        StartCoroutine(PumpPlayerInitCoroutine());

        var diag = GetComponent <PhotonStatsGui>();
        if (diag != null)
        {
            diag.enabled = false;
        }

        try
        {
            // Don't destroy things created by clients. We will need to clean up the
            // player ghost object only. Even if we're not the master client, we still
            // need this. Because if the master quits, we might become the master, and
            // we need to prevent Photon from destroying all objects!
            if (!PhotonNetwork.inRoom)
            {
                PhotonNetwork.autoCleanUpPlayerObjects = false;
            }
            else
            {
                // Setting this causes an error if we're in a room, but we should make
                // sure it's false still.
                Debug.Assert(PhotonNetwork.autoCleanUpPlayerObjects == false);
            }

            PhotonNetwork.autoJoinLobby = false;
            bool isMultiplayer = GameBuilderApplication.CurrentGameOptions.playOptions.isMultiplayer;

            System.DateTime banLastDate;
            if (isMultiplayer && IsUserBanned(GetSteamID(), out banLastDate))
            {
                popups.Show($"You have been temporarily banned from multiplayer games for reported inappropriate or abusive behavior. You will be able to play again after {banLastDate.ToString("MMMM dd, yyyy")}.", "Back", () =>
                {
                    scenes.LoadSplashScreen();
                }, 800f);
                return;
            }

            if (isMultiplayer)
            {
                Util.Log($"Multiplayer!");
                SetupPlayerProperties();
                mode = Mode.Online;
                PhotonNetwork.offlineMode = false;
            }
            else
            {
            }

            if (PhotonNetwork.connected && PhotonNetwork.inRoom)
            {
                // We are still connected and in a room. This is a map switch.
                mode = Mode.Online;
                Util.Log($"StayOnline mode. Pretending we just joined the room.");
                didSwitchMaps = true;
                OnJoinedRoom();
            }
            else if (PhotonNetwork.connected && PhotonNetwork.insideLobby && isMultiplayer)
            {
                // Joining or creating.

                string roomToJoin = GameBuilderApplication.CurrentGameOptions.joinCode;
                if (!roomToJoin.IsNullOrEmpty())
                {
                    // We're joining a room from the lobby
                    Util.Log($"Trying to join room {roomToJoin}..");
                    // Try to join existing room
                    PhotonNetwork.JoinRoom(roomToJoin.ToLower());
                }
                else
                {
                    // We're creating a new game, and happen to be in a lobby already.
                    TryCreateRoom();
                }
            }
            else
            {
                switch (mode)
                {
                case Mode.Online:
                    string gameVersion = GetPhotonGameVersion();
                    // If we're trying to join a game, make sure we connect to their region.
                    string joinCode = GameBuilderApplication.CurrentGameOptions.joinCode;
                    if (joinCode == "*")
                    {
                        // TEMP join random visible game in best region
                        PhotonNetwork.ConnectToBestCloudServer(gameVersion);
                    }
                    else if (joinCode != null)
                    {
                        string regionCodeStr = joinCode.Split(JoinCodeSeparator)[0];
                        try
                        {
                            CloudRegionCode regionCode = (CloudRegionCode)System.Int32.Parse(regionCodeStr);
                            PhotonNetwork.ConnectToRegion(regionCode, gameVersion);
                        }
                        catch (System.OverflowException)
                        {
                            OnInvalidJoinCodeRegion();
                        }
                    }
                    else
                    {
                        // Ok we're starting a new game, so just connect to the best.
                        PhotonNetwork.ConnectToBestCloudServer(gameVersion);
                    }
                    break;

                case Mode.Offline:
                    if (PhotonUtil.ActuallyConnected())
                    {
                        PhotonNetwork.Disconnect();
                    }
                    DestroyImmediate(GetComponent <ExitGames.UtilityScripts.PlayerRoomIndexing>());
                    Util.Log($"Starting offline mode, t = {Time.realtimeSinceStartup}");
                    PhotonNetwork.offlineMode = true;
                    break;
                }
            }
        }
        catch (System.FormatException e)
        {
            OnFatalError(BadJoinCodeMessage);
        }
        catch (System.Exception e)
        {
            OnFatalError(e.ToString());
        }
#else
        // Non-PUN route
        mode = Mode.Offline;
        StartCoroutine(NonPunStartRoutine());
#endif
    }
 public bool GetIsInMultiplayer()
 {
     return(mode == Mode.Online && PhotonUtil.ActuallyConnected());
 }
 public static bool CanDoMultiplayerMapSwitch()
 {
     return(PhotonUtil.ActuallyConnected() && PhotonNetwork.inRoom && PhotonNetwork.isMasterClient);
 }