2016-05-03 10 views
7

Sto lavorando a un gioco in cui i punteggi vengono sottoposti a classifiche in un'attività e i nuovi punteggi più alti vengono visualizzati con gradi in un frammento. Ho qualcosa (in qualche modo) funzionale, ma il tasso di successo è ~ 10%.Invio di punteggi alle classifiche di Google Play Giochi e visualizzazione di nuove classifiche

Il flusso è la seguente:

handleLeaders Metodo

Questo metodo ottiene i punteggi attuali per ogni classifica, e se il nuovo punteggio è migliore, si è presentata e un nuovo oggetto newHigh viene creato con il punteggio e aggiunto a un ArrayList. Dopo aver gestito tutte e 3 le classifiche, viene chiamato il metodo setHighs. (Errori sono controllati dai rispettivi inviti leaderboard)

public void handleLeaders(boolean win, int size, double t, final int toupees) { 
    if(win) { 
     final long time = (long) t; 
     // Toupees 
     Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, 
       getString(R.string.leaderboard_trumps_toupeed), 
       LeaderboardVariant.TIME_SPAN_ALL_TIME, 
       LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
       new ResultCallback<Leaderboards.LoadPlayerScoreResult>() { 

        @Override 
        public void onResult(Leaderboards.LoadPlayerScoreResult arg0) { 
         LeaderboardScore c = arg0.getScore(); 
         int old; 
         if (c != null) 
          old = (int) c.getRawScore(); 
         else 
          old = 0; 
         Games.Leaderboards.submitScore(mGoogleApiClient, getResources().getString(R.string.leaderboard_trumps_toupeed), old + toupees); 

         GameEndOverlay.newHighs.add(new newHigh("Trumps Toupee'd", old + toupees)); 

         Status status = arg0.getStatus(); 
         int statusCode = status.getStatusCode(); 

         if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) 
          GameEndOverlay.highsError = true; 


         if(++GameEndOverlay.leaderboardsCompleted == 3) 
          ((GameEndOverlay) gameEndOverlayFrag).setHighs(); 

        } 
       }); 

     if (size == getResources().getInteger(R.integer.size_apprentice)) { 

      // Wins 
      Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, 
        getString(R.string.leaderboard_apprentice_wins), 
        LeaderboardVariant.TIME_SPAN_ALL_TIME, 
        LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
        new ResultCallback<Leaderboards.LoadPlayerScoreResult>() { 

         @Override 
         public void onResult(Leaderboards.LoadPlayerScoreResult arg0) { 
          LeaderboardScore c = arg0.getScore(); 
          int wins; 
          if (c != null) 
           wins = (int) c.getRawScore(); 
          else 
           wins = 0; 
          Games.Leaderboards.submitScore(mGoogleApiClient, getResources().getString(R.string.leaderboard_apprentice_wins), wins + 1); 

          GameEndOverlay.newHighs.add(new newHigh("Apprentice Wins", wins + 1)); 

          Status status = arg0.getStatus(); 
          int statusCode = status.getStatusCode(); 

          if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) 
           GameEndOverlay.highsError = true; 


          if(++GameEndOverlay.leaderboardsCompleted == 3) 
           ((GameEndOverlay) gameEndOverlayFrag).setHighs(); 
         } 
        }); 

      // Speed 
      Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, 
        getString(R.string.leaderboard_fastest_apprentice), 
        LeaderboardVariant.TIME_SPAN_ALL_TIME, 
        LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
        new ResultCallback<Leaderboards.LoadPlayerScoreResult>() { 

         @Override 
         public void onResult(Leaderboards.LoadPlayerScoreResult arg0) { 
          LeaderboardScore c = arg0.getScore(); 
          long old_time; 
          if(c != null) { 
           old_time = c.getRawScore(); 
           Log.d("time", old_time + ""); 
           if(time < old_time) { 
            Games.Leaderboards.submitScore(mGoogleApiClient, getResources().getString(R.string.leaderboard_fastest_apprentice), time); 
            GameEndOverlay.newHighs.add(new newHigh("Fastest Apprentice", time)); 
           } 
          } 
          else { 
           Games.Leaderboards.submitScore(mGoogleApiClient, getResources().getString(R.string.leaderboard_fastest_apprentice), time); 
           GameEndOverlay.newHighs.add(new newHigh("Fastest Apprentice", time)); 
          } 

          Status status = arg0.getStatus(); 
          int statusCode = status.getStatusCode(); 

          if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) 
           GameEndOverlay.highsError = true; 


          if(++GameEndOverlay.leaderboardsCompleted == 3) 
           ((GameEndOverlay) gameEndOverlayFrag).setHighs(); 

         } 
        }); 
     } 
} 

Metodo setHighs

Questo metodo ottiene i ranghi di ciascun corrispondente newHigh e memorizza il nuovo rango all'interno dell'oggetto. Dopo che tutti i ranghi sono stati raccolti, viene chiamato il metodo setSecondHighs. (Errori sono controllati dai rispettivi classifica chiamate)

public void setHighs() { 
    if(getActivity() == null) 
     return; 

    ranksComputed = 0; 

    for(newHigh highRaw : newHighs) { 
     final newHigh high = highRaw; 
     switch(high.getName()) { 
      case "Trumps Toupee'd": 
       Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, 
         getString(R.string.leaderboard_trumps_toupeed), 
         LeaderboardVariant.TIME_SPAN_ALL_TIME, 
         LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
         new ResultCallback<Leaderboards.LoadPlayerScoreResult>() { 

          @Override 
          public void onResult(Leaderboards.LoadPlayerScoreResult arg0) { 
           if(arg0.getScore() == null) { 
            highsError = true; 
            ranksComputed++; 
            if(ranksComputed >= newHighs.size()) 
             setSecondHighs(); 
            return; 
           } 
           high.setRank(arg0.getScore().getRank()); 

           Status status = arg0.getStatus(); 
           int statusCode = status.getStatusCode(); 

           if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) 
            GameEndOverlay.highsError = true; 

           ranksComputed++; 
           if(ranksComputed >= newHighs.size()) 
            setSecondHighs(); 
          } 
         }); 
       break; 
      case "Apprentice Wins": 
       Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, 
         getString(R.string.leaderboard_apprentice_wins), 
         LeaderboardVariant.TIME_SPAN_ALL_TIME, 
         LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
         new ResultCallback<Leaderboards.LoadPlayerScoreResult>() { 

          @Override 
          public void onResult(Leaderboards.LoadPlayerScoreResult arg0) { 
           if(arg0.getScore() == null) { 
            highsError = true; 
            ranksComputed++; 
            if(ranksComputed >= newHighs.size()) 
             setSecondHighs(); 
            return; 
           } 
           high.setRank(arg0.getScore().getRank()); 

           Status status = arg0.getStatus(); 
           int statusCode = status.getStatusCode(); 

           if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) 
            GameEndOverlay.highsError = true; 

           ranksComputed++; 
           if(ranksComputed >= newHighs.size()) 
            setSecondHighs(); 
          } 
         }); 
       break; 
      case "Fastest Apprentice": 
       Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, 
         getString(R.string.leaderboard_fastest_apprentice), 
         LeaderboardVariant.TIME_SPAN_ALL_TIME, 
         LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(
         new ResultCallback<Leaderboards.LoadPlayerScoreResult>() { 

          @Override 
          public void onResult(Leaderboards.LoadPlayerScoreResult arg0) { 
           if(arg0.getScore() == null) { 
            highsError = true; 
            ranksComputed++; 
            if(ranksComputed >= newHighs.size()) 
             setSecondHighs(); 
            return; 
           } 
           high.setRank(arg0.getScore().getRank()); 

           Status status = arg0.getStatus(); 
           int statusCode = status.getStatusCode(); 

           if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) 
            GameEndOverlay.highsError = true; 

           ranksComputed++; 
           if(ranksComputed >= newHighs.size()) 
            setSecondHighs(); 
          } 
         }); 
       break; 

     } 
    } 
} 

Metodo setSecondHighs

Questo metodo o visualizza un errore o nuovi ranghi + punteggio all'utente

public void setSecondHighs() { 
    if(highsError) 
    // display an error to the user 
    else 
    // display ranks+score to user 
} 

Il problema è che ci sono un sacco di chiamate API qui, e le presentazioni sono sospese in diversi punti delle chiamate. So che ci deve essere un modo migliore per farlo. Qualsiasi aiuto sarebbe molto apprezzato.

Cheers!

+0

Sembra che tu stia implementando molte cose di livello più basso di quanto non faccia di solito quando lavoro con le classifiche. submitScore() è attivo e si dimentica in base ai documenti. Se hai un problema con l'utilizzo di questo modulo dell'API, dai un'occhiata a submitScoreImmediate(). Ho notato che sotto il cofano, usando submitScore(), GoogleApiClient a volte memorizza nella cache gli invii e li invia come Scores.submitMultiple(). – JimENewtron

+0

il tasso di successo è ~ 10% <- vuoi dire che puoi pubblicare il tuo log.d e dire un po 'di più su come ciò che ti aspetti si confronta con i risultati che osservi? – kpie

+0

Dopo un maggior numero di debug, molte delle chiamate onResult restituiscono un oggetto LoadPlayerScoreResult null indipendentemente dalla mia connessione Internet. Non è neanche una volta. A volte è nullo, a volte no. Questo problema si verifica in genere nel metodo setHighs, ma occasionalmente si verifica il metodo handleLeaders – Brandacus

risposta

0

Ero di fronte allo stesso problema quando provo ad incrementare i punteggi della classifica, Google ha messo un limite al numero di richieste che puoi fare in un periodo di tempo non documentato/non confermato. Di solito passano 3 richieste consecutive per recuperare i dati della classifica e il resto restituisce gli errori relativi alla rete. Maggiori dettagli per gli altri utenti che affrontano lo stesso problema possono essere visualizzati qui: Android - Google play service : Leaderboard, limited number of requests