mirror of
https://github.com/magefree/mage.git
synced 2025-12-24 12:31:59 -08:00
Improved new version cleanup and more:
* fixed db cleanup on new version (sets + cards); * fixed empty sets list after update; * fixed NPE errors in sets list on new install/version; * added joke sets filter to deckeditor.
This commit is contained in:
parent
554e8076cf
commit
f01b3d3ca3
11 changed files with 243 additions and 112 deletions
|
|
@ -14,6 +14,7 @@ import mage.cards.CardSetInfo;
|
|||
import mage.constants.CardType;
|
||||
import mage.constants.SetType;
|
||||
import mage.constants.SuperType;
|
||||
import mage.game.events.Listener;
|
||||
import mage.util.RandomUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
|
|
@ -22,12 +23,14 @@ import java.sql.SQLException;
|
|||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author North
|
||||
* @author North, JayDi85
|
||||
*/
|
||||
public enum CardRepository {
|
||||
|
||||
instance;
|
||||
|
||||
private static final Logger logger = Logger.getLogger(CardRepository.class);
|
||||
|
||||
private static final String JDBC_URL = "jdbc:h2:file:./db/cards.h2;AUTO_SERVER=TRUE";
|
||||
private static final String VERSION_ENTITY_NAME = "card";
|
||||
// raise this if db structure was changed
|
||||
|
|
@ -36,6 +39,7 @@ public enum CardRepository {
|
|||
private static final long CARD_CONTENT_VERSION = 123;
|
||||
private Dao<CardInfo, Object> cardDao;
|
||||
private Set<String> classNames;
|
||||
private RepositoryEventSource eventSource = new RepositoryEventSource();
|
||||
|
||||
CardRepository() {
|
||||
File file = new File("db");
|
||||
|
|
@ -48,32 +52,50 @@ public enum CardRepository {
|
|||
boolean isObsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, CARD_DB_VERSION);
|
||||
boolean isNewBuild = RepositoryUtil.isNewBuildRun(connectionSource, VERSION_ENTITY_NAME, CardRepository.class); // recreate db on new build
|
||||
if (isObsolete || isNewBuild) {
|
||||
//System.out.println("Local cards db is outdated, cleaning...");
|
||||
TableUtils.dropTable(connectionSource, CardInfo.class, true);
|
||||
}
|
||||
|
||||
TableUtils.createTableIfNotExists(connectionSource, CardInfo.class);
|
||||
cardDao = DaoManager.createDao(connectionSource, CardInfo.class);
|
||||
|
||||
eventSource.fireRepositoryDbLoaded();
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CardRepository.class).error("Error creating card repository - ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void addCards(final List<CardInfo> cards) {
|
||||
public void subscribe(Listener<RepositoryEvent> listener) {
|
||||
eventSource.addListener(listener);
|
||||
}
|
||||
|
||||
public void saveCards(final List<CardInfo> newCards, long newContentVersion) {
|
||||
try {
|
||||
cardDao.callBatchTasks(() -> {
|
||||
try {
|
||||
for (CardInfo card : cards) {
|
||||
cardDao.create(card);
|
||||
if (classNames != null) {
|
||||
classNames.add(card.getClassName());
|
||||
// add
|
||||
if (newCards != null && newCards.size() > 0) {
|
||||
logger.info("DB: need to add " + newCards.size() + " new cards");
|
||||
try {
|
||||
for (CardInfo card : newCards) {
|
||||
cardDao.create(card);
|
||||
if (classNames != null) {
|
||||
classNames.add(card.getClassName());
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CardRepository.class).error("Error adding cards to DB - ", ex);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CardRepository.class).error("Error adding cards to DB - ", ex);
|
||||
}
|
||||
|
||||
// no card updates
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
setContentVersion(newContentVersion);
|
||||
eventSource.fireRepositoryDbUpdated();
|
||||
} catch (Exception ex) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
|
||||
package mage.cards.repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import mage.cards.*;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author North
|
||||
*/
|
||||
public final class CardScanner {
|
||||
|
|
@ -27,14 +26,15 @@ public final class CardScanner {
|
|||
scanned = true;
|
||||
|
||||
List<CardInfo> cardsToAdd = new ArrayList<>();
|
||||
int setsUpdatedCount = 0;
|
||||
int setsAddedCount = 0;
|
||||
List<ExpansionInfo> setsToAdd = new ArrayList<>();
|
||||
List<ExpansionInfo> setsToUpdate = new ArrayList<>();
|
||||
|
||||
// check sets
|
||||
for (ExpansionSet set : Sets.getInstance().values()) {
|
||||
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(set.getCode());
|
||||
if (expansionInfo == null) {
|
||||
setsAddedCount += 1;
|
||||
ExpansionRepository.instance.add(new ExpansionInfo(set));
|
||||
// need add
|
||||
setsToAdd.add(new ExpansionInfo(set));
|
||||
} else if (!expansionInfo.name.equals(set.getName())
|
||||
|| !expansionInfo.code.equals(set.getCode())
|
||||
|| (expansionInfo.blockName == null ? set.getBlockName() != null : !expansionInfo.blockName.equals(set.getBlockName()))
|
||||
|
|
@ -42,22 +42,17 @@ public final class CardScanner {
|
|||
|| expansionInfo.type != set.getSetType()
|
||||
|| expansionInfo.boosters != set.hasBoosters()
|
||||
|| expansionInfo.basicLands != set.hasBasicLands()) {
|
||||
setsUpdatedCount += 1;
|
||||
ExpansionRepository.instance.update(expansionInfo);
|
||||
// need update
|
||||
setsToUpdate.add(expansionInfo);
|
||||
}
|
||||
}
|
||||
ExpansionRepository.instance.setContentVersion(ExpansionRepository.instance.getContentVersionConstant());
|
||||
|
||||
if (setsAddedCount > 0) {
|
||||
logger.info("DB: need to add " + setsAddedCount + " new sets");
|
||||
}
|
||||
if (setsUpdatedCount > 0) {
|
||||
logger.info("DB: need to update " + setsUpdatedCount + " sets");
|
||||
}
|
||||
ExpansionRepository.instance.saveSets(setsToAdd, setsToUpdate, ExpansionRepository.instance.getContentVersionConstant());
|
||||
|
||||
// check cards (only add mode, without updates)
|
||||
for (ExpansionSet set : Sets.getInstance().values()) {
|
||||
for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) {
|
||||
if (CardRepository.instance.findCard(set.getCode(), setInfo.getCardNumber()) == null) {
|
||||
// need add
|
||||
Card card = CardImpl.createCard(
|
||||
setInfo.getCardClass(),
|
||||
new CardSetInfo(setInfo.getName(), set.getCode(), setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo()),
|
||||
|
|
@ -73,11 +68,6 @@ public final class CardScanner {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!cardsToAdd.isEmpty()) {
|
||||
logger.info("DB: need to add " + cardsToAdd.size() + " new cards");
|
||||
CardRepository.instance.addCards(cardsToAdd);
|
||||
}
|
||||
CardRepository.instance.setContentVersion(CardRepository.instance.getContentVersionConstant());
|
||||
CardRepository.instance.saveCards(cardsToAdd, CardRepository.instance.getContentVersionConstant());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import com.j256.ormlite.stmt.QueryBuilder;
|
|||
import com.j256.ormlite.stmt.SelectArg;
|
||||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
import mage.game.events.Listener;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -18,7 +19,7 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author North
|
||||
* @author North, JayDi85
|
||||
*/
|
||||
public enum ExpansionRepository {
|
||||
|
||||
|
|
@ -32,7 +33,7 @@ public enum ExpansionRepository {
|
|||
private static final long EXPANSION_CONTENT_VERSION = 17;
|
||||
|
||||
private Dao<ExpansionInfo, Object> expansionDao;
|
||||
|
||||
private RepositoryEventSource eventSource = new RepositoryEventSource();
|
||||
public boolean instanceInitialized = false;
|
||||
|
||||
ExpansionRepository() {
|
||||
|
|
@ -46,31 +47,59 @@ public enum ExpansionRepository {
|
|||
boolean isObsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, EXPANSION_DB_VERSION);
|
||||
boolean isNewBuild = RepositoryUtil.isNewBuildRun(connectionSource, VERSION_ENTITY_NAME, ExpansionRepository.class); // recreate db on new build
|
||||
if (isObsolete || isNewBuild) {
|
||||
//System.out.println("Local sets db is outdated, cleaning...");
|
||||
TableUtils.dropTable(connectionSource, ExpansionInfo.class, true);
|
||||
}
|
||||
|
||||
TableUtils.createTableIfNotExists(connectionSource, ExpansionInfo.class);
|
||||
expansionDao = DaoManager.createDao(connectionSource, ExpansionInfo.class);
|
||||
instanceInitialized = true;
|
||||
|
||||
eventSource.fireRepositoryDbLoaded();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void add(ExpansionInfo expansion) {
|
||||
try {
|
||||
expansionDao.create(expansion);
|
||||
} catch (SQLException ex) {
|
||||
logger.error(ex);
|
||||
}
|
||||
public void subscribe(Listener<RepositoryEvent> listener) {
|
||||
eventSource.addListener(listener);
|
||||
}
|
||||
|
||||
public void update(ExpansionInfo expansion) {
|
||||
public void saveSets(final List<ExpansionInfo> newSets, final List<ExpansionInfo> updatedSets, long newContentVersion) {
|
||||
try {
|
||||
expansionDao.update(expansion);
|
||||
} catch (SQLException ex) {
|
||||
logger.error(ex);
|
||||
expansionDao.callBatchTasks(() -> {
|
||||
// add
|
||||
if (newSets != null && newSets.size() > 0) {
|
||||
logger.info("DB: need to add " + newSets.size() + " new sets");
|
||||
try {
|
||||
for (ExpansionInfo exp : newSets) {
|
||||
expansionDao.create(exp);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CardRepository.class).error("Error adding expansions to DB - ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// update
|
||||
if (updatedSets != null && updatedSets.size() > 0) {
|
||||
logger.info("DB: need to update " + updatedSets.size() + " sets");
|
||||
try {
|
||||
for (ExpansionInfo exp : updatedSets) {
|
||||
expansionDao.update(exp);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CardRepository.class).error("Error adding expansions to DB - ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
setContentVersion(newContentVersion);
|
||||
eventSource.fireRepositoryDbUpdated();
|
||||
} catch (Exception ex) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package mage.cards.repository;
|
||||
|
||||
import mage.game.events.ExternalEvent;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.EventObject;
|
||||
|
||||
/**
|
||||
* @author JayDi85
|
||||
*/
|
||||
public class RepositoryEvent extends EventObject implements ExternalEvent, Serializable {
|
||||
|
||||
public enum RepositoryEventType {
|
||||
DB_LOADED, DB_UPDATED
|
||||
}
|
||||
|
||||
private RepositoryEventType eventType;
|
||||
|
||||
public RepositoryEvent(RepositoryEventType eventType) {
|
||||
super(eventType);
|
||||
this.eventType = eventType;
|
||||
}
|
||||
|
||||
public RepositoryEventType getEventType() {
|
||||
return eventType;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package mage.cards.repository;
|
||||
|
||||
import mage.game.events.EventDispatcher;
|
||||
import mage.game.events.EventSource;
|
||||
import mage.game.events.Listener;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author JayDi85
|
||||
*/
|
||||
public class RepositoryEventSource implements EventSource<RepositoryEvent>, Serializable {
|
||||
|
||||
protected final EventDispatcher<RepositoryEvent> dispatcher = new EventDispatcher<RepositoryEvent>() {
|
||||
};
|
||||
|
||||
@Override
|
||||
public void addListener(Listener<RepositoryEvent> listener) {
|
||||
dispatcher.addListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllListener() {
|
||||
dispatcher.removeAllListener();
|
||||
}
|
||||
|
||||
public void fireRepositoryDbLoaded() {
|
||||
dispatcher.fireEvent(new RepositoryEvent(RepositoryEvent.RepositoryEventType.DB_LOADED));
|
||||
}
|
||||
|
||||
public void fireRepositoryDbUpdated() {
|
||||
dispatcher.fireEvent(new RepositoryEvent(RepositoryEvent.RepositoryEventType.DB_UPDATED));
|
||||
}
|
||||
}
|
||||
|
|
@ -8,16 +8,25 @@ import com.j256.ormlite.stmt.SelectArg;
|
|||
import com.j256.ormlite.support.ConnectionSource;
|
||||
import com.j256.ormlite.table.TableUtils;
|
||||
import mage.util.JarVersion;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author North
|
||||
* @author North, JayDi85
|
||||
*/
|
||||
public final class RepositoryUtil {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(RepositoryUtil.class);
|
||||
|
||||
public static void bootstrapLocalDb() {
|
||||
// call local db to init all sets and cards repository (need for correct updates cycle, not on random request)
|
||||
logger.info("Loading database...");
|
||||
ExpansionRepository.instance.getContentVersionConstant();
|
||||
CardRepository.instance.getContentVersionConstant();
|
||||
}
|
||||
|
||||
public static boolean isDatabaseObsolete(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
|
||||
TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
|
||||
Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
|
||||
|
|
@ -37,6 +46,7 @@ public final class RepositoryUtil {
|
|||
|
||||
public static boolean isNewBuildRun(ConnectionSource connectionSource, String entityName, Class clazz) throws SQLException {
|
||||
// build time checks only for releases, not runtime (e.g. IDE debug)
|
||||
// that's check uses for cards db cleanup on new version/build
|
||||
String currentBuild = JarVersion.getBuildTime(clazz);
|
||||
if (!JarVersion.isBuildTimeOk(currentBuild)) {
|
||||
return false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue