CheckMasterClient
How do I use Check Master Client
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
671 | this.CheckMasterClient(actorID); | 1 |
690 | private void CheckMasterClient(int leavingPlayerId) | 2 |
874 | this.CheckMasterClient(-1); | 3 |
File name: NetworkingPeer.cs
Copy
650 private void HandleEventLeave(int actorID)
651 {
652 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
653 Debug.Log("HandleEventLeave for player ID: " + actorID);
654
655
656 // actorNr is fetched out of event above
657 if (actorID < 0 || !this.mActors.ContainsKey(actorID))
658 {
659 Debug.LogError(String.Format("Received event Leave for unknown player ID: {0}", actorID));
660 return;
661 }
662
663 PhotonPlayer player = this.GetPlayerWithID(actorID);
664 if (player == null)
665 {
666 Debug.LogError("HandleEventLeave for player ID: " + actorID + " has no PhotonPlayer!");
667 }
668
669 // having a new master before calling destroy for the leaving player is important!
670 // so we elect a new masterclient and ignore the leaving player (who is still in playerlists).
671 this.CheckMasterClient(actorID);
672
673
674 // destroy objects & buffered messages
675 if (this.mCurrentGame != null && this.mCurrentGame.autoCleanUp)
676 {
677 this.DestroyPlayerObjects(actorID, true);
678 }
679
680 RemovePlayer(actorID, player);
681
682 // finally, send notification (the playerList and masterclient are now updated)
683 SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerDisconnected, player);
684 }
File name: NetworkingPeer.cs
Copy
690 private void CheckMasterClient(int leavingPlayerId)
691 {
692 bool currentMasterIsLeaving = this.mMasterClient != null && this.mMasterClient.ID == leavingPlayerId;
693 bool someoneIsLeaving = leavingPlayerId > 0;
694
695 // return early if SOME player (leavingId > 0) is leaving AND it's NOT the current master
696 if (someoneIsLeaving && !currentMasterIsLeaving)
697 {
698 return;
699 }
700
701 // picking the player with lowest ID (longest in game).
702 if (this.mActors.Count <= 1)
703 {
704 this.mMasterClient = this.mLocalActor;
705 }
706 else
707 {
708 // keys in mActors are their actorNumbers
709 int lowestActorNumber = Int32.MaxValue;
710 foreach (int key in this.mActors.Keys)
711 {
712 if (key < lowestActorNumber && key != leavingPlayerId)
713 {
714 lowestActorNumber = key;
715 }
716 }
717
718 this.mMasterClient = this.mActors[lowestActorNumber];
719 }
720
721 // make a callback ONLY when a player/Master left
722 if (someoneIsLeaving)
723 {
724 SendMonoMessage(PhotonNetworkingMessage.OnMasterClientSwitched, this.mMasterClient);
725 }
726 }
File name: NetworkingPeer.cs
Copy
822 private void GameEnteredOnGameServer(OperationResponse operationResponse)
823 {
824 if (operationResponse.ReturnCode != 0)
825 {
826 switch (operationResponse.OperationCode)
827 {
828 case OperationCode.CreateGame:
829 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
830 {
831 Debug.Log("Create failed on GameServer. Changing back to MasterServer. Msg: " + operationResponse.DebugMessage);
832 }
833 SendMonoMessage(PhotonNetworkingMessage.OnPhotonCreateRoomFailed, operationResponse.ReturnCode, operationResponse.DebugMessage);
834 break;
835 case OperationCode.JoinGame:
836 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
837 {
838 Debug.Log("Join failed on GameServer. Changing back to MasterServer. Msg: " + operationResponse.DebugMessage);
839 if (operationResponse.ReturnCode == ErrorCode.GameDoesNotExist)
840 {
841 Debug.Log("Most likely the game became empty during the switch to GameServer.");
842 }
843 }
844 SendMonoMessage(PhotonNetworkingMessage.OnPhotonJoinRoomFailed, operationResponse.ReturnCode, operationResponse.DebugMessage);
845 break;
846 case OperationCode.JoinRandomGame:
847 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
848 {
849 Debug.Log("Join failed on GameServer. Changing back to MasterServer. Msg: " + operationResponse.DebugMessage);
850 if (operationResponse.ReturnCode == ErrorCode.GameDoesNotExist)
851 {
852 Debug.Log("Most likely the game became empty during the switch to GameServer.");
853 }
854 }
855 SendMonoMessage(PhotonNetworkingMessage.OnPhotonRandomJoinFailed, operationResponse.ReturnCode, operationResponse.DebugMessage);
856 break;
857 }
858
859 this.DisconnectToReconnect();
860 return;
861 }
862
863 this.State = global::PeerState.Joined;
864 this.mRoomToGetInto.isLocalClientInside = true;
865
866 Hashtable actorProperties = (Hashtable)operationResponse[ParameterCode.PlayerProperties];
867 Hashtable gameProperties = (Hashtable)operationResponse[ParameterCode.GameProperties];
868 this.ReadoutProperties(gameProperties, actorProperties, 0);
869
870 // the local player's actor-properties are not returned in join-result. add this player to the list
871 int localActorNr = (int)operationResponse[ParameterCode.ActorNr];
872
873 this.ChangeLocalID(localActorNr);
874 this.CheckMasterClient(-1);
875
876 if (this.mPlayernameHasToBeUpdated)
877 {
878 this.SendPlayerName();
879 }
880
881 switch (operationResponse.OperationCode)
882 {
883 case OperationCode.CreateGame:
884 SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom);
885 break;
886 case OperationCode.JoinGame:
887 case OperationCode.JoinRandomGame:
888 // the mono message for this is sent at another place
889 break;
890 }
891 }
CheckMasterClient 95 lượt xem
Gõ tìm kiếm nhanh...