mirror of
https://github.com/magefree/mage.git
synced 2026-01-09 04:12:14 -08:00
* Rework of booster generation to reduce DB load while starting a tournament.
This commit is contained in:
parent
354daf736e
commit
b4636b2ce1
9 changed files with 174 additions and 111 deletions
|
|
@ -189,7 +189,7 @@ public class CardsImpl extends LinkedHashSet<UUID> implements Cards, Serializabl
|
|||
if (card != null) {
|
||||
cards.add(card);
|
||||
} else {
|
||||
// this bug seems to happen, if cause is removed, this check can also be removed
|
||||
// seems like this can happen during the cancelation of a game
|
||||
logger.error("Card not found cardId: " + cardId + " gameId: " + game.getId() );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ package mage.cards;
|
|||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import mage.cards.repository.CardCriteria;
|
||||
|
|
@ -66,6 +67,8 @@ public abstract class ExpansionSet implements Serializable {
|
|||
protected String packageName;
|
||||
protected int maxCardNumberInBooster;
|
||||
|
||||
protected final EnumMap<Rarity, List<CardInfo>> savedCards;
|
||||
|
||||
public ExpansionSet(String name, String code, String packageName, Date releaseDate, SetType setType) {
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
|
|
@ -73,6 +76,7 @@ public abstract class ExpansionSet implements Serializable {
|
|||
this.setType = setType;
|
||||
this.packageName = packageName;
|
||||
this.maxCardNumberInBooster = Integer.MAX_VALUE;
|
||||
savedCards = new EnumMap<>(Rarity.class);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
|
@ -113,50 +117,28 @@ public abstract class ExpansionSet implements Serializable {
|
|||
if (!hasBoosters) {
|
||||
return booster;
|
||||
}
|
||||
|
||||
List<CardInfo> common = getCommon();
|
||||
|
||||
CardCriteria criteria = new CardCriteria();
|
||||
criteria.setCodes(this.code).rarities(Rarity.UNCOMMON).doubleFaced(false);
|
||||
if (maxCardNumberInBooster != Integer.MAX_VALUE) {
|
||||
criteria.maxCardNumber(maxCardNumberInBooster);
|
||||
}
|
||||
List<CardInfo> uncommon = CardRepository.instance.findCards(criteria);
|
||||
|
||||
criteria = new CardCriteria();
|
||||
criteria.setCodes(this.code).rarities(Rarity.RARE).doubleFaced(false);
|
||||
if (maxCardNumberInBooster != Integer.MAX_VALUE) {
|
||||
criteria.maxCardNumber(maxCardNumberInBooster);
|
||||
}
|
||||
List<CardInfo> rare = CardRepository.instance.findCards(criteria);
|
||||
|
||||
criteria = new CardCriteria();
|
||||
criteria.setCodes(this.code).rarities(Rarity.MYTHIC).doubleFaced(false);
|
||||
if (maxCardNumberInBooster != Integer.MAX_VALUE) {
|
||||
criteria.maxCardNumber(maxCardNumberInBooster);
|
||||
}
|
||||
List<CardInfo> mythic = CardRepository.instance.findCards(criteria);
|
||||
|
||||
|
||||
if (numBoosterLands > 0) {
|
||||
criteria = new CardCriteria();
|
||||
criteria.setCodes(!hasBasicLands && parentSet != null ? parentSet.code : this.code).rarities(Rarity.LAND).doubleFaced(false);
|
||||
List<CardInfo> basicLand = CardRepository.instance.findCards(criteria);
|
||||
List<CardInfo> basicLands = getCardsByRarity(Rarity.LAND);
|
||||
for (int i = 0; i < numBoosterLands; i++) {
|
||||
addToBooster(booster, basicLand);
|
||||
addToBooster(booster, basicLands);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
List<CardInfo> commons = getCardsByRarity(Rarity.COMMON);
|
||||
for (int i = 0; i < numBoosterCommon; i++) {
|
||||
addToBooster(booster, common);
|
||||
addToBooster(booster, commons);
|
||||
}
|
||||
List<CardInfo> uncommons = getCardsByRarity(Rarity.UNCOMMON);
|
||||
for (int i = 0; i < numBoosterUncommon; i++) {
|
||||
addToBooster(booster, uncommon);
|
||||
addToBooster(booster, uncommons);
|
||||
}
|
||||
List<CardInfo> rares = getCardsByRarity(Rarity.RARE);
|
||||
List<CardInfo> mythics = getCardsByRarity(Rarity.MYTHIC);
|
||||
for (int i = 0; i < numBoosterRare; i++) {
|
||||
if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) {
|
||||
addToBooster(booster, mythic);
|
||||
addToBooster(booster, mythics);
|
||||
} else {
|
||||
addToBooster(booster, rare);
|
||||
addToBooster(booster, rares);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -189,7 +171,7 @@ public abstract class ExpansionSet implements Serializable {
|
|||
if (specialCards > 0) {
|
||||
for (int i = 0; i < numBoosterSpecial; i++) {
|
||||
if (rnd.nextInt(15) < 10) {
|
||||
if (specialCommon != null) {
|
||||
if (specialCommon != null && !specialCommon.isEmpty()) {
|
||||
addToBooster(booster, specialCommon);
|
||||
} else {
|
||||
i--;
|
||||
|
|
@ -197,7 +179,7 @@ public abstract class ExpansionSet implements Serializable {
|
|||
continue;
|
||||
}
|
||||
if (rnd.nextInt(4) < 3) {
|
||||
if (specialUncommon != null) {
|
||||
if (specialUncommon != null && !specialUncommon.isEmpty()) {
|
||||
addToBooster(booster, specialUncommon);
|
||||
} else {
|
||||
i--;
|
||||
|
|
@ -205,15 +187,15 @@ public abstract class ExpansionSet implements Serializable {
|
|||
continue;
|
||||
}
|
||||
if (rnd.nextInt(8) < 7) {
|
||||
if (specialRare != null) {
|
||||
if (specialRare != null && !specialRare.isEmpty()) {
|
||||
addToBooster(booster, specialRare);
|
||||
} else {
|
||||
i--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (specialMythic != null) {
|
||||
if (specialBonus != null) {
|
||||
if (specialMythic != null && !specialMythic.isEmpty()) {
|
||||
if (specialBonus != null && !specialBonus.isEmpty()) {
|
||||
if (rnd.nextInt(3) < 2) {
|
||||
addToBooster(booster, specialMythic);
|
||||
continue;
|
||||
|
|
@ -225,7 +207,7 @@ public abstract class ExpansionSet implements Serializable {
|
|||
} else {
|
||||
i--;
|
||||
}
|
||||
if (specialBonus != null) {
|
||||
if (specialBonus != null && !specialBonus.isEmpty()) {
|
||||
addToBooster(booster, specialBonus);
|
||||
}
|
||||
}
|
||||
|
|
@ -276,13 +258,25 @@ public abstract class ExpansionSet implements Serializable {
|
|||
return hasBasicLands;
|
||||
}
|
||||
|
||||
public List<CardInfo> getCommon() {
|
||||
CardCriteria criteria = new CardCriteria();
|
||||
criteria.setCodes(this.code).rarities(Rarity.COMMON).doubleFaced(false);
|
||||
if (maxCardNumberInBooster != Integer.MAX_VALUE) {
|
||||
criteria.maxCardNumber(maxCardNumberInBooster);
|
||||
public List<CardInfo> getCardsByRarity(Rarity rarity) {
|
||||
List<CardInfo> savedCardsInfos = savedCards.get(rarity);
|
||||
if (savedCardsInfos == null) {
|
||||
CardCriteria criteria = new CardCriteria();
|
||||
if (rarity.equals(Rarity.LAND)) {
|
||||
criteria.setCodes(!hasBasicLands && parentSet != null ? parentSet.code : this.code);
|
||||
} else {
|
||||
criteria.setCodes(this.code);
|
||||
}
|
||||
criteria.rarities(rarity).doubleFaced(false);
|
||||
if (maxCardNumberInBooster != Integer.MAX_VALUE) {
|
||||
criteria.maxCardNumber(maxCardNumberInBooster);
|
||||
}
|
||||
savedCardsInfos = CardRepository.instance.findCards(criteria);
|
||||
savedCards.put(rarity, savedCardsInfos);
|
||||
}
|
||||
return CardRepository.instance.findCards(criteria);
|
||||
List<CardInfo> cards = new ArrayList<>();
|
||||
cards.addAll(savedCardsInfos);
|
||||
return cards;
|
||||
}
|
||||
|
||||
public List<CardInfo> getSpecialCommon() {
|
||||
|
|
@ -304,4 +298,8 @@ public abstract class ExpansionSet implements Serializable {
|
|||
public List<CardInfo> getSpecialBonus() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeSavedCards() {
|
||||
savedCards.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ public class BoosterDraft extends DraftImpl {
|
|||
fireUpdatePlayersEvent();
|
||||
}
|
||||
}
|
||||
resetBufferedCards();
|
||||
this.fireEndDraftEvent();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -70,6 +70,8 @@ public interface Draft extends MageItem, Serializable {
|
|||
void addPlayerQueryEventListener(Listener<PlayerQueryEvent> listener);
|
||||
void firePickCardEvent(UUID playerId);
|
||||
|
||||
void resetBufferedCards();
|
||||
|
||||
boolean isAbort();
|
||||
void setAbort(boolean abort);
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
package mage.game.draft;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -351,4 +352,17 @@ public abstract class DraftImpl implements Draft {
|
|||
started = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetBufferedCards() {
|
||||
HashSet<ExpansionSet> setsDone = new HashSet<>();
|
||||
for(ExpansionSet set: sets) {
|
||||
if (!setsDone.contains(set)) {
|
||||
set.removeSavedCards();
|
||||
setsDone.add(set);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -97,11 +97,11 @@ public class DraftPlayer {
|
|||
picking = false;
|
||||
}
|
||||
|
||||
public void openBooster(ExpansionSet set) {
|
||||
synchronized(booster) {
|
||||
booster = set.createBooster();
|
||||
}
|
||||
}
|
||||
// public void openBooster(ExpansionSet set) {
|
||||
// synchronized(booster) {
|
||||
// booster = set.createBooster();
|
||||
// }
|
||||
// }
|
||||
|
||||
public void setBooster(List<Card> booster) {
|
||||
this.booster = booster;
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
|
@ -399,9 +400,20 @@ public abstract class TournamentImpl implements Tournament {
|
|||
}
|
||||
}
|
||||
}
|
||||
resetBufferedCards();
|
||||
nextStep();
|
||||
}
|
||||
|
||||
public void resetBufferedCards() {
|
||||
HashSet<ExpansionSet> setsDone = new HashSet<>();
|
||||
for(ExpansionSet set: sets) {
|
||||
if (!setsDone.contains(set)) {
|
||||
set.removeSavedCards();
|
||||
setsDone.add(set);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void playMatch(TournamentPairing pair) {
|
||||
options.getMatchOptions().getPlayerTypes().clear();
|
||||
options.getMatchOptions().getPlayerTypes().add(pair.getPlayer1().getPlayerType());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue