* Reworked DB comparison between client and server.

This commit is contained in:
LevelX2 2015-01-25 19:21:50 +01:00
parent 05a4a99ec6
commit 6ef8b4f976
12 changed files with 346 additions and 177 deletions

View file

@ -48,8 +48,12 @@ public class ServerState implements Serializable {
private final String[] draftCubes;
private final boolean testMode;
private final MageVersion version;
private final long cardsContentVersion;
private final long expansionsContentVersion;
public ServerState(List<GameTypeView> gameTypes, List<TournamentTypeView> tournamentTypes, String[] playerTypes, String[] deckTypes, String[] draftCubes, boolean testMode, MageVersion version) {
public ServerState(List<GameTypeView> gameTypes, List<TournamentTypeView> tournamentTypes,
String[] playerTypes, String[] deckTypes, String[] draftCubes, boolean testMode,
MageVersion version, long cardsContentVersion, long expansionsContentVersion) {
this.gameTypes = gameTypes;
this.tournamentTypes = tournamentTypes;
this.playerTypes = playerTypes;
@ -57,6 +61,9 @@ public class ServerState implements Serializable {
this.draftCubes = draftCubes;
this.testMode = testMode;
this.version = version;
this.cardsContentVersion = cardsContentVersion;
this.expansionsContentVersion = expansionsContentVersion;
}
public List<GameTypeView> getGameTypes() {
@ -96,4 +103,13 @@ public class ServerState implements Serializable {
public MageVersion getVersion() {
return version;
}
public long getCardsContentVersion() {
return cardsContentVersion;
}
public long getExpansionsContentVersion() {
return expansionsContentVersion;
}
}

View file

@ -51,6 +51,8 @@ public class Connection {
private int proxyPort;
private String proxyUsername;
private String proxyPassword;
private int clientCardDatabaseVersion;
private boolean forceDBComparison;
private int avatarId;
private boolean showAbilityPickerForced;
@ -202,8 +204,9 @@ public class Connection {
public static InetAddress getLocalAddress() throws SocketException {
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
NetworkInterface iface = interfaces.nextElement( );
if (iface.isLoopback())
if (iface.isLoopback()) {
continue;
}
for (InterfaceAddress addr: iface.getInterfaceAddresses()) {
if (addr != null) {
InetAddress iaddr = addr.getAddress();
@ -247,4 +250,12 @@ public class Connection {
this.userSkipPrioritySteps = userSkipPrioritySteps;
}
public boolean isForceDBComparison() {
return forceDBComparison;
}
public void setForceDBComparison(boolean forceDBComparison) {
this.forceDBComparison = forceDBComparison;
}
}

View file

@ -49,6 +49,7 @@ import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.cards.repository.ExpansionInfo;
import mage.cards.repository.ExpansionRepository;
import mage.cards.repository.RepositoryUtil;
import mage.constants.Constants.SessionState;
import mage.constants.ManaType;
import mage.constants.PlayerAction;
@ -287,7 +288,7 @@ public class SessionImpl implements Session {
sessionState = SessionState.CONNECTED;
serverState = server.getServerState();
if (!connection.getUsername().equals("Admin")) {
updateDatabase();
updateDatabase(connection.isForceDBComparison(), serverState);
}
logger.info("Connected as " + (this.getUserName() == null ? "":this.getUserName()) + " to MAGE server at " + connection.getHost() + ":" + connection.getPort());
client.connected(this.getUserName() == null ? "":this.getUserName() +"@" + connection.getHost() + ":" + connection.getPort() +" ");
@ -322,15 +323,27 @@ public class SessionImpl implements Session {
return false;
}
private void updateDatabase() {
List<String> classNames = CardRepository.instance.getClassNames();
List<CardInfo> cards = server.getMissingCardsData(classNames);
CardRepository.instance.addCards(cards);
private void updateDatabase(boolean forceDBComparison, ServerState serverState) {
long cardDBVersion = CardRepository.instance.getContentVersionFromDB();
if (forceDBComparison || serverState.getCardsContentVersion() > cardDBVersion) {
List<String> classNames = CardRepository.instance.getClassNames();
List<CardInfo> cards = server.getMissingCardsData(classNames);
CardRepository.instance.addCards(cards);
CardRepository.instance.setContentVersion(serverState.getCardsContentVersion());
logger.info("Updating client cards DB - existing cards: " + classNames.size() + " new cards: " + cards.size() +
" content versions - server: " + serverState.getCardsContentVersion() + " client: " + cardDBVersion);
}
List<String> setCodes = ExpansionRepository.instance.getSetCodes();
List<ExpansionInfo> expansions = server.getMissingExpansionData(setCodes);
for (ExpansionInfo expansion : expansions) {
ExpansionRepository.instance.add(expansion);
long expansionDBVersion = ExpansionRepository.instance.getContentVersionFromDB();
if (forceDBComparison || serverState.getExpansionsContentVersion() > expansionDBVersion) {
List<String> setCodes = ExpansionRepository.instance.getSetCodes();
List<ExpansionInfo> expansions = server.getMissingExpansionData(setCodes);
for (ExpansionInfo expansion : expansions) {
ExpansionRepository.instance.add(expansion);
}
ExpansionRepository.instance.setContentVersion(serverState.getExpansionsContentVersion());
logger.info("Updating client expansions DB - existing sets: " + setCodes.size() + " new sets: " + expansions.size()+
" content versions - server: " + serverState.getExpansionsContentVersion() + " client: " + expansionDBVersion);
}
}
@ -366,10 +379,10 @@ public class SessionImpl implements Session {
/**
*
* @param errorCall - was connection lost because of error - ask user if he want to try to reconnect
* @param askForReconnect - true = connection was lost because of error and ask the user if he want to try to reconnect
*/
@Override
public synchronized void disconnect(boolean errorCall) {
public synchronized void disconnect(boolean askForReconnect) {
if (isConnected()) {
logger.info("DISCONNECT (still connected)");
sessionState = SessionState.DISCONNECTING;
@ -389,10 +402,10 @@ public class SessionImpl implements Session {
if (sessionState == SessionState.DISCONNECTING || sessionState == SessionState.CONNECTING) {
sessionState = SessionState.DISCONNECTED;
logger.info("Disconnected ... ");
if (errorCall) {
if (askForReconnect) {
client.showError("Network error. You have been disconnected");
}
client.disconnected(errorCall); // MageFrame with check to reconnect
client.disconnected(askForReconnect); // MageFrame with check to reconnect
pingTime.clear();
}
}