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);
if (pv != null)
{
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);
}