Пример #1
0
  @Override
  public void updatePlayers(List<PlayerInfo> players) {
    lock.writeLock().lock();
    try {
      if (players == null) {
        return;
      }
      Iterator<PlayerInfo> iterator = players.iterator();
      while (iterator.hasNext()) {
        Player player = iterator.next();
        if (player.getName() == null) {
          iterator.remove();
        }
      }

      if (this.players.size() != players.size()) {
        throw new IllegalArgumentException("Diff players count");
      }

      for (int index = 0; index < players.size(); index++) {
        Player playerToUpdate = this.players.get(index);
        Player newPlayer = players.get(index);

        playerToUpdate.setCallbackUrl(newPlayer.getCallbackUrl());
        playerToUpdate.setName(newPlayer.getName());
      }
    } finally {
      lock.writeLock().unlock();
    }
  }
Пример #2
0
  @Override
  public void nextStepForAllGames() {
    lock.writeLock().lock();
    try {
      for (Game game : games) {
        if (game.isGameOver()) {
          game.newGame();
        }
        game.tick();
      }

      HashMap<Player, PlayerData> map = new HashMap<Player, PlayerData>();
      for (int i = 0; i < games.size(); i++) {
        Game game = games.get(i);

        Player player = players.get(i);

        map.put(
            player,
            new PlayerData(
                gameType.getBoardSize(),
                decoder.encode(game.getBoardAsString()),
                player.getScore(),
                game.getMaxScore(),
                game.getCurrentScore(),
                player.getCurrentLevel() + 1,
                player.getMessage()));
      }

      screenSender.sendUpdates(map);

      for (int index = 0; index < players.size(); index++) {
        Player player = players.get(index);
        Game game = games.get(index);
        try {
          String board = game.getBoardAsString().replace("\n", "");

          if (logger.isDebugEnabled()) {
            logger.debug(String.format("Sent for player '%s' board \n%s", player, board));
          }

          controllers.get(index).requestControl(player, board);
        } catch (IOException e) {
          logger.error(
              "Unable to send control request to player "
                  + player.getName()
                  + " URL: "
                  + player.getCallbackUrl(),
              e);
        }
      }
    } catch (Error e) {
      e.printStackTrace();
      logger.error("nextStepForAllGames throws", e);
    } finally {
      lock.writeLock().unlock();
    }
  }
Пример #3
0
 @Override
 public Player findPlayerByIp(String ip) {
   lock.readLock().lock();
   try {
     for (Player player : players) {
       if (player.getCallbackUrl().contains(ip)) {
         return player;
       }
     }
     return new NullPlayer();
   } finally {
     lock.readLock().unlock();
   }
 }
Пример #4
0
 @Override
 public void updatePlayer(Player player) {
   lock.writeLock().lock();
   try {
     for (Player playerToUpdate : players) {
       if (playerToUpdate.getName().equals(player.getName())) {
         playerToUpdate.setCallbackUrl(player.getCallbackUrl());
         return;
       }
     }
   } finally {
     lock.writeLock().unlock();
   }
 }