Search in sources :

Example 1 with MessageToClient

use of mage.util.MessageToClient in project mage by magefree.

the class HumanPlayer method chooseTarget.

// choose one or multiple target cards
@Override
public boolean chooseTarget(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) {
    if (gameInCheckPlayableState(game)) {
        return true;
    }
    if (cards == null || cards.isEmpty()) {
        return false;
    }
    UUID abilityControllerId = playerId;
    if (target.getTargetController() != null && target.getAbilityController() != null) {
        abilityControllerId = target.getAbilityController();
    }
    while (canRespond()) {
        boolean required = target.isRequiredExplicitlySet() ? target.isRequired() : target.isRequired(source);
        int count = cards.count(target.getFilter(), abilityControllerId, game);
        if (count == 0 || target.getTargets().size() >= target.getNumberOfTargets()) {
            required = false;
        }
        Map<String, Serializable> options = getOptions(target, null);
        java.util.List<UUID> chosen = target.getTargets();
        options.put("chosen", (Serializable) chosen);
        java.util.List<UUID> choosable = new ArrayList<>();
        for (UUID cardId : cards) {
            if (target.canTarget(abilityControllerId, cardId, source, cards, game)) {
                choosable.add(cardId);
            }
        }
        if (!choosable.isEmpty()) {
            options.put("choosable", (Serializable) choosable);
        }
        // if nothing to choose then show dialog (user must see non selectable items and click on any of them)
        if (required && choosable.isEmpty()) {
            required = false;
        }
        updateGameStatePriority("chooseTarget(5)", game);
        prepareForResponse(game);
        if (!isExecutingMacro()) {
            game.fireSelectTargetEvent(playerId, new MessageToClient(target.getMessage(), getRelatedObjectName(source, game)), cards, required, options);
        }
        waitForResponse(game);
        UUID responseId = getFixedResponseUUID(game);
        if (responseId != null) {
            if (target.getTargets().contains(responseId)) {
                // if already included remove it
                target.remove(responseId);
            } else if (target.canTarget(abilityControllerId, responseId, source, cards, game)) {
                target.addTarget(responseId, source, game);
                if (target.doneChosing()) {
                    return true;
                }
            }
        } else {
            if (target.getTargets().size() >= target.getNumberOfTargets()) {
                return true;
            }
            if (!required) {
                return false;
            }
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) java.util(java.util) MessageToClient(mage.util.MessageToClient)

Example 2 with MessageToClient

use of mage.util.MessageToClient in project mage by magefree.

the class HumanPlayer method chooseTarget.

@Override
public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game game) {
    if (gameInCheckPlayableState(game)) {
        return true;
    }
    // choose one or multiple targets
    UUID abilityControllerId = playerId;
    if (target.getAbilityController() != null) {
        abilityControllerId = target.getAbilityController();
    }
    Map<String, Serializable> options = new HashMap<>();
    while (canRespond()) {
        Set<UUID> possibleTargets = target.possibleTargets(source == null ? null : source.getSourceId(), abilityControllerId, game);
        boolean required = target.isRequired(source != null ? source.getSourceId() : null, game);
        if (possibleTargets.isEmpty() || target.getTargets().size() >= target.getNumberOfTargets()) {
            required = false;
        }
        java.util.List<UUID> chosen = target.getTargets();
        options.put("chosen", (Serializable) chosen);
        updateGameStatePriority("chooseTarget", game);
        prepareForResponse(game);
        if (!isExecutingMacro()) {
            game.fireSelectTargetEvent(getId(), new MessageToClient(target.getMessage(), getRelatedObjectName(source, game)), possibleTargets, required, getOptions(target, options));
        }
        waitForResponse(game);
        UUID responseId = getFixedResponseUUID(game);
        if (responseId != null) {
            // remove selected
            if (target.getTargets().contains(responseId)) {
                target.remove(responseId);
                continue;
            }
            if (possibleTargets.contains(responseId)) {
                if (target.canTarget(abilityControllerId, responseId, source, game)) {
                    target.addTarget(responseId, source, game);
                    if (target.doneChosing()) {
                        return true;
                    }
                }
            }
        } else {
            if (target.getTargets().size() >= target.getNumberOfTargets()) {
                return true;
            }
            if (!required) {
                return false;
            }
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) java.util(java.util) MessageToClient(mage.util.MessageToClient)

Example 3 with MessageToClient

use of mage.util.MessageToClient in project mage by magefree.

the class HumanPlayer method chooseTargetAmount.

@Override
public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability source, Game game) {
    // human can choose or un-choose MULTIPLE targets at once
    if (gameInCheckPlayableState(game)) {
        return true;
    }
    UUID abilityControllerId = playerId;
    if (target.getAbilityController() != null) {
        abilityControllerId = target.getAbilityController();
    }
    int amountTotal = target.getAmountTotal(game, source);
    // 1. Select targets
    while (canRespond()) {
        Set<UUID> possibleTargets = target.possibleTargets(source == null ? null : source.getSourceId(), abilityControllerId, game);
        boolean required = target.isRequired(source != null ? source.getSourceId() : null, game);
        if (possibleTargets.isEmpty() || target.getSize() >= target.getNumberOfTargets()) {
            required = false;
        }
        // selected
        Map<String, Serializable> options = getOptions(target, null);
        java.util.List<UUID> chosen = target.getTargets();
        options.put("chosen", (Serializable) chosen);
        // selectable
        java.util.List<UUID> choosable = new ArrayList<>();
        for (UUID targetId : possibleTargets) {
            if (target.canTarget(abilityControllerId, targetId, source, game)) {
                choosable.add(targetId);
            }
        }
        if (!choosable.isEmpty()) {
            options.put("choosable", (Serializable) choosable);
        }
        // if nothing to choose then show dialog (user must see non selectable items and click on any of them)
        if (required && choosable.isEmpty()) {
            required = false;
        }
        updateGameStatePriority("chooseTargetAmount", game);
        prepareForResponse(game);
        if (!isExecutingMacro()) {
            // target amount uses for damage only, if you see another use case then message must be changed here and on getMultiAmount call
            String message = String.format("Select targets to distribute %d damage (selected %d)", amountTotal, target.getTargets().size());
            game.fireSelectTargetEvent(playerId, new MessageToClient(message, getRelatedObjectName(source, game)), possibleTargets, required, options);
        }
        waitForResponse(game);
        UUID responseId = getFixedResponseUUID(game);
        if (responseId != null) {
            if (target.contains(responseId)) {
                // unselect
                target.remove(responseId);
            } else if (possibleTargets.contains(responseId) && target.canTarget(abilityControllerId, responseId, source, game)) {
                // select
                target.addTarget(responseId, source, game);
            }
        } else if (!required) {
            break;
        }
    }
    // no targets to choose or disconnected
    List<UUID> targets = target.getTargets();
    if (targets.isEmpty()) {
        return false;
    }
    // 2. Distribute amount between selected targets
    // prepare targets list with p/t or life stats (cause that's dialog used for damage distribute)
    List<String> targetNames = new ArrayList<>();
    for (UUID targetId : targets) {
        MageObject targetObject = game.getObject(targetId);
        if (targetObject != null) {
            targetNames.add(String.format("%s, P/T: %d/%d", targetObject.getIdName(), targetObject.getPower().getValue(), targetObject.getToughness().getValue()));
        } else {
            Player player = game.getPlayer(targetId);
            if (player != null) {
                targetNames.add(String.format("%s, life: %d", player.getName(), player.getLife()));
            } else {
                targetNames.add("ERROR, unknown target " + targetId.toString());
            }
        }
    }
    // ask and assign new amount
    List<Integer> targetValues = getMultiAmount(outcome, targetNames, 1, amountTotal, MultiAmountType.DAMAGE, game);
    for (int i = 0; i < targetValues.size(); i++) {
        int newAmount = targetValues.get(i);
        UUID targetId = targets.get(i);
        if (newAmount <= 0) {
            // remove target
            target.remove(targetId);
        } else {
            // set amount
            target.setTargetAmount(targetId, newAmount, source, game);
        }
    }
    return true;
}
Also used : Serializable(java.io.Serializable) Player(mage.players.Player) MessageToClient(mage.util.MessageToClient) MageObject(mage.MageObject) java.util(java.util)

Example 4 with MessageToClient

use of mage.util.MessageToClient in project mage by magefree.

the class HumanPlayer method chooseMulligan.

@Override
public boolean chooseMulligan(Game game) {
    if (gameInCheckPlayableState(game)) {
        return true;
    }
    while (canRespond()) {
        int nextHandSize = game.mulliganDownTo(playerId);
        String cardsCountInfo = nextHandSize + (nextHandSize == 1 ? " card" : " cards");
        String message;
        if (getHand().size() > nextHandSize) {
            // pay
            message = "Mulligan " + HintUtils.prepareText("down to " + cardsCountInfo, Color.YELLOW) + "?";
        } else {
            // free
            message = "Mulligan " + HintUtils.prepareText("for free", Color.GREEN) + ", draw another " + cardsCountInfo + "?";
        }
        Map<String, Serializable> options = new HashMap<>();
        options.put("UI.left.btn.text", "Mulligan");
        options.put("UI.right.btn.text", "Keep");
        updateGameStatePriority("chooseMulligan", game);
        prepareForResponse(game);
        if (!isExecutingMacro()) {
            game.fireAskPlayerEvent(playerId, new MessageToClient(message), null, options);
        }
        waitForResponse(game);
        if (response.getBoolean() != null) {
            return response.getBoolean();
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) MessageToClient(mage.util.MessageToClient)

Example 5 with MessageToClient

use of mage.util.MessageToClient in project mage by magefree.

the class HumanPlayer method chooseUse.

@Override
public boolean chooseUse(Outcome outcome, String message, String secondMessage, String trueText, String falseText, Ability source, Game game) {
    if (game.inCheckPlayableState()) {
        return true;
    }
    MessageToClient messageToClient = new MessageToClient(message, secondMessage);
    Map<String, Serializable> options = new HashMap<>(2);
    if (trueText != null) {
        options.put("UI.left.btn.text", trueText);
    }
    if (falseText != null) {
        options.put("UI.right.btn.text", falseText);
    }
    if (source != null) {
    // options.put(Constants.Option.ORIGINAL_ID, "")
    }
    // auto-answer
    Boolean answer = null;
    if (source != null) {
        // ability + text
        answer = requestAutoAnswerId.get(source.getOriginalId() + "#" + message);
    }
    if (answer == null) {
        // text
        answer = requestAutoAnswerText.get(message);
    }
    if (answer != null) {
        return answer;
    }
    while (canRespond()) {
        if (messageToClient.getSecondMessage() == null) {
            messageToClient.setSecondMessage(getRelatedObjectName(source, game));
        }
        updateGameStatePriority("chooseUse", game);
        prepareForResponse(game);
        if (!isExecutingMacro()) {
            game.fireAskPlayerEvent(playerId, messageToClient, source, options);
        }
        waitForResponse(game);
        if (response.getBoolean() != null) {
            return response.getBoolean();
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) MessageToClient(mage.util.MessageToClient)

Aggregations

MessageToClient (mage.util.MessageToClient)8 Serializable (java.io.Serializable)6 java.util (java.util)5 MageObject (mage.MageObject)2 TargetPermanent (mage.target.TargetPermanent)2 ManaAbility (mage.abilities.mana.ManaAbility)1 CombatGroup (mage.game.combat.CombatGroup)1 Permanent (mage.game.permanent.Permanent)1 Player (mage.players.Player)1 TargetAttackingCreature (mage.target.common.TargetAttackingCreature)1