Search code examples
c#unity-game-enginegoogle-play-games

Get All TurnBased Matches Info from GooglePlayServices, Unity Plugin


I'm using the TicTacToss code as a basis for a turn based game on android in Unity, using this : https://github.com/playgameservices/play-games-plugin-for-unity

Everything is working great, but there's no functionality exposed to return a list of games the current user is engaged in, active and completed. I need this for the main UI, so I can show the user what games they are playing, against whom etc. Without, this API seems pretty lacking.

Has anyone been able to get this going? Seems to me that the functionality might be there, as there is a function called GetAllTurnbasedMatches(), but my C# isn't strong enough to understand how it works. (see in https://github.com/playgameservices/play-games-plugin-for-unity/blob/master/source/PluginDev/Assets/GooglePlayGames/Platforms/Native/PInvoke/TurnBasedManager.cs)


Solution

  • (more info @ https://github.com/playgameservices/play-games-plugin-for-unity/issues/414)

    I managed to get this working back adding the following to NativeTurnBasedMultiplayerClient.cs:

    public void GetAllTBMatches(Action<TurnBasedMatch, TurnBasedMatch.MatchTurnStatus> callback)
    {
        mTurnBasedManager.GetAllTurnbasedMatches(allMatches =>
        {
            foreach (var match in allMatches.MatchesMyTurn())
            {
                var converted = match.AsTurnBasedMatch(mNativeClient.GetUserId());
                Logger.d("MY turn : Passing converted match to user callback:" + converted);
                callback(converted, TurnBasedMatch.MatchTurnStatus.MyTurn);
            }
            foreach (var match in allMatches.MatchesTheirTurn())
            {
                var converted = match.AsTurnBasedMatch(mNativeClient.GetUserId());
                Logger.d("Their Turn : Passing converted match to user callback:" + converted);
                callback(converted, TurnBasedMatch.MatchTurnStatus.TheirTurn);
            }
            foreach (var match in allMatches.MatchesCompleted())
            {
                var converted = match.AsTurnBasedMatch(mNativeClient.GetUserId());
                Logger.d("Completed : Passing converted match to user callback:" + converted);
                callback(converted, TurnBasedMatch.MatchTurnStatus.Complete);
            }
    
            callback(null, TurnBasedMatch.MatchTurnStatus.Unknown);
        });
    }
    
    public void GetAllTBInvitations(Action<Invitation> callback)
    {
        mTurnBasedManager.GetAllTurnbasedMatches(allMatches =>
        {
            foreach (var invitation in allMatches.Invitations())
            {
                var converted = invitation.AsInvitation();
                Logger.d("Passing converted invitation to user callback:" + converted);
                callback(converted);
            }
    
            callback(null);
        });
    }
    

    You also need to edit the following in TurnBasedManager.cs

    internal class TurnBasedMatchesResponse : BaseReferenceHolder {
        internal TurnBasedMatchesResponse(IntPtr selfPointer) : base(selfPointer) {
        }
    
        protected override void CallDispose(HandleRef selfPointer) {
            C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_Dispose(SelfPtr());
        }
    
        internal CommonErrorStatus.MultiplayerStatus Status() {
            return C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetStatus(SelfPtr());
        }
    
        internal IEnumerable<MultiplayerInvitation> Invitations()
        {
            return PInvokeUtilities.ToEnumerable(
                C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetInvitations_Length(SelfPtr()),
                index => new MultiplayerInvitation(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetInvitations_GetElement(SelfPtr(), index)));
        }
    
        internal IEnumerable<NativeTurnBasedMatch> MatchesMyTurn()
        {
            return PInvokeUtilities.ToEnumerable(
                C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetMyTurnMatches_Length(SelfPtr()),
                index => new NativeTurnBasedMatch(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetMyTurnMatches_GetElement(SelfPtr(), index)));
        }
    
        internal IEnumerable<NativeTurnBasedMatch> MatchesTheirTurn()
        {
            return PInvokeUtilities.ToEnumerable(
                C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetTheirTurnMatches_Length(SelfPtr()),
                index => new NativeTurnBasedMatch(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetTheirTurnMatches_GetElement(SelfPtr(), index)));
        }
    
        internal IEnumerable<NativeTurnBasedMatch> MatchesCompleted()
        {
            return PInvokeUtilities.ToEnumerable(
                C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetCompletedMatches_Length(SelfPtr()),
                index => new NativeTurnBasedMatch(C.TurnBasedMultiplayerManager_TurnBasedMatchesResponse_GetCompletedMatches_GetElement(SelfPtr(), index)));
        }
    
        internal static TurnBasedMatchesResponse FromPointer(IntPtr pointer)
        {
            if (PInvokeUtilities.IsNull(pointer)) {
                return null;
            }
    
            return new TurnBasedMatchesResponse(pointer);
        }
    }