mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 10:40:06 -08:00
rewrite some code to streams
This commit is contained in:
parent
cd0c0b3bdd
commit
877a355a7d
12 changed files with 108 additions and 100 deletions
|
|
@ -0,0 +1,40 @@
|
|||
package org.mage.test.combat;
|
||||
|
||||
import mage.constants.PhaseStep;
|
||||
import mage.constants.Zone;
|
||||
import org.junit.Test;
|
||||
import org.mage.test.serverside.base.CardTestPlayerBase;
|
||||
|
||||
public class FirstStrikeTest extends CardTestPlayerBase {
|
||||
|
||||
|
||||
@Test
|
||||
public void firstStrikeAttacker(){
|
||||
addCard(Zone.BATTLEFIELD, playerA, "Silver Knight", 1);
|
||||
addCard(Zone.BATTLEFIELD, playerB, "Grizzly Bears", 1);
|
||||
|
||||
attack(1, playerA, "Silver Knight");
|
||||
block(1, playerB, "Grizzly Bears", "Silver Knight");
|
||||
|
||||
setStopAt(1, PhaseStep.POSTCOMBAT_MAIN);
|
||||
execute();
|
||||
|
||||
assertGraveyardCount(playerB, "Grizzly Bears", 1);
|
||||
assertGraveyardCount(playerA, "Silver Knight", 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void firstStrikeBlocker(){
|
||||
addCard(Zone.BATTLEFIELD, playerB, "Silver Knight", 1);
|
||||
addCard(Zone.BATTLEFIELD, playerA, "Grizzly Bears", 1);
|
||||
|
||||
attack(1, playerA, "Grizzly Bears");
|
||||
block(1, playerB, "Silver Knight", "Grizzly Bears");
|
||||
|
||||
setStopAt(1, PhaseStep.POSTCOMBAT_MAIN);
|
||||
execute();
|
||||
|
||||
assertGraveyardCount(playerA, "Grizzly Bears", 1);
|
||||
assertGraveyardCount(playerB, "Silver Knight", 0);
|
||||
}
|
||||
}
|
||||
|
|
@ -59,7 +59,7 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
|
|||
|
||||
protected UUID ownerId;
|
||||
protected String cardNumber;
|
||||
public String expansionSetCode;
|
||||
protected String expansionSetCode;
|
||||
protected String tokenSetCode;
|
||||
protected String tokenDescriptor;
|
||||
protected Rarity rarity;
|
||||
|
|
|
|||
|
|
@ -28,13 +28,8 @@ public class AbilityPredicate implements Predicate<MageObject> {
|
|||
} else {
|
||||
abilities = input.getAbilities();
|
||||
}
|
||||
|
||||
for (Ability ability : abilities) {
|
||||
if (abilityClass.equals(ability.getClass())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return abilities.stream().anyMatch(ability -> ability.getClass().equals(abilityClass));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -16,12 +16,8 @@ public class VariableManaCostPredicate implements Predicate<MageObject> {
|
|||
|
||||
@Override
|
||||
public boolean apply(MageObject input, Game game) {
|
||||
for (ManaCost manaCost : input.getManaCost()) {
|
||||
if (manaCost instanceof VariableManaCost) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return input.getManaCost().stream().anyMatch(manaCost -> manaCost instanceof VariableManaCost);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ public class OwnerIdPredicate implements Predicate<Card> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(Card input, Game game) {
|
||||
return ownerId.equals(input.getOwnerId());
|
||||
public boolean apply(Card card, Game game) {
|
||||
return card.isOwnedBy(ownerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -18,11 +18,14 @@ public class PlayerCanGainLifePredicate implements ObjectSourcePlayerPredicate<O
|
|||
@Override
|
||||
public boolean apply(ObjectSourcePlayer<Player> input, Game game) {
|
||||
Player player = input.getObject();
|
||||
return player.isCanGainLife();
|
||||
if(player != null) {
|
||||
return player.isCanGainLife();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Player can gain live";
|
||||
return "Player can gain life";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,12 +17,8 @@ public class CounterAnyPredicate implements Predicate<Permanent> {
|
|||
|
||||
@Override
|
||||
public boolean apply(Permanent input, Game game) {
|
||||
for (Counter counter: input.getCounters(game).values()) {
|
||||
if (counter.getCount()> 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return input.getCounters(game).values().stream().anyMatch(counter -> counter.getCount() > 0);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
|
||||
package mage.filter.predicate.permanent;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import mage.MageObject;
|
||||
import mage.filter.predicate.Predicate;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
|
@ -14,13 +17,11 @@ public class EnchantedPredicate implements Predicate<Permanent> {
|
|||
|
||||
@Override
|
||||
public boolean apply(Permanent input, Game game) {
|
||||
for (UUID attachmentId : input.getAttachments()) {
|
||||
Permanent attachment = game.getPermanent(attachmentId);
|
||||
if (attachment != null && attachment.isEnchantment()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return input.getAttachments()
|
||||
.stream()
|
||||
.map(game::getPermanent)
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(MageObject::isEnchantment);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -5,27 +5,26 @@
|
|||
*/
|
||||
package mage.filter.predicate.permanent;
|
||||
|
||||
import java.util.UUID;
|
||||
import mage.constants.SubType;
|
||||
import mage.filter.predicate.Predicate;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author LevelX2
|
||||
*/
|
||||
public class EquippedPredicate implements Predicate<Permanent> {
|
||||
|
||||
@Override
|
||||
public boolean apply(Permanent input, Game game) {
|
||||
for (UUID attachmentId : input.getAttachments()) {
|
||||
Permanent attachment = game.getPermanent(attachmentId);
|
||||
if (attachment != null && attachment.hasSubtype(SubType.EQUIPMENT, game)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return input.getAttachments()
|
||||
.stream()
|
||||
.map(game::getPermanent)
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(attachment -> attachment.hasSubtype(SubType.EQUIPMENT, game));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
|
||||
package mage.game.combat;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import mage.MageObject;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.effects.RequirementEffect;
|
||||
|
|
@ -33,6 +31,9 @@ import mage.util.Copyable;
|
|||
import mage.util.trace.TraceUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
|
|
@ -47,7 +48,7 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
|
||||
protected List<CombatGroup> groups = new ArrayList<>();
|
||||
protected Map<UUID, CombatGroup> blockingGroups = new HashMap<>();
|
||||
// player and plainswalker ids
|
||||
// player and planeswalker ids
|
||||
protected Set<UUID> defenders = new HashSet<>();
|
||||
// how many creatures attack defending player
|
||||
protected Map<UUID, Set<UUID>> numberCreaturesDefenderAttackedBy = new HashMap<>();
|
||||
|
|
@ -310,31 +311,28 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
for (UUID targetId : target.getTargets()) {
|
||||
Permanent permanent = game.getPermanent(targetId);
|
||||
if (permanent != null) {
|
||||
if (permanent != null) {
|
||||
|
||||
for (UUID bandedId : attacker.getBandedCards()) {
|
||||
permanent.addBandedCard(bandedId);
|
||||
Permanent banded = game.getPermanent(bandedId);
|
||||
if (banded != null) {
|
||||
banded.addBandedCard(targetId);
|
||||
}
|
||||
}
|
||||
permanent.addBandedCard(creatureId);
|
||||
attacker.addBandedCard(targetId);
|
||||
if (!permanent.getAbilities().containsKey(BandingAbility.getInstance().getId())) {
|
||||
filter.add(new AbilityPredicate(BandingAbility.class));
|
||||
for (UUID bandedId : attacker.getBandedCards()) {
|
||||
permanent.addBandedCard(bandedId);
|
||||
Permanent banded = game.getPermanent(bandedId);
|
||||
if (banded != null) {
|
||||
banded.addBandedCard(targetId);
|
||||
}
|
||||
}
|
||||
permanent.addBandedCard(creatureId);
|
||||
attacker.addBandedCard(targetId);
|
||||
if (!permanent.getAbilities().containsKey(BandingAbility.getInstance().getId())) {
|
||||
filter.add(new AbilityPredicate(BandingAbility.class));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isBanded) {
|
||||
StringBuilder sb = new StringBuilder(player.getLogName()).append(" formed a band with ").append((attacker.getBandedCards().size() + 1) + " creatures: ");
|
||||
sb.append(attacker.getLogName());
|
||||
int i = 0;
|
||||
for (UUID id : attacker.getBandedCards()) {
|
||||
i++;
|
||||
sb.append(", ");
|
||||
Permanent permanent = game.getPermanent(id);
|
||||
if (permanent != null) {
|
||||
|
|
@ -415,7 +413,6 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param player
|
||||
* @param game
|
||||
* @return true if the attack with that set of creatures and attacked
|
||||
|
|
@ -486,7 +483,7 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
* Handle the blocker selection process
|
||||
*
|
||||
* @param blockController player that controlls how to block, if null the
|
||||
* defender is the controller
|
||||
* defender is the controller
|
||||
* @param game
|
||||
*/
|
||||
public void selectBlockers(Player blockController, Game game) {
|
||||
|
|
@ -532,7 +529,6 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
|
||||
/**
|
||||
* Add info about attacker blocked by blocker to the game log
|
||||
*
|
||||
*/
|
||||
private void logBlockerInfo(Player defender, Game game) {
|
||||
boolean shownDefendingPlayer = game.getPlayers().size() < 3; // only two players no ned to sow the attacked player
|
||||
|
|
@ -1191,9 +1187,7 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
}
|
||||
break;
|
||||
case MULTIPLE:
|
||||
for (UUID opponentId : game.getOpponents(attackingPlayerId)) {
|
||||
attackablePlayers.add(opponentId);
|
||||
}
|
||||
attackablePlayers.addAll(game.getOpponents(attackingPlayerId));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -1281,10 +1275,10 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
if (defenderAttackedBy.size() >= defendingPlayer.getMaxAttackedBy()) {
|
||||
Player attackingPlayer = game.getPlayer(game.getControllerId(attackerId));
|
||||
if (attackingPlayer != null && !game.isSimulation()) {
|
||||
game.informPlayer(attackingPlayer, new StringBuilder("No more than ")
|
||||
.append(CardUtil.numberToText(defendingPlayer.getMaxAttackedBy()))
|
||||
.append(" creatures can attack ")
|
||||
.append(defendingPlayer.getLogName()).toString());
|
||||
game.informPlayer(attackingPlayer, "No more than " +
|
||||
CardUtil.numberToText(defendingPlayer.getMaxAttackedBy()) +
|
||||
" creatures can attack " +
|
||||
defendingPlayer.getLogName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1314,7 +1308,7 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
* @param playerId
|
||||
* @param game
|
||||
* @param solveBanding check whether also add creatures banded with
|
||||
* attackerId
|
||||
* attackerId
|
||||
*/
|
||||
public void addBlockingGroup(UUID blockerId, UUID attackerId, UUID playerId, Game game, boolean solveBanding) {
|
||||
Permanent blocker = game.getPermanent(blockerId);
|
||||
|
|
@ -1325,9 +1319,7 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
// add all blocked attackers
|
||||
for (CombatGroup group : groups) {
|
||||
if (group.getBlockers().contains(blockerId)) {
|
||||
for (UUID attacker : group.attackers) {
|
||||
newGroup.attackers.add(attacker);
|
||||
}
|
||||
newGroup.attackers.addAll(group.attackers);
|
||||
}
|
||||
}
|
||||
blockingGroups.put(blockerId, newGroup);
|
||||
|
|
@ -1423,12 +1415,8 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
}
|
||||
|
||||
public boolean hasFirstOrDoubleStrike(Game game) {
|
||||
for (CombatGroup group : groups) {
|
||||
if (group.hasFirstOrDoubleStrike(game)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return groups.stream()
|
||||
.anyMatch(group -> group.hasFirstOrDoubleStrike(game));
|
||||
}
|
||||
|
||||
public CombatGroup findGroup(UUID attackerId) {
|
||||
|
|
@ -1449,7 +1437,7 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
return null;
|
||||
}
|
||||
|
||||
// public int totalUnblockedDamage(Game game) {
|
||||
// public int totalUnblockedDamage(Game game) {
|
||||
// int total = 0;
|
||||
// for (CombatGroup group : groups) {
|
||||
// if (group.getBlockers().isEmpty()) {
|
||||
|
|
@ -1482,7 +1470,6 @@ public class Combat implements Serializable, Copyable<Combat> {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param attackerId
|
||||
* @return uuid of defending player or planeswalker
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package mage.game.combat;
|
|||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import mage.abilities.common.ControllerAssignCombatDamageToBlockersAbility;
|
||||
import mage.abilities.common.ControllerDivideCombatDamageAbility;
|
||||
import mage.abilities.common.DamageAsThoughNotBlockedAbility;
|
||||
|
|
@ -61,19 +63,11 @@ public class CombatGroup implements Serializable, Copyable<CombatGroup> {
|
|||
}
|
||||
|
||||
public boolean hasFirstOrDoubleStrike(Game game) {
|
||||
for (UUID permId : attackers) {
|
||||
Permanent attacker = game.getPermanent(permId);
|
||||
if (attacker != null && hasFirstOrDoubleStrike(attacker)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (UUID permId : blockers) {
|
||||
Permanent blocker = game.getPermanent(permId);
|
||||
if (blocker != null && hasFirstOrDoubleStrike(blocker)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return Stream.concat(attackers.stream(), blockers.stream())
|
||||
.map(id -> game.getPermanent(id))
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(this::hasFirstOrDoubleStrike);
|
||||
|
||||
}
|
||||
|
||||
public UUID getDefenderId() {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
package mage.util;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import mage.MageObject;
|
||||
import mage.Mana;
|
||||
import mage.abilities.Ability;
|
||||
|
|
@ -354,13 +356,8 @@ public final class CardUtil {
|
|||
}
|
||||
|
||||
public static boolean checkNumeric(String s) {
|
||||
return s.chars().allMatch(Character::isDigit);
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (!Character.isDigit(s.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -442,9 +439,9 @@ public final class CardUtil {
|
|||
public static String getObjectZoneString(String text, MageObject mageObject, Game game) {
|
||||
int zoneChangeCounter = 0;
|
||||
if (mageObject instanceof Permanent) {
|
||||
zoneChangeCounter = ((Permanent) mageObject).getZoneChangeCounter(game);
|
||||
zoneChangeCounter = mageObject.getZoneChangeCounter(game);
|
||||
} else if (mageObject instanceof Card) {
|
||||
zoneChangeCounter = ((Card) mageObject).getZoneChangeCounter(game);
|
||||
zoneChangeCounter = mageObject.getZoneChangeCounter(game);
|
||||
}
|
||||
return getObjectZoneString(text, mageObject.getId(), game, zoneChangeCounter, false);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue