Introducing Optionals

This commit is contained in:
igoudt 2017-01-14 21:52:40 +01:00
parent 97261fdcca
commit f98afdf4ad
21 changed files with 407 additions and 483 deletions

View file

@ -30,15 +30,13 @@ package mage.server;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.*;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import mage.MageException;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists;
@ -59,7 +57,6 @@ import mage.server.util.ThreadExecutor;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class TableManager {
@ -67,21 +64,21 @@ public class TableManager {
protected static final ScheduledExecutorService expireExecutor = Executors.newSingleThreadScheduledExecutor();
// protected static ScheduledExecutorService expireExecutor = ThreadExecutor.getInstance().getExpireExecutor();
private static final TableManager INSTANCE = new TableManager();
private static final Logger logger = Logger.getLogger(TableManager.class);
private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
private final ConcurrentHashMap<UUID, TableController> controllers = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, Table> tables = new ConcurrentHashMap<>();
/**
* Defines how often checking process should be run on server.
*
* <p>
* In minutes.
*/
private static final int EXPIRE_CHECK_PERIOD = 10;
public static TableManager getInstance() {
return INSTANCE;
}
@ -90,7 +87,7 @@ public class TableManager {
expireExecutor.scheduleAtFixedRate(() -> {
try {
checkTableHealthState();
} catch(Exception ex) {
} catch (Exception ex) {
logger.fatal("Check table health state job error:", ex);
}
}, EXPIRE_CHECK_PERIOD, EXPIRE_CHECK_PERIOD, TimeUnit.MINUTES);
@ -154,11 +151,11 @@ public class TableManager {
if (controllers.containsKey(tableId)) {
return controllers.get(tableId).submitDeck(userId, deckList);
}
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
UserManager.getInstance().getUser(userId).ifPresent(user -> {
user.removeSideboarding(tableId);
user.showUserMessage("Submit deck", "Table no longer active");
}
});
// return true so the panel closes
return true;
}
@ -171,7 +168,7 @@ public class TableManager {
// remove user from all tournament sub tables
public void userQuitTournamentSubTables(UUID userId) {
for (TableController controller: controllers.values()) {
for (TableController controller : controllers.values()) {
if (controller.getTable() != null) {
if (controller.getTable().isTournamentSubTable()) {
controller.leaveTable(userId);
@ -184,7 +181,7 @@ public class TableManager {
// remove user from all sub tables of a tournament
public void userQuitTournamentSubTables(UUID tournamentId, UUID userId) {
for (TableController controller: controllers.values()) {
for (TableController controller : controllers.values()) {
if (controller.getTable().isTournamentSubTable() && controller.getTable().getTournament().getId().equals(tournamentId)) {
if (controller.hasPlayer(userId)) {
controller.leaveTable(userId);
@ -231,7 +228,7 @@ public class TableManager {
/**
* Starts the Match from a non tournament table
*
* @param userId table owner
* @param userId table owner
* @param roomId
* @param tableId
*/
@ -239,7 +236,7 @@ public class TableManager {
if (controllers.containsKey(tableId)) {
controllers.get(tableId).startMatch(userId);
// chat of start dialog can be killed
ChatManager.getInstance().destroyChatSession(controllers.get(tableId).getChatId());
ChatManager.getInstance().destroyChatSession(controllers.get(tableId).getChatId());
}
}
@ -258,9 +255,9 @@ public class TableManager {
public void startTournament(UUID userId, UUID roomId, UUID tableId) {
if (controllers.containsKey(tableId)) {
controllers.get(tableId).startTournament(userId);
ChatManager.getInstance().destroyChatSession(controllers.get(tableId).getChatId());
}
ChatManager.getInstance().destroyChatSession(controllers.get(tableId).getChatId());
}
}
public void startDraft(UUID tableId, Draft draft) {
if (controllers.containsKey(tableId)) {
@ -327,11 +324,11 @@ public class TableManager {
}
public void removeTable(UUID tableId) {
TableController tableController = controllers.get(tableId);
TableController tableController = controllers.get(tableId);
if (tableController != null) {
controllers.remove(tableId);
tableController.cleanUp(); // deletes the table chat and references to users
Table table = tables.get(tableId);
tables.remove(tableId);
Match match = table.getMatch();
@ -340,17 +337,17 @@ public class TableManager {
game = match.getGame();
if (game != null && !game.hasEnded()) {
game.end();
}
}
}
// 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()!=TableState.FINISHED) {
if (table.getState() != TableState.FINISHED) {
if (game != null) {
GameManager.getInstance().removeGame(game.getId());
}
GamesRoomManager.getInstance().removeTable(tableId);
}
}
}
@ -358,10 +355,10 @@ public class TableManager {
logger.debug("--- Server state ----------------------------------------------");
Collection<User> users = UserManager.getInstance().getUsers();
logger.debug("--------User: " + users.size() + " [userId | since | lock | name -----------------------");
for (User user :users) {
for (User user : users) {
Session session = SessionManager.getInstance().getSession(user.getSessionId());
String sessionState = "N";
if (session != null) {
if (session!=null) {
if (session.isLocked()) {
sessionState = "L";
} else {
@ -371,21 +368,21 @@ public class TableManager {
logger.debug(user.getId()
+ " | " + formatter.format(user.getConnectionTime())
+ " | " + sessionState
+ " | " + user.getName() +" (" +user.getUserState().toString() + " - " + user.getPingInfo() + ")");
+ " | " + user.getName() + " (" + user.getUserState().toString() + " - " + user.getPingInfo() + ")");
}
ArrayList<ChatSession> chatSessions = ChatManager.getInstance().getChatSessions();
logger.debug("------- ChatSessions: " + chatSessions.size() + " ----------------------------------");
for (ChatSession chatSession: chatSessions) {
logger.debug(chatSession.getChatId() + " " +formatter.format(chatSession.getCreateTime()) +" " + chatSession.getInfo()+ " "+ chatSession.getClients().values().toString());
for (ChatSession chatSession : chatSessions) {
logger.debug(chatSession.getChatId() + " " + formatter.format(chatSession.getCreateTime()) + " " + chatSession.getInfo() + " " + chatSession.getClients().values().toString());
}
logger.debug("------- Games: " + GameManager.getInstance().getNumberActiveGames() + " --------------------------------------------");
logger.debug(" Active Game Worker: " + ThreadExecutor.getInstance().getActiveThreads(ThreadExecutor.getInstance().getGameExecutor()));
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("--- Server state END ------------------------------------------");
}
private void checkTableHealthState() {
if (logger.isDebugEnabled()) {
debugServerState();
@ -395,13 +392,13 @@ public class TableManager {
tableCopy.addAll(tables.values());
for (Table table : tableCopy) {
try {
if (table.getState()!=TableState.FINISHED) {
if (table.getState() != TableState.FINISHED) {
// remove tables and games not valid anymore
logger.debug(table.getId() + " [" + table.getName()+ "] " + formatter.format(table.getStartTime() == null ? table.getCreateTime() : table.getCreateTime()) +" (" + table.getState().toString() + ") " + (table.isTournament() ? "- Tournament":""));
logger.debug(table.getId() + " [" + table.getName() + "] " + formatter.format(table.getStartTime() == null ? table.getCreateTime() : table.getCreateTime()) + " (" + table.getState().toString() + ") " + (table.isTournament() ? "- Tournament" : ""));
TableController tableController = getController(table.getId());
if (tableController != null) {
if ((table.isTournament() && !tableController.isTournamentStillValid()) ||
(!table.isTournament() && !tableController.isMatchTableStillValid())) {
(!table.isTournament() && !tableController.isMatchTableStillValid())) {
try {
logger.warn("Removing unhealthy tableId " + table.getId());
removeTable(table.getId());