コード例 #1
0
ファイル: TableController.java プロジェクト: magefree/mage
 public boolean isMatchTableStillValid() {
   // check only normal match table with state != Finished
   if (!table.isTournament()) {
     int humanPlayers = 0;
     int aiPlayers = 0;
     int validHumanPlayers = 0;
     if (!(table.getState().equals(TableState.WAITING)
         || table.getState().equals(TableState.STARTING)
         || table.getState().equals(TableState.READY_TO_START))) {
       if (match == null) {
         logger.debug("- Match table with no match:");
         logger.debug("-- matchId:" + match.getId() + " [" + match.getName() + "]");
         // return false;
       } else if (match.isDoneSideboarding() && match.getGame() == null) {
         // no sideboarding and not active game -> match seems to hang (maybe the Draw bug)
         logger.debug("- Match with no active game and not in sideboard state:");
         logger.debug("-- matchId:" + match.getId() + " [" + match.getName() + "]");
         // return false;
       }
     }
     // check for active players
     for (Map.Entry<UUID, UUID> userPlayerEntry : userPlayerMap.entrySet()) {
       MatchPlayer matchPlayer = match.getPlayer(userPlayerEntry.getValue());
       if (matchPlayer == null) {
         logger.debug("- Match player not found:");
         logger.debug("-- matchId:" + match.getId());
         logger.debug("-- userId:" + userPlayerEntry.getKey());
         logger.debug("-- playerId:" + userPlayerEntry.getValue());
         continue;
       }
       if (matchPlayer.getPlayer().isHuman()) {
         humanPlayers++;
         if ((table.getState().equals(TableState.WAITING)
                 || table.getState().equals(TableState.STARTING)
                 || table.getState().equals(TableState.READY_TO_START))
             || !match.isDoneSideboarding()
             || (!matchPlayer.hasQuit()
                 && match.getGame() != null
                 && matchPlayer.getPlayer().isInGame())) {
           User user = UserManager.getInstance().getUser(userPlayerEntry.getKey());
           if (user == null) {
             logger.debug("- Active user of match is missing: " + matchPlayer.getName());
             logger.debug("-- matchId:" + match.getId());
             logger.debug("-- userId:" + userPlayerEntry.getKey());
             logger.debug("-- playerId:" + userPlayerEntry.getValue());
             return false;
           }
           // user exits on the server and match player has not quit -> player is valid
           validHumanPlayers++;
         }
       } else {
         aiPlayers++;
       }
     }
     // if at least 2 human players are valid (multiplayer) or all human players are valid the
     // table is valid or it's an AI match
     return validHumanPlayers >= 2 || validHumanPlayers == humanPlayers || aiPlayers > 1;
   }
   return true;
 }
コード例 #2
0
ファイル: TableController.java プロジェクト: magefree/mage
  private void startGame(UUID choosingPlayerId) throws GameException {
    try {
      match.startGame();
      table.initGame();
      GameOptions gameOptions = new GameOptions();
      gameOptions.rollbackTurnsAllowed = match.getOptions().isRollbackTurnsAllowed();
      match.getGame().setGameOptions(gameOptions);
      GameManager.getInstance()
          .createGameSession(
              match.getGame(), userPlayerMap, table.getId(), choosingPlayerId, gameOptions);
      String creator = null;
      StringBuilder opponent = new StringBuilder();
      for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { // no AI players
        if (match.getPlayer(entry.getValue()) != null
            && !match.getPlayer(entry.getValue()).hasQuit()) {
          User user = UserManager.getInstance().getUser(entry.getKey());
          if (user != null) {
            user.ccGameStarted(match.getGame().getId(), entry.getValue());

            if (creator == null) {
              creator = user.getName();
            } else {
              if (opponent.length() > 0) {
                opponent.append(" - ");
              }
              opponent.append(user.getName());
            }
          } else {
            logger.error(
                "Unable to find user: "******"  playerId: " + entry.getValue());
            MatchPlayer matchPlayer = match.getPlayer(entry.getValue());
            if (matchPlayer != null && !matchPlayer.hasQuit()) {
              matchPlayer.setQuit(true);
            }
          }
        }
      }
      // Append AI opponents to the log file
      for (MatchPlayer mPlayer : match.getPlayers()) {
        if (!mPlayer.getPlayer().isHuman()) {
          if (opponent.length() > 0) {
            opponent.append(" - ");
          }
          opponent.append(mPlayer.getName());
        }
      }
      ServerMessagesUtil.getInstance().incGamesStarted();

      // log about game started
      logger.info(
          "GAME started "
              + (match.getGame() != null ? match.getGame().getId() : "no Game")
              + " ["
              + match.getName()
              + "] "
              + creator
              + " - "
              + opponent.toString());
      logger.debug("- matchId: " + match.getId() + " [" + match.getName() + "]");
      if (match.getGame() != null) {
        logger.debug("- chatId:  " + GameManager.getInstance().getChatId(match.getGame().getId()));
      }
    } catch (Exception ex) {
      logger.fatal("Error starting game table: " + table.getId(), ex);
      if (table != null) {
        TableManager.getInstance().removeTable(table.getId());
      }
      if (match != null) {
        Game game = match.getGame();
        if (game != null) {
          GameManager.getInstance().removeGame(game.getId());
        }
      }
    }
  }