Search code examples
androidgoogle-play-games

Cannot close turnbased multiplayer match with Google Play Games


I have a problem closing my game after the last player close the last match. My turn scheme is:

  1. Player A
  2. Player B
  3. Player B
  4. Player A
  5. Player A
  6. Player B

The game works well but in turn "6" when player B try to close the match, player A always see the matsh as "my turn" and not as "completed"

here there is the code thar rules turns and game ended:

@Override
    public void onGameEnd(final NMXGameData updatedData) {
        super.onGameEnd(updatedData);

        if (updatedData.getMatchNumber() == NMXGameConfig.MATCHES) {
            boolean iWin = updatedData.getResultPoints()[1] > updatedData.getResultPoints()[0];
            boolean tile = updatedData.getResultPoints()[1] == updatedData.getResultPoints()[0];

            ParticipantResult opponentParticipantResult;
            ParticipantResult myParticipantResult;

            if (tile) {
                opponentParticipantResult = new ParticipantResult(getOpponentId(), ParticipantResult.MATCH_RESULT_TIE, 1);
                myParticipantResult = new ParticipantResult(getCurrentPlayerId(), ParticipantResult.MATCH_RESULT_TIE, 1);
            } else {
                if (iWin) {
                    opponentParticipantResult = new ParticipantResult(getOpponentId(), ParticipantResult.MATCH_RESULT_LOSS, 2);
                    myParticipantResult = new ParticipantResult(getCurrentPlayerId(), ParticipantResult.MATCH_RESULT_WIN, 1);
                } else {
                    opponentParticipantResult = new ParticipantResult(getOpponentId(), ParticipantResult.MATCH_RESULT_WIN, 1);
                    myParticipantResult = new ParticipantResult(getCurrentPlayerId(), ParticipantResult.MATCH_RESULT_LOSS, 2);
                }
            }

            ArrayList<ParticipantResult> participantResultArrayList = new ArrayList<>();
            participantResultArrayList.add(opponentParticipantResult);
            participantResultArrayList.add(myParticipantResult);

            Games.TurnBasedMultiplayer.finishMatch(getApiClient(), match.getMatchId(), new Gson().toJson(updatedData).getBytes(), opponentParticipantResult, myParticipantResult).setResultCallback(new ResultCallback<TurnBasedMultiplayer.UpdateMatchResult>() {
                @Override
                public void onResult(TurnBasedMultiplayer.UpdateMatchResult updateMatchResult) {
                    finish();
                }
            });

        } else if (updatedData.getMatchNumber() < NMXGameConfig.MATCHES) {

            if (getNextPlayerIndex(updatedData.getMatchNumber()) != getNextPlayerIndex(updatedData.getMatchNumber() - 1)) {
                Games.TurnBasedMultiplayer.takeTurn(getApiClient(), match.getMatchId(), new Gson().toJson(updatedData).getBytes(), getNextParticipantId());
            } else {
                Games.TurnBasedMultiplayer.takeTurn(getApiClient(), match.getMatchId(), new Gson().toJson(updatedData).getBytes(), getCurrentPlayerId());
                startActivity(startNewOnlineGameIntent(this, updatedData, match.getMatchId()));
            }
            finish();
        }


    }

    private String getCurrentPlayerId() {
        return match.getParticipantId(Games.Players.getCurrentPlayerId(getApiClient()));

    }

    private String getOpponentId() {
        for (String id : match.getParticipantIds()) {
            if (!id.equals(getCurrentPlayerId())) {
                return id;
            }
        }
        return null;
    }

    private int getNextPlayerIndex(int nextRoundIndex) {
        nextRoundIndex = nextRoundIndex + 1;
        return (nextRoundIndex / 2) % 2;
    }

Solution

  • I finally figured it out.

    I don't know if that is the desired behavior but when in round 6 player_B calls:

    Games.TurnBasedMultiplayer.finishMatch(getApiClient(), match.getMatchId(), new Gson().toJson(updatedData).getBytes(), opponentParticipantResult, myParticipantResult).setResultCallback(new ResultCallback<TurnBasedMultiplayer.UpdateMatchResult>() {
                    @Override
                    public void onResult(TurnBasedMultiplayer.UpdateMatchResult updateMatchResult) {
                        finish();
                    }
                });
    

    The turn goes to player_A that see that match as "my turn". At this point player A must call Games.TurnBasedMultiplayer.finishMatch(getApiClient(), match.getMatchId()) (without playing a real game) and the game is completed for both players