added handlers for create table + adjusted connection code to better handle failed connections

This commit is contained in:
betasteward 2015-06-08 14:05:00 -04:00
parent 71c7bc38e1
commit c47da1f5b3
31 changed files with 542 additions and 152 deletions

View file

@ -242,16 +242,16 @@ public class Main implements MageServer {
}
@Override
public boolean registerClient(String userName, String sessionId, MageVersion version) {
public boolean registerClient(String userName, String sessionId, MageVersion version, String host) {
try {
if (version.compareTo(Main.getVersion()) != 0) {
logger.info("MageVersionException: userName=" + userName + ", version=" + version);
LogServiceImpl.instance.log(LogKeys.KEY_WRONG_VERSION, userName, version.toString(), Main.getVersion().toString(), sessionId);
server.informClient(sessionId, "Wrong version", MessageType.ERROR);
String message = "Wrong client version " + version + ", expecting version " + Main.getVersion() + ". \r\n\r\nPlease download needed version from http://XMage.de or http://www.slightlymagic.net/forum/viewforum.php?f=70";
server.informClient(sessionId, "Wrong version", message, MessageType.ERROR);
return false;
// throw new MageVersionException(version, Main.getVersion());
}
return SessionManager.getInstance().registerUser(sessionId, userName);
return SessionManager.getInstance().registerUser(sessionId, userName, host);
} catch (MageException ex) {
// if (ex instanceof MageVersionException) {
// throw (MageVersionException)ex;
@ -287,26 +287,26 @@ public class Main implements MageServer {
// return false;
// }
//
// @Override
// public TableView createTable(final String sessionId, final UUID roomId, final MatchOptions options) throws MageException {
@Override
public TableView createTable(final String sessionId, final UUID roomId, final MatchOptions options) {
// return executeWithResult("createTable", sessionId, new ActionWithTableViewResult() {
// @Override
// public TableView execute() throws MageException {
// UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
// TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(userId, options);
// if (logger.isDebugEnabled()) {
// User user = UserManager.getInstance().getUser(userId);
// if (user != null) {
// logger.debug("TABLE created - tableId: " + table.getTableId() + " " + table.getTableName());
// logger.debug("- " + user.getName() + " userId: " + user.getId());
// logger.debug("- chatId: " + TableManager.getInstance().getChatId(table.getTableId()));
// }
// }
// LogServiceImpl.instance.log(LogKeys.KEY_TABLE_CREATED, sessionId, userId.toString(), table.getTableId().toString());
// return table;
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(userId, options);
if (logger.isDebugEnabled()) {
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
logger.debug("TABLE created - tableId: " + table.getTableId() + " " + table.getTableName());
logger.debug("- " + user.getName() + " userId: " + user.getId());
logger.debug("- chatId: " + TableManager.getInstance().getChatId(table.getTableId()));
}
}
LogServiceImpl.instance.log(LogKeys.KEY_TABLE_CREATED, sessionId, userId.toString(), table.getTableId().toString());
return table;
// }
// });
// }
}
//
// @Override
// public TableView createTournamentTable(final String sessionId, final UUID roomId, final TournamentOptions options) throws MageException {
@ -356,22 +356,22 @@ public class Main implements MageServer {
// });
// }
//
// @Override
// public boolean joinTable(final String sessionId, final UUID roomId, final UUID tableId, final String name, final String playerType, final int skill, final DeckCardLists deckList, final String password) throws MageException, GameException {
@Override
public boolean joinTable(final String sessionId, final UUID roomId, final UUID tableId, final String name, final String playerType, final int skill, final DeckCardLists deckList, final String password) {
// return executeWithResult("joinTable", sessionId, new ActionWithBooleanResult() {
// @Override
// public Boolean execute() throws MageException {
// UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
// logger.debug(name + " joins tableId: " + tableId);
// if (userId == null) {
// logger.fatal("Got no userId from sessionId" + sessionId + " tableId" + tableId);
// return false;
// }
// boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTable(userId, tableId, name, playerType, skill, deckList, password);
// return ret;
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
logger.debug(name + " joins tableId: " + tableId);
if (userId == null) {
logger.fatal("Got no userId from sessionId" + sessionId + " tableId" + tableId);
return false;
}
boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTable(userId, tableId, name, playerType, skill, deckList, password);
return ret;
// }
// });
// }
}
//
// @Override
// public boolean joinTournamentTable(final String sessionId, final UUID roomId, final UUID tableId, final String name, final String playerType, final int skill, final DeckCardLists deckList, final String password) throws MageException, GameException {
@ -586,14 +586,14 @@ public class Main implements MageServer {
@Override
public void receiveChatMessage(final UUID chatId, final String sessionId, final String message) {
execute("receiveChatMessage", sessionId, new Action() {
@Override
public void execute() {
// execute("receiveChatMessage", sessionId, new Action() {
// @Override
// public void execute() {
User user = SessionManager.getInstance().getUser(sessionId);
if (user != null)
ChatManager.getInstance().broadcast(chatId, user, StringEscapeUtils.escapeHtml4(message), MessageColor.BLUE);
}
});
// }
// });
// }
// catch (Exception ex) {
// handleException(sessionId, ex);
@ -1088,7 +1088,7 @@ public class Main implements MageServer {
public void handleException(String sessionId, Exception ex) {
if (!ex.getMessage().equals("No message")) {
logger.fatal("", ex);
server.informClient(sessionId, "Server error: " + ex.getMessage(), MessageType.ERROR);
server.informClient(sessionId, "Server error", ex.getMessage(), MessageType.ERROR);
// throw new MageException("Server error: " + ex.getMessage());
}
}
@ -1219,57 +1219,57 @@ public class Main implements MageServer {
// }
@Override
public void receiveBroadcastMessage(String message, String sessionId) {
public void receiveBroadcastMessage(String title, String message, String sessionId) {
if (SessionManager.getInstance().isAdmin(sessionId)) {
if (message.toLowerCase(Locale.ENGLISH).startsWith("warn")) {
server.informClients(message, MessageType.WARNING);
server.informClients(title, message, MessageType.WARNING);
} else {
server.informClients(message, MessageType.INFORMATION);
server.informClients(title, message, MessageType.INFORMATION);
}
}
}
public void informClient(String sessionId, String mesage, MessageType type) {
server.informClient(sessionId, mesage, type);
public void informClient(String sessionId, String title, String message, MessageType type) {
server.informClient(sessionId, title, message, type);
}
protected void execute(final String actionName, final String sessionId, final Action action, boolean checkAdminRights) throws MageException {
if (checkAdminRights) {
if (!SessionManager.getInstance().isAdmin(sessionId)) {
LogServiceImpl.instance.log(LogKeys.KEY_NOT_ADMIN, actionName, sessionId);
return;
}
}
execute(actionName, sessionId, action);
}
protected void execute(final String actionName, final String sessionId, final Action action) {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
callExecutor.execute(
new Runnable() {
@Override
public void run() {
if (SessionManager.getInstance().isValidSession(sessionId)) {
try {
action.execute();
} catch (MageException me) {
throw new RuntimeException(me);
}
} else {
LogServiceImpl.instance.log(LogKeys.KEY_NOT_VALID_SESSION_INTERNAL, actionName, sessionId);
}
}
}
);
}
catch (Exception ex) {
handleException(sessionId, ex);
}
} else {
LogServiceImpl.instance.log(LogKeys.KEY_NOT_VALID_SESSION, actionName, sessionId);
}
}
// protected void execute(final String actionName, final String sessionId, final Action action, boolean checkAdminRights) throws MageException {
// if (checkAdminRights) {
// if (!SessionManager.getInstance().isAdmin(sessionId)) {
// LogServiceImpl.instance.log(LogKeys.KEY_NOT_ADMIN, actionName, sessionId);
// return;
// }
// }
// execute(actionName, sessionId, action);
// }
//
// protected void execute(final String actionName, final String sessionId, final Action action) {
// if (SessionManager.getInstance().isValidSession(sessionId)) {
// try {
// callExecutor.execute(
// new Runnable() {
// @Override
// public void run() {
// if (SessionManager.getInstance().isValidSession(sessionId)) {
// try {
// action.execute();
// } catch (MageException me) {
// throw new RuntimeException(me);
// }
// } else {
// LogServiceImpl.instance.log(LogKeys.KEY_NOT_VALID_SESSION_INTERNAL, actionName, sessionId);
// }
// }
// }
// );
// }
// catch (Exception ex) {
// handleException(sessionId, ex);
// }
// } else {
// LogServiceImpl.instance.log(LogKeys.KEY_NOT_VALID_SESSION, actionName, sessionId);
// }
// }
// protected <T> T executeWithResult(String actionName, final String sessionId, final ActionWithResult<T> action, boolean checkAdminRights) throws MageException {
// if (checkAdminRights) {

View file

@ -73,9 +73,10 @@ public class SessionManager {
// sessions.put(sessionId, session);
// }
public boolean registerUser(String sessionId, String userName) throws MageException {
public boolean registerUser(String sessionId, String userName, String host) throws MageException {
Session session = new Session(sessionId);
sessions.put(sessionId, session);
session.setHost(host);
// Session session = sessions.get(sessionId);
// if (session != null) {
String returnMessage = session.registerUser(userName);
@ -89,7 +90,7 @@ public class SessionManager {
return true;
}
logger.debug(userName + " not registered: " + returnMessage);
Main.getInstance().informClient(sessionId, returnMessage, MessageType.ERROR);
Main.getInstance().informClient(sessionId, "Connection Error", returnMessage, MessageType.ERROR);
// Server.informClient(sessionId, returnMessage, MessageType.ERROR);
// } else {

View file

@ -35,6 +35,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import mage.MageException;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists;
@ -149,20 +150,21 @@ public class TableController {
);
}
public synchronized boolean joinTournament(UUID userId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws GameException {
public synchronized boolean joinTournament(UUID userId, String name, String playerType, int skill, DeckCardLists deckList, String password) {
if (table.getState() != TableState.WAITING) {
return false;
}
Seat seat = table.getNextAvailableSeat(playerType);
if (seat == null) {
throw new GameException("No available seats.");
}
User user = UserManager.getInstance().getUser(userId);
if (user == null) {
logger.fatal(new StringBuilder("couldn't get user ").append(name).append(" for join tournament userId = ").append(userId).toString());
return false;
}
Seat seat = table.getNextAvailableSeat(playerType);
if (seat == null) {
user.showUserMessage("Join Table", "No available seats");
return false;
}
// check password
if (!table.getTournament().getOptions().getPassword().isEmpty() && playerType.equals("Human")) {
if (!table.getTournament().getOptions().getPassword().equals(password)) {
@ -176,8 +178,14 @@ public class TableController {
}
Deck deck = null;
if (!table.getTournament().getTournamentType().isLimited()) {
if (deckList != null) {
deck = Deck.load(deckList, false, false);
if (deckList != null) {
try {
deck = Deck.load(deckList, false, false);
} catch (GameException ex) {
logger.error("Error loading deck", ex);
user.showUserMessage("Join Table", "Error loading deck");
return false;
}
} else {
user.showUserMessage("Join Table", "No valid deck selected!");
return false;
@ -220,7 +228,8 @@ public class TableController {
return true;
} else {
throw new GameException("Playertype " + seat.getPlayerType() + " could not be created.");
logger.error("Playertype " + seat.getPlayerType() + " could not be created.");
return false;
}
}
@ -244,7 +253,7 @@ public class TableController {
return true;
}
public synchronized boolean joinTable(UUID userId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws MageException {
public synchronized boolean joinTable(UUID userId, String name, String playerType, int skill, DeckCardLists deckList, String password) {
User user = UserManager.getInstance().getUser(userId);
if (user == null) {
return false;
@ -269,7 +278,15 @@ public class TableController {
user.showUserMessage("Join Table", "No available seats.");
return false;
}
Deck deck = Deck.load(deckList, false, false);
Deck deck;
try {
deck = Deck.load(deckList, false, false);
} catch (GameException ex) {
logger.error("Error load deck", ex);
user.showUserMessage("Join Table", "Error loading deck");
return false;
}
if (!Main.getInstance().isTestMode() && !table.getValidator().validate(deck)) {
StringBuilder sb = new StringBuilder("You (").append(name).append(") have an invalid deck for the selected ").append(table.getValidator().getName()).append(" Format. \n\n");
@ -297,7 +314,10 @@ public class TableController {
return false;
}
match.addPlayer(player, deck);
table.joinTable(player, seat);
if (table.joinTable(player, seat) == null) {
user.showUserMessage("Join Table", "Seat is occupied");
return false;
}
logger.trace(player.getName() + " joined tableId: " + table.getId());
//only inform human players and add them to sessionPlayerMap
if (seat.getPlayer().isHuman()) {

View file

@ -140,14 +140,14 @@ public class TableManager {
return controllers.get(tableId);
}
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws MageException {
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) {
if (controllers.containsKey(tableId)) {
return controllers.get(tableId).joinTable(userId, name, playerType, skill, deckList, password);
}
return false;
}
public boolean joinTournament(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws GameException {
public boolean joinTournament(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) {
if (controllers.containsKey(tableId)) {
return controllers.get(tableId).joinTournament(userId, name, playerType, skill, deckList, password);
}

View file

@ -53,6 +53,7 @@ import mage.server.tournament.TournamentSession;
import mage.server.util.SystemUtil;
import mage.view.TableClientMessage;
import org.apache.log4j.Logger;
import org.mage.network.model.MessageType;
/**
@ -220,11 +221,12 @@ public class User {
fireCallback(new ClientCallback("showGameEndDialog", gameId));
}
public void showUserMessage(final String titel, String message) {
List<String> messageData = new LinkedList<>();
messageData.add(titel);
messageData.add(message);
fireCallback(new ClientCallback("showUserMessage", null, messageData ));
public void showUserMessage(final String title, String message) {
// List<String> messageData = new LinkedList<>();
// messageData.add(titel);
// messageData.add(message);
// fireCallback(new ClientCallback("showUserMessage", null, messageData ));
Main.getInstance().informClient(sessionId, title, message, MessageType.INFORMATION);
}
public boolean ccWatchGame(final UUID gameId) {

View file

@ -49,8 +49,8 @@ public interface GamesRoom extends Room {
List<TableView> getTables();
List<MatchView> getFinished();
RoomUsersView getRoomUsersInfo();
boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws MageException;
boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws GameException;
boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password);
boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password);
TableView createTable(UUID userId, MatchOptions options);
TableView createTournamentTable(UUID userId, TournamentOptions options);
void removeTable(UUID userId, UUID tableId);

View file

@ -146,7 +146,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
@Override
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws MageException {
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) {
if (tables.containsKey(tableId)) {
return TableManager.getInstance().joinTable(userId, tableId, name, playerType, skill, deckList, password);
} else {
@ -162,7 +162,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
@Override
public boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws GameException {
public boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList, String password) {
if (tables.containsKey(tableId)) {
return TableManager.getInstance().joinTournament(userId, tableId, name, playerType, skill, deckList, password);
} else {