Refactor DoIfCostPaid; Inform player on cost not paid. (#10942)

This commit is contained in:
Susucre 2023-08-26 23:29:30 +02:00 committed by GitHub
parent 51d9d8d990
commit aa71f0ba8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -100,52 +100,47 @@ public class DoIfCostPaid extends OneShotEffect {
message = CardUtil.replaceSourceName(message, mageObject.getName()); message = CardUtil.replaceSourceName(message, mageObject.getName());
boolean result = true; boolean result = true;
Outcome payOutcome = executingEffects.getOutcome(source, this.outcome); Outcome payOutcome = executingEffects.getOutcome(source, this.outcome);
if (cost.canPay(source, source, player.getId(), game) boolean canPay = cost.canPay(source, source, player.getId(), game);
&& (!optional || player.chooseUse(payOutcome, message, source, game))) { boolean didPay = false;
if (canPay && (!optional || player.chooseUse(payOutcome, message, source, game))) {
cost.clearPaid(); cost.clearPaid();
int bookmark = game.bookmarkState(); int bookmark = game.bookmarkState();
if (cost.pay(source, game, source, player.getId(), false)) { if (cost.pay(source, game, source, player.getId(), false)) {
didPay = true;
game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message); game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message);
if (!executingEffects.isEmpty()) { result &= applyEffects(game, source, executingEffects);
for (Effect effect : executingEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
player.resetStoredBookmark(game); // otherwise you can e.g. undo card drawn with Mentor of the Meek player.resetStoredBookmark(game); // otherwise you can e.g. undo card drawn with Mentor of the Meek
} else { } else {
// Paying cost was cancels so try to undo payment so far // Paying cost was cancels so try to undo payment so far
player.restoreState(bookmark, DoIfCostPaid.class.getName(), game); player.restoreState(bookmark, DoIfCostPaid.class.getName(), game);
if (!otherwiseEffects.isEmpty()) {
for (Effect effect : otherwiseEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
}
} else if (!otherwiseEffects.isEmpty()) {
for (Effect effect : otherwiseEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
} }
} }
if (!didPay) {
// Not leaking the information in the game log that the player could
// not actually pay the cost, in case it is an hidden one.
game.informPlayers(player.getLogName() + " did not pay for " + mageObject.getLogName() + " - " + message);
result &= applyEffects(game, source, otherwiseEffects);
}
return result; return result;
} }
return false; return false;
} }
private boolean applyEffects(Game game, Ability source, Effects effects) {
boolean result = true;
if (!effects.isEmpty()) {
for (Effect effect : effects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
return result;
}
protected Player getPayingPlayer(Game game, Ability source) { protected Player getPayingPlayer(Game game, Ability source) {
return game.getPlayer(source.getControllerId()); return game.getPlayer(source.getControllerId());
} }
@ -153,7 +148,7 @@ public class DoIfCostPaid extends OneShotEffect {
public Cost getCost() { public Cost getCost() {
return cost; return cost;
} }
@Override @Override
public String getText(Mode mode) { public String getText(Mode mode) {
if (!staticText.isEmpty()) { if (!staticText.isEmpty()) {