Exemplo n.º 1
0
 public synchronized void startTournament(UUID userId) {
   try {
     if (userId.equals(this.userId) && table.getState().equals(TableState.STARTING)) {
       tournament.setStartTime();
       TournamentManager.getInstance()
           .createTournamentSession(tournament, userPlayerMap, table.getId());
       for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
         User user = UserManager.getInstance().getUser(entry.getKey());
         if (user != null) {
           logger.info(
               new StringBuilder("User ")
                   .append(user.getName())
                   .append(" tournament started: ")
                   .append(tournament.getId())
                   .append(" userId: ")
                   .append(user.getId()));
           user.ccTournamentStarted(tournament.getId(), entry.getValue());
         }
       }
       ServerMessagesUtil.getInstance().incTournamentsStarted();
     }
   } catch (Exception ex) {
     logger.fatal("Error starting tournament", ex);
     TableManager.getInstance().removeTable(table.getId());
     TournamentManager.getInstance().quit(tournament.getId(), userId);
   }
 }
Exemplo n.º 2
0
 public void endDraft(Draft draft) {
   if (!draft.isAbort()) {
     for (DraftPlayer player : draft.getPlayers()) {
       player.prepareDeck();
       tournament.getPlayer(player.getPlayer().getId()).setDeck(player.getDeck());
     }
   }
   tournament.clearDraft(); // free the draft resources after draft step has ended
   tournament.nextStep();
 }
Exemplo n.º 3
0
  public synchronized boolean replaceDraftPlayer(
      Player oldPlayer, String name, String playerType, int skill) {
    Player newPlayer = createPlayer(name, playerType, skill);
    if (newPlayer == null || table.getState() != TableState.DRAFTING) {
      return false;
    }
    TournamentPlayer oldTournamentPlayer = tournament.getPlayer(oldPlayer.getId());
    tournament.removePlayer(oldPlayer.getId());
    tournament.addPlayer(newPlayer, playerType);

    TournamentPlayer newTournamentPlayer = tournament.getPlayer(newPlayer.getId());
    newTournamentPlayer.setState(oldTournamentPlayer.getState());
    newTournamentPlayer.setReplacedTournamentPlayer(oldTournamentPlayer);

    DraftManager.getInstance().getController(table.getId()).replacePlayer(oldPlayer, newPlayer);
    return true;
  }
Exemplo n.º 4
0
 private void submitDeck(UUID userId, UUID playerId, Deck deck) {
   if (table.getState() == TableState.SIDEBOARDING) {
     match.submitDeck(playerId, deck);
     UserManager.getInstance().getUser(userId).removeSideboarding(table.getId());
   } else {
     TournamentManager.getInstance().submitDeck(tournament.getId(), playerId, deck);
     UserManager.getInstance().getUser(userId).removeConstructing(playerId);
   }
 }
Exemplo n.º 5
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
   }
 }
Exemplo n.º 6
0
 public boolean isUserStillActive(UUID userId) {
   UUID playerId = userPlayerMap.get(userId);
   if (playerId != null) {
     if (tournament != null) {
       TournamentPlayer tournamentPlayer = tournament.getPlayer(playerId);
       if (tournamentPlayer != null) {
         return tournamentPlayer.isInTournament();
       }
     } else if (match != null) {
       MatchPlayer matchPlayer = match.getPlayer(playerId);
       return matchPlayer != null && !matchPlayer.hasQuit();
     }
   }
   return false;
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
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);
     }
   }
 }
Exemplo n.º 9
0
  public synchronized boolean joinTournament(
      UUID userId,
      String name,
      String playerType,
      int skill,
      DeckCardLists deckList,
      String password)
      throws GameException {
    if (table.getState() != TableState.WAITING) {
      return false;
    }

    Seat seat = table.getNextAvailableSeat(playerType);
    if (seat == null) {
      throw new GameException("No available seats.");
    }
    User user = UserManager.getInstance().getUser(userId);
    if (user == null) {
      logger.fatal(
          new StringBuilder("couldn't get user ")
              .append(name)
              .append(" for join tournament userId = ")
              .append(userId)
              .toString());
      return false;
    }
    // check password
    if (!table.getTournament().getOptions().getPassword().isEmpty() && playerType.equals("Human")) {
      if (!table.getTournament().getOptions().getPassword().equals(password)) {
        user.showUserMessage("Join Table", "Wrong password.");
        return false;
      }
    }
    if (userPlayerMap.containsKey(userId) && playerType.equals("Human")) {
      user.showUserMessage("Join Table", "You can join a table only one time.");
      return false;
    }
    Deck deck = null;
    if (!table.getTournament().getTournamentType().isLimited()) {
      if (deckList != null) {
        deck = Deck.load(deckList, false, false);
      } else {
        user.showUserMessage("Join Table", "No valid deck selected!");
        return false;
      }
      if (!Main.isTestMode() && !table.getValidator().validate(deck)) {
        StringBuilder sb =
            new StringBuilder("You (")
                .append(name)
                .append(") have an invalid deck for the selected ")
                .append(table.getValidator().getName())
                .append(" Format. \n\n");
        for (Map.Entry<String, String> entry : table.getValidator().getInvalid().entrySet()) {
          sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
        }
        sb.append("\n\nSelect a deck that is appropriate for the selected format and try again!");
        user.showUserMessage("Join Table", sb.toString());
        if (isOwner(userId)) {
          logger.debug(
              "New table removed because owner submitted invalid deck tableId " + table.getId());
          TableManager.getInstance().removeTable(table.getId());
        }
        return false;
      }
    }
    // Check quit ratio.
    int quitRatio = table.getTournament().getOptions().getQuitRatio();
    if (quitRatio < user.getTourneyQuitRatio()) {
      String message =
          new StringBuilder("Your quit ratio ")
              .append(user.getTourneyQuitRatio())
              .append("% is higher than the table requirement ")
              .append(quitRatio)
              .append("%")
              .toString();
      user.showUserMessage("Join Table", message);
      return false;
    }

    Player player = createPlayer(name, seat.getPlayerType(), skill);
    if (player != null) {
      if (!player.canJoinTable(table)) {
        user.showUserMessage(
            "Join Table",
            new StringBuilder("A ")
                .append(seat.getPlayerType())
                .append(" player can't join this table.")
                .toString());
        return false;
      }
      tournament.addPlayer(player, seat.getPlayerType());
      TournamentPlayer tournamentPlayer = tournament.getPlayer(player.getId());
      if (deck != null && tournamentPlayer != null) {
        tournamentPlayer.submitDeck(deck);
      }
      table.joinTable(player, seat);
      logger.debug(
          "Player "
              + player.getName()
              + " id: "
              + player.getId()
              + " joined tableId: "
              + table.getId());
      // only inform human players and add them to sessionPlayerMap
      if (seat.getPlayer().isHuman()) {
        seat.getPlayer().setUserData(user.getUserData());
        user.addTable(player.getId(), table);
        user.ccJoinedTable(table.getRoomId(), table.getId(), true);
        userPlayerMap.put(userId, player.getId());
      }

      return true;
    } else {
      throw new GameException("Playertype " + seat.getPlayerType() + " could not be created.");
    }
  }