Beispiel #1
0
  public void onEndGame() {
    if (!GameSettings.isOwner()) {
      if (hasEnded) return;

      hasEnded = true;
    }

    // The host updates the high scores
    if (travelledDistance > GameSettings.getProfile().getHighscore()) {
      dataServer.sendRequest(
          ServerCommand.SET_HIGHSCORE,
          ImmutableMap.of(
              "id", GameSettings.getPlayerId(),
              "token", GameSettings.getPlayerToken(),
              "highscore", String.valueOf(Math.round(travelledDistance))),
          new HttpConnector.Callback() {
            @Override
            public void handleResult(String data) {
              showWinner();
            }
          });
    } else {
      showWinner();
    }
  }
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();
    }
  }
Beispiel #3
0
  @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();
    }
  }