rm ColorChanger.java (unused)

mv2core BoosterGenerator and UnOpenedProduct
This commit is contained in:
Maxmtg
2013-11-19 22:54:04 +00:00
parent 6e9a316460
commit 0786012ed8
8 changed files with 36 additions and 189 deletions

View File

@@ -1470,7 +1470,8 @@ public class Card extends GameEntity implements Comparable<Card> {
if (s.equals("")) {
s = "0";
}
this.getCharacteristics().getCardColor().add(new CardColor(s, false, true));
ManaCost mc = new ManaCost(new ManaCostParser(s));
this.getCharacteristics().getCardColor().add(new CardColor(mc.getColorProfile()));
}
/**
@@ -1492,7 +1493,7 @@ public class Card extends GameEntity implements Comparable<Card> {
if (bIncrease) {
CardColor.increaseTimestamp();
}
this.getCharacteristics().getCardColor().add(new CardColor(s, addToColors, false));
this.getCharacteristics().getCardColor().add(new CardColor(s, addToColors));
return CardColor.getTimestamp();
}
@@ -1523,22 +1524,6 @@ public class Card extends GameEntity implements Comparable<Card> {
}
}
/**
* <p>
* determineColor.
* </p>
*
* @return a {@link forge.CardColor} object.
*/
public final ColorSet determineColor() {
if (this.isImmutable()) {
return ColorSet.getNullColor();
}
final List<CardColor> globalChanges = getOwner() == null ? new ArrayList<CardColor>() : getOwner().getGame().getColorChanger().getColorChanges();
return this.determineColor(globalChanges);
}
/**
* <p>
* setColor.
@@ -1570,48 +1555,21 @@ public class Card extends GameEntity implements Comparable<Card> {
* an ArrayList<CardColor>
* @return a CardColor
*/
final ColorSet determineColor(final List<CardColor> globalChanges) {
public final ColorSet determineColor() {
if (this.isImmutable()) {
return ColorSet.getNullColor();
}
List<CardColor> colorList = this.getCharacteristics().getCardColor();
byte colors = 0;
int i = this.getCharacteristics().getCardColor().size() - 1;
int j = -1;
if (globalChanges != null) {
j = globalChanges.size() - 1;
}
// if both have changes, see which one is most recent
while ((i >= 0) && (j >= 0)) {
CardColor cc = null;
if (this.getCharacteristics().getCardColor().get(i).getStamp() > globalChanges.get(j).getStamp()) {
// Card has a more recent color stamp
cc = this.getCharacteristics().getCardColor().get(i);
i--;
} else {
// Global effect has a more recent color stamp
cc = globalChanges.get(j);
j--;
}
for(int i = colorList.size() - 1;i >= 0;i--) {
final CardColor cc = colorList.get(i);
colors |= cc.getColorMask();
if (!cc.isAdditional()) {
return ColorSet.fromMask(colors);
}
}
while (i >= 0) {
final CardColor cc = this.getCharacteristics().getCardColor().get(i);
i--;
colors |= cc.getColorMask();
if (!cc.isAdditional()) {
return ColorSet.fromMask(colors);
}
}
while (j >= 0) {
final CardColor cc = globalChanges.get(j);
j--;
colors |= cc.getColorMask();
if (!cc.isAdditional()) {
return ColorSet.fromMask(colors);
}
}
return ColorSet.fromMask(colors);
}

View File

@@ -71,15 +71,11 @@ public class CardColor {
* @param baseColor
* a boolean.
*/
CardColor(final String colors, final boolean addToColors, final boolean baseColor) {
CardColor(final String colors, final boolean addToColors) {
this.additional = addToColors;
ManaCost mc = new ManaCost(new ManaCostParser(colors));
this.colorMask = mc.getColorProfile();
if (baseColor) {
this.stamp = 0;
} else {
this.stamp = CardColor.timeStamp;
}
this.stamp = CardColor.timeStamp;
}
public CardColor(byte mask) {

View File

@@ -1,109 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge;
import java.util.ArrayList;
import java.util.List;
/**
* class ColorChanger. TODO Write javadoc for this type.
*
*/
public class ColorChanger {
private final List<CardColor> globalColorChanges = new ArrayList<CardColor>();
/**
* <p>
* addColorChanges.
* </p>
*
* @param s
* a {@link java.lang.String} object.
* @param c
* a {@link forge.Card} object.
* @param addToColors
* a boolean.
* @param bIncrease
* a boolean.
* @return a long.
*/
public final long addColorChanges(final String s, final boolean addToColors, final boolean bIncrease) {
if (bIncrease) {
CardColor.increaseTimestamp();
}
this.globalColorChanges.add(new CardColor(s, addToColors, false));
return CardColor.getTimestamp();
}
/**
* <p>
* removeColorChanges.
* </p>
*
* @param s
* a {@link java.lang.String} object.
* @param c
* a {@link forge.Card} object.
* @param addTo
* a boolean.
* @param timestamp
* a long.
*/
public final void removeColorChanges(final String s, final Card c, final boolean addTo, final long timestamp) {
CardColor removeCol = null;
for (final CardColor cc : this.globalColorChanges) {
if (cc.equals(s, c, addTo, timestamp)) {
removeCol = cc;
}
}
if (removeCol != null) {
this.globalColorChanges.remove(removeCol);
}
}
/**
* <p>
* reset = clearColorChanges.
* </p>
*/
public final void reset() {
this.clearColorChanges();
}
/**
* <p>
* clearColorChanges.
* </p>
*/
public final void clearColorChanges() {
// clear the global color changes at end of each game
this.globalColorChanges.clear();
}
/**
* <p>
* getColorChanges.
* </p>
*
* @return a {@link java.util.ArrayList} object.
*/
public final List<CardColor> getColorChanges() {
return this.globalColorChanges;
}
}

View File

@@ -1,187 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.card;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import forge.Singletons;
import forge.item.PaperCard;
import forge.item.IPaperCard;
import forge.util.TextUtil;
/**
* <p>
* BoosterGenerator class.
* </p>
*
* @author Forge
* @version $Id$
*/
public class BoosterGenerator {
private final static Map<String, PrintSheet> cachedSheets = new TreeMap<String, PrintSheet>(String.CASE_INSENSITIVE_ORDER);
private static final synchronized PrintSheet getPrintSheet(String key) {
if( !cachedSheets.containsKey(key) )
cachedSheets.put(key, makeSheet(key, Singletons.getMagicDb().getCommonCards().getAllCards()));
return cachedSheets.get(key);
}
public static final List<PaperCard> getBoosterPack(SealedProductTemplate booster) {
List<PaperCard> result = new ArrayList<PaperCard>();
for(Pair<String, Integer> slot : booster.getSlots()) {
String slotType = slot.getLeft(); // add expansion symbol here?
int numCards = slot.getRight().intValue();
String[] sType = TextUtil.splitWithParenthesis(slotType, ' ');
String setCode = sType.length == 1 && booster.getEdition() != null ? booster.getEdition() : null;
String sheetKey = Singletons.getMagicDb().getEditions().contains(setCode) ? slotType.trim() + " " + setCode: slotType.trim();
PrintSheet ps = getPrintSheet(sheetKey);
result.addAll(ps.random(numCards, true));
}
return result;
}
@SuppressWarnings("unchecked")
public static final PrintSheet makeSheet(String sheetKey, Iterable<PaperCard> src) {
PrintSheet ps = new PrintSheet(sheetKey);
String[] sKey = TextUtil.splitWithParenthesis(sheetKey, ' ', 2);
Predicate<PaperCard> setPred = (Predicate<PaperCard>) (sKey.length > 1 ? IPaperCard.Predicates.printedInSets(sKey[1].split(" ")) : Predicates.alwaysTrue());
List<String> operators = new LinkedList<String>(Arrays.asList(TextUtil.splitWithParenthesis(sKey[0], ':')));
Predicate<PaperCard> extraPred = buildExtraPredicate(operators);
// source replacement operators - if one is applied setPredicate will be ignored
Iterator<String> itMod = operators.iterator();
while(itMod.hasNext()) {
String mainCode = itMod.next();
if ( mainCode.regionMatches(true, 0, "fromSheet", 0, 9)) { // custom print sheet
String sheetName = StringUtils.strip(mainCode.substring(9), "()\" ");
src = Singletons.getMagicDb().getPrintSheets().get(sheetName).toFlatList();
setPred = Predicates.alwaysTrue();
} else if (mainCode.startsWith("promo")) { // get exactly the named cards, that's a tiny inlined print sheet
String list = StringUtils.strip(mainCode.substring(5), "() ");
String[] cardNames = TextUtil.splitWithParenthesis(list, ',', '"', '"');
List<PaperCard> srcList = new ArrayList<PaperCard>();
for(String cardName: cardNames)
srcList.add(Singletons.getMagicDb().getCommonCards().getCard(cardName));
src = srcList;
setPred = Predicates.alwaysTrue();
} else
continue;
itMod.remove();
}
// only special operators should remain by now - the ones that could not be turned into one predicate
String mainCode = operators.isEmpty() ? null : operators.get(0).trim();
if( null == mainCode || mainCode.equalsIgnoreCase(BoosterSlots.ANY) ) { // no restriction on rarity
Predicate<PaperCard> predicate = Predicates.and(setPred, extraPred);
ps.addAll(Iterables.filter(src, predicate));
} else if ( mainCode.equalsIgnoreCase(BoosterSlots.UNCOMMON_RARE) ) { // for sets like ARN, where U1 cards are considered rare and U3 are uncommon
Predicate<PaperCard> predicateRares = Predicates.and(setPred, IPaperCard.Predicates.Presets.IS_RARE, extraPred);
ps.addAll(Iterables.filter(src, predicateRares));
Predicate<PaperCard> predicateUncommon = Predicates.and( setPred, IPaperCard.Predicates.Presets.IS_UNCOMMON, extraPred);
ps.addAll(Iterables.filter(src, predicateUncommon), 3);
} else if ( mainCode.equalsIgnoreCase(BoosterSlots.RARE_MYTHIC) ) {
// Typical ratio of rares to mythics is 53:15, changing to 35:10 in smaller sets.
// To achieve the desired 1:8 are all mythics are added once, and all rares added twice per print sheet.
Predicate<PaperCard> predicateMythic = Predicates.and( setPred, IPaperCard.Predicates.Presets.IS_MYTHIC_RARE, extraPred);
ps.addAll(Iterables.filter(src, predicateMythic));
Predicate<PaperCard> predicateRare = Predicates.and( setPred, IPaperCard.Predicates.Presets.IS_RARE, extraPred);
ps.addAll(Iterables.filter(src, predicateRare), 2);
} else
throw new IllegalArgumentException("Booster generator: operator could not be parsed - " + mainCode);
return ps;
}
/**
* This method also modifies passed parameter
*/
private static Predicate<PaperCard> buildExtraPredicate(List<String> operators) {
List<Predicate<PaperCard>> conditions = new ArrayList<Predicate<PaperCard>>();
Iterator<String> itOp = operators.iterator();
while(itOp.hasNext()) {
String operator = itOp.next();
if(StringUtils.isEmpty(operator)) {
itOp.remove();
continue;
}
if(operator.endsWith("s"))
operator = operator.substring(0, operator.length()-1);
boolean invert = operator.charAt(0) == '!';
if( invert ) operator = operator.substring(1);
Predicate<PaperCard> toAdd = null;
if( operator.equalsIgnoreCase("dfc") ) { toAdd = Predicates.compose(CardRulesPredicates.splitType(CardSplitType.Transform), PaperCard.FN_GET_RULES);
} else if ( operator.equalsIgnoreCase(BoosterSlots.LAND) ) { toAdd = Predicates.compose(CardRulesPredicates.Presets.IS_LAND, PaperCard.FN_GET_RULES);
} else if ( operator.equalsIgnoreCase(BoosterSlots.BASIC_LAND)) { toAdd = IPaperCard.Predicates.Presets.IS_BASIC_LAND;
} else if ( operator.equalsIgnoreCase(BoosterSlots.TIME_SHIFTED)) { toAdd = IPaperCard.Predicates.Presets.IS_SPECIAL;
} else if ( operator.equalsIgnoreCase(BoosterSlots.MYTHIC)) { toAdd = IPaperCard.Predicates.Presets.IS_MYTHIC_RARE;
} else if ( operator.equalsIgnoreCase(BoosterSlots.RARE)) { toAdd = IPaperCard.Predicates.Presets.IS_RARE;
} else if ( operator.equalsIgnoreCase(BoosterSlots.UNCOMMON)) { toAdd = IPaperCard.Predicates.Presets.IS_UNCOMMON;
} else if ( operator.equalsIgnoreCase(BoosterSlots.COMMON)) { toAdd = IPaperCard.Predicates.Presets.IS_COMMON;
} else if ( operator.startsWith("name(") ) {
operator = StringUtils.strip(operator.substring(4), "() ");
String[] cardNames = TextUtil.splitWithParenthesis(operator, ',', '"', '"');
toAdd = IPaperCard.Predicates.names(Lists.newArrayList(cardNames));
}
if(toAdd == null)
continue;
else
itOp.remove();
if( invert )
toAdd = Predicates.not(toAdd);
conditions.add(toAdd);
}
if( conditions.isEmpty() )
return Predicates.alwaysTrue();
return Predicates.and(conditions);
}
}

View File

@@ -1,83 +0,0 @@
package forge.card;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import forge.Singletons;
import forge.item.PaperCard;
import forge.util.ItemPoolView;
public class UnOpenedProduct implements IUnOpenedProduct {
private final SealedProductTemplate tpl;
private final Map<String, PrintSheet> sheets;
private boolean poolLimited = false; // if true after successful generation cards are removed from printsheets.
public final boolean isPoolLimited() {
return poolLimited;
}
public final void setLimitedPool(boolean considerNumbersInPool) {
this.poolLimited = considerNumbersInPool; // TODO: Add 0 to parameter's name.
}
// Means to select from all unique cards (from base game, ie. no schemes or avatars)
public UnOpenedProduct(SealedProductTemplate template) {
tpl = template;
sheets = null;
}
// Invoke this constructor only if you are sure that the pool is not equal to deafult carddb
public UnOpenedProduct(SealedProductTemplate template, ItemPoolView<PaperCard> pool) {
this(template, pool.toFlatList());
}
public UnOpenedProduct(SealedProductTemplate template, Iterable<PaperCard> cards) {
tpl = template;
sheets = new TreeMap<String, PrintSheet>();
prebuildSheets(cards);
}
public UnOpenedProduct(SealedProductTemplate sealedProductTemplate, Predicate<PaperCard> filterPrinted) {
this(sealedProductTemplate, Iterables.filter(Singletons.getMagicDb().getCommonCards().getAllCards(), filterPrinted));
}
private void prebuildSheets(Iterable<PaperCard> sourceList) {
for(Pair<String, Integer> cc : tpl.getSlots()) {
sheets.put(cc.getKey(), BoosterGenerator.makeSheet(cc.getKey(), sourceList));
}
}
@Override
public List<PaperCard> get() {
return sheets == null ? BoosterGenerator.getBoosterPack(tpl) : getBoosterPack();
}
// If they request cards from an arbitrary pool, there's no use to cache printsheets.
private final List<PaperCard> getBoosterPack() {
List<PaperCard> result = new ArrayList<PaperCard>();
for(Pair<String, Integer> slot : tpl.getSlots()) {
PrintSheet ps = sheets.get(slot.getLeft());
if(ps.isEmpty() && poolLimited ) {
throw new IllegalStateException("The cardpool has been depleted and has no more cards for slot " + slot.getKey());
}
List<PaperCard> foundCards = ps.random(slot.getRight().intValue(), true);
if(poolLimited)
ps.removeAll(foundCards);
result.addAll(foundCards);
}
return result;
}
}

View File

@@ -27,7 +27,6 @@ import com.google.common.eventbus.EventBus;
import forge.Card;
import forge.CardLists;
import forge.ColorChanger;
import forge.FThreads;
import forge.GameLog;
import forge.StaticEffects;
@@ -72,7 +71,6 @@ public class Game {
private final ReplacementHandler replacementHandler = new ReplacementHandler(this);
private final EventBus events = new EventBus("game events");
private final GameLog gameLog = new GameLog();
private final ColorChanger colorChanger = new ColorChanger();
private final Zone stackZone = new Zone(ZoneType.Stack, this);
@@ -437,14 +435,6 @@ public class Game {
return all;
}
/**
* TODO: Write javadoc for this method.
* @return
*/
public ColorChanger getColorChanger() {
return colorChanger;
}
public final GameAction getAction() {
return action;
}