- Improving performance Mana.toString().

- Slightly improved performance of  getPossiblePayCombinations().
- Flattened getNetMana slightly.

For #7710.
This commit is contained in:
Alex Vasile 2022-05-16 21:03:39 -06:00
parent 47db2b5721
commit 5e26066b24
3 changed files with 82 additions and 61 deletions

View file

@ -9,6 +9,7 @@ import org.apache.log4j.Logger;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
@ -540,39 +541,47 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
any = 0;
}
/**
* Used in order to reorder mana combinations so they're returned in the order found on cards.
*/
private static final Map<String, String> colorLetterMap = new HashMap<>();
static {
colorLetterMap.put("wr", "rw");
colorLetterMap.put("wg", "gw");
colorLetterMap.put("ug", "gu");
colorLetterMap.put("wrg", "rgw");
colorLetterMap.put("wug", "gwu");
colorLetterMap.put("wur", "urw");
colorLetterMap.put("urg", "gur");
colorLetterMap.put("ubg", "bgu");
colorLetterMap.put("wbr", "rwb");
colorLetterMap.put("wbrg", "brgw");
colorLetterMap.put("wurg", "rgwu");
colorLetterMap.put("wubg", "gwub");
colorLetterMap.put("WR", "RW");
colorLetterMap.put("WG", "GW");
colorLetterMap.put("UG", "GU");
colorLetterMap.put("WRG", "RGW");
colorLetterMap.put("WUG", "GWU");
colorLetterMap.put("WUR", "URW");
colorLetterMap.put("URG", "GUR");
colorLetterMap.put("UBG", "BGU");
colorLetterMap.put("WBG", "RWB");
colorLetterMap.put("WBRG", "BRGW");
colorLetterMap.put("WURG", "RGWU");
colorLetterMap.put("WUBG", "GWUB");
}
/**
* The use of a StringBuilder was
*
* @return
*/
private String getColorsInOrder() {
StringBuilder sb = new StringBuilder();
if (white > 0) {
sb.append('w');
sb.append("W");
}
if (blue > 0) {
sb.append('u');
sb.append("U");
}
if (black > 0) {
sb.append('b');
sb.append("B");
}
if (red > 0) {
sb.append('r');
sb.append("R");
}
if (green > 0) {
sb.append('g');
sb.append("G");
}
String manaString = sb.toString();
return colorLetterMap.getOrDefault(manaString, manaString);
@ -580,15 +589,15 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
private int colorCharToAmount(char color) {
switch (color) {
case 'w':
case 'W':
return white;
case 'u':
case 'U':
return blue;
case 'b':
case 'B':
return black;
case 'r':
case 'R':
return red;
case 'g':
case 'G':
return green;
}
return 0;
@ -602,9 +611,11 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
@Override
public String toString() {
StringBuilder sbMana = new StringBuilder();
if (generic > 0) {
sbMana.append('{').append(generic).append('}');
}
// normal mana
if (colorless < 20) {
for (int i = 0; i < colorless; i++) {
@ -613,6 +624,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} else {
sbMana.append(colorless).append("{C}");
}
String colorsInOrder = getColorsInOrder();
for (char c : colorsInOrder.toCharArray()) {
int amount = colorCharToAmount(c);
@ -624,6 +636,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
sbMana.append(amount).append('{').append(c).append('}');
}
}
if (any < 20) {
for (int i = 0; i < any; i++) {
sbMana.append("{Any}");
@ -632,7 +645,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
sbMana.append(any).append("{Any}");
}
return sbMana.toString().toUpperCase().replace("ANY", "Any");
return sbMana.toString();
}
/**
@ -1159,14 +1172,16 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
/**
* Returns the mana that is more colored or has a greater amount but does
* not contain one less mana in any color but generic if you call with
* {1}{W}{R} and {G}{W}{R} you get back {G}{W}{R} if you call with {G}{W}{R}
* and {G}{W}{R} you get back {G}{W}{R} if you call with {G}{W}{B} and
* {G}{W}{R} you get back null
* not contain one less mana in any color but generic.
*
* @param mana1
* @param mana2
* @return
* Examples:
* {1}{W}{R} and {G}{W}{R} -> {G}{W}{R}
* {G}{W}{R} and {G}{W}{R} -> {G}{W}{R}
* {G}{W}{B} and {G}{W}{R} -> null
*
* @param mana1 The 1st mana to compare.
* @param mana2 The 2nd mana to compare.
* @return The greater of the two manas, or null if they're the same
*/
public static Mana getMoreValuableMana(final Mana mana1, final Mana mana2) {
String conditionString1 = "";

View file

@ -80,30 +80,34 @@ public class AddManaInAnyCombinationEffect extends ManaEffect {
@Override
public List<Mana> getNetMana(Game game, Ability source) {
List<Mana> netMana = new ArrayList<>();
if (game != null) {
if (game.inCheckPlayableState()) {
int count = netAmount.calculate(game, source, this);
if (count > 0) {
// add color combinations
ManaOptions allPossibleMana = new ManaOptions();
for (int i = 0; i < count; ++i) {
ManaOptions currentPossibleMana = new ManaOptions();
for (ColoredManaSymbol coloredManaSymbol : manaSymbols) {
currentPossibleMana.add(new Mana(coloredManaSymbol));
}
allPossibleMana.addMana(currentPossibleMana);
}
allPossibleMana.removeDuplicated();
return allPossibleMana.stream().collect(Collectors.toList());
}
} else {
int amountOfManaLeft = amount.calculate(game, source, this);
if (amountOfManaLeft > 0) {
netMana.add(Mana.AnyMana(amountOfManaLeft));
}
}
if (game == null) {
return netMana;
}
if (game.inCheckPlayableState()) {
int count = netAmount.calculate(game, source, this);
if (count <= 0) {
return netMana;
}
// add color combinations
ManaOptions allPossibleMana = new ManaOptions();
for (int i = 0; i < count; ++i) {
ManaOptions currentPossibleMana = new ManaOptions();
for (ColoredManaSymbol coloredManaSymbol : manaSymbols) {
currentPossibleMana.add(new Mana(coloredManaSymbol));
}
allPossibleMana.addMana(currentPossibleMana);
}
allPossibleMana.removeDuplicated();
return allPossibleMana.stream().collect(Collectors.toList());
} else {
int amountOfManaLeft = amount.calculate(game, source, this);
if (amountOfManaLeft > 0) {
netMana.add(Mana.AnyMana(amountOfManaLeft));
}
return netMana;
}
return netMana;
}
@Override

View file

@ -466,7 +466,7 @@ public class ManaOptions extends ArrayList<Mana> {
}
/**
* @param number of generic mana
* @param manaCost
* @param manaAvailable
* @return
*/
@ -498,32 +498,34 @@ public class ManaOptions extends ArrayList<Mana> {
for (Mana existingMana : existingManas) {
Mana manaToPayFrom = manaAfterFixedPayment.copy();
manaToPayFrom.subtract(existingMana);
if (manaToPayFrom.getBlack() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.BlackMana(1).toString())) {
String manaString = existingMana.toString();
if (manaToPayFrom.getBlack() > 0 && !payCombinationsStrings.contains(manaString + Mana.BlackMana(1))) {
manaToPayFrom.subtract(Mana.BlackMana(1));
ManaOptions.addManaCombination(Mana.BlackMana(1), existingMana, payCombinations, payCombinationsStrings);
}
if (manaToPayFrom.getBlue() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.BlueMana(1).toString())) {
if (manaToPayFrom.getBlue() > 0 && !payCombinationsStrings.contains(manaString + Mana.BlueMana(1).toString())) {
manaToPayFrom.subtract(Mana.BlueMana(1));
ManaOptions.addManaCombination(Mana.BlueMana(1), existingMana, payCombinations, payCombinationsStrings);
}
if (manaToPayFrom.getGreen() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.GreenMana(1).toString())) {
if (manaToPayFrom.getGreen() > 0 && !payCombinationsStrings.contains(manaString + Mana.GreenMana(1).toString())) {
manaToPayFrom.subtract(Mana.GreenMana(1));
ManaOptions.addManaCombination(Mana.GreenMana(1), existingMana, payCombinations, payCombinationsStrings);
}
if (manaToPayFrom.getRed() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.RedMana(1).toString())) {
if (manaToPayFrom.getRed() > 0 && !payCombinationsStrings.contains(manaString + Mana.RedMana(1).toString())) {
manaToPayFrom.subtract(Mana.RedMana(1));
ManaOptions.addManaCombination(Mana.RedMana(1), existingMana, payCombinations, payCombinationsStrings);
}
if (manaToPayFrom.getWhite() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.WhiteMana(1).toString())) {
if (manaToPayFrom.getWhite() > 0 && !payCombinationsStrings.contains(manaString + Mana.WhiteMana(1).toString())) {
manaToPayFrom.subtract(Mana.WhiteMana(1));
ManaOptions.addManaCombination(Mana.WhiteMana(1), existingMana, payCombinations, payCombinationsStrings);
}
if (manaToPayFrom.getColorless() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.ColorlessMana(1).toString())) {
if (manaToPayFrom.getColorless() > 0 && !payCombinationsStrings.contains(manaString + Mana.ColorlessMana(1).toString())) {
manaToPayFrom.subtract(Mana.ColorlessMana(1));
ManaOptions.addManaCombination(Mana.ColorlessMana(1), existingMana, payCombinations, payCombinationsStrings);
}
// Pay with any only needed if colored payment was not possible
if (payCombinations.isEmpty() && manaToPayFrom.getAny() > 0 && !payCombinationsStrings.contains(existingMana.toString() + Mana.AnyMana(1).toString())) {
if (payCombinations.isEmpty() && manaToPayFrom.getAny() > 0 && !payCombinationsStrings.contains(manaString + Mana.AnyMana(1).toString())) {
manaToPayFrom.subtract(Mana.AnyMana(1));
ManaOptions.addManaCombination(Mana.AnyMana(1), existingMana, payCombinations, payCombinationsStrings);
}