use of mage.util.MessageToClient in project mage by magefree.
the class HumanPlayer method choose.
@Override
public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) {
if (gameInCheckPlayableState(game)) {
return true;
}
// choose one or multiple cards
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.isRequired(null, game);
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, null, 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("choose(4)", game);
prepareForResponse(game);
if (!isExecutingMacro()) {
game.fireSelectTargetEvent(playerId, new MessageToClient(target.getMessage()), cards, required, options);
}
waitForResponse(game);
UUID responseId = getFixedResponseUUID(game);
if (responseId != null) {
if (target.getTargets().contains(responseId)) {
// if already included remove it with
target.remove(responseId);
} else {
if (target.canTarget(abilityControllerId, responseId, null, cards, game)) {
target.add(responseId, game);
if (target.doneChosing()) {
return true;
}
}
}
} else {
if (target.getTargets().size() >= target.getNumberOfTargets()) {
return true;
}
if (!required) {
return false;
}
}
}
return false;
}
use of mage.util.MessageToClient in project mage by magefree.
the class HumanPlayer method selectCombatGroup.
protected void selectCombatGroup(UUID defenderId, UUID blockerId, Game game) {
if (gameInCheckPlayableState(game)) {
return;
}
TargetAttackingCreature target = new TargetAttackingCreature();
// TODO: add canRespond cycle?
if (!canRespond()) {
return;
}
updateGameStatePriority("selectCombatGroup", game);
prepareForResponse(game);
if (!isExecutingMacro()) {
// possible attackers to block
Set<UUID> attackers = target.possibleTargets(null, playerId, game);
Permanent blocker = game.getPermanent(blockerId);
Set<UUID> possibleTargets = new HashSet<>();
for (UUID attackerId : attackers) {
CombatGroup group = game.getCombat().findGroup(attackerId);
if (group != null && blocker != null && group.canBlock(blocker, game)) {
possibleTargets.add(attackerId);
}
}
game.fireSelectTargetEvent(playerId, new MessageToClient("Select attacker to block", getRelatedObjectName(blockerId, game)), possibleTargets, false, getOptions(target, null));
}
waitForResponse(game);
UUID responseId = getFixedResponseUUID(game);
if (response.getBoolean() != null) {
// do nothing
} else if (responseId != null) {
CombatGroup group = game.getCombat().findGroup(responseId);
if (group != null) {
// check if already blocked, if not add
if (!group.getBlockers().contains(blockerId)) {
declareBlocker(defenderId, blockerId, responseId, game);
} else {
// else remove from block
game.getCombat().removeBlockerGromGroup(blockerId, group, game);
}
}
}
}
use of mage.util.MessageToClient in project mage by magefree.
the class HumanPlayer method choose.
@Override
public boolean choose(Outcome outcome, Target target, UUID sourceId, Game game, Map<String, Serializable> options) {
if (gameInCheckPlayableState(game)) {
return true;
}
// choose one or multiple permanents
UUID abilityControllerId = playerId;
if (target.getTargetController() != null && target.getAbilityController() != null) {
abilityControllerId = target.getAbilityController();
}
if (options == null) {
options = new HashMap<>();
}
while (canRespond()) {
Set<UUID> targetIds = target.possibleTargets(sourceId, abilityControllerId, game);
if (targetIds == null || targetIds.isEmpty()) {
return target.getTargets().size() >= target.getNumberOfTargets();
}
boolean required = target.isRequired(sourceId, game);
if (target.getTargets().size() >= target.getNumberOfTargets()) {
required = false;
}
java.util.List<UUID> chosen = target.getTargets();
options.put("chosen", (Serializable) chosen);
updateGameStatePriority("choose(5)", game);
prepareForResponse(game);
if (!isExecutingMacro()) {
game.fireSelectTargetEvent(getId(), new MessageToClient(target.getMessage(), getRelatedObjectName(sourceId, game)), targetIds, 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 (!targetIds.contains(responseId)) {
continue;
}
if (target instanceof TargetPermanent) {
if (((TargetPermanent) target).canTarget(abilityControllerId, responseId, sourceId, game, false)) {
target.add(responseId, game);
if (target.doneChosing()) {
return true;
}
}
} else {
MageObject object = game.getObject(sourceId);
if (object instanceof Ability) {
if (target.canTarget(responseId, (Ability) object, game)) {
if (target.getTargets().contains(responseId)) {
// if already included remove it with
target.remove(responseId);
} else {
target.addTarget(responseId, (Ability) object, game);
if (target.doneChosing()) {
return true;
}
}
}
} else if (target.canTarget(responseId, game)) {
if (target.getTargets().contains(responseId)) {
// if already included remove it with
target.remove(responseId);
} else {
target.addTarget(responseId, null, game);
if (target.doneChosing()) {
return true;
}
}
}
}
} else {
// auto-complete on all selected
if (target.getTargets().size() >= target.getNumberOfTargets()) {
return true;
}
// cancel/done button
if (!required) {
return false;
}
}
}
return false;
}
Aggregations