/** Is called by the host when either the time runs out or when there are no players left */ public void endGame() { if (hasEnded) return; hasEnded = true; // The host stores his own score and initializes all of the other scores to -1 playerScores = new HashMap<>(); for (Integer player : GameSettings.getPlayersInGame()) { if (player == GameSettings.getUserId()) playerScores.put(String.valueOf(player), travelledDistance); else playerScores.put(String.valueOf(player), -1.0); } // Tell the other players that the game has ended gameUpdateHandler.sendEndGame(); // Wait for the final scores of the other players before ending the game new Handler() .postDelayed( new Runnable() { @Override public void run() { winner = processFinalScores(); notifyEndGame(winner); } }, FINAL_SCORE_TIMEOUT * 1000); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lobby); gameToJoin = getIntent().getIntExtra(JOIN_GAME_EXTRA, -1); if (gameToJoin != -1) { // We started from a notification => make sure basic initializations, normally // made in MainActivity are performed here: GameSettings.setProfile(Profile.Load(PreferenceManager.getDefaultSharedPreferences(this))); DrawableManager.InitializeCache(5); } dataServer = new HttpConnector(getString(R.string.dataserver_url)); ListView lobbyList = (ListView) findViewById(R.id.mainList); checkBoxView = (CheckBox) findViewById(R.id.spectatorCheckboxView); lobbyList.setClickable(true); listGames(); if (GameSettings.getLastPlayTime() != -1) { // A game has been played, push new playtime to server dataServer.sendRequest( ServerCommand.SET_PLAYTIME, ImmutableMap.of( "id", String.valueOf(GameSettings.getUserId()), "token", GameSettings.getPlayerToken(), "playtime", String.valueOf( GameSettings.getProfile().getPlaytime() + GameSettings.getLastPlayTime())), new HttpConnector.Callback() { @Override public void handleResult(String data) {} }); GameSettings.resetLastPlaytime(); } }