Search in sources :

Example 1 with DeckValidatorError

use of mage.cards.decks.DeckValidatorError in project mage by magefree.

the class TableController method joinTournament.

public synchronized boolean joinTournament(UUID userId, String name, PlayerType 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.");
    }
    Optional<User> _user = managerFactory.userManager().getUser(userId);
    if (!_user.isPresent()) {
        logger.fatal("couldn't get user " + name + " for join tournament userId = " + userId);
        return false;
    }
    User user = _user.get();
    // check password
    if (!table.getTournament().getOptions().getPassword().isEmpty() && playerType == PlayerType.HUMAN) {
        if (!table.getTournament().getOptions().getPassword().equals(password)) {
            user.showUserMessage("Join Table", "Wrong password.");
            return false;
        }
    }
    if (userPlayerMap.containsKey(userId) && playerType == PlayerType.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");
            List<DeckValidatorError> errorsList = table.getValidator().getErrorsListSorted();
            errorsList.stream().forEach(error -> {
                sb.append(error.getGroup()).append(": ").append(error.getMessage()).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());
                managerFactory.tableManager().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;
    }
    // Check minimum rating.
    int minimumRating = table.getTournament().getOptions().getMinimumRating();
    int userRating;
    if (table.getTournament().getOptions().getMatchOptions().isLimited()) {
        userRating = user.getUserData().getLimitedRating();
    } else {
        userRating = user.getUserData().getConstructedRating();
    }
    if (userRating < minimumRating) {
        String message = new StringBuilder("Your rating ").append(userRating).append(" is lower than the table requirement ").append(minimumRating).toString();
        user.showUserMessage("Join Table", message);
        return false;
    }
    Optional<Player> playerOptional = createPlayer(name, seat.getPlayerType(), skill);
    if (playerOptional.isPresent()) {
        Player player = playerOptional.get();
        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.");
    }
}
Also used : MatchPlayer(mage.game.match.MatchPlayer) Player(mage.players.Player) DraftPlayer(mage.game.draft.DraftPlayer) TournamentPlayer(mage.game.tournament.TournamentPlayer) TournamentPlayer(mage.game.tournament.TournamentPlayer) Deck(mage.cards.decks.Deck) DeckValidatorError(mage.cards.decks.DeckValidatorError)

Example 2 with DeckValidatorError

use of mage.cards.decks.DeckValidatorError in project mage by magefree.

the class HistoricalType2 method validate.

/**
 * Overridden validate function. Changes the standard sets, then uses the
 * regular validation function to test validity.
 *
 * @param deck - the deck to validate.
 * @return
 */
@Override
public boolean validate(Deck deck) {
    List<DeckValidatorError> leastInvalid = null;
    boolean valid = false;
    errorsList.clear();
    // first, check whether misty and batterskull are in the same deck.
    Map<String, Integer> counts = new HashMap<>();
    countCards(counts, deck.getCards());
    countCards(counts, deck.getSideboard());
    if (counts.containsKey("Stoneforge Mystic") && counts.containsKey("Batterskull")) {
        // if both, then skip all following tests by returning
        return false;
    }
    // iterate through the array of standards.
    for (String[] sets : standards) {
        // clear the invalid list
        errorsList.clear();
        // add the sets to the setCodes.
        setCodes = new ArrayList<>(Arrays.asList(sets));
        // validate it. If it validates, clear the invalid cards and break.
        if (super.validate(deck)) {
            valid = true;
            break;
        }
        // copy of the current invalid list.
        if (leastInvalid == null) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
            continue;
        }
        // to leastInvalid.
        if (leastInvalid.size() > this.getErrorsList().size()) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
        }
    }
    // After testing the first few standards, do the regular ones.
    // set the initial starting and ending date, as well as the current.
    GregorianCalendar start = new GregorianCalendar(2006, Calendar.SEPTEMBER, 1);
    GregorianCalendar end = new GregorianCalendar(2008, Calendar.SEPTEMBER, 1);
    GregorianCalendar current = new GregorianCalendar();
    // the date for each standard.
    while (end.before(current) && !valid) {
        // clear the invalid list and set codes.
        setCodes.clear();
        errorsList.clear();
        // increment the start and end dates.
        start.set(Calendar.YEAR, start.get(Calendar.YEAR) + 1);
        end.set(Calendar.YEAR, start.get(Calendar.YEAR) + 2);
        // (code taken from standard.java)
        for (ExpansionSet set : Sets.getInstance().values()) {
            if (set.getSetType().isStandardLegal() && set.getReleaseDate().after(start.getTime()) && set.getReleaseDate().before(end.getTime())) {
                setCodes.add(set.getCode());
            }
        }
        // validate it. If it validates, clear the invalid cards and break.
        if (super.validate(deck)) {
            errorsList.clear();
            valid = true;
            break;
        }
        // to leastInvalid.
        if (leastInvalid == null) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
        } else if (leastInvalid.size() > this.getErrorsList().size()) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
        }
    }
    // invalid that had the least errors.
    if (!valid) {
        this.errorsList = new ArrayList<>(leastInvalid);
    }
    // return the validity.
    return valid;
}
Also used : DeckValidatorError(mage.cards.decks.DeckValidatorError) ExpansionSet(mage.cards.ExpansionSet)

Example 3 with DeckValidatorError

use of mage.cards.decks.DeckValidatorError in project mage by magefree.

the class SuperType2 method validate.

/**
 * Overridden validate function. Changes the standard sets, then uses the
 * regular validation function to test validity.
 *
 * @param deck - the deck to validate.
 * @return boolean if valid deck
 */
@Override
public boolean validate(Deck deck) {
    List<DeckValidatorError> leastInvalid = null;
    boolean valid = false;
    errorsList.clear();
    // first, check whether misty and batterskull are in the same deck.
    Map<String, Integer> counts = new HashMap<>();
    countCards(counts, deck.getCards());
    countCards(counts, deck.getSideboard());
    if (counts.containsKey("Stoneforge Mystic") && counts.containsKey("Batterskull")) {
        // if both, then skip all following tests by returning
        return false;
    }
    // iterate through the array of standards.
    for (String[] sets : standards) {
        // clear the invalid list
        errorsList.clear();
        // add the sets to the setCodes.
        setCodes = new ArrayList<>(Arrays.asList(sets));
        // misty and darksteel citadel
        if (setCodes.contains("MRD") || setCodes.contains("SOM")) {
            banned.add("Darksteel Citadel");
        } else {
            banned.remove("Darksteel Citadel");
        }
        // validate it. If it validates, clear the invalid cards and break.
        if (super.validate(deck)) {
            valid = true;
            break;
        }
        // copy of the current invalid list.
        if (leastInvalid == null) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
            continue;
        }
        // to leastInvalid.
        if (leastInvalid.size() > this.getErrorsList().size()) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
        }
    }
    // After testing the first few standards, do the regular ones.
    // set the initial starting and ending date, as well as the current.
    GregorianCalendar start = new GregorianCalendar(2006, Calendar.SEPTEMBER, 1);
    GregorianCalendar end = new GregorianCalendar(2008, Calendar.SEPTEMBER, 1);
    GregorianCalendar current = new GregorianCalendar();
    // the date for each standard.
    while (end.before(current) && !valid) {
        // clear the invalid list and set codes.
        setCodes.clear();
        errorsList.clear();
        // increment the start and end dates.
        start.set(Calendar.YEAR, start.get(Calendar.YEAR) + 1);
        end.set(Calendar.YEAR, start.get(Calendar.YEAR) + 2);
        // (code taken from standard.java)
        for (ExpansionSet set : Sets.getInstance().values()) {
            if (set.getSetType().isStandardLegal() && set.getReleaseDate().after(start.getTime()) && set.getReleaseDate().before(end.getTime())) {
                setCodes.add(set.getCode());
            }
        }
        // misty and darksteel citadel
        if (setCodes.contains("MRD") || setCodes.contains("SOM")) {
            banned.add("Darksteel Citadel");
        } else {
            banned.remove("Darksteel Citadel");
        }
        // validate it. If it validates, clear the invalid cards and break.
        if (super.validate(deck)) {
            errorsList.clear();
            valid = true;
            break;
        }
        // to leastInvalid.
        if (leastInvalid == null) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
        } else if (leastInvalid.size() > this.getErrorsList().size()) {
            leastInvalid = new ArrayList<>(this.getErrorsList());
        }
    }
    // invalid that had the least errors.
    if (!valid) {
        this.errorsList = new ArrayList<>(leastInvalid);
    }
    // return the validity.
    return valid;
}
Also used : DeckValidatorError(mage.cards.decks.DeckValidatorError) ExpansionSet(mage.cards.ExpansionSet)

Example 4 with DeckValidatorError

use of mage.cards.decks.DeckValidatorError in project mage by magefree.

the class TableController method submitDeck.

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()) {
            // so the construct panel closes after submit
            return true;
        }
    } else if (table.getMatch() != null) {
        MatchPlayer mPlayer = table.getMatch().getPlayer(playerId);
        if (mPlayer == null || mPlayer.hasQuit()) {
            // so the construct panel closes after submit
            return true;
        }
        if (table.isTournamentSubTable()) {
            TournamentPlayer tournamentPlayer = table.getTournament().getPlayer(mPlayer.getPlayer().getId());
            if (tournamentPlayer != null) {
                // reset sideboarding state
                tournamentPlayer.setStateInfo("");
            }
        }
    }
    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)) {
        Optional<User> _user = managerFactory.userManager().getUser(userId);
        if (!_user.isPresent()) {
            return false;
        }
        StringBuilder sb = new StringBuilder("Invalid deck for the selected ").append(table.getValidator().getName()).append(" format. \n\n");
        List<DeckValidatorError> errorsList = table.getValidator().getErrorsListSorted();
        errorsList.stream().forEach(error -> {
            sb.append(error.getGroup()).append(": ").append(error.getMessage()).append("\n");
        });
        sb.append("\n\nAdd enough cards and try again!");
        _user.get().showUserMessage("Submit deck", sb.toString());
        return false;
    }
    submitDeck(userId, playerId, deck);
    return true;
}
Also used : DeckValidatorError(mage.cards.decks.DeckValidatorError) TournamentPlayer(mage.game.tournament.TournamentPlayer) MatchPlayer(mage.game.match.MatchPlayer) Deck(mage.cards.decks.Deck)

Example 5 with DeckValidatorError

use of mage.cards.decks.DeckValidatorError in project mage by magefree.

the class TableController method joinTable.

public synchronized boolean joinTable(UUID userId, String name, PlayerType playerType, int skill, DeckCardLists deckList, String password) throws MageException {
    Optional<User> _user = managerFactory.userManager().getUser(userId);
    if (!_user.isPresent()) {
        logger.error("Join Table: can't find user to join " + name + " Id = " + userId);
        return false;
    }
    User user = _user.get();
    if (userPlayerMap.containsKey(userId) && playerType == PlayerType.HUMAN) {
        user.showUserMessage("Join Table", new StringBuilder("You can join a table only one time.").toString());
        return false;
    }
    if (table.getState() != TableState.WAITING) {
        user.showUserMessage("Join Table", "No available seats.");
        return false;
    }
    // check password
    if (!table.getMatch().getOptions().getPassword().isEmpty() && playerType == PlayerType.HUMAN) {
        if (!table.getMatch().getOptions().getPassword().equals(password)) {
            user.showUserMessage("Join Table", "Wrong password.");
            return false;
        }
    }
    Seat seat = table.getNextAvailableSeat(playerType);
    if (seat == null) {
        user.showUserMessage("Join Table", "No available seats.");
        return false;
    }
    Deck deck = Deck.load(deckList, false, 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");
        List<DeckValidatorError> errorsList = table.getValidator().getErrorsListSorted();
        errorsList.stream().forEach(error -> {
            sb.append(error.getGroup()).append(": ").append(error.getMessage()).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());
            managerFactory.tableManager().removeTable(table.getId());
        }
        return false;
    }
    // Check quit ratio.
    int quitRatio = table.getMatch().getOptions().getQuitRatio();
    if (quitRatio < user.getMatchQuitRatio()) {
        String message = new StringBuilder("Your quit ratio ").append(user.getMatchQuitRatio()).append("% is higher than the table requirement ").append(quitRatio).append('%').toString();
        user.showUserMessage("Join Table", message);
        return false;
    }
    // Check minimum rating.
    int minimumRating = table.getMatch().getOptions().getMinimumRating();
    int userRating;
    if (table.getMatch().getOptions().isLimited()) {
        userRating = user.getUserData().getLimitedRating();
    } else {
        userRating = user.getUserData().getConstructedRating();
    }
    if (userRating < minimumRating) {
        String message = new StringBuilder("Your rating ").append(userRating).append(" is lower than the table requirement ").append(minimumRating).toString();
        user.showUserMessage("Join Table", message);
        return false;
    }
    // Check power level for table (currently only used for EDH/Commander table)
    int edhPowerLevel = table.getMatch().getOptions().getEdhPowerLevel();
    if (edhPowerLevel > 0 && table.getValidator().getName().toLowerCase(Locale.ENGLISH).equals("commander")) {
        int deckEdhPowerLevel = table.getValidator().getEdhPowerLevel(deck);
        if (deckEdhPowerLevel % 100 > edhPowerLevel) {
            String message = new StringBuilder("Your deck appears to be too powerful for this table.\n\nReduce the number of extra turn cards, infect, counters, fogs, reconsider your commander. ").append("\nThe table requirement has a maximum power level of ").append(edhPowerLevel).append(" whilst your deck has a calculated power level of ").append(deckEdhPowerLevel % 100).toString();
            user.showUserMessage("Join Table", message);
            return false;
        }
        boolean restrictedColor = false;
        String badColor = "";
        int colorVal = edhPowerLevel % 10;
        if (colorVal == 6 && deckEdhPowerLevel >= 10000000) {
            restrictedColor = true;
            badColor = "white";
        }
        if (colorVal == 4 && deckEdhPowerLevel % 10000000 >= 1000000) {
            restrictedColor = true;
            badColor = "blue";
        }
        if (colorVal == 3 && deckEdhPowerLevel % 1000000 >= 100000) {
            restrictedColor = true;
            badColor = "black";
        }
        if (colorVal == 2 && deckEdhPowerLevel % 100000 >= 10000) {
            restrictedColor = true;
            badColor = "red";
        }
        if (colorVal == 1 && deckEdhPowerLevel % 10000 >= 1000) {
            restrictedColor = true;
            badColor = "green";
        }
        if (restrictedColor) {
            String message = new StringBuilder("Your deck contains ").append(badColor).append(".  The creator of the table has requested no ").append(badColor).append(" cards to be on the table!").toString();
            user.showUserMessage("Join Table", message);
            return false;
        }
    }
    Optional<Player> playerOpt = createPlayer(name, seat.getPlayerType(), skill);
    if (!playerOpt.isPresent()) {
        String message = "Could not create player " + name + " of type " + seat.getPlayerType();
        logger.warn("User: " + user.getName() + " => " + message);
        user.showUserMessage("Join Table", message);
        return false;
    }
    Player player = playerOpt.get();
    if (!player.canJoinTable(table)) {
        user.showUserMessage("Join Table", "A " + seat.getPlayerType() + " player can't join this table.");
        return false;
    }
    match.addPlayer(player, deck);
    table.joinTable(player, seat);
    logger.trace(player.getName() + " joined tableId: " + table.getId());
    // only inform human players and add them to sessionPlayerMap
    if (seat.getPlayer().isHuman()) {
        seat.getPlayer().setUserData(user.getUserData());
        if (!table.isTournamentSubTable()) {
            user.addTable(player.getId(), table);
        }
        user.ccJoinedTable(table.getRoomId(), table.getId(), false);
        userPlayerMap.put(userId, player.getId());
    }
    return true;
}
Also used : DeckValidatorError(mage.cards.decks.DeckValidatorError) MatchPlayer(mage.game.match.MatchPlayer) Player(mage.players.Player) DraftPlayer(mage.game.draft.DraftPlayer) TournamentPlayer(mage.game.tournament.TournamentPlayer) Deck(mage.cards.decks.Deck)

Aggregations

DeckValidatorError (mage.cards.decks.DeckValidatorError)5 Deck (mage.cards.decks.Deck)3 MatchPlayer (mage.game.match.MatchPlayer)3 TournamentPlayer (mage.game.tournament.TournamentPlayer)3 ExpansionSet (mage.cards.ExpansionSet)2 DraftPlayer (mage.game.draft.DraftPlayer)2 Player (mage.players.Player)2