* Fixed a bug in table expired check. Changed expired check. Some fixed for table / tournament state handling.

This commit is contained in:
LevelX2 2014-09-03 16:17:43 +02:00
parent 8b331eefce
commit 6f9a6ccabf
2 changed files with 24 additions and 39 deletions

View file

@ -79,20 +79,6 @@ public class TableManager {
*/ */
private static final int EXPIRE_CHECK_PERIOD = 5; private static final int EXPIRE_CHECK_PERIOD = 5;
/**
* This parameters defines when table can be counted as expired.
* Uses EXPIRE_TIME_UNIT_VALUE as unit of measurement.
*
* The time pass is calculated as (table_created_at - now) / EXPIRE_TIME_UNIT_VALUE.
* Then this values is compared to EXPIRE_TIME.
*/
private static final int EXPIRE_TIME = 3;
/**
* Defines unit of measurement for expiration time of tables created.
*/
private static final int EXPIRE_TIME_UNIT_VALUE = 1000 * 60 * 60; // 1 hour
public static TableManager getInstance() { public static TableManager getInstance() {
return INSTANCE; return INSTANCE;
} }
@ -101,7 +87,12 @@ public class TableManager {
expireExecutor.scheduleAtFixedRate(new Runnable() { expireExecutor.scheduleAtFixedRate(new Runnable() {
@Override @Override
public void run() { public void run() {
checkExpired(); try {
checkTableHealthState();
} catch(Exception ex) {
logger.fatal("Check table health state job error:");
ex.printStackTrace();
}
} }
}, EXPIRE_CHECK_PERIOD, EXPIRE_CHECK_PERIOD, TimeUnit.MINUTES); }, EXPIRE_CHECK_PERIOD, EXPIRE_CHECK_PERIOD, TimeUnit.MINUTES);
} }
@ -344,7 +335,7 @@ public class TableManager {
Table table = tables.get(tableId); Table table = tables.get(tableId);
tables.remove(tableId); tables.remove(tableId);
// If table is not finished, the table has to be removed completly (if finished it will be removed in GamesRoomImpl.Update()) // If table is not finished, the table has to be removed completly because it's not a normal state (if finished it will be removed in GamesRoomImpl.Update())
if (!table.getState().equals(TableState.FINISHED)) { if (!table.getState().equals(TableState.FINISHED)) {
GamesRoomManager.getInstance().removeTable(tableId); GamesRoomManager.getInstance().removeTable(tableId);
} }
@ -366,10 +357,6 @@ public class TableManager {
for (ChatSession chatSession: chatSessions) { for (ChatSession chatSession: chatSessions) {
logger.debug(chatSession.getChatId() + " " +formatter.format(chatSession.getCreateTime()) +" " + chatSession.getInfo()+ " "+ chatSession.getClients().values().toString()); logger.debug(chatSession.getChatId() + " " +formatter.format(chatSession.getCreateTime()) +" " + chatSession.getInfo()+ " "+ chatSession.getClients().values().toString());
} }
logger.debug("------- Tables: " + tables.size() + " --------------------------------------------");
for (Table table: tables.values()) {
logger.debug(table.getId() + " [" + table.getName()+ "] " + formatter.format(table.getStartTime()) +" (" + table.getState().toString() + ")");
}
logger.debug("------- Games: " + GameManager.getInstance().getNumberActiveGames() + " --------------------------------------------"); logger.debug("------- Games: " + GameManager.getInstance().getNumberActiveGames() + " --------------------------------------------");
for (Entry<UUID, GameController> entry: GameManager.getInstance().getGameController().entrySet()) { for (Entry<UUID, GameController> entry: GameManager.getInstance().getGameController().entrySet()) {
logger.debug(entry.getKey() + entry.getValue().getPlayerNameList()); logger.debug(entry.getKey() + entry.getValue().getPlayerNameList());
@ -377,30 +364,30 @@ public class TableManager {
logger.debug("--- Server state END ------------------------------------------"); logger.debug("--- Server state END ------------------------------------------");
} }
private void checkExpired() { private void checkTableHealthState() {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
debugServerState(); debugServerState();
} }
Date now = new Date(); logger.debug("TABLE HEALTH CHECK");
List<UUID> toRemove = new ArrayList<>(); List<UUID> toRemove = new ArrayList<>();
for (Table table : tables.values()) { for (Table table : tables.values()) {
if (!table.getState().equals(TableState.FINISHED)) { if (!table.getState().equals(TableState.FINISHED)) {
// remove all not finished tables created more than expire_time ago // remove tables and games not valid anymore
long diff = (now.getTime() - table.getCreateTime().getTime()) / EXPIRE_TIME_UNIT_VALUE; logger.debug(table.getId() + " [" + table.getName()+ "] " + formatter.format(table.getStartTime()) +" (" + table.getState().toString() + ") " + (table.isTournament() ? "- Tournament":""));
if (diff >= EXPIRE_TIME) { TableController tableController = getController(table.getId());
logger.warn("Table expired: id = " + table.getId() + ", created_by=" + table.getControllerName() + ". Removing..."); if (tableController != null) {
if (table.isTournament()) {
if (!tableController.isTournamentStillValid()) {
toRemove.add(table.getId()); toRemove.add(table.getId());
} }
// remove tables not valid anymore } else {
else if (!table.isTournament()) {
TableController tableController = getController(table.getId());
if (!tableController.isMatchTableStillValid()) { if (!tableController.isMatchTableStillValid()) {
logger.warn("Table with no active human player: id = " + table.getId() + ", created_by=" + table.getControllerName() + ". Removing...");
toRemove.add(table.getId()); toRemove.add(table.getId());
} }
} }
} }
} }
}
for (UUID tableId : toRemove) { for (UUID tableId : toRemove) {
try { try {
removeTable(tableId); removeTable(tableId);
@ -408,6 +395,7 @@ public class TableManager {
logger.error(e); logger.error(e);
} }
} }
logger.debug("TABLE HEALTH CHECK - END");
} }
} }

View file

@ -296,10 +296,8 @@ public abstract class AbilityImpl implements Ability {
if (getTargets().size() > 0 && getTargets().chooseTargets(getEffects().get(0).getOutcome(), this.controllerId, this, game) == false) { if (getTargets().size() > 0 && getTargets().chooseTargets(getEffects().get(0).getOutcome(), this.controllerId, this, game) == false) {
if (variableManaCost != null || announceString != null) { if (variableManaCost != null || announceString != null) {
game.informPlayer(controller, new StringBuilder(sourceObject != null ? sourceObject.getLogName(): "").append(": no valid targets with this value of X").toString()); game.informPlayer(controller, new StringBuilder(sourceObject != null ? sourceObject.getLogName(): "").append(": no valid targets with this value of X").toString());
} else {
logger.debug("activate failed - target");
} }
return false; return false; // when activation of ability is canceled during target selection
} }
} // end modes } // end modes
@ -344,8 +342,7 @@ public abstract class AbilityImpl implements Ability {
//20100716 - 601.2f (noMana is not used here, because mana costs were cleared for this ability before adding additional costs and applying cost modification effects) //20100716 - 601.2f (noMana is not used here, because mana costs were cleared for this ability before adding additional costs and applying cost modification effects)
if (!manaCostsToPay.pay(this, game, sourceId, activatorId, false)) { if (!manaCostsToPay.pay(this, game, sourceId, activatorId, false)) {
logger.debug("activate failed - mana"); return false; // cancel during mana payment
return false;
} }
} }