Add a limit to how many tokens are made (#7469)

* added a simple token limit

* updated implementation of token limit

* added token limit test
This commit is contained in:
Evan Kranzler 2021-01-31 12:55:30 -05:00 committed by GitHub
parent 9d84a22412
commit 3a4b0159a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,54 @@
package org.mage.test.cards.rules;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
/**
* @author TheElk801
*/
public class TokenLimitTest extends CardTestPlayerBase {
private static final String secure = "Secure the Wastes";
private static final String procession = "Anointed Procession";
private static final String warrior = "Warrior";
@Test
public void testOnePlayerHitsLimit() {
addCard(Zone.BATTLEFIELD, playerA, procession, 4);
addCard(Zone.BATTLEFIELD, playerA, "Plains", 34);
addCard(Zone.HAND, playerA, secure, 2);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, secure);
setChoice(playerA, "X=16");
castSpell(1, PhaseStep.POSTCOMBAT_MAIN, playerA, secure);
setChoice(playerA, "X=16");
setStopAt(1, PhaseStep.END_TURN);
execute();
assertPermanentCount(playerA, warrior, 500);
}
@Test
public void testOnePlayerHitsLimitWithOtherPlayer() {
addCard(Zone.BATTLEFIELD, playerA, procession, 4);
addCard(Zone.BATTLEFIELD, playerA, "Plains", 33);
addCard(Zone.BATTLEFIELD, playerB, "Plains", 3);
addCard(Zone.HAND, playerA, secure);
addCard(Zone.HAND, playerB, secure);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, secure);
setChoice(playerA, "X=32");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerB, secure);
setChoice(playerB, "X=3");
setStopAt(1, PhaseStep.END_TURN);
execute();
assertPermanentCount(playerA, warrior, 500);
assertPermanentCount(playerB, warrior, 2);
}
}

View file

@ -372,4 +372,14 @@ public class Battlefield implements Serializable {
return controlChanged;
}
public int countTokens(UUID controllerId) {
return field
.values()
.stream()
.filter(Objects::nonNull)
.filter(PermanentToken.class::isInstance)
.map(permanent -> permanent.isControlledBy(controllerId))
.mapToInt(x -> x ? 1 : 0)
.sum();
}
}

View file

@ -30,6 +30,7 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
private String tokenDescriptor;
private boolean expansionSetCodeChecked;
private Card copySourceCard; // the card the Token is a copy from
private static final int MAX_TOKENS_PER_GAME = 500;
// list of set codes token images are available for
protected List<String> availableImageSetCodes = new ArrayList<>();
@ -174,6 +175,15 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
CreateTokenEvent event = new CreateTokenEvent(source, controllerId, amount, this);
if (!created || !game.replaceEvent(event)) {
int currentTokens = game.getBattlefield().countTokens(event.getPlayerId());
int tokenSlots = Math.max(MAX_TOKENS_PER_GAME - currentTokens, 0);
if (event.getAmount() > tokenSlots) {
game.informPlayers(
"The token limit per player is " + MAX_TOKENS_PER_GAME + ", " + controller.getName()
+ " will only create " + tokenSlots + " tokens."
);
}
event.setAmount(Math.min(event.getAmount(), tokenSlots));
putOntoBattlefieldHelper(event, game, source, tapped, attacking, attackedPlayer, created);
return true;
}