Inheritance: TypedLobby
Example #1
0
    protected void OnLobbyStatisticsUpdate()
    {
        List <TypedLobbyInfo> lobbyList = PhotonNetwork.LobbyStatistics;
        int totalPlayers = 1; // Add me

        for (int i = 0; i < lobbyList.Count; i++)
        {
            TypedLobbyInfo lobbyInfo = lobbyList[i];
            totalPlayers += lobbyInfo.PlayerCount;
        }

        text.text = totalPlayers.ToString("N0");
    }
Example #2
0
        private void Networking()
        {
            LogFormat("PhotonNetwork.connectionState= {0}", PhotonNetwork.connectionState);
            LogFormat("PhotonNetwork.connecting= {0}", PhotonNetwork.connecting);
            LogFormat("PhotonNetwork.connected= {0}", PhotonNetwork.connected);
            LogFormat("PhotonNetwork.connectedAndReady= {0}", PhotonNetwork.connectedAndReady);
            LogFormat("PhotonNetwork.connectionStateDetailed= {0}", PhotonNetwork.connectionStateDetailed);


            LogFormat("PhotonNetwork.CrcCheckEnabled= {0}", PhotonNetwork.CrcCheckEnabled);
            LogFormat("PhotonNetwork.inRoom= {0}", PhotonNetwork.inRoom);
            LogFormat("PhotonNetwork.isNonMasterClientInRoom= {0}", PhotonNetwork.isNonMasterClientInRoom);

            LogFormat("PhotonNetwork.isMasterClient= {0}", PhotonNetwork.isMasterClient);
            LogFormat("PhotonNetwork.insideLobby= {0}", PhotonNetwork.insideLobby);
            LogFormat("PhotonNetwork.playerName= {0}", PhotonNetwork.playerName);



            LogFormat("");
            if (PhotonNetwork.lobby == null)
            {
                LogFormat("PhotonNetwork.lobby= {0}", PhotonNetwork.lobby);
            }
            else
            {
                LogFormat("lobby.Name= {0}", PhotonNetwork.lobby.Name);
                LogFormat("lobby.Type= {0}", PhotonNetwork.lobby.Type);
            }


            LogFormat("");
            if (PhotonNetwork.LobbyStatistics == null)
            {
                LogFormat("PhotonNetwork.LobbyStatistics= {0}", PhotonNetwork.LobbyStatistics);
            }
            else
            {
                LogFormat("LobbyStatistics.Count= {0}", PhotonNetwork.LobbyStatistics.Count);
                for (int i = 0; i < PhotonNetwork.LobbyStatistics.Count; i++)
                {
                    LogFormat("-----");
                    TypedLobbyInfo item = PhotonNetwork.LobbyStatistics[i];
                    LogFormat("LobbyStatistics[{0}].Name= {1}", i, item.Name);
                    LogFormat("LobbyStatistics[{0}].Name= {1}", i, item.Type);
                    LogFormat("LobbyStatistics[{0}].PlayerCount= {1}", i, item.PlayerCount);
                    LogFormat("LobbyStatistics[{0}].RoomCount= {1}", i, item.RoomCount);
                }
            }


            LogFormat("");
            if (PhotonNetwork.room == null)
            {
                LogFormat("PhotonNetwork.room= {0}", PhotonNetwork.room);
            }
            else
            {
                LogFormat("room.Name= {0}", PhotonNetwork.room.Name);
                LogFormat("room.IsOpen= {0}", PhotonNetwork.room.IsOpen);
                LogFormat("room.IsVisible= {0}", PhotonNetwork.room.IsVisible);
                LogFormat("room.MaxPlayers= {0}", PhotonNetwork.room.MaxPlayers);
                LogFormat("room.PlayerCount= {0}", PhotonNetwork.room.PlayerCount);
                LogFormat("room.IsLocalClientInside= {0}", PhotonNetwork.room.IsLocalClientInside);
                LogFormat("room.MasterClientId= {0}", PhotonNetwork.room.MasterClientId);
                LogFormat("room.AutoCleanUp= {0}", PhotonNetwork.room.AutoCleanUp);
            }


            LogFormat("");
            RoomInfo[] roomList = PhotonNetwork.GetRoomList();
            LogFormat("PhotonNetwork.GetRoomList().Length= {0}", roomList.Length);
            for (int i = 0; i < roomList.Length; i++)
            {
                LogFormat("-----");
                RoomInfo item = roomList[i];

                LogFormat("roomList[{0}] Name= {1}, IsOpen={1}, IsVisible={2}, MaxPlayers={3}, PlayerCount={4}, isLocalClientInside={5}",
                          i,
                          item.Name,
                          item.IsOpen,
                          item.IsVisible,
                          item.MaxPlayers,
                          item.PlayerCount,
                          item.IsLocalClientInside
                          );
            }

            LogFormat("");
            if (PhotonNetwork.player == null)
            {
                LogFormat("PhotonNetwork.player= {0}", PhotonNetwork.player);
            }
            else
            {
                LogFormat("player.ID= {0}", PhotonNetwork.player.ID);
                LogFormat("player.NickName= {0}", PhotonNetwork.player.NickName);
                LogFormat("player.IsLocal= {0}", PhotonNetwork.player.IsLocal);
                LogFormat("player.IsMasterClient= {0}", PhotonNetwork.player.IsMasterClient);
                LogFormat("player.IsInactive= {0}", PhotonNetwork.player.IsInactive);
            }



            LogFormat("");
            LogFormat("PhotonNetwork.playerList.Length= {0}", PhotonNetwork.playerList.Length);
            for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
            {
                LogFormat("-----");
                PhotonPlayer item = PhotonNetwork.playerList[i];
                LogFormat("playerList[{0}].ID= {1}", i, item.ID);
                LogFormat("playerList[{0}].NickName= {1}", i, item.NickName);
                LogFormat("playerList[{0}].IsLocal= {1}", i, item.IsLocal);
                LogFormat("playerList[{0}].IsMasterClient= {1}", i, item.IsMasterClient);
                LogFormat("playerList[{0}].IsInactive= {1}", i, item.IsInactive);
            }
        }
    public void OnEvent(EventData photonEvent)
    {
        if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
            Debug.Log(string.Format("OnEvent: {0}", photonEvent.ToString()));

        int actorNr = -1;
        PhotonPlayer originatingPlayer = null;

        if (photonEvent.Parameters.ContainsKey(ParameterCode.ActorNr))
        {
            actorNr = (int)photonEvent[ParameterCode.ActorNr];
            originatingPlayer = this.GetPlayerWithId(actorNr);

            //else
            //{
            //    // the actor sending this event is not in actorlist. this is usually no problem
            //    if (photonEvent.Code != (byte)LiteOpCode.Join)
            //    {
            //        Debug.LogWarning("Received event, but we do not have this actor:  " + actorNr);
            //    }
            //}
        }

        switch (photonEvent.Code)
        {
            case PunEvent.OwnershipRequest:
            {
                int[] requestValues = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
                int requestedViewId = requestValues[0];
                int currentOwner = requestValues[1];
                Debug.Log("Ev OwnershipRequest: " + photonEvent.Parameters.ToStringFull() + " ViewID: " + requestedViewId + " from: " + currentOwner + " Time: " + Environment.TickCount%1000);

                PhotonView requestedView = PhotonView.Find(requestedViewId);
                if (requestedView == null)
                {
                    Debug.LogWarning("Can't find PhotonView of incoming OwnershipRequest. ViewId not found: " + requestedViewId);
                    break;
                }

                Debug.Log("Ev OwnershipRequest PhotonView.ownershipTransfer: " + requestedView.ownershipTransfer + " .ownerId: " + requestedView.ownerId + " isOwnerActive: " + requestedView.isOwnerActive + ". This client's player: " + PhotonNetwork.player.ToStringFull());

                switch (requestedView.ownershipTransfer)
                {
                    case OwnershipOption.Fixed:
                        Debug.LogWarning("Ownership mode == fixed. Ignoring request.");
                        break;
                    case OwnershipOption.Takeover:
                        if (currentOwner == requestedView.ownerId)
                        {
                            // a takeover is successful automatically, if taken from current owner
                            requestedView.ownerId = actorNr;
                        }
                        break;
                    case OwnershipOption.Request:
                        if (currentOwner == PhotonNetwork.player.ID || PhotonNetwork.player.isMasterClient)
                        {
                            if ((requestedView.ownerId == PhotonNetwork.player.ID) || (PhotonNetwork.player.isMasterClient && !requestedView.isOwnerActive))
                            {
                                SendMonoMessage(PhotonNetworkingMessage.OnOwnershipRequest, new object[] {requestedView, originatingPlayer});
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
                break;

            case PunEvent.OwnershipTransfer:
                {
                    int[] transferViewToUserID = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
                    Debug.Log("Ev OwnershipTransfer. ViewID " + transferViewToUserID[0] + " to: " + transferViewToUserID[1] + " Time: " + Environment.TickCount%1000);

                    int requestedViewId = transferViewToUserID[0];
                    int newOwnerId = transferViewToUserID[1];

                    PhotonView pv = PhotonView.Find(requestedViewId);
                    pv.ownerId = newOwnerId;

                    break;
                }
            case EventCode.GameList:
                {
                    this.mGameList = new Dictionary<string, RoomInfo>();
                    Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
                    foreach (DictionaryEntry game in games)
                    {
                        string gameName = (string)game.Key;
                        this.mGameList[gameName] = new RoomInfo(gameName, (Hashtable)game.Value);
                    }
                    mGameListCopy = new RoomInfo[mGameList.Count];
                    mGameList.Values.CopyTo(mGameListCopy, 0);
                    SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
                    break;
                }

            case EventCode.GameListUpdate:
                {
                    Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
                    foreach (DictionaryEntry room in games)
                    {
                        string gameName = (string)room.Key;
                        RoomInfo game = new RoomInfo(gameName, (Hashtable)room.Value);
                        if (game.removedFromList)
                        {
                            this.mGameList.Remove(gameName);
                        }
                        else
                        {
                            this.mGameList[gameName] = game;
                        }
                    }
                    this.mGameListCopy = new RoomInfo[this.mGameList.Count];
                    this.mGameList.Values.CopyTo(this.mGameListCopy, 0);
                    SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
                    break;
                }

            case EventCode.QueueState:
                // not used anymore
                break;

            case EventCode.AppStats:
                // Debug.LogInfo("Received stats!");
                this.mPlayersInRoomsCount = (int)photonEvent[ParameterCode.PeerCount];
                this.mPlayersOnMasterCount = (int)photonEvent[ParameterCode.MasterPeerCount];
                this.mGameCount = (int)photonEvent[ParameterCode.GameCount];
                break;

            case EventCode.Join:
                // actorNr is fetched out of event above
                Hashtable actorProperties = (Hashtable)photonEvent[ParameterCode.PlayerProperties];
                if (originatingPlayer == null)
                {
                    bool isLocal = this.mLocalActor.ID == actorNr;
                    this.AddNewPlayer(actorNr, new PhotonPlayer(isLocal, actorNr, actorProperties));
                    this.ResetPhotonViewsOnSerialize(); // This sets the correct OnSerializeState for Reliable OnSerialize
                }
                else
                {
                    originatingPlayer.InternalCacheProperties(actorProperties);
                    //originatingPlayer.IsInactive = false;
                }

                if (actorNr == this.mLocalActor.ID)
                {
                    // in this player's 'own' join event, we get a complete list of players in the room, so check if we know all players
                    int[] actorsInRoom = (int[])photonEvent[ParameterCode.ActorList];
                    this.UpdatedActorList(actorsInRoom);

                    // joinWithCreateOnDemand can turn an OpJoin into creating the room. Then actorNumber is 1 and callback: OnCreatedRoom()
                    if (this.mLastJoinType == JoinType.JoinOrCreateOnDemand && this.mLocalActor.ID == 1)
                    {
                        SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom);
                    }
                    SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom); //Always send OnJoinedRoom

                }
                else
                {
                    SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerConnected, this.mActors[actorNr]);
                }
                break;

            case EventCode.Leave:
                this.HandleEventLeave(actorNr, photonEvent);
                break;

            case EventCode.PropertiesChanged:
                int targetActorNr = (int)photonEvent[ParameterCode.TargetActorNr];
                Hashtable gameProperties = null;
                Hashtable actorProps = null;
                if (targetActorNr == 0)
                {
                    gameProperties = (Hashtable)photonEvent[ParameterCode.Properties];
                }
                else
                {
                    actorProps = (Hashtable)photonEvent[ParameterCode.Properties];
                }

                this.ReadoutProperties(gameProperties, actorProps, targetActorNr);
                break;

            case PunEvent.RPC:
                //ts: each event now contains a single RPC. execute this
                // Debug.Log("Ev RPC from: " + originatingPlayer);
                this.ExecuteRpc(photonEvent[ParameterCode.Data] as Hashtable, originatingPlayer);
                break;

            case PunEvent.SendSerialize:
            case PunEvent.SendSerializeReliable:
                Hashtable serializeData = (Hashtable)photonEvent[ParameterCode.Data];
                //Debug.Log(serializeData.ToStringFull());

                int remoteUpdateServerTimestamp = (int)serializeData[(byte)0];
                short remoteLevelPrefix = -1;
                short initialDataIndex = 1;
                if (serializeData.ContainsKey((byte)1))
                {
                    remoteLevelPrefix = (short)serializeData[(byte)1];
                    initialDataIndex = 2;
                }

                for (short s = initialDataIndex; s < serializeData.Count; s++)
                {
                    this.OnSerializeRead(serializeData[s] as Hashtable, originatingPlayer, remoteUpdateServerTimestamp, remoteLevelPrefix);
                }
                break;

            case PunEvent.Instantiation:
                this.DoInstantiate((Hashtable)photonEvent[ParameterCode.Data], originatingPlayer, null);
                break;

            case PunEvent.CloseConnection:
                // MasterClient "requests" a disconnection from us
                if (originatingPlayer == null || !originatingPlayer.isMasterClient)
                {
                    Debug.LogError("Error: Someone else(" + originatingPlayer + ") then the masterserver requests a disconnect!");
                }
                else
                {
                    PhotonNetwork.LeaveRoom();
                }

                break;

            case PunEvent.DestroyPlayer:
                Hashtable evData = (Hashtable)photonEvent[ParameterCode.Data];
                int targetPlayerId = (int)evData[(byte)0];
                if (targetPlayerId >= 0)
                {
                    this.DestroyPlayerObjects(targetPlayerId, true);
                }
                else
                {
                    if (this.DebugOut >= DebugLevel.INFO) Debug.Log("Ev DestroyAll! By PlayerId: " + actorNr);
                    this.DestroyAll(true);
                }
                break;

            case PunEvent.Destroy:
                evData = (Hashtable)photonEvent[ParameterCode.Data];
                int instantiationId = (int)evData[(byte)0];
                // Debug.Log("Ev Destroy for viewId: " + instantiationId + " sent by owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == actorNr) + " this client is owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == this.mLocalActor.ID));

                PhotonView pvToDestroy = null;
                if (this.photonViewList.TryGetValue(instantiationId, out pvToDestroy))
                {
                    this.RemoveInstantiatedGO(pvToDestroy.gameObject, true);
                }
                else
                {
                    if (this.DebugOut >= DebugLevel.ERROR) Debug.LogError("Ev Destroy Failed. Could not find PhotonView with instantiationId " + instantiationId + ". Sent by actorNr: " + actorNr);
                }

                break;

            case PunEvent.AssignMaster:
                evData = (Hashtable)photonEvent[ParameterCode.Data];
                int newMaster = (int)evData[(byte)1];
                this.SetMasterClient(newMaster, false);
                break;

            case EventCode.LobbyStats:
                //Debug.Log("LobbyStats EV: " + photonEvent.ToStringFull());

                string[] names = photonEvent[ParameterCode.LobbyName] as string[];
                byte[] types = photonEvent[ParameterCode.LobbyType] as byte[];
                int[] peers = photonEvent[ParameterCode.PeerCount] as int[];
                int[] rooms = photonEvent[ParameterCode.GameCount] as int[];

                this.LobbyStatistics.Clear();
                for (int i = 0; i < names.Length; i++)
                {
                    TypedLobbyInfo info = new TypedLobbyInfo();
                    info.Name = names[i];
                    info.Type = (LobbyType)types[i];
                    info.PlayerCount = peers[i];
                    info.RoomCount = rooms[i];

                    this.LobbyStatistics.Add(info);
                }

                SendMonoMessage(PhotonNetworkingMessage.OnLobbyStatisticsUpdate);
                break;

            default:
                if (photonEvent.Code < 200)
                {
                    if (PhotonNetwork.OnEventCall != null)
                    {
                        object content = photonEvent[ParameterCode.Data];
                        PhotonNetwork.OnEventCall(photonEvent.Code, content, actorNr);
                    }
                    else
                    {
                        Debug.LogWarning("Warning: Unhandled event " + photonEvent + ". Set PhotonNetwork.OnEventCall.");
                    }
                }
                break;
        }

        //this.externalListener.OnEvent(photonEvent);
    }