public void FinishMatch() { // define the match's outcome MatchOutcome outcome = new MatchOutcome(); outcome.SetParticipantResult(mMatch.SelfParticipantId, MatchOutcome.ParticipantResult.Win); if (mMatch.SelfParticipantId == mMatchData.Player1) { outcome.SetParticipantResult(mMatchData.Player2, MatchOutcome.ParticipantResult.Loss); } else { outcome.SetParticipantResult(mMatchData.Player1, MatchOutcome.ParticipantResult.Loss); } mMatchData.PlayerWon = mMatch.SelfParticipantId; PlayGamesPlatform.Instance.TurnBased.Finish(mMatch.MatchId, mMatchData.ToBytes(), outcome, (bool success) => { if (success) { mFinalMessage = "YOU WON!!"; } else { mFinalMessage = "Error winning"; } }); Debug.Log(mFinalMessage); }
private void EndGame(MatchOutcome.ParticipantResult localPlayerResult, MatchOutcome.ParticipantResult opponentResult, int x, int y, Action <TicTacToeGameModel.Mark> successCallback) { if (model == null) { return; } MatchOutcome outcome = new MatchOutcome(); outcome.SetParticipantResult(model.Match.SelfParticipantId, localPlayerResult); outcome.SetParticipantResult(model.Opponent.ParticipantId, opponentResult); model.TransferDatas.IsGameOver = true; model.TransferDatas.FinalResult = outcome; model.TransferDatas.CurrentTurn = GetOppositeMark(model.TransferDatas.CurrentTurn); model.TransferDatas.MoveCount++; CanMove = false; view.StartProgressUI("Ending game"); GameServices.TurnBased.Finish(model.Match, model.TransferDatas.ToByteArray(), outcome, success => { view.StopProgressUI("Ending game: " + GetResultMessage(success)); if (!success) { CanMove = true; model.TransferDatas.CurrentTurn = GetOppositeMark(model.TransferDatas.CurrentTurn); model.TransferDatas.IsGameOver = false; model.TransferDatas.FinalResult = null; model.TransferDatas.MoveCount--; model.TransferDatas.Board[x][y] = TicTacToeGameModel.TileState.Blank; } else { view.EndGame(localPlayerResult); if (successCallback != null) { successCallback(GetOppositeMark(model.TransferDatas.CurrentTurn)); } } }); }
void DoTbmpFinish() { if (mMatch == null) { mStatus = "No match is active."; return; } if (mMatch.TurnStatus != TurnBasedMatch.MatchTurnStatus.MyTurn) { mStatus = "Not my turn."; return; } // I win; every one else loses MatchOutcome outcome = new MatchOutcome(); foreach (Participant p in mMatch.Participants) { if (p.ParticipantId.Equals(mMatch.SelfParticipantId)) { outcome.SetParticipantResult(p.ParticipantId, MatchOutcome.ParticipantResult.Win, 1); } else { outcome.SetParticipantResult(p.ParticipantId, MatchOutcome.ParticipantResult.Loss, 2); } } SetStandBy("Finishing match..."); PlayGamesPlatform.Instance.TurnBased.Finish(mMatch, System.Text.ASCIIEncoding.Default.GetBytes("the end!"), outcome, (bool success) => { EndStandBy(); ShowEffect(success); mStatus = success ? "Successfully finished match." : "Failed to finish match."; if (success) { mMatch = null; mUi = Ui.Tbmp; } }); }
MatchOutcome CreateMatchOutcomeWithResults() { MatchOutcome buffer = new MatchOutcome(); for (int i = 0; i < participantIds.Length; i++) { buffer.SetParticipantResult((string)participantIds.Values[i], (MatchOutcome.ParticipantResult)results.Values[i]); } return(buffer); }
void FinishMatch() { bool winnerIsMe = mMatchData.Winner == mMyMark; // define the match's outcome MatchOutcome outcome = new MatchOutcome(); outcome.SetParticipantResult(mMatch.SelfParticipantId, winnerIsMe ? MatchOutcome.ParticipantResult.Win : MatchOutcome.ParticipantResult.Loss); outcome.SetParticipantResult(GetAdversaryParticipantId(), winnerIsMe ? MatchOutcome.ParticipantResult.Loss : MatchOutcome.ParticipantResult.Win); // finish the match SetStandBy("Sending..."); PlayGamesPlatform.Instance.TurnBased.Finish(mMatch, mMatchData.ToBytes(), outcome, (bool success) => { EndStandBy(); mFinalMessage = success ? (winnerIsMe ? "YOU WON!" : "YOU LOST!") : "ERROR finishing match."; }); }
public void Finish() { if (CurrentMatch == null) { NativeUI.Alert("Error", NullMatchMessage); return; } if (!IsMyTurn) { NativeUI.Alert("Warning", "Not your turn."); return; } if (CurrentMatchData == null) { NativeUI.Alert("Error", "Couldn't find any match data."); return; } MatchOutcome outcome = new MatchOutcome(); outcome.SetParticipantResult(CurrentMatch.SelfParticipantId, MatchOutcome.ParticipantResult.Won); foreach (var id in CurrentOpponents.Select(p => p.ParticipantId)) { outcome.SetParticipantResult(id, MatchOutcome.ParticipantResult.Lost); } CurrentMatchData.WinnerName = CurrentMatch.Self.DisplayName; var callback = GetAlertCallbackAction("Finished the match successfully.", "Failed to finish the match."); callback += success => { if (success) { canTakeTurn = false; } }; GameServices.TurnBased.Finish(CurrentMatch, CurrentMatchData.ToByteArray(), outcome, callback); }
public void OnMatchReceived(TurnBasedMatch match, bool autoLaunch, bool playerWantsToQuit) { if (match == null) { NativeUI.Alert("Error", "Received a null match."); return; } // This only happens on Game Center platform when the local player // removes the match in the matches UI while being the turn holder. // We'll end the local player's turn and pass the match to a next // participant. If there's no active participant left we'll end the match. if (playerWantsToQuit) { if (match.HasVacantSlot) { GameServices.TurnBased.LeaveMatchInTurn(match, string.Empty, null); return; } var nextParticipant = match.Participants.FirstOrDefault( p => p.ParticipantId != match.SelfParticipantId && (p.Status == Participant.ParticipantStatus.Joined || p.Status == Participant.ParticipantStatus.Invited || p.Status == Participant.ParticipantStatus.Matching)); if (nextParticipant != default(Participant)) { GameServices.TurnBased.LeaveMatchInTurn( match, nextParticipant.ParticipantId, null ); } else { // No valid next participant, match ends here. // In this case we'll set the outcome for all players as Tied for demo purpose. // In a real game you may determine the outcome based on the game data and your game logic. MatchOutcome outcome = new MatchOutcome(); foreach (var id in match.Participants.Select(p => p.ParticipantId)) { var result = MatchOutcome.ParticipantResult.Tied; outcome.SetParticipantResult(id, result); } GameServices.TurnBased.Finish(match, match.Data, outcome, null); } return; } if (CurrentMatch != null && CurrentMatch.MatchId != match.MatchId) { var alert = NativeUI.ShowTwoButtonAlert("Received Different Match", "A different match has been arrived, do you want to replace it with the current one?", "Yes", "No"); if (alert != null) { alert.OnComplete += button => { if (button == 0) { CheckAndPlayMatch(match, playerWantsToQuit); } }; return; } CheckAndPlayMatch(match, playerWantsToQuit); return; } CheckAndPlayMatch(match, playerWantsToQuit); }
private void CheckAndPlayMatch(TurnBasedMatch match, bool playerWantsToQuit, int boardSize) { if (playerWantsToQuit) { // This only happens on Game Center platform when the local player // removes the match in the matches UI while being the turn holder. // We'll end the local player's turn and pass the match to a next // participant. If there's no active participant left we'll end the match. if (match.HasVacantSlot) { GameServices.TurnBased.LeaveMatchInTurn(match, "", null); return; } var nextParticipant = match.Participants.FirstOrDefault( p => p.ParticipantId != match.SelfParticipantId && (p.Status == Participant.ParticipantStatus.Joined || p.Status == Participant.ParticipantStatus.Invited || p.Status == Participant.ParticipantStatus.Matching)); if (nextParticipant != default(Participant)) { GameServices.TurnBased.LeaveMatchInTurn( match, nextParticipant.ParticipantId, null ); } else { // No valid next participant, match ends here. // In this case we'll set the outcome for all players as Tied for demo purpose. // In a real game you may determine the outcome based on the game data and your game logic. MatchOutcome outcome = new MatchOutcome(); foreach (var id in match.Participants.Select(p => p.ParticipantId)) { var result = MatchOutcome.ParticipantResult.Tied; outcome.SetParticipantResult(id, result); } GameServices.TurnBased.Finish(match, match.Data, outcome, null); } return; } model = new TicTacToeGameModel(match, boardSize); if (match.Status == TurnBasedMatch.MatchStatus.Ended) { CanMove = false; view.StartProgressUI("Acknowledging finished match"); GameServices.TurnBased.AcknowledgeFinished(match, flag => { view.StopProgressUI("Acknowledging finished match: " + GetResultMessage(flag)); view.CreateBoard(model); view.ShowGameOverUI(model.LocalFinalResult, true); }); return; } var opponent = match.Participants.Where(p => p.ParticipantId != match.SelfParticipantId).FirstOrDefault(); if (opponent != default(Participant) && (opponent.Status == Participant.ParticipantStatus.Done || opponent.Status == Participant.ParticipantStatus.Left)) { view.ShowGameOverUI(MatchOutcome.ParticipantResult.Won, false); NativeUI.Alert("Game Over", "You won. Your opponent has left the match."); return; } CanMove = true; view.CreateBoard(model); }
private void finishMatch(TurnBasedMatch match, int other_score) { MatchOutcome outcome = new MatchOutcome(); var score = PlayerPrefs.GetInt("score"); bool selfWon = false; if (score > other_score) { selfWon = true; } foreach (Participant p in match.Participants) { MatchOutcome.ParticipantResult result; uint placement; if (p.ParticipantId.Equals(match.SelfParticipantId)) { if (selfWon) { result = MatchOutcome.ParticipantResult.Win; placement = 1; } else { result = MatchOutcome.ParticipantResult.Loss; placement = 2; } } else { if (selfWon) { result = MatchOutcome.ParticipantResult.Loss; placement = 2; } else { result = MatchOutcome.ParticipantResult.Win; placement = 1; } } var gm = GameObject.Find("MPController"); GameObject.DontDestroyOnLoad(gm); outcome.SetParticipantResult(p.ParticipantId, result, placement); } string winner; if (selfWon && !(isPlayerTwo)) { winner = "Player One Wins!"; } else { winner = "Player Two Wins!"; } PlayerPrefs.SetString("winner", winner); Debug.Log("In finish, winner ==> " + winner); Byte[] finalData = Encoding.ASCII.GetBytes(winner); PlayGamesPlatform.Instance.TurnBased.Finish(match, finalData, outcome, (bool success) => { if (success) { Debug.Log("Game over!"); StartCoroutine(resetMap()); var myout = outcome.GetResultFor(match.SelfParticipantId); if (myout == MatchOutcome.ParticipantResult.Win) { PlayerPrefs.SetString("winner", "You Won, score==> " + score); } else { PlayerPrefs.SetString("winner", "You Lost, score==> " + score); } Application.LoadLevel("gameover"); } }); }