Exemple #1
0
 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;
 }
Exemple #2
0
 private void updateDeck(UUID userId, UUID playerId, Deck deck) {
   if (table.isTournament()) {
     if (tournament != null) {
       TournamentManager.getInstance().updateDeck(tournament.getId(), playerId, deck);
     } else {
       logger.fatal("Tournament == null  table: " + table.getId() + " userId: " + userId);
     }
   } else if (TableState.SIDEBOARDING.equals(table.getState())) {
     match.updateDeck(playerId, deck);
   } else {
     // deck was meanwhile submitted so the autoupdate can be ignored
   }
 }
Exemple #3
0
 public boolean watchTable(UUID userId) {
   if (table.isTournament()) {
     UserManager.getInstance().getUser(userId).ccShowTournament(table.getTournament().getId());
     return true;
   } else {
     if (table.isTournamentSubTable() && !table.getTournament().getOptions().isWatchingAllowed()) {
       return false;
     }
     if (table.getState() != TableState.DUELING) {
       return false;
     }
     // you can't watch your own game
     if (userPlayerMap.get(userId) != null) {
       return false;
     }
     return UserManager.getInstance().getUser(userId).ccWatchGame(match.getGame().getId());
   }
 }
Exemple #4
0
 private void checkExpired() {
   debugServerState();
   Date now = new Date();
   List<UUID> toRemove = new ArrayList<>();
   for (Table table : tables.values()) {
     if (!table.getState().equals(TableState.FINISHED)) {
       // remove all not finished tables created more than expire_time ago
       long diff = (now.getTime() - table.getCreateTime().getTime()) / EXPIRE_TIME_UNIT_VALUE;
       if (diff >= EXPIRE_TIME) {
         logger.warn(
             "Table expired: id = "
                 + table.getId()
                 + ", created_by="
                 + table.getControllerName()
                 + ". Removing...");
         toRemove.add(table.getId());
       }
       // remove tables not valid anymore
       else if (!table.isTournament()) {
         TableController tableController = getController(table.getId());
         if (!tableController.isMatchTableStillValid()) {
           logger.warn(
               "Table with no active human player: id = "
                   + table.getId()
                   + ", created_by="
                   + table.getControllerName()
                   + ". Removing...");
           toRemove.add(table.getId());
         }
       }
     }
   }
   for (UUID tableId : toRemove) {
     try {
       removeTable(tableId);
     } catch (Exception e) {
       logger.error(e);
     }
   }
 }
Exemple #5
0
 private void matchEnd() {
   if (match != null) {
     for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
       MatchPlayer matchPlayer = match.getPlayer(entry.getValue());
       // opponent(s) left during sideboarding
       if (matchPlayer != null) {
         if (!matchPlayer.hasQuit()) {
           User user = UserManager.getInstance().getUser(entry.getKey());
           if (user != null) {
             if (table.getState().equals(TableState.SIDEBOARDING)) {
               StringBuilder sb = new StringBuilder();
               if (table.isTournamentSubTable()) {
                 sb.append("Your tournament match of round ");
                 sb.append(table.getTournament().getRounds().size());
                 sb.append(" is over. ");
               } else {
                 sb.append("Match [").append(match.getName()).append("] is over. ");
               }
               if (match.getPlayers().size() > 2) {
                 sb.append("All your opponents have lost or quit the match.");
               } else {
                 sb.append("Your opponent has quit the match.");
               }
               user.showUserMessage("Match info", sb.toString());
             }
             // remove table from user - table manager holds table for display of finished matches
             if (!table.isTournamentSubTable()) {
               user.removeTable(entry.getValue());
             }
           }
         }
       }
     }
     // free resources no longer needed
     match.cleanUpOnMatchEnd(
         ConfigSettings.getInstance().isSaveGameActivated(), table.isTournament());
   }
 }
Exemple #6
0
 public synchronized boolean submitDeck(UUID userId, DeckCardLists deckList) throws MageException {
   UUID playerId = userPlayerMap.get(userId);
   if (table.isTournament()) {
     TournamentPlayer player = tournament.getPlayer(playerId);
     if (player == null || player.hasQuit()) {
       return true; // so the construct panel closes after submit
     }
   } else if (table.getMatch() != null) {
     MatchPlayer mPlayer = table.getMatch().getPlayer(playerId);
     if (mPlayer == null || mPlayer.hasQuit()) {
       return true; // so the construct panel closes after submit
     }
     if (table.isTournamentSubTable()) {
       TournamentPlayer tournamentPlayer =
           table.getTournament().getPlayer(mPlayer.getPlayer().getId());
       if (tournamentPlayer != null) {
         tournamentPlayer.setStateInfo(""); // reset sideboarding state
       }
     }
   }
   if (table.getState() != TableState.SIDEBOARDING
       && table.getState() != TableState.CONSTRUCTING) {
     return false;
   }
   Deck deck = Deck.load(deckList, false, false);
   if (table.getState() == TableState.SIDEBOARDING && table.getMatch() != null) {
     MatchPlayer mPlayer = table.getMatch().getPlayer(playerId);
     if (mPlayer != null) {
       deck.setName(mPlayer.getDeck().getName());
     }
   }
   if (!Main.isTestMode() && !table.getValidator().validate(deck)) {
     throw new InvalidDeckException(
         "Invalid deck for this format", table.getValidator().getInvalid());
   }
   submitDeck(userId, playerId, deck);
   return true;
 }
Exemple #7
0
 public synchronized void leaveTable(UUID userId) {
   if (table == null) {
     logger.error("No table object - userId: " + userId);
     return;
   }
   if (table.isTournament() && tournament == null) {
     logger.error("No tournament object - userId: " + userId + "  table: " + table.getId());
     return;
   }
   if (table != null
       && this.userId != null
       && this.userId.equals(userId) // tourn. sub tables have no creator user
       && (table.getState().equals(TableState.WAITING)
           || table.getState().equals(TableState.READY_TO_START))) {
     // table not started yet and user is the owner, remove the table
     TableManager.getInstance().removeTable(table.getId());
   } else {
     UUID playerId = userPlayerMap.get(userId);
     if (playerId != null) {
       if (table.getState() == TableState.WAITING
           || table.getState() == TableState.READY_TO_START) {
         table.leaveNotStartedTable(playerId);
         if (table.isTournament()) {
           tournament.removePlayer(playerId);
         } else {
           match.quitMatch(playerId);
         }
         User user = UserManager.getInstance().getUser(userId);
         if (user != null) {
           ChatManager.getInstance()
               .broadcast(
                   chatId,
                   user.getName(),
                   "has left the table",
                   ChatMessage.MessageColor.BLUE,
                   true,
                   ChatMessage.MessageType.STATUS,
                   ChatMessage.SoundToPlay.PlayerLeft);
           if (!table.isTournamentSubTable()) {
             user.removeTable(playerId);
           }
         } else {
           logger.debug("User not found - userId: " + userId + " tableId:" + table.getId());
         }
         userPlayerMap.remove(userId);
       } else if (!table.getState().equals(TableState.FINISHED)) {
         if (table.isTournament()) {
           logger.debug("Quit tournament sub tables for userId: " + userId);
           TableManager.getInstance().userQuitTournamentSubTables(tournament.getId(), userId);
           logger.debug(
               "Quit tournament  Id: "
                   + table.getTournament().getId()
                   + "("
                   + table.getTournament().getTournamentState()
                   + ")");
           TournamentManager.getInstance().quit(tournament.getId(), userId);
         } else {
           MatchPlayer matchPlayer = match.getPlayer(playerId);
           if (matchPlayer != null && !match.hasEnded() && !matchPlayer.hasQuit()) {
             Game game = match.getGame();
             if (game != null && !game.hasEnded()) {
               Player player = match.getPlayer(playerId).getPlayer();
               if (player != null && player.isInGame()) {
                 GameManager.getInstance().quitMatch(game.getId(), userId);
               }
               match.quitMatch(playerId);
             } else {
               if (table.getState().equals(TableState.SIDEBOARDING)) {
                 if (!matchPlayer.isDoneSideboarding()) {
                   // submit deck to finish sideboarding and trigger match start / end
                   matchPlayer.submitDeck(matchPlayer.getDeck());
                 }
               }
               match.quitMatch(playerId);
             }
           }
         }
       }
     } else {
       logger.error("No playerId found for userId: " + userId);
     }
   }
 }
Exemple #8
0
  private void update() {
    ArrayList<TableView> tableList = new ArrayList<>();
    ArrayList<MatchView> matchList = new ArrayList<>();
    List<Table> allTables = new ArrayList<>(tables.values());
    Collections.sort(allTables, new TableListSorter());
    for (Table table : allTables) {
      if (table.getState() != TableState.FINISHED) {
        tableList.add(new TableView(table));
      } else if (matchList.size() < 50) {
        matchList.add(new MatchView(table));
      } else {
        // more since 50 matches finished since this match so remove it
        if (table.isTournament()) {
          TournamentManager.getInstance().removeTournament(table.getTournament().getId());
        }
        this.removeTable(table.getId());
      }
    }
    tableView = tableList;
    matchView = matchList;
    List<UsersView> users = new ArrayList<>();
    for (User user : UserManager.getInstance().getUsers()) {
      try {
        users.add(
            new UsersView(
                user.getUserData().getFlagName(),
                user.getName(),
                user.getMatchHistory(),
                user.getMatchQuitRatio(),
                user.getTourneyHistory(),
                user.getTourneyQuitRatio(),
                user.getGameInfo(),
                user.getPingInfo()));
      } catch (Exception ex) {
        logger.fatal("User update exception: " + user.getName() + " - " + ex.toString(), ex);
        users.add(
            new UsersView(
                (user.getUserData() != null && user.getUserData().getFlagName() != null)
                    ? user.getUserData().getFlagName()
                    : "world",
                user.getName() != null ? user.getName() : "<no name>",
                user.getMatchHistory() != null ? user.getMatchHistory() : "<no match history>",
                user.getMatchQuitRatio(),
                user.getTourneyHistory() != null
                    ? user.getTourneyHistory()
                    : "<no tourney history>",
                user.getTourneyQuitRatio(),
                "[exception]",
                user.getPingInfo() != null ? user.getPingInfo() : "<no ping>"));
      }
    }

    Collections.sort(users, new UserNameSorter());
    List<RoomUsersView> roomUserInfo = new ArrayList<>();
    roomUserInfo.add(
        new RoomUsersView(
            users,
            GameManager.getInstance().getNumberActiveGames(),
            ThreadExecutor.getInstance()
                .getActiveThreads(ThreadExecutor.getInstance().getGameExecutor()),
            ConfigSettings.getInstance().getMaxGameThreads()));
    roomUsersView = roomUserInfo;
  }