internal override void HandleConnectedSetChanged(NativeRealTimeRoom room) { HashSet <string> newConnectedSet = new HashSet <string>(); foreach (var participant in room.Participants()) { using (participant) { if (participant.IsConnectedToRoom()) { newConnectedSet.Add(participant.Id()); } } } // If the connected set hasn't actually changed, bail out. if (mConnectedParticipants.Equals(newConnectedSet)) { Logger.w("Received connected set callback with unchanged connected set!"); return; } var noLongerConnected = mConnectedParticipants.Except(newConnectedSet); // Check whether a participant that was in the connected set has left it. // If so, we will never reach a fully connected state, and should fail room // creation. if (noLongerConnected.Any()) { Logger.e("Participants disconnected during room setup, failing. " + "Participants were: " + string.Join(",", noLongerConnected.ToArray())); LeaveRoom(); return; } var newlyConnected = newConnectedSet.Except(mConnectedParticipants); Logger.d("New participants connected: " + string.Join(",", newlyConnected.ToArray())); // If we're fully connected, transition to the Active state and signal the client. if (newConnectedSet.Count() == room.ParticipantCount()) { Logger.d("Fully connected! Transitioning to active state."); mSession.EnterState(new ActiveState(room, mSession)); mSession.OnGameThreadListener().RoomConnected(true); return; } // Otherwise, we're not fully there. Increment the progress by the appropriate // amount and inform the client. mPercentComplete += mPercentPerParticipant * (float)newlyConnected.Count(); mConnectedParticipants = newConnectedSet; mSession.OnGameThreadListener().RoomSetupProgress(mPercentComplete); }
internal void UpdateCurrentRoom(NativeRealTimeRoom room) { if (mRoom != null) { mRoom.Dispose(); } mRoom = Misc.CheckNotNull(room); mNativeParticipants = mRoom.Participants().ToDictionary((GooglePlayGames.Native.PInvoke.MultiplayerParticipant p) => p.Id()); mParticipants = (from p in mNativeParticipants.Values select p.AsParticipant()).ToDictionary((Participant p) => p.ParticipantId); }
internal void UpdateCurrentRoom(NativeRealTimeRoom room) { if (mRoom != null) { mRoom.Dispose(); } mRoom = Misc.CheckNotNull(room); mNativeParticipants = mRoom.Participants().ToDictionary(p => p.Id()); mParticipants = mNativeParticipants.Values .Select(p => p.AsParticipant()) .ToDictionary(p => p.ParticipantId); }
internal override void HandleConnectedSetChanged(NativeRealTimeRoom room) { List <string> newlyConnected = new List <string>(); List <string> newlyLeft = new List <string>(); var updatedParticipants = room.Participants().ToDictionary(p => p.Id()); foreach (var participantId in mNativeParticipants.Keys) { var freshParticipant = updatedParticipants[participantId]; var staleParticipant = mNativeParticipants[participantId]; if (staleParticipant.IsConnectedToRoom() && !freshParticipant.IsConnectedToRoom()) { newlyLeft.Add(participantId); } if (!staleParticipant.IsConnectedToRoom() && freshParticipant.IsConnectedToRoom()) { newlyConnected.Add(participantId); } } // Update the cached participants to reflect the new statuses by cleaning up the old // ones and then updating the new values. foreach (var participant in mNativeParticipants.Values) { participant.Dispose(); } mNativeParticipants = updatedParticipants; mParticipants = mNativeParticipants.Values .Select(p => p.AsParticipant()) .ToDictionary(p => p.ParticipantId); Logger.d("Updated participant statuses: " + string.Join(",", mParticipants.Values.Select(p => p.ToString()).ToArray())); // Check whether the current player was disconnected from the room. if (newlyLeft.Contains(GetSelf().ParticipantId)) { Logger.w("Player was disconnected from the multiplayer session."); } // Strip out the participant ID of the local player - this player is not a "peer". var selfId = GetSelf().ParticipantId; newlyConnected = newlyConnected.Where(peerId => !peerId.Equals(selfId)).ToList(); newlyLeft = newlyLeft.Where(peerId => !peerId.Equals(selfId)).ToList(); // Otherwise inform the client about changes in room participants, screening out // results about the local player. if (newlyConnected.Count > 0) { newlyConnected.Sort(); mSession.OnGameThreadListener() .PeersConnected(newlyConnected.Where(peer => !peer.Equals(selfId)).ToArray()); } if (newlyLeft.Count > 0) { newlyLeft.Sort(); mSession.OnGameThreadListener() .PeersDisconnected(newlyLeft.Where(peer => !peer.Equals(selfId)).ToArray()); } }
internal override void HandleConnectedSetChanged(NativeRealTimeRoom room) { List <string> list = new List <string>(); List <string> list2 = new List <string>(); Dictionary <string, GooglePlayGames.Native.PInvoke.MultiplayerParticipant> dictionary = room.Participants().ToDictionary((GooglePlayGames.Native.PInvoke.MultiplayerParticipant p) => p.Id()); foreach (string key in mNativeParticipants.Keys) { GooglePlayGames.Native.PInvoke.MultiplayerParticipant multiplayerParticipant = dictionary[key]; GooglePlayGames.Native.PInvoke.MultiplayerParticipant multiplayerParticipant2 = mNativeParticipants[key]; if (!multiplayerParticipant.IsConnectedToRoom()) { list2.Add(key); } if (!multiplayerParticipant2.IsConnectedToRoom() && multiplayerParticipant.IsConnectedToRoom()) { list.Add(key); } } foreach (GooglePlayGames.Native.PInvoke.MultiplayerParticipant value in mNativeParticipants.Values) { value.Dispose(); } mNativeParticipants = dictionary; mParticipants = (from p in mNativeParticipants.Values select p.AsParticipant()).ToDictionary((Participant p) => p.ParticipantId); Logger.d("Updated participant statuses: " + string.Join(",", (from p in mParticipants.Values select p.ToString()).ToArray())); if (list2.Contains(GetSelf().ParticipantId)) { Logger.w("Player was disconnected from the multiplayer session."); } string selfId = GetSelf().ParticipantId; list = (from peerId in list where !peerId.Equals(selfId) select peerId).ToList(); list2 = (from peerId in list2 where !peerId.Equals(selfId) select peerId).ToList(); if (list.Count > 0) { list.Sort(); mSession.OnGameThreadListener().PeersConnected((from peer in list where !peer.Equals(selfId) select peer).ToArray()); } if (list2.Count > 0) { list2.Sort(); mSession.OnGameThreadListener().PeersDisconnected((from peer in list2 where !peer.Equals(selfId) select peer).ToArray()); } }
internal override void HandleConnectedSetChanged(NativeRealTimeRoom room) { HashSet <string> hashSet = new HashSet <string>(); if ((room.Status() == Types.RealTimeRoomStatus.AUTO_MATCHING || room.Status() == Types.RealTimeRoomStatus.CONNECTING) && mSession.MinPlayersToStart <= room.ParticipantCount()) { mSession.MinPlayersToStart += room.ParticipantCount(); mPercentPerParticipant = 80f / (float)(double)mSession.MinPlayersToStart; } foreach (GooglePlayGames.Native.PInvoke.MultiplayerParticipant item in room.Participants()) { using (item) { if (item.IsConnectedToRoom()) { hashSet.Add(item.Id()); } } } if (mConnectedParticipants.Equals(hashSet)) { Logger.w("Received connected set callback with unchanged connected set!"); } else { IEnumerable <string> source = mConnectedParticipants.Except(hashSet); if (room.Status() == Types.RealTimeRoomStatus.DELETED) { Logger.e("Participants disconnected during room setup, failing. Participants were: " + string.Join(",", source.ToArray())); mSession.OnGameThreadListener().RoomConnected(false); mSession.EnterState(new ShutdownState(mSession)); } else { IEnumerable <string> source2 = hashSet.Except(mConnectedParticipants); Logger.d("New participants connected: " + string.Join(",", source2.ToArray())); if (room.Status() == Types.RealTimeRoomStatus.ACTIVE) { Logger.d("Fully connected! Transitioning to active state."); mSession.EnterState(new ActiveState(room, mSession)); mSession.OnGameThreadListener().RoomConnected(true); } else { mPercentComplete += mPercentPerParticipant * (float)source2.Count(); mConnectedParticipants = hashSet; mSession.OnGameThreadListener().RoomSetupProgress(mPercentComplete); } } } }