public override void OnEnter()
        {
            TurnBasedMatchObject temp = (TurnBasedMatchObject)matchObject.Value;

            TurnBasedMatch match = temp.Match;

            MatchOutcome outcome = null;

            switch (param)
            {
            case ResultParam.ParticipantResult:
                outcome = CreateMatchOutcomeWithResults();
                break;

            case ResultParam.ParticipantPlacement:
                outcome = CreateMatchOutcomeWithPlacements();
                break;

            default:
                outcome = CreateMatchOutcomeWithResultandPlacement();
                break;
            }
            if (outcome != null)
            {
                byte[] binaryData = Convert.FromBase64String(data.Value);
                GameServices.TurnBased.Finish(match, binaryData, outcome, OutComeCallBack);
                return;
            }
        }
        public void RegisterMatchDelegate(MatchDelegate del)
        {
            mRegisteredMatchDelegate = del;

            // Report pending matches if any.
            if (mRegisteredMatchDelegate != null && mPendingMatchDelegates != null && mPendingMatchDelegates.Count > 0)
            {
                RuntimeHelper.RunOnMainThread(() =>
                {
                    while (mPendingMatchDelegates.Count > 0)
                    {
                        var pendingDel = mPendingMatchDelegates.Dequeue();
                        mRegisteredMatchDelegate(TurnBasedMatch.FromGPGSTurnBasedMatch(pendingDel.Match), pendingDel.ShouldAutoLaunch, false);      // playerWantsToQuit doesn't occur on GPG.
                    }

                    mPendingMatchDelegates = null;
                });
            }

            // Register the delegate with GPGS.
            PlayGamesPlatform.Instance.TurnBased.RegisterMatchDelegate((match, flag) =>
            {
                if (del == null)
                {
                    return;
                }

                del(TurnBasedMatch.FromGPGSTurnBasedMatch(match), flag, false);     // playerWantsToQuit doesn't occur on GPG.
            }
                                                                       );
        }
        public string GetTurnbasedMatchDisplayString(TurnBasedMatch match)
        {
            if (match == null)
            {
                return("null\n");
            }

            string result = "[TurnBasedMatch]\n";

            result += "MatchId: " + (match.MatchId ?? "null") + "\n";
            result += "Status: " + match.Status + "\n";
            result += "CurrentParticipantId: " + (match.CurrentParticipantId ?? "null") + "\n";
            result += "SelfParticipantId: " + (match.SelfParticipantId ?? "null") + "\n";
            result += "PlayerCount: " + match.PlayerCount + "\n";
            result += "IsMyTurn: " + match.IsMyTurn + "\n";

            bool hasData = (match.Data != null && match.Data.Length > 0);

            result += "Has Data: " + hasData + "\n";

            if (hasData)
            {
                result += "Data size: " + (match.Data != null ? match.Data.Length : 0) + "\n";
            }

            return(result);
        }
示例#4
0
        void RematchCallback(bool isRematchSuccess, TurnBasedMatch match)
        {
            if (isRematchSuccess)
            {
                TurnBasedMatchObject temp = new TurnBasedMatchObject();
                temp.Match           = match;
                matchObjectOut.Value = temp;

                if (match.Data != null)
                {
                    data.Value = Convert.ToBase64String(match.Data);
                }
                else
                {
                    data.Value = null;
                }


                isSuccess.Value = true;
                Fsm.Event(eventTarget, isSuccessEvent);
            }
            else
            {
                isSuccess.Value = false;
                Fsm.Event(eventTarget, isNotSuccessEvent);
            }
        }
示例#5
0
    void DoTbmpCancel()
    {
        if (mMatch == null)
        {
            mStatus = "No match is active.";
            return;
        }
        if (mMatch.Status != TurnBasedMatch.MatchStatus.Active)
        {
            mStatus = "Match is not active.";
            return;
        }

        SetStandBy("Cancelling match...");
        PlayGamesPlatform.Instance.TurnBased.Cancel(mMatch.MatchId, (bool success) => {
            EndStandBy();
            ShowEffect(success);
            mStatus = success ? "Successfully cancelled match." : "Failed to cancel match.";
            if (success)
            {
                mMatch = null;
                mUi    = Ui.Tbmp;
            }
        });
    }
        private void FindEqualVersionMatchWithParticipant(TurnBasedMatch match, string participantId,
                                                          Action <bool> onFailure, Action <Participant, TurnBasedMatch> onFoundParticipantAndMatch)
        {
            FindEqualVersionMatch(match, (success, foundMatch) =>
            {
                if (!success)
                {
                    onFailure(true);
                }

                // If we received a null participantId, we're using an automatching player instead -
                // issue the callback using that.
                if (participantId == null)
                {
                    onFoundParticipantAndMatch(CreateAutomatchingSentinel(), foundMatch);
                    return;
                }

                Participant participant = foundMatch.GetParticipant(participantId);
                if (participant == null)
                {
                    OurUtils.Logger.e(string.Format("Located match {0} but desired participant with ID " +
                                                    "{1} could not be found", match.MatchId, participantId));
                    onFailure(false);
                    return;
                }

                onFoundParticipantAndMatch(participant, foundMatch);
            });
        }
 public void Dismiss(TurnBasedMatch match)
 {
     FindEqualVersionMatch(match, success => {
         // actually just called on failure
         Logger.e(string.Format("Could not find match {0}", match.MatchId));
     }, mTurnBasedManager.DismissMatch);
 }
        public void Rematch(TurnBasedMatch match, Action <bool, TurnBasedMatch> callback)
        {
            callback = ToOnGameThread(callback);

            FindEqualVersionMatch(match, (success, foundMatch) =>
            {
                if (!success)
                {
                    callback(false, null);
                    return;
                }

                using (var task = mClient.Call <AndroidJavaObject>("rematch", match.MatchId))
                {
                    AndroidTaskUtils.AddOnSuccessListener <AndroidJavaObject>(
                        task,
                        turnBasedMatch =>
                        callback(true, AndroidJavaConverter.ToTurnBasedMatch(turnBasedMatch)));

                    AndroidTaskUtils.AddOnFailureListener(
                        task,
                        e => callback(false, null));
                }
            });
        }
示例#9
0
        public void LeaveMatch(TurnBasedMatch match, Action <bool> callback)
        {
            Util.NullArgumentTest(match);

            // Set the match outcome as Quit.
            var matchOutcome = new GKTBMOutcome()
            {
                outcome = GKTBMOutcome.Outcome.Quit
            };

            // Quitting match out of turn.
            match.GC_TurnBasedMatch.ParticipantQuitOutOfTurn(
                matchOutcome,
                error =>
            {
                if (error != null)
                {
                    Debug.Log("Failed to leave match with error " + error.LocalizedDescription);
                }

                if (callback != null)
                {
                    callback(error == null);
                }
            });
        }
示例#10
0
    void DoTbmpTakeTurn()
    {
        if (mMatch == null)
        {
            mStatus = "No match is active.";
            return;
        }
        if (mMatch.TurnStatus != TurnBasedMatch.MatchTurnStatus.MyTurn)
        {
            mStatus = "Not my turn.";
            return;
        }

        SetStandBy("Taking turn...");
        PlayGamesPlatform.Instance.TurnBased.TakeTurn(mMatch.MatchId,
                                                      System.Text.ASCIIEncoding.Default.GetBytes(GenString()),
                                                      GetNextToPlay(mMatch),
                                                      (bool success) => {
            EndStandBy();
            ShowEffect(success);
            mStatus = success ? "Successfully took turn." : "Failed to take turn.";
            if (success)
            {
                mMatch = null;
                mUi    = Ui.Tbmp;
            }
        });
    }
示例#11
0
 public void Rematch(TurnBasedMatch match, Action <bool, TurnBasedMatch> callback)
 {
     callback = Callbacks.AsOnGameThreadCallback(callback);
     FindEqualVersionMatch(match, failed => callback(false, null), foundMatch => {
         mTurnBasedManager.Rematch(foundMatch, BridgeMatchToUserCallback(callback));
     });
 }
示例#12
0
 public void Cancel(TurnBasedMatch match, Action <bool> callback)
 {
     callback = Callbacks.AsOnGameThreadCallback(callback);
     FindEqualVersionMatch(match, callback, foundMatch => {
         mTurnBasedManager.CancelMatch(foundMatch, status => callback(status > 0));
     });
 }
示例#13
0
        public void GetAllMatches(Action <TurnBasedMatch[]> callback)
        {
            mTurnBasedManager.GetAllTurnbasedMatches(allMatches =>
            {
                int count = allMatches.MyTurnMatchesCount() +
                            allMatches.TheirTurnMatchesCount() +
                            allMatches.CompletedMatchesCount();

                TurnBasedMatch[] matches = new TurnBasedMatch[count];
                int i = 0;
                foreach (var match in allMatches.MyTurnMatches())
                {
                    matches[i++] = match.AsTurnBasedMatch(mNativeClient.GetUserId());
                }
                foreach (var match in allMatches.TheirTurnMatches())
                {
                    matches[i++] = match.AsTurnBasedMatch(mNativeClient.GetUserId());
                }
                foreach (var match in allMatches.CompletedMatches())
                {
                    matches[i++] = match.AsTurnBasedMatch(mNativeClient.GetUserId());
                }
                callback(matches);
            });
        }
示例#14
0
 public override void PlayerWantsToQuitMatch(GKPlayer player, GKTurnBasedMatch match)
 {
     if (MatchDelegate != null)
     {
         MatchDelegate(TurnBasedMatch.FromGKTurnBasedMatch(match), false, true);
     }
 }
示例#15
0
    void DoTbmpRematch()
    {
        if (mMatch == null)
        {
            mStatus = "No match is active.";
            return;
        }
        if (!mMatch.CanRematch)
        {
            mStatus = "Match can't be rematched.";
            return;
        }

        SetStandBy("Rematching match...");
        PlayGamesPlatform.Instance.TurnBased.Rematch(mMatch.MatchId,
                                                     (bool success, TurnBasedMatch match) => {
            EndStandBy();
            ShowEffect(success);
            mMatch  = match;
            mStatus = success ? "Successfully rematched." : "Failed to rematch.";
            if (success)
            {
                // if we succeed, it will be our turn, so go to the appropriate UI
                mUi = Ui.TbmpMatch;
            }
        });
    }
示例#16
0
        public override void OnEnter()
        {
            TurnBasedMatchObject temp = (TurnBasedMatchObject)matchObject.Value;

            TurnBasedMatch match = temp.Match;

            matchId.Value        = match.MatchId;
            playerCount          = match.PlayerCount;
            hasVacantSlot        = match.HasVacantSlot;
            isMyTurn             = match.IsMyTurn;
            currentParticipantId = match.CurrentParticipantId;
            selfParticipantId    = match.SelfParticipantId;


            if (match.PlayerCount > 0)
            {
                participantIds.Resize(match.PlayerCount);

                Participant[] participants = match.Participants;
                for (int i = 0; i < match.PlayerCount; i++)
                {
                    participantIds.Set(i, participants[i].ParticipantId);
                }
            }
            Finish();
        }
        public override void OnEnter()
        {
            DemoUtils_TurnBasedManager manager      = turnBaseManager.Value.GetComponent <DemoUtils_TurnBasedManager>();
            TurnBasedMatch             currentMatch = manager.CurrentMatch;

            if (currentMatch == null)
            {
                alertString.Value = "Please create a match first.";
                Fsm.Event(eventTarget, isNotSuccessEvent);
            }

            if (manager.CurrentMatchData == null)
            {
                alertString.Value = "The current match doesn't have any data to show.";
                Fsm.Event(eventTarget, isNotSuccessEvent);
            }

            var    bytes    = manager.CurrentMatchData.ToByteArray();
            string dataSize = bytes != null ? (bytes.Length.ToString() + " byte(s)") : "Error";

            alertString.Value = string.Format("Turn Count: {0}\nWinner Name: {1}\nSize: {2}",
                                              manager.CurrentMatchData.TurnCount, manager.CurrentMatchData.WinnerName, dataSize);

            matchData.Value = Convert.ToBase64String(bytes);
            Fsm.Event(eventTarget, isSuccessEvent);
        }
示例#18
0
        private void FindEqualVersionMatchWithParticipant(TurnBasedMatch match,
                                                          string participantId, Action <bool> onFailure,
                                                          Action <MultiplayerParticipant, NativeTurnBasedMatch> onFoundParticipantAndMatch)
        {
            FindEqualVersionMatch(match, onFailure, foundMatch =>
            {
                // If we received a null participantId, we're using an automatching player instead -
                // issue the callback using that.
                if (participantId == null)
                {
                    using (var sentinelParticipant = MultiplayerParticipant.AutomatchingSentinel())
                    {
                        onFoundParticipantAndMatch(sentinelParticipant, foundMatch);
                        return;
                    }
                }

                using (var participant = foundMatch.ParticipantWithId(participantId))
                {
                    if (participant == null)
                    {
                        Logger.e(string.Format("Located match {0} but desired participant with ID " +
                                               "{1} could not be found", match.MatchId, participantId));
                        onFailure(false);
                        return;
                    }

                    onFoundParticipantAndMatch(participant, foundMatch);
                }
            });
        }
示例#19
0
 public void TakeTurn(TurnBasedMatch match, byte[] data, string pendingParticipantId,
                      Action <bool> callback)
 {
     Logger.describe(data);
     callback = Callbacks.AsOnGameThreadCallback(callback);
     // Find the indicated match, take the turn if the match is:
     // a) Still present
     // b) Still of a matching version (i.e. there were no updates to it since the passed match
     //    was retrieved)
     // c) The indicated pending participant matches a participant in the match.
     FindEqualVersionMatchWithParticipant(match, pendingParticipantId, callback,
                                          (pendingParticipant, foundMatch) =>
     {
         // If we got here, the match and the participant are in a good state.
         mTurnBasedManager.TakeTurn(foundMatch, data, pendingParticipant,
                                    result =>
         {
             if (result.RequestSucceeded())
             {
                 callback(true);
             }
             else
             {
                 Logger.d("Taking turn failed: " + result.ResponseStatus());
                 callback(false);
             }
         });
     });
 }
示例#20
0
        private void FindEqualVersionMatch(TurnBasedMatch match, Action <bool> onFailure,
                                           Action <NativeTurnBasedMatch> onVersionMatch)
        {
            mTurnBasedManager.GetMatch(match.MatchId, response =>
            {
                using (var foundMatch = response.Match())
                {
                    if (foundMatch == null)
                    {
                        Logger.e(string.Format("Could not find match {0}", match.MatchId));
                        onFailure(false);
                        return;
                    }

                    if (foundMatch.Version() != match.Version)
                    {
                        Logger.e(string.Format("Attempted to update a stale version of the " +
                                               "match. Expected version was {0} but current version is {1}.",
                                               match.Version, foundMatch.Version()));
                        onFailure(false);
                        return;
                    }

                    onVersionMatch(foundMatch);
                }
            });
        }
示例#21
0
 public void Leave(TurnBasedMatch match, Action <bool> callback)
 {
     callback = Callbacks.AsOnGameThreadCallback(callback);
     FindEqualVersionMatch(match, callback, foundMatch => {
         mTurnBasedManager.LeaveMatchDuringTheirTurn(foundMatch, status => callback(status > 0));
     });
 }
示例#22
0
    void DoTbmpLeaveDuringTurn()
    {
        if (mMatch == null)
        {
            mStatus = "No match is active.";
            return;
        }
        if (mMatch.TurnStatus != TurnBasedMatch.MatchTurnStatus.MyTurn)
        {
            mStatus = "It's not my turn.";
            return;
        }

        SetStandBy("Leaving match during turn...");
        PlayGamesPlatform.Instance.TurnBased.LeaveDuringTurn(mMatch.MatchId, GetNextToPlay(mMatch),
                                                             (bool success) => {
            EndStandBy();
            ShowEffect(success);
            mStatus = success ? "Successfully left match during turn." :
                      "Failed to leave match during turn.";
            if (success)
            {
                mMatch = null;
                mUi    = Ui.Tbmp;
            }
        });
    }
示例#23
0
    void DoTbmpAckFinish()
    {
        if (mMatch == null)
        {
            mStatus = "No match is active.";
            return;
        }
        if (mMatch.Status != TurnBasedMatch.MatchStatus.Complete)
        {
            mStatus = "Match is not complete";
            return;
        }

        SetStandBy("Ack'ing finished match");
        PlayGamesPlatform.Instance.TurnBased.AcknowledgeFinished(mMatch.MatchId, (bool success) => {
            EndStandBy();
            ShowEffect(success);
            mStatus = success ? "Successfully ack'ed finish." : "Failed to ack finish.";
            if (success)
            {
                mMatch = null;
                mUi    = Ui.Tbmp;
            }
        });
    }
示例#24
0
    public MatchData(byte[] b, TurnBasedMatch match) : this()
    {
        if (b != null)
        {
            ReadFromBytes(b);
            if (this.Player2 == "")
            {
                if (match.AvailableAutomatchSlots == 0)
                {
                    this.Player2 = match.Participants[1].ParticipantId;
                    Debug.Log("Opponent Updated:" + this.Player2);
                }
            }
        }
        else
        {
            this.Player1 = match.SelfParticipantId;
            Debug.Log("First Time:" + this.Player1);

            this.CurrentPlayer = match.SelfParticipantId;
            if (match.AvailableAutomatchSlots == 0)
            {
                this.Player2 = match.Participants[1].ParticipantId;
                Debug.Log("Opponent:" + this.Player2);
            }
        }
    }
示例#25
0
 public void AcknowledgeFinished(TurnBasedMatch match, Action <bool> callback)
 {
     callback = Callbacks.AsOnGameThreadCallback(callback);
     FindEqualVersionMatch(match, callback, foundMatch => {
         mTurnBasedManager.ConfirmPendingCompletion(
             foundMatch, response => callback(response.RequestSucceeded()));
     });
 }
示例#26
0
        public override void OnEnter()
        {
            TurnBasedMatchObject temp = (TurnBasedMatchObject)matchObject.Value;

            TurnBasedMatch match = temp.Match;

            GameServices.TurnBased.AcknowledgeFinished(match, AcknowledgeFinishedCallback);
        }
示例#27
0
        public override void OnEnter()
        {
            TurnBasedMatchObject temp = (TurnBasedMatchObject)matchObject.Value;

            TurnBasedMatch match = temp.Match;

            GameServices.TurnBased.Rematch(match, RematchCallback);
        }
示例#28
0
 private void Reset()
 {
     mMatch            = null;
     mMatchData        = null;
     mFinalMessage     = null;
     mEndingTurn       = false;
     mEndTurnCountdown = MaxTurnTime;
 }
        private void CheckAndPlayMatch(TurnBasedMatch match, bool playerWantsToQuit)
        {
            if (match.Data != null && match.Data.Length > 0 && MatchData.FromByteArray(match.Data) == null)
            {
                NativeUI.Alert("Error", "The arrived match can't be opened in this scene. You might want to open it in the TicTacToe demo instead.");
                return;
            }

            CurrentMatch     = match;
            CurrentOpponents = CurrentMatch.Participants.Where(p => p.ParticipantId != CurrentMatch.SelfParticipantId).ToArray();
            RefreshParticipantsDropDown();
            canTakeTurn = true;

            if (CurrentMatch.Data == null || CurrentMatch.Data.Length < 1) /// New game detected...
            {
                CurrentMatchData = new MatchData()
                {
                    TurnCount = 0
                }
            }
            ;
            else
            {
                CurrentMatchData = MatchData.FromByteArray(CurrentMatch.Data);
            }

            if (CurrentMatch.Status == TurnBasedMatch.MatchStatus.Ended)
            {
                canTakeTurn = false;
                var result = string.Format("Winner: {0}\nTurnCount: {1}\n\n", CurrentMatchData.WinnerName ?? "null", CurrentMatchData.TurnCount);
                NativeUI.Alert("Finished Match Arrived", result + MatchFinishedMessage + "\n\nMatch info:\n" + GetTurnbasedMatchDisplayString(CurrentMatch));
                return;
            }
            else if (CurrentMatch.Status == TurnBasedMatch.MatchStatus.Cancelled)
            {
                NativeUI.Alert("Cancelled Match Arrived", CancelledMatchMessage);
                return;
            }
            else if (CurrentMatch.Status == TurnBasedMatch.MatchStatus.Deleted)
            {
                NativeUI.Alert("Deleted Match Arrived", DeletedMatchMessage);
                return;
            }
            else if (CurrentMatch.Status == TurnBasedMatch.MatchStatus.Expired)
            {
                NativeUI.Alert("Expired Match Arrived", ExpiredMatchMessage);
                return;
            }

            if (AllOpponensLeft)
            {
                NativeUI.Alert("Game Over", AllOpponentsLeftMessage);
                return;
            }

            CurrentMatchData.TurnCount++;
            NativeUI.Alert("Match Arrived", "New match data has been arrived:\n" + GetTurnbasedMatchDisplayString(match));
        }
示例#30
0
 internal byte[] Data()
 {
     if (!TurnBasedMatch.TurnBasedMatch_HasData(base.SelfPtr()))
     {
         Logger.d("Match has no data.");
         return(null);
     }
     return(PInvokeUtilities.OutParamsToArray <byte>((bytes, size) => TurnBasedMatch.TurnBasedMatch_Data(base.SelfPtr(), bytes, size)));
 }