use of mage.abilities.Mode in project mage by magefree.
the class ExportJsonGameplayDataTest method exportTest.
@Test
@Ignore
public /**
* It's export code example for https://github.com/mtgjson/mtgjson4
*/
void exportTest() {
List<Card> cards = new ArrayList<>();
Collection<ExpansionSet> sets = Sets.getInstance().values();
for (ExpansionSet set : sets) {
for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) {
// catch cards creation errors and report (e.g. on wrong card code)
try {
Card card = CardImpl.createCard(setInfo.getCardClass(), new CardSetInfo(setInfo.getName(), set.getCode(), setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo()));
if (card != null) {
cards.add(card);
}
} catch (Throwable e) {
logger.error("Can't create card " + setInfo.getName() + ": " + e.getMessage(), e);
}
}
JsonObject res = new JsonObject();
for (Card card : cards) {
try {
JsonObject resCard = new JsonObject();
res.add(card.getName(), resCard);
JsonArray resAbilities = new JsonArray();
resCard.add("abilities", resAbilities);
for (Ability ability : card.getAbilities()) {
JsonObject resAbility = new JsonObject();
resAbilities.add(resAbility);
// basic
resAbility.addProperty("cost", ability.getManaCosts().getText());
resAbility.addProperty("name", ability.toString());
resAbility.addProperty("class", ability.getClass().getSimpleName());
// resAbility.addProperty("rule", ability.getRule());
// modes
JsonArray resModes = new JsonArray();
resAbility.add("modes", resModes);
for (Mode mode : ability.getModes().values()) {
JsonObject resMode = new JsonObject();
resModes.add(resMode);
// basic
// resMode.addProperty("name", mode.toString());
// effects
JsonArray resEffects = new JsonArray();
resMode.add("effects", resEffects);
for (Effect effect : mode.getEffects()) {
JsonObject resEffect = new JsonObject();
resEffects.add(resEffect);
resEffect.addProperty("class", effect.getClass().getSimpleName());
resEffect.addProperty("outcome", effect.getOutcome().toString());
resEffect.addProperty("text", effect.getText(mode));
}
if (resEffects.size() == 0) {
resMode.remove("effects");
}
// targets
JsonArray resTargets = new JsonArray();
resMode.add("targets", resTargets);
for (Target target : mode.getTargets()) {
JsonObject resTarget = new JsonObject();
resTargets.add(resTarget);
resTarget.addProperty("name", target.getTargetName());
resTarget.addProperty("class", target.getClass().getSimpleName());
resTarget.addProperty("min", target.getMinNumberOfTargets());
resTarget.addProperty("max", target.getMaxNumberOfTargets());
}
if (resTargets.size() == 0) {
resMode.remove("targets");
}
if (resMode.get("effects") == null && resMode.get("targets") == null) {
resModes.remove(resMode);
}
}
if (resModes.size() == 0) {
resAbility.remove("modes");
}
}
} catch (Throwable e) {
logger.error("Inner error for " + card.getName() + ": " + e.getMessage(), e);
break;
}
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
if (MTGJSON_WRITE_TO_FILES) {
String filePath = System.getProperty("user.dir") + "/json/" + set.getCode() + ".json";
File outputFile = new File(filePath);
final boolean mkdirs = outputFile.getParentFile().mkdirs();
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile, false), StandardCharsets.UTF_8))) {
writer.write(gson.toJson(res));
System.out.println("Wrote " + set.getCode() + " to file");
} catch (Exception e) {
e.printStackTrace();
}
} else {
// System.out.println(gson.toJson(res));
}
}
}
use of mage.abilities.Mode in project mage by magefree.
the class ReflectiveGolemTriggeredAbility method checkTrigger.
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (!isControlledBy(event.getPlayerId())) {
return false;
}
Spell spell = game.getSpellOrLKIStack(event.getTargetId());
if (spell == null || !spell.isInstantOrSorcery(game)) {
return false;
}
if (spell.getSpellAbilities().stream().map(AbilityImpl::getModes).flatMap(m -> m.getSelectedModes().stream().map(m::get)).filter(Objects::nonNull).map(Mode::getTargets).flatMap(Collection::stream).filter(t -> !t.isNotTarget()).map(Target::getTargets).flatMap(Collection::stream).filter(Objects::nonNull).distinct().filter(getSourceId()::equals).count() != 1) {
return false;
}
this.getEffects().setTargetPointer(new FixedTarget(spell, game));
return true;
}
use of mage.abilities.Mode in project mage by magefree.
the class ChangeATargetOfTargetSpellAbilityToSourceEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
MageObject sourceObject = game.getObject(source.getSourceId());
if (stackObject != null && sourceObject != null) {
Targets targets = new Targets();
Ability sourceAbility;
String oldTargetName = null;
if (stackObject instanceof Spell) {
Spell spell = (Spell) stackObject;
sourceAbility = spell.getSpellAbility();
} else if (stackObject instanceof StackAbility) {
StackAbility stackAbility = (StackAbility) stackObject;
sourceAbility = stackAbility;
} else {
return false;
}
for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
Mode mode = sourceAbility.getModes().get(modeId);
targets.addAll(mode.getTargets());
}
boolean twoTimesTarget = false;
if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
Target target = targets.get(0);
if (target.getFirstTarget().equals(source.getSourceId())) {
// Target was already the same source, so no change / target event to create
return true;
}
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
oldTargetName = getTargetName(targets.getFirstTarget(), game);
target.clearChosen();
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
}
} else {
Player controller = game.getPlayer(source.getControllerId());
boolean validTargets = false;
do {
for (Target target : targets) {
for (UUID targetId : target.getTargets()) {
String name = getTargetName(targets.getFirstTarget(), game);
if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) {
// you can't change this target to source because the source is already another targetId of that target.
twoTimesTarget = true;
continue;
}
if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
validTargets = true;
if (name != null && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + '?', source, game)) {
oldTargetName = getTargetName(targetId, game);
target.remove(targetId);
// The source is still the spell on the stack
target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
break;
}
}
}
if (oldTargetName != null) {
break;
}
}
if (oldTargetName == null) {
game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + '!');
}
} while (validTargets && oldTargetName == null);
}
if (oldTargetName != null) {
game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
} else if (twoTimesTarget) {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
} else {
game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
}
return true;
}
return false;
}
use of mage.abilities.Mode in project mage by magefree.
the class StackAbilityView method updateTargets.
private void updateTargets(Game game, StackAbility ability) {
List<String> names = new ArrayList<>();
for (UUID modeId : ability.getModes().getSelectedModes()) {
Mode mode = ability.getModes().get(modeId);
if (!mode.getTargets().isEmpty()) {
addTargets(mode.getTargets(), mode.getEffects(), ability, game);
} else {
List<UUID> targetList = new ArrayList<>();
for (Effect effect : mode.getEffects()) {
TargetPointer targetPointer = effect.getTargetPointer();
if (targetPointer instanceof FixedTarget) {
targetList.add(((FixedTarget) targetPointer).getTarget());
}
}
if (!targetList.isEmpty()) {
overrideTargets(targetList);
for (UUID uuid : targetList) {
MageObject mageObject = game.getObject(uuid);
if (mageObject != null) {
if ((mageObject instanceof Card) && ((Card) mageObject).isFaceDown(game)) {
continue;
}
String newName = GameLog.getColoredObjectIdNameForTooltip(mageObject);
if (!names.contains(newName)) {
names.add(newName);
}
}
}
}
}
}
if (!names.isEmpty()) {
getRules().add("<i>Related objects: " + names + "</i>");
}
// show for modal ability, which mode was choosen
if (ability.isModal()) {
Modes modes = ability.getModes();
for (UUID modeId : modes.getSelectedModes()) {
Mode mode = modes.get(modeId);
this.rules.add("<span color='green'><i>Chosen mode: " + mode.getEffects().getText(mode) + "</i></span>");
}
}
if (HintUtils.ABILITY_HINTS_ENABLE) {
List<String> abilityHints = new ArrayList<>();
for (Hint hint : ability.getHints()) {
abilityHints.add(hint.getText(game, ability));
}
// total hints
if (!abilityHints.isEmpty()) {
rules.add(HintUtils.HINT_START_MARK);
HintUtils.appendHints(rules, abilityHints);
}
}
}
Aggregations