Beispiel #1
0
  /** Quit game activity */
  public void quitGame() {
    // Clear data from GameSettings
    GameSettings.setGameId(-1);
    GameSettings.setGameName(null);
    GameSettings.setGameToken(null);
    GameSettings.setCanBreakWall(false);
    GameSettings.setTimeLimit(-1);
    GameSettings.setLastPlayTime((System.currentTimeMillis() - startTime) / 1000);

    if (countdown != null) {
      countdown.removeCallbacks(countdownRunnable);
      countdownRunnable = null;
      countdown = null;
    }

    // Start Lobby activity while destroying RoomActivity and HostActivity
    Intent intent = new Intent(this, LobbyActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

    // Pause the socket
    Log.d("Socket", "quitting socket");
    connection.pause();

    // Close the GameActivity
    finish();
  }
Beispiel #2
0
  private void joinGame(LobbyListItem item) {
    if (!checkBoxView.isChecked() && item.getPlayerCount() >= item.getMaxPlayersAsInteger()) {
      Toast.makeText(this, getString(R.string.too_many_players), Toast.LENGTH_SHORT).show();
      return;
    }

    String gameName = item.getGamename();

    GameSettings.setGameName(gameName);
    GameSettings.setGameId(roomIds.get(gameName));
    GameSettings.setOwnerId(item.getHostId());
    GameSettings.setCanBreakWall(item.getCanBreakWall());
    GameSettings.setTimeLimit(item.getTimeLimit());
    GameSettings.setMaxDistance(item.getMaxDist());
    GameSettings.setMaxPlayers(item.getMaxPlayersAsInteger());
    GameSettings.setSpectate(checkBoxView.isChecked());
    showToast(R.string.joinging_message, gameName);

    if (!GameSettings.getSpectate()) {
      Map<String, String> query =
          ImmutableMap.of(
              "gameId", String.valueOf(roomIds.get(gameName)),
              "id", String.valueOf(GameSettings.getPlayerId()),
              "token", GameSettings.getPlayerToken());

      dataServer.sendRequest(
          ServerCommand.JOIN_GAME,
          query,
          new HttpConnector.Callback() {
            @Override
            public void handleResult(String data) {
              try {
                JSONObject result = new JSONObject(data);
                // TODO check for errors
                showRoomActivity();

              } catch (JSONException e) {
                e.printStackTrace();
              }
            }
          });
    } else {
      showRoomActivity();
    }
  }