コード例 #1
0
  public boolean removePlayer(RocketPlayer player) {
    logger.log(Level.INFO, "{0} leaves {1}", new Object[] {player.getName(), this.getName()});

    AppContext.getDataManager().markForUpdate(this);
    Channel tableChannel = tableChannelRef.getForUpdate();
    tableChannel.leave(player.getClientSessionRef().get());

    // The reference created below should be identical to one of the players
    // on our table. I think createReference has some clever logic to
    // not create another reference if one already exists.
    ManagedReference<RocketPlayer> playerRef = AppContext.getDataManager().createReference(player);

    if (playerRef.equals(player1Ref)) {
      player1Ref = null;
    } else if (playerRef.equals(player2Ref)) {
      player2Ref = null;
    } else if (playerRef.equals(player3Ref)) {
      player3Ref = null;
    } else if (playerRef.equals(player4Ref)) {
      player4Ref = null;
    } else {
      logger.log(
          Level.SEVERE, "player " + player.getName() + " doesn't appear to be one of this table!");
      return false;
    }

    setTableAvailable(true);
    maybeChangeStatusOnRemovePlayer();
    return true;
  }
コード例 #2
0
  public boolean addPlayer(RocketPlayer player) {
    ManagedReference<RocketPlayer> playerRef = AppContext.getDataManager().createReference(player);
    AppContext.getDataManager().markForUpdate(this);
    Channel tableChannel =
        tableChannelRef
            .getForUpdate(); // ?? Do I really need to get for update when adding new sessions?

    int playerId = 0;

    if (player1Ref == null) {
      player1Ref = playerRef;
      playerId = 1;
    } else if (player2Ref == null) {
      player2Ref = playerRef;
      playerId = 2;
    } else if (player3Ref == null) {
      player3Ref = playerRef;
      playerId = 3;
    } else if (player4Ref == null) {
      player4Ref = playerRef;
      playerId = 4;
    } else {
      // Theoretically, this should never happen as the tableAvailability
      // won't let players in if the table is full.
      logger.log(
          Level.SEVERE,
          "Not sure how, but a player has tried to join a table with no free spaces!");
      return false;
    }

    tableChannel.join(player.getClientSessionRef().get());
    player.setMyCurrentTable(AppContext.getDataManager().createReference(this));
    logger.log(
        Level.INFO,
        "{0} enters {1} as player " + playerId,
        new Object[] {player.getName(), this.getName()});
    tableChannel.send(
        Utils.encodeString(
            ClientServerMessageInteractor.createTableJoinSuccessMessage(
                player.getName(), playerId)));
    updateTableAvailabilty();
    maybeChangeStatusOnAddPlayer();
    return true;
  }