Replaced Session with User - needed for reconnect

This commit is contained in:
BetaSteward 2011-07-03 22:55:14 -04:00
parent 3d540160a8
commit 1a43757ed0
24 changed files with 610 additions and 525 deletions

View file

@ -54,12 +54,12 @@ public class ChatManager {
return chatSession.getChatId();
}
public void joinChat(UUID chatId, String sessionId, String userName) {
chatSessions.get(chatId).join(userName, sessionId);
public void joinChat(UUID chatId, UUID userId) {
chatSessions.get(chatId).join(userId);
}
public void leaveChat(UUID chatId, String sessionId) {
chatSessions.get(chatId).kill(sessionId);
public void leaveChat(UUID chatId, UUID userId) {
chatSessions.get(chatId).kill(userId);
}
public void destroyChatSession(UUID chatId) {
@ -70,9 +70,9 @@ public class ChatManager {
chatSessions.get(chatId).broadcast(userName, message, color);
}
void removeSession(String sessionId) {
void removeUser(UUID userId) {
for (ChatSession chat: chatSessions.values()) {
chat.kill(sessionId);
chat.kill(userId);
}
}
}

View file

@ -32,6 +32,7 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import mage.interfaces.callback.ClientCallback;
@ -46,7 +47,7 @@ import org.apache.log4j.Logger;
public class ChatSession {
private final static Logger logger = Logger.getLogger(ChatSession.class);
private ConcurrentHashMap<String, String> clients = new ConcurrentHashMap<String, String>();
private ConcurrentHashMap<UUID, String> clients = new ConcurrentHashMap<UUID, String>();
private UUID chatId;
private DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
@ -56,16 +57,20 @@ public class ChatSession {
chatId = UUID.randomUUID();
}
public void join(String userName, String sessionId) {
clients.put(sessionId, userName);
broadcast(userName, " has joined", MessageColor.BLACK);
logger.info(userName + " joined chat " + chatId);
public void join(UUID userId) {
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
String userName = user.getName();
clients.put(userId, userName);
broadcast(userName, " has joined", MessageColor.BLACK);
logger.info(userName + " joined chat " + chatId);
}
}
public void kill(String sessionId) {
if (clients.containsKey(sessionId)) {
String userName = clients.get(sessionId);
clients.remove(sessionId);
public void kill(UUID userId) {
if (clients.containsKey(userId)) {
String userName = clients.get(userId);
clients.remove(userId);
broadcast(userName, " has left", MessageColor.BLACK);
logger.info(userName + " has left chat " + chatId);
}
@ -77,12 +82,12 @@ public class ChatSession {
final String time = timeFormatter.format(cal.getTime());
final String username = userName;
logger.debug("Broadcasting '" + msg + "' for " + chatId);
for (String sessionId: clients.keySet()) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null)
session.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color)));
for (UUID userId: clients.keySet()) {
User user = UserManager.getInstance().getUser(userId);
if (user != null)
user.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color)));
else
kill(sessionId);
kill(userId);
}
}

View file

@ -107,7 +107,8 @@ public class MageServerImpl implements MageServer {
public TableView createTable(String sessionId, UUID roomId, MatchOptions options) throws MageException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(sessionId, options);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(userId, options);
logger.info("Table " + table.getTableId() + " created");
return table;
}
@ -122,7 +123,8 @@ public class MageServerImpl implements MageServer {
public TableView createTournamentTable(String sessionId, UUID roomId, TournamentOptions options) throws MageException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTournamentTable(sessionId, options);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTournamentTable(userId, options);
logger.info("Tournament table " + table.getTableId() + " created");
return table;
}
@ -141,7 +143,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TableManager.getInstance().removeTable(sessionId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().removeTable(userId, tableId);
}
}
);
@ -156,7 +159,8 @@ public class MageServerImpl implements MageServer {
public boolean joinTable(String sessionId, UUID roomId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException, GameException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTable(sessionId, tableId, name, playerType, skill, deckList);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTable(userId, tableId, name, playerType, skill, deckList);
logger.info("Session " + sessionId + " joined table " + tableId);
return ret;
}
@ -173,7 +177,8 @@ public class MageServerImpl implements MageServer {
public boolean joinTournamentTable(String sessionId, UUID roomId, UUID tableId, String name, String playerType, int skill) throws MageException, GameException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTournamentTable(sessionId, tableId, name, playerType, skill);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTournamentTable(userId, tableId, name, playerType, skill);
logger.info("Session " + sessionId + " joined table " + tableId);
return ret;
}
@ -190,7 +195,8 @@ public class MageServerImpl implements MageServer {
public boolean submitDeck(String sessionId, UUID tableId, DeckCardLists deckList) throws MageException, GameException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
boolean ret = TableManager.getInstance().submitDeck(sessionId, tableId, deckList);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
boolean ret = TableManager.getInstance().submitDeck(userId, tableId, deckList);
logger.info("Session " + sessionId + " submitted deck");
return ret;
}
@ -218,8 +224,8 @@ public class MageServerImpl implements MageServer {
public List<String> getConnectedPlayers(UUID roomId) throws MageException {
try {
List<String> players = new ArrayList<String>();
for (Session session : SessionManager.getInstance().getSessions().values()) {
players.add(session.getUser().getName());
for (User user : UserManager.getInstance().getUsers()) {
players.add(user.getName());
}
return players;
}
@ -266,7 +272,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TableManager.getInstance().startMatch(sessionId, roomId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().startMatch(userId, roomId, tableId);
}
}
);
@ -285,7 +292,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TableManager.getInstance().startChallenge(sessionId, roomId, tableId, challengeId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().startChallenge(userId, roomId, tableId, challengeId);
}
}
);
@ -304,7 +312,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TableManager.getInstance().startTournament(sessionId, roomId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().startTournament(userId, roomId, tableId);
}
}
);
@ -350,7 +359,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ChatManager.getInstance().joinChat(chatId, sessionId, userName);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ChatManager.getInstance().joinChat(chatId, userId);
}
}
);
@ -367,7 +377,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ChatManager.getInstance().leaveChat(chatId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ChatManager.getInstance().leaveChat(chatId, userId);
}
}
);
@ -402,7 +413,8 @@ public class MageServerImpl implements MageServer {
@Override
public boolean isTableOwner(String sessionId, UUID roomId, UUID tableId) throws MageException {
try {
return TableManager.getInstance().isTableOwner(tableId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return TableManager.getInstance().isTableOwner(tableId, userId);
}
catch (Exception ex) {
handleException(ex);
@ -418,7 +430,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TableManager.getInstance().swapSeats(tableId, sessionId, seatNum1, seatNum2);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().swapSeats(tableId, userId, seatNum1, seatNum2);
}
}
);
@ -437,7 +450,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GamesRoomManager.getInstance().getRoom(roomId).leaveTable(sessionId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GamesRoomManager.getInstance().getRoom(roomId).leaveTable(userId, tableId);
}
}
);
@ -467,7 +481,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().joinGame(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().joinGame(gameId, userId);
}
}
);
@ -486,7 +501,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
DraftManager.getInstance().joinDraft(draftId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
DraftManager.getInstance().joinDraft(draftId, userId);
}
}
);
@ -505,7 +521,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TournamentManager.getInstance().joinTournament(tournamentId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TournamentManager.getInstance().joinTournament(tournamentId, userId);
}
}
);
@ -546,7 +563,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().sendPlayerUUID(gameId, sessionId, data);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().sendPlayerUUID(gameId, userId, data);
}
}
);
@ -565,7 +583,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().sendPlayerString(gameId, sessionId, data);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().sendPlayerString(gameId, userId, data);
}
}
);
@ -584,7 +603,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().sendPlayerBoolean(gameId, sessionId, data);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().sendPlayerBoolean(gameId, userId, data);
}
}
);
@ -603,7 +623,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().sendPlayerInteger(gameId, sessionId, data);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().sendPlayerInteger(gameId, userId, data);
}
}
);
@ -618,7 +639,8 @@ public class MageServerImpl implements MageServer {
public DraftPickView sendCardPick(final UUID draftId, final String sessionId, final UUID cardPick) throws MageException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
return DraftManager.getInstance().sendCardPick(draftId, sessionId, cardPick);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return DraftManager.getInstance().sendCardPick(draftId, userId, cardPick);
}
}
catch (Exception ex) {
@ -635,7 +657,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().concedeGame(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().concedeGame(gameId, userId);
}
}
);
@ -650,7 +673,8 @@ public class MageServerImpl implements MageServer {
public boolean watchTable(String sessionId, UUID roomId, UUID tableId) throws MageException {
try {
if (SessionManager.getInstance().isValidSession(sessionId)) {
return GamesRoomManager.getInstance().getRoom(roomId).watchTable(sessionId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return GamesRoomManager.getInstance().getRoom(roomId).watchTable(userId, tableId);
}
}
catch (Exception ex) {
@ -667,7 +691,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().watchGame(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().watchGame(gameId, userId);
}
}
);
@ -686,7 +711,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().stopWatching(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().stopWatching(gameId, userId);
}
}
);
@ -705,7 +731,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ReplayManager.getInstance().replayGame(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ReplayManager.getInstance().replayGame(gameId, userId);
}
}
);
@ -724,7 +751,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ReplayManager.getInstance().startReplay(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ReplayManager.getInstance().startReplay(gameId, userId);
}
}
);
@ -743,7 +771,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ReplayManager.getInstance().stopReplay(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ReplayManager.getInstance().stopReplay(gameId, userId);
}
}
);
@ -762,7 +791,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ReplayManager.getInstance().nextPlay(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ReplayManager.getInstance().nextPlay(gameId, userId);
}
}
);
@ -781,7 +811,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
ReplayManager.getInstance().previousPlay(gameId, sessionId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
ReplayManager.getInstance().previousPlay(gameId, userId);
}
}
);
@ -817,8 +848,10 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
if (testMode)
GameManager.getInstance().cheat(gameId, sessionId, playerId, deckList);
if (testMode) {
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
GameManager.getInstance().cheat(gameId, userId, playerId, deckList);
}
}
}
);
@ -833,7 +866,8 @@ public class MageServerImpl implements MageServer {
public boolean cheat(final UUID gameId, final String sessionId, final UUID playerId, final String cardName) throws MageException {
if (testMode) {
if (SessionManager.getInstance().isValidSession(sessionId)) {
return GameManager.getInstance().cheat(gameId, sessionId, playerId, cardName);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return GameManager.getInstance().cheat(gameId, userId, playerId, cardName);
}
}
return false;
@ -847,15 +881,20 @@ public class MageServerImpl implements MageServer {
@Override
public GameView getGameView(final UUID gameId, final String sessionId, final UUID playerId) {
if (SessionManager.getInstance().isValidSession(sessionId)) {
return GameManager.getInstance().getGameView(gameId, sessionId, playerId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
return GameManager.getInstance().getGameView(gameId, userId, playerId);
}
return null;
}
@Override
public List<UserView> getUsers(String sessionId) throws MageException {
if (SessionManager.getInstance().isValidSession(sessionId)) {
return SessionManager.getInstance().getUsers(sessionId);
if (SessionManager.getInstance().isValidSession(sessionId) && SessionManager.getInstance().isAdmin(sessionId)) {
List<UserView> users = new ArrayList<UserView>();
for (User user: UserManager.getInstance().getUsers()) {
users.add(new UserView(user.getName(), "", user.getSessionId(), user.getConnectionTime()));
}
return users;
}
return null;
}
@ -887,7 +926,8 @@ public class MageServerImpl implements MageServer {
new Runnable() {
@Override
public void run() {
TableManager.getInstance().removeTable(sessionId, tableId);
UUID userId = SessionManager.getInstance().getSession(sessionId).getUserId();
TableManager.getInstance().removeTable(userId, tableId);
}
}
);

View file

@ -131,7 +131,12 @@ public class Main {
public void handleConnectionException(Throwable throwable, Client client) {
Session session = SessionManager.getInstance().getSession(client.getSessionId());
if (session != null) {
String sessionName = session.getUser().getName() + " at " + session.getHost();
String sessionName;
User user = UserManager.getInstance().getUser(session.getUserId());
if (user != null)
sessionName = user.getName() + " at " + session.getHost();
else
sessionName = session.getHost();
if (throwable instanceof ClientDisconnectedException) {
logger.info("client disconnected - " + sessionName);
}

View file

@ -31,10 +31,7 @@ package mage.server;
import java.util.Date;
import java.util.UUID;
import mage.MageException;
import mage.cards.decks.Deck;
import mage.interfaces.callback.ClientCallback;
import mage.server.game.GameManager;
import mage.view.TableClientMessage;
import org.apache.log4j.Logger;
import org.jboss.remoting.callback.AsynchInvokerCallbackHandler;
import org.jboss.remoting.callback.Callback;
@ -50,8 +47,9 @@ public class Session {
private final static Logger logger = Logger.getLogger(Session.class);
private String sessionId;
private User user;
private UUID userId;
private String host;
private int messageId = 0;
private Date timeConnected;
private boolean isAdmin = false;
private AsynchInvokerCallbackHandler callbackHandler;
@ -65,25 +63,30 @@ public class Session {
public void registerUser(String userName) throws MageException {
this.isAdmin = false;
User user = UserManager.getInstance().findUser(userName);
if (user == null) {
user = UserManager.getInstance().createUser(userName, host);
}
else {
if (userName.equals("Admin"))
throw new MageException("User name already in use");
User user = UserManager.getInstance().createUser(userName, host);
if (user == null) { // user already exists
user = UserManager.getInstance().findUser(userName);
if (user.getHost().equals(host)) {
logger.info("Reconnecting session for " + userName);
if (user.getSessionId().isEmpty())
logger.info("Reconnecting session for " + userName);
else
throw new MageException("This machine is already connected");
}
else {
throw new MageException("User name already in use");
}
}
UserManager.getInstance().connectToSession(sessionId, userName);
this.user = user;
if (!UserManager.getInstance().connectToSession(sessionId, user.getId()))
throw new MageException("Error connecting");
this.userId = user.getId();
}
public void registerAdmin() {
this.isAdmin = true;
this.user = UserManager.getInstance().createUser("Admin", host);
User user = UserManager.getInstance().createUser("Admin", host);
this.userId = user.getId();
}
public String getId() {
@ -91,50 +94,23 @@ public class Session {
}
public void kill() {
TableManager.getInstance().removeSession(sessionId);
GameManager.getInstance().removeSession(sessionId);
ChatManager.getInstance().removeSession(sessionId);
UserManager.getInstance().disconnect(user.getName());
// TableManager.getInstance().removeSession(sessionId);
// GameManager.getInstance().removeSession(sessionId);
// ChatManager.getInstance().removeSession(sessionId);
UserManager.getInstance().disconnect(userId);
}
public synchronized void fireCallback(final ClientCallback call) {
synchronized void fireCallback(final ClientCallback call) {
try {
call.setMessageId(messageId++);
callbackHandler.handleCallbackOneway(new Callback(call));
} catch (HandleCallbackException ex) {
logger.fatal("Session fireCallback error", ex);
}
}
public void gameStarted(final UUID gameId, final UUID playerId) {
fireCallback(new ClientCallback("startGame", gameId, new TableClientMessage(gameId, playerId)));
}
public void draftStarted(final UUID draftId, final UUID playerId) {
fireCallback(new ClientCallback("startDraft", draftId, new TableClientMessage(draftId, playerId)));
}
public void tournamentStarted(final UUID tournamentId, final UUID playerId) {
fireCallback(new ClientCallback("startTournament", tournamentId, new TableClientMessage(tournamentId, playerId)));
}
public void sideboard(final Deck deck, final UUID tableId, final int time) {
fireCallback(new ClientCallback("sideboard", tableId, new TableClientMessage(deck, tableId, time)));
}
public void construct(final Deck deck, final UUID tableId, final int time) {
fireCallback(new ClientCallback("construct", tableId, new TableClientMessage(deck, tableId, time)));
}
public void watchGame(final UUID gameId) {
fireCallback(new ClientCallback("watchGame", gameId));
}
public void replayGame(final UUID gameId) {
fireCallback(new ClientCallback("replayGame", gameId));
}
public User getUser() {
return user;
public UUID getUserId() {
return userId;
}
public boolean isAdmin() {

View file

@ -99,17 +99,6 @@ public class SessionManager {
return map;
}
List<UserView> getUsers(String sessionId) {
List<UserView> users = new ArrayList<UserView>();
Session admin = sessions.get(sessionId);
if (admin != null && admin.isAdmin()) {
for (Session session: sessions.values()) {
users.add(new UserView(session.getUser().getName(), "", session.getId(), session.getConnectionTime()));
}
}
return users;
}
public void disconnectUser(String sessionId, String userSessionId) {
if (isAdmin(sessionId)) {
Session session = sessions.get(userSessionId);

View file

@ -28,7 +28,6 @@
package mage.server;
import java.util.logging.Level;
import mage.Constants.RangeOfInfluence;
import mage.Constants.TableState;
import mage.cards.decks.Deck;
@ -67,36 +66,38 @@ public class TableController {
private final static Logger logger = Logger.getLogger(TableController.class);
private String sessionId;
private UUID userId;
private UUID chatId;
private String controllerName;
private Table table;
private Match match;
private MatchOptions options;
private Tournament tournament;
private ConcurrentHashMap<String, UUID> sessionPlayerMap = new ConcurrentHashMap<String, UUID>();
private ConcurrentHashMap<UUID, UUID> userPlayerMap = new ConcurrentHashMap<UUID, UUID>();
public TableController(UUID roomId, String sessionId, MatchOptions options) {
this.sessionId = sessionId;
public TableController(UUID roomId, UUID userId, MatchOptions options) {
this.userId = userId;
chatId = ChatManager.getInstance().createChatSession();
this.options = options;
match = GameFactory.getInstance().createMatch(options.getGameType(), options);
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null)
controllerName = session.getUser().getName();
if (userId != null) {
User user = UserManager.getInstance().getUser(userId);
controllerName = user.getName();
}
else
controllerName = "System";
table = new Table(roomId, options.getGameType(), options.getName(), controllerName, DeckValidatorFactory.getInstance().createDeckValidator(options.getDeckType()), options.getPlayerTypes(), match);
init();
}
public TableController(UUID roomId, String sessionId, TournamentOptions options) {
this.sessionId = sessionId;
public TableController(UUID roomId, UUID userId, TournamentOptions options) {
this.userId = userId;
chatId = ChatManager.getInstance().createChatSession();
tournament = TournamentFactory.getInstance().createTournament(options.getTournamentType(), options);
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null)
controllerName = session.getUser().getName();
if (userId != null) {
User user = UserManager.getInstance().getUser(userId);
controllerName = user.getName();
}
else
controllerName = "System";
table = new Table(roomId, options.getTournamentType(), options.getName(), controllerName, DeckValidatorFactory.getInstance().createDeckValidator(options.getMatchOptions().getDeckType()), options.getPlayerTypes(), tournament);
@ -124,7 +125,7 @@ public class TableController {
);
}
public synchronized boolean joinTournament(String sessionId, String name, String playerType, int skill) throws GameException {
public synchronized boolean joinTournament(UUID userId, String name, String playerType, int skill) throws GameException {
if (table.getState() != TableState.WAITING) {
return false;
}
@ -138,13 +139,13 @@ public class TableController {
logger.info("player joined " + player.getId());
//only add human players to sessionPlayerMap
if (seat.getPlayer().isHuman()) {
sessionPlayerMap.put(sessionId, player.getId());
userPlayerMap.put(userId, player.getId());
}
return true;
}
public synchronized boolean joinTable(String sessionId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
public synchronized boolean joinTable(UUID userId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
if (table.getState() != TableState.WAITING) {
return false;
}
@ -163,13 +164,13 @@ public class TableController {
logger.info("player joined " + player.getId());
//only add human players to sessionPlayerMap
if (seat.getPlayer().isHuman()) {
sessionPlayerMap.put(sessionId, player.getId());
userPlayerMap.put(userId, player.getId());
}
return true;
}
public void addPlayer(String sessionId, Player player, String playerType, Deck deck) throws GameException {
public void addPlayer(UUID userId, Player player, String playerType, Deck deck) throws GameException {
if (table.getState() != TableState.WAITING) {
return;
}
@ -180,15 +181,12 @@ public class TableController {
match.addPlayer(player, deck);
table.joinTable(player, seat);
if (player.isHuman()) {
sessionPlayerMap.put(sessionId, player.getId());
userPlayerMap.put(userId, player.getId());
}
}
public boolean submitDeck(String sessionId, DeckCardLists deckList) throws MageException {
return submitDeck(sessionPlayerMap.get(sessionId), deckList);
}
public synchronized boolean submitDeck(UUID playerId, DeckCardLists deckList) throws MageException {
public synchronized boolean submitDeck(UUID userId, DeckCardLists deckList) throws MageException {
UUID playerId = userPlayerMap.get(userId);
if (table.getState() != TableState.SIDEBOARDING && table.getState() != TableState.CONSTRUCTING) {
return false;
}
@ -209,19 +207,19 @@ public class TableController {
}
}
public boolean watchTable(String sessionId) {
public boolean watchTable(UUID userId) {
if (table.getState() != TableState.DUELING) {
return false;
}
SessionManager.getInstance().getSession(sessionId).watchGame(match.getGame().getId());
UserManager.getInstance().getUser(userId).watchGame(match.getGame().getId());
return true;
}
public boolean replayTable(String sessionId) {
public boolean replayTable(UUID userId) {
if (table.getState() != TableState.FINISHED) {
return false;
}
ReplayManager.getInstance().replayGame(table.getId(), sessionId);
ReplayManager.getInstance().replayGame(table.getId(), userId);
return true;
}
@ -237,24 +235,24 @@ public class TableController {
return player;
}
public void kill(String sessionId) {
leaveTable(sessionId);
sessionPlayerMap.remove(sessionId);
public void kill(UUID userId) {
leaveTable(userId);
userPlayerMap.remove(userId);
}
public synchronized void leaveTable(String sessionId) {
public synchronized void leaveTable(UUID userId) {
if (table.getState() == TableState.WAITING || table.getState() == TableState.STARTING)
table.leaveTable(sessionPlayerMap.get(sessionId));
table.leaveTable(userPlayerMap.get(userId));
}
public synchronized void startMatch(String sessionId) {
if (sessionId.equals(this.sessionId)) {
public synchronized void startMatch(UUID userId) {
if (userId.equals(this.userId)) {
startMatch();
}
}
public synchronized void startChallenge(String sessionId, UUID challengeId) {
if (sessionId.equals(this.sessionId)) {
public synchronized void startChallenge(UUID userId, UUID challengeId) {
if (userId.equals(this.userId)) {
try {
match.startMatch();
match.startGame();
@ -262,11 +260,10 @@ public class TableController {
GameOptions options = new GameOptions();
options.testMode = true;
// match.getGame().setGameOptions(options);
GameManager.getInstance().createGameSession(match.getGame(), sessionPlayerMap, table.getId(), null);
GameManager.getInstance().createGameSession(match.getGame(), userPlayerMap, table.getId(), null);
ChallengeManager.getInstance().prepareChallenge(getPlayerId(), match);
SessionManager sessionManager = SessionManager.getInstance();
for (Entry<String, UUID> entry: sessionPlayerMap.entrySet()) {
sessionManager.getSession(entry.getKey()).gameStarted(match.getGame().getId(), entry.getValue());
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
UserManager.getInstance().getUser(entry.getKey()).gameStarted(match.getGame().getId(), entry.getValue());
}
} catch (GameException ex) {
logger.fatal(null, ex);
@ -276,7 +273,7 @@ public class TableController {
private UUID getPlayerId() throws GameException {
UUID playerId = null;
for (Entry<String, UUID> entry : sessionPlayerMap.entrySet()) {
for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
playerId = entry.getValue();
break;
}
@ -301,12 +298,11 @@ public class TableController {
try {
match.startGame();
table.initGame();
GameManager.getInstance().createGameSession(match.getGame(), sessionPlayerMap, table.getId(), choosingPlayerId);
SessionManager sessionManager = SessionManager.getInstance();
for (Entry<String, UUID> entry: sessionPlayerMap.entrySet()) {
Session session = sessionManager.getSession(entry.getKey());
if (session != null) {
session.gameStarted(match.getGame().getId(), entry.getValue());
GameManager.getInstance().createGameSession(match.getGame(), userPlayerMap, table.getId(), choosingPlayerId);
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
User user = UserManager.getInstance().getUser(entry.getKey());
if (user != null) {
user.gameStarted(match.getGame().getId(), entry.getValue());
}
else {
TableManager.getInstance().removeTable(table.getId());
@ -323,38 +319,35 @@ public class TableController {
}
}
public synchronized void startTournament(String sessionId) {
public synchronized void startTournament(UUID userId) {
try {
if (sessionId.equals(this.sessionId) && table.getState() == TableState.STARTING) {
TournamentManager.getInstance().createTournamentSession(tournament, sessionPlayerMap, table.getId());
SessionManager sessionManager = SessionManager.getInstance();
for (Entry<String, UUID> entry: sessionPlayerMap.entrySet()) {
Session session = sessionManager.getSession(entry.getKey());
session.tournamentStarted(tournament.getId(), entry.getValue());
if (userId.equals(this.userId) && table.getState() == TableState.STARTING) {
TournamentManager.getInstance().createTournamentSession(tournament, userPlayerMap, table.getId());
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
User user = UserManager.getInstance().getUser(entry.getKey());
user.tournamentStarted(tournament.getId(), entry.getValue());
}
}
}
catch (Exception ex) {
logger.fatal("Error starting tournament", ex);
TableManager.getInstance().removeTable(table.getId());
TournamentManager.getInstance().kill(tournament.getId(), sessionId);
TournamentManager.getInstance().kill(tournament.getId(), userId);
}
}
public void startDraft(Draft draft) {
table.initDraft();
DraftManager.getInstance().createDraftSession(draft, sessionPlayerMap, table.getId());
SessionManager sessionManager = SessionManager.getInstance();
for (Entry<String, UUID> entry: sessionPlayerMap.entrySet()) {
sessionManager.getSession(entry.getKey()).draftStarted(draft.getId(), entry.getValue());
DraftManager.getInstance().createDraftSession(draft, userPlayerMap, table.getId());
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
UserManager.getInstance().getUser(entry.getKey()).draftStarted(draft.getId(), entry.getValue());
}
}
private void sideboard(UUID playerId, Deck deck, int timeout) throws MageException {
SessionManager sessionManager = SessionManager.getInstance();
for (Entry<String, UUID> entry: sessionPlayerMap.entrySet()) {
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
if (entry.getValue().equals(playerId)) {
sessionManager.getSession(entry.getKey()).sideboard(deck, table.getId(), timeout);
UserManager.getInstance().getUser(entry.getKey()).sideboard(deck, table.getId(), timeout);
break;
}
}
@ -404,8 +397,8 @@ public class TableController {
}
}
public boolean isOwner(String sessionId) {
return sessionId.equals(this.sessionId);
public boolean isOwner(UUID userId) {
return userId.equals(this.userId);
}
public Table getTable() {

View file

@ -60,22 +60,22 @@ public class TableManager {
return INSTANCE;
}
public Table createTable(UUID roomId, String sessionId, MatchOptions options) {
TableController tableController = new TableController(roomId, sessionId, options);
public Table createTable(UUID roomId, UUID userId, MatchOptions options) {
TableController tableController = new TableController(roomId, userId, options);
controllers.put(tableController.getTable().getId(), tableController);
tables.put(tableController.getTable().getId(), tableController.getTable());
return tableController.getTable();
}
public Table createTable(UUID roomId, MatchOptions options) {
TableController tableController = new TableController(roomId, UUID.randomUUID().toString(), options);
TableController tableController = new TableController(roomId, null, options);
controllers.put(tableController.getTable().getId(), tableController);
tables.put(tableController.getTable().getId(), tableController.getTable());
return tableController.getTable();
}
public Table createTournamentTable(UUID roomId, String sessionId, TournamentOptions options) {
TableController tableController = new TableController(roomId, sessionId, options);
public Table createTournamentTable(UUID roomId, UUID userId, TournamentOptions options) {
TableController tableController = new TableController(roomId, userId, options);
controllers.put(tableController.getTable().getId(), tableController);
tables.put(tableController.getTable().getId(), tableController.getTable());
return tableController.getTable();
@ -95,47 +95,47 @@ public class TableManager {
return tables.values();
}
public boolean joinTable(String sessionId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
if (controllers.containsKey(tableId))
return controllers.get(tableId).joinTable(sessionId, name, playerType, skill, deckList);
return controllers.get(tableId).joinTable(userId, name, playerType, skill, deckList);
return false;
}
public boolean joinTournament(String sessionId, UUID tableId, String name, String playerType, int skill) throws GameException {
public boolean joinTournament(UUID userId, UUID tableId, String name, String playerType, int skill) throws GameException {
if (controllers.containsKey(tableId))
return controllers.get(tableId).joinTournament(sessionId, name, playerType, skill);
return controllers.get(tableId).joinTournament(userId, name, playerType, skill);
return false;
}
public boolean submitDeck(String sessionId, UUID tableId, DeckCardLists deckList) throws MageException {
public boolean submitDeck(UUID userId, UUID tableId, DeckCardLists deckList) throws MageException {
if (controllers.containsKey(tableId))
return controllers.get(tableId).submitDeck(sessionId, deckList);
return controllers.get(tableId).submitDeck(userId, deckList);
return false;
}
public void removeSession(String sessionId) {
public void removeSession(UUID userId) {
for (TableController controller: controllers.values()) {
controller.kill(sessionId);
controller.kill(userId);
}
}
public boolean isTableOwner(UUID tableId, String sessionId) {
public boolean isTableOwner(UUID tableId, UUID userId) {
if (controllers.containsKey(tableId))
return controllers.get(tableId).isOwner(sessionId);
return controllers.get(tableId).isOwner(userId);
return false;
}
public boolean removeTable(String sessionId, UUID tableId) {
if (isTableOwner(tableId, sessionId) || SessionManager.getInstance().isAdmin(sessionId)) {
public boolean removeTable(UUID userId, UUID tableId) {
if (isTableOwner(tableId, userId) || UserManager.getInstance().isAdmin(userId)) {
removeTable(tableId);
return true;
}
return false;
}
public void leaveTable(String sessionId, UUID tableId) {
public void leaveTable(UUID userId, UUID tableId) {
if (controllers.containsKey(tableId))
controllers.get(tableId).leaveTable(sessionId);
controllers.get(tableId).leaveTable(userId);
}
public UUID getChatId(UUID tableId) {
@ -144,9 +144,9 @@ public class TableManager {
return null;
}
public void startMatch(String sessionId, UUID roomId, UUID tableId) {
public void startMatch(UUID userId, UUID roomId, UUID tableId) {
if (controllers.containsKey(tableId))
controllers.get(tableId).startMatch(sessionId);
controllers.get(tableId).startMatch(userId);
}
public void startMatch(UUID roomId, UUID tableId) {
@ -154,14 +154,14 @@ public class TableManager {
controllers.get(tableId).startMatch();
}
public void startChallenge(String sessionId, UUID roomId, UUID tableId, UUID challengeId) {
public void startChallenge(UUID userId, UUID roomId, UUID tableId, UUID challengeId) {
if (controllers.containsKey(tableId))
controllers.get(tableId).startChallenge(sessionId, challengeId);
controllers.get(tableId).startChallenge(userId, challengeId);
}
public void startTournament(String sessionId, UUID roomId, UUID tableId) {
public void startTournament(UUID userId, UUID roomId, UUID tableId) {
if (controllers.containsKey(tableId))
controllers.get(tableId).startTournament(sessionId);
controllers.get(tableId).startTournament(userId);
}
public void startDraft(UUID tableId, Draft draft) {
@ -169,15 +169,15 @@ public class TableManager {
controllers.get(tableId).startDraft(draft);
}
public boolean watchTable(String sessionId, UUID tableId) {
public boolean watchTable(UUID userId, UUID tableId) {
if (controllers.containsKey(tableId))
return controllers.get(tableId).watchTable(sessionId);
return controllers.get(tableId).watchTable(userId);
return false;
}
public boolean replayTable(String sessionId, UUID tableId) {
public boolean replayTable(UUID userId, UUID tableId) {
if (controllers.containsKey(tableId))
return controllers.get(tableId).replayTable(sessionId);
return controllers.get(tableId).replayTable(userId);
return false;
}
@ -191,8 +191,8 @@ public class TableManager {
controllers.get(tableId).endDraft(draft);
}
public void swapSeats(UUID tableId, String sessionId, int seatNum1, int seatNum2) {
if (controllers.containsKey(tableId) && isTableOwner(tableId, sessionId)) {
public void swapSeats(UUID tableId, UUID userId, int seatNum1, int seatNum2) {
if (controllers.containsKey(tableId) && isTableOwner(tableId, userId)) {
controllers.get(tableId).swapSeats(seatNum1, seatNum2);
}
}
@ -202,9 +202,9 @@ public class TableManager {
controllers.get(tableId).construct();
}
public void addPlayer(String sessionId, UUID tableId, Player player, String playerType, Deck deck) throws GameException {
public void addPlayer(UUID userId, UUID tableId, Player player, String playerType, Deck deck) throws GameException {
if (controllers.containsKey(tableId))
controllers.get(tableId).addPlayer(sessionId, player, playerType, deck);
controllers.get(tableId).addPlayer(userId, player, playerType, deck);
}
public void removeTable(UUID tableId) {

View file

@ -27,7 +27,12 @@
*/
package mage.server;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import mage.cards.decks.Deck;
import mage.interfaces.callback.ClientCallback;
import mage.view.TableClientMessage;
/**
*
@ -35,14 +40,23 @@ import java.util.UUID;
*/
public class User {
public enum UserState {
Created, Connected, Disconnected, Reconnected;
}
private UUID userId = UUID.randomUUID();
private String userName;
private String sessionId;
private String sessionId = "";
private String host;
private Date connectionTime = new Date();
private Date lastActivity = new Date();
private UserState userState;
private CountDownLatch connectionSignal = new CountDownLatch(1);
public User(String userName, String host) {
this.userName = userName;
this.host = host;
this.userState = UserState.Created;
}
public String getName() {
@ -63,5 +77,60 @@ public class User {
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
if (sessionId.isEmpty())
userState = UserState.Disconnected;
else if (userState == UserState.Created)
userState = UserState.Connected;
else {
userState = UserState.Reconnected;
reconnect();
}
}
public boolean isConnected() {
return userState == UserState.Connected;
}
public Date getConnectionTime() {
return connectionTime;
}
public synchronized void fireCallback(final ClientCallback call) {
if (isConnected()) {
Session session = SessionManager.getInstance().getSession(sessionId);
session.fireCallback(call);
}
}
public void gameStarted(final UUID gameId, final UUID playerId) {
fireCallback(new ClientCallback("startGame", gameId, new TableClientMessage(gameId, playerId)));
}
public void draftStarted(final UUID draftId, final UUID playerId) {
fireCallback(new ClientCallback("startDraft", draftId, new TableClientMessage(draftId, playerId)));
}
public void tournamentStarted(final UUID tournamentId, final UUID playerId) {
fireCallback(new ClientCallback("startTournament", tournamentId, new TableClientMessage(tournamentId, playerId)));
}
public void sideboard(final Deck deck, final UUID tableId, final int time) {
fireCallback(new ClientCallback("sideboard", tableId, new TableClientMessage(deck, tableId, time)));
}
public void construct(final Deck deck, final UUID tableId, final int time) {
fireCallback(new ClientCallback("construct", tableId, new TableClientMessage(deck, tableId, time)));
}
public void watchGame(final UUID gameId) {
fireCallback(new ClientCallback("watchGame", gameId));
}
public void replayGame(final UUID gameId) {
fireCallback(new ClientCallback("replayGame", gameId));
}
private void reconnect() {
}
}

View file

@ -27,6 +27,7 @@
*/
package mage.server;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -44,30 +45,51 @@ public class UserManager {
private UserManager() {}
private ConcurrentHashMap<String, User> users = new ConcurrentHashMap<String, User>();
private ConcurrentHashMap<UUID, User> users = new ConcurrentHashMap<UUID, User>();
public User createUser(String userName, String host) {
if (users.containsKey(userName))
return null;
if (findUser(userName) != null)
return null; //user already exists
User user = new User(userName, host);
users.put(userName, user);
users.put(user.getId(), user);
return user;
}
public User getUser(UUID userId) {
return users.get(userId);
}
public User findUser(String userName) {
return users.get(userName);
for (User user: users.values()) {
if (user.getName().equals(userName))
return user;
}
return null;
}
public Collection<User> getUsers() {
return users.values();
}
public void connectToSession(String sessionId, String userName) {
if (users.containsKey(userName)) {
users.get(userName).setSessionId(sessionId);
public boolean connectToSession(String sessionId, UUID userId) {
if (users.containsKey(userId)) {
users.get(userId).setSessionId(sessionId);
return true;
}
return false;
}
public void disconnect(UUID userId) {
if (users.containsKey(userId)) {
users.get(userId).setSessionId("");
}
}
public void disconnect(String userName) {
if (users.containsKey(userName)) {
users.get(userName).setSessionId("");
public boolean isAdmin(UUID userId) {
if (users.containsKey(userId)) {
return users.get(userId).getName().equals("Admin");
}
return false;
}
}

View file

@ -41,6 +41,7 @@ import mage.game.events.TableEvent;
import mage.MageException;
import mage.server.game.GameController;
import mage.server.TableManager;
import mage.server.UserManager;
import mage.server.util.ThreadExecutor;
import mage.view.DraftPickView;
import mage.view.DraftView;
@ -56,14 +57,14 @@ public class DraftController {
public static final String INIT_FILE_PATH = "config" + File.separator + "init.txt";
private ConcurrentHashMap<UUID, DraftSession> draftSessions = new ConcurrentHashMap<UUID, DraftSession>();
private ConcurrentHashMap<String, UUID> sessionPlayerMap;
private ConcurrentHashMap<UUID, UUID> userPlayerMap;
private UUID draftSessionId;
private Draft draft;
private UUID tableId;
public DraftController(Draft draft, ConcurrentHashMap<String, UUID> sessionPlayerMap, UUID tableId) {
public DraftController(Draft draft, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
draftSessionId = UUID.randomUUID();
this.sessionPlayerMap = sessionPlayerMap;
this.userPlayerMap = userPlayerMap;
this.draft = draft;
this.tableId = tableId;
init();
@ -116,15 +117,15 @@ public class DraftController {
checkStart();
}
private UUID getPlayerId(String sessionId) {
return sessionPlayerMap.get(sessionId);
private UUID getPlayerId(UUID userId) {
return userPlayerMap.get(userId);
}
public void join(String sessionId) {
UUID playerId = sessionPlayerMap.get(sessionId);
DraftSession draftSession = new DraftSession(draft, sessionId, playerId);
public void join(UUID userId) {
UUID playerId = userPlayerMap.get(userId);
DraftSession draftSession = new DraftSession(draft, userId, playerId);
draftSessions.put(playerId, draftSession);
logger.info("player " + playerId + " has joined draft " + draft.getId());
logger.info("User " + UserManager.getInstance().getUser(userId).getName() + " has joined draft " + draft.getId());
draft.getPlayer(playerId).setJoined();
checkStart();
}
@ -163,8 +164,8 @@ public class DraftController {
return true;
}
private void leave(String sessionId) {
draft.leave(getPlayerId(sessionId));
private void leave(UUID userId) {
draft.leave(getPlayerId(userId));
}
private void endDraft() throws MageException {
@ -174,18 +175,18 @@ public class DraftController {
TableManager.getInstance().endDraft(tableId, draft);
}
public void kill(String sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) {
draftSessions.get(sessionPlayerMap.get(sessionId)).setKilled();
draftSessions.remove(sessionPlayerMap.get(sessionId));
leave(sessionId);
sessionPlayerMap.remove(sessionId);
public void kill(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
draftSessions.get(userPlayerMap.get(userId)).setKilled();
draftSessions.remove(userPlayerMap.get(userId));
leave(userId);
userPlayerMap.remove(userId);
}
}
public void timeout(String sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) {
draft.autoPick(sessionPlayerMap.get(sessionId));
public void timeout(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
draft.autoPick(userPlayerMap.get(userId));
}
}
@ -193,9 +194,9 @@ public class DraftController {
return this.draftSessionId;
}
public DraftPickView sendCardPick(String sessionId, UUID cardId) {
if (draftSessions.get(sessionPlayerMap.get(sessionId)).sendCardPick(cardId)) {
return getDraftPickView(sessionPlayerMap.get(sessionId), 0);
public DraftPickView sendCardPick(UUID userId, UUID cardId) {
if (draftSessions.get(userPlayerMap.get(userId)).sendCardPick(cardId)) {
return getDraftPickView(userPlayerMap.get(userId), 0);
}
return null;
}

View file

@ -48,36 +48,36 @@ public class DraftManager {
private ConcurrentHashMap<UUID, DraftController> draftControllers = new ConcurrentHashMap<UUID, DraftController>();
public UUID createDraftSession(Draft draft, ConcurrentHashMap<String, UUID> sessionPlayerMap, UUID tableId) {
DraftController draftController = new DraftController(draft, sessionPlayerMap, tableId);
public UUID createDraftSession(Draft draft, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
DraftController draftController = new DraftController(draft, userPlayerMap, tableId);
draftControllers.put(draft.getId(), draftController);
return draftController.getSessionId();
}
public void joinDraft(UUID draftId, String sessionId) {
draftControllers.get(draftId).join(sessionId);
public void joinDraft(UUID draftId, UUID userId) {
draftControllers.get(draftId).join(userId);
}
public void destroyChatSession(UUID gameId) {
draftControllers.remove(gameId);
}
public DraftPickView sendCardPick(UUID draftId, String sessionId, UUID cardId) {
return draftControllers.get(draftId).sendCardPick(sessionId, cardId);
public DraftPickView sendCardPick(UUID draftId, UUID userId, UUID cardId) {
return draftControllers.get(draftId).sendCardPick(userId, cardId);
}
public void removeSession(String sessionId) {
public void removeSession(UUID userId) {
for (DraftController controller: draftControllers.values()) {
controller.kill(sessionId);
controller.kill(userId);
}
}
public void kill(UUID draftId, String sessionId) {
draftControllers.get(draftId).kill(sessionId);
public void kill(UUID draftId, UUID userId) {
draftControllers.get(draftId).kill(userId);
}
public void timeout(UUID gameId, String sessionId) {
draftControllers.get(gameId).timeout(sessionId);
public void timeout(UUID gameId, UUID userId) {
draftControllers.get(gameId).timeout(userId);
}
public void removeDraft(UUID draftId) {

View file

@ -35,8 +35,8 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import mage.game.draft.Draft;
import mage.interfaces.callback.ClientCallback;
import mage.server.Session;
import mage.server.SessionManager;
import mage.server.User;
import mage.server.UserManager;
import mage.server.util.ThreadExecutor;
import mage.view.DraftClientMessage;
import mage.view.DraftPickView;
@ -51,7 +51,7 @@ public class DraftSession {
protected final static Logger logger = Logger.getLogger(DraftSession.class);
protected String sessionId;
protected UUID userId;
protected UUID playerId;
protected Draft draft;
protected boolean killed = false;
@ -59,17 +59,17 @@ public class DraftSession {
private ScheduledFuture<?> futureTimeout;
protected static ScheduledExecutorService timeoutExecutor = ThreadExecutor.getInstance().getTimeoutExecutor();
public DraftSession(Draft draft, String sessionId, UUID playerId) {
this.sessionId = sessionId;
public DraftSession(Draft draft, UUID userId, UUID playerId) {
this.userId = userId;
this.draft = draft;
this.playerId = playerId;
}
public boolean init(final DraftView draftView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("draftInit", draft.getId(), draftView));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("draftInit", draft.getId(), draftView));
return true;
}
}
@ -86,27 +86,27 @@ public class DraftSession {
public void update(final DraftView draftView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("draftUpdate", draft.getId(), draftView));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("draftUpdate", draft.getId(), draftView));
}
}
}
public void inform(final String message, final DraftView draftView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("draftInform", draft.getId(), new DraftClientMessage(draftView, message)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("draftInform", draft.getId(), new DraftClientMessage(draftView, message)));
}
}
}
public void draftOver() {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("draftOver", draft.getId()));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("draftOver", draft.getId()));
}
}
}
@ -114,9 +114,9 @@ public class DraftSession {
public void pickCard(final DraftPickView draftPickView, int timeout) {
if (!killed) {
setupTimeout(timeout);
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("draftPick", draft.getId(), new DraftClientMessage(draftPickView)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("draftPick", draft.getId(), new DraftClientMessage(draftPickView)));
}
}
}
@ -128,7 +128,7 @@ public class DraftSession {
new Runnable() {
@Override
public void run() {
DraftManager.getInstance().timeout(draft.getId(), sessionId);
DraftManager.getInstance().timeout(draft.getId(), userId);
}
},
seconds, TimeUnit.SECONDS
@ -144,7 +144,7 @@ public class DraftSession {
protected void handleRemoteException(RemoteException ex) {
logger.fatal("DraftSession error ", ex);
DraftManager.getInstance().kill(draft.getId(), sessionId);
DraftManager.getInstance().kill(draft.getId(), userId);
}
public void setKilled() {

View file

@ -29,9 +29,7 @@
package mage.server.game;
import java.io.BufferedOutputStream;
import java.util.logging.Level;
import mage.game.LookedAt;
import mage.MageException;
import mage.server.TableManager;
import java.io.File;
@ -81,8 +79,8 @@ public class GameController implements GameCallback {
public static final String INIT_FILE_PATH = "config" + File.separator + "init.txt";
private ConcurrentHashMap<UUID, GameSession> gameSessions = new ConcurrentHashMap<UUID, GameSession>();
private ConcurrentHashMap<String, GameWatcher> watchers = new ConcurrentHashMap<String, GameWatcher>();
private ConcurrentHashMap<String, UUID> sessionPlayerMap;
private ConcurrentHashMap<UUID, GameWatcher> watchers = new ConcurrentHashMap<UUID, GameWatcher>();
private ConcurrentHashMap<UUID, UUID> userPlayerMap;
private UUID gameSessionId;
private Game game;
private UUID chatId;
@ -91,9 +89,9 @@ public class GameController implements GameCallback {
private Future<?> gameFuture;
public GameController(Game game, ConcurrentHashMap<String, UUID> sessionPlayerMap, UUID tableId, UUID choosingPlayerId) {
public GameController(Game game, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId, UUID choosingPlayerId) {
gameSessionId = UUID.randomUUID();
this.sessionPlayerMap = sessionPlayerMap;
this.userPlayerMap = userPlayerMap;
chatId = ChatManager.getInstance().createChatSession();
this.game = game;
this.tableId = tableId;
@ -180,13 +178,13 @@ public class GameController implements GameCallback {
checkStart();
}
private UUID getPlayerId(String sessionId) {
return sessionPlayerMap.get(sessionId);
private UUID getPlayerId(UUID userId) {
return userPlayerMap.get(userId);
}
public void join(String sessionId) {
UUID playerId = sessionPlayerMap.get(sessionId);
GameSession gameSession = new GameSession(game, sessionId, playerId);
public void join(UUID userId) {
UUID playerId = userPlayerMap.get(userId);
GameSession gameSession = new GameSession(game, userId, playerId);
gameSessions.put(playerId, gameSession);
logger.info("player " + playerId + " has joined game " + game.getId());
ChatManager.getInstance().broadcast(chatId, "", game.getPlayer(playerId).getName() + " has joined the game", MessageColor.BLACK);
@ -228,27 +226,27 @@ public class GameController implements GameCallback {
return true;
}
public void watch(String sessionId) {
GameWatcher gameWatcher = new GameWatcher(sessionId, game.getId());
watchers.put(sessionId, gameWatcher);
public void watch(UUID userId) {
GameWatcher gameWatcher = new GameWatcher(userId, game.getId());
watchers.put(userId, gameWatcher);
gameWatcher.init(getGameView());
ChatManager.getInstance().broadcast(chatId, "", " has started watching", MessageColor.BLACK);
}
public void stopWatching(String sessionId) {
watchers.remove(sessionId);
public void stopWatching(UUID userId) {
watchers.remove(userId);
ChatManager.getInstance().broadcast(chatId, "", " has stopped watching", MessageColor.BLACK);
}
public void concede(String sessionId) {
game.concede(getPlayerId(sessionId));
public void concede(UUID userId) {
game.concede(getPlayerId(userId));
}
private void leave(String sessionId) {
game.quit(getPlayerId(sessionId));
private void leave(UUID userId) {
game.quit(getPlayerId(userId));
}
public void cheat(String sessionId, UUID playerId, DeckCardLists deckList) {
public void cheat(UUID userId, UUID playerId, DeckCardLists deckList) {
Deck deck;
try {
deck = Deck.load(deckList);
@ -263,7 +261,7 @@ public class GameController implements GameCallback {
updateGame();
}
public boolean cheat(String sessionId, UUID playerId, String cardName) {
public boolean cheat(UUID userId, UUID playerId, String cardName) {
Card card = Sets.findCard(cardName, true);
if (card != null) {
Set<Card> cards = new HashSet<Card>();
@ -276,23 +274,23 @@ public class GameController implements GameCallback {
}
}
public void kill(String sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) {
gameSessions.get(sessionPlayerMap.get(sessionId)).setKilled();
gameSessions.remove(sessionPlayerMap.get(sessionId));
leave(sessionId);
sessionPlayerMap.remove(sessionId);
public void kill(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
gameSessions.get(userPlayerMap.get(userId)).setKilled();
gameSessions.remove(userPlayerMap.get(userId));
leave(userId);
userPlayerMap.remove(userId);
}
if (watchers.containsKey(sessionId)) {
watchers.get(sessionId).setKilled();
watchers.remove(sessionId);
if (watchers.containsKey(userId)) {
watchers.get(userId).setKilled();
watchers.remove(userId);
}
}
public void timeout(String sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) {
ChatManager.getInstance().broadcast(chatId, "", game.getPlayer(sessionPlayerMap.get(sessionId)).getName() + " has timed out. Auto concede.", MessageColor.BLACK);
concede(sessionId);
public void timeout(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
ChatManager.getInstance().broadcast(chatId, "", game.getPlayer(userPlayerMap.get(userId)).getName() + " has timed out. Auto concede.", MessageColor.BLACK);
concede(userId);
}
}
@ -314,20 +312,20 @@ public class GameController implements GameCallback {
return chatId;
}
public void sendPlayerUUID(String sessionId, UUID data) {
gameSessions.get(sessionPlayerMap.get(sessionId)).sendPlayerUUID(data);
public void sendPlayerUUID(UUID userId, UUID data) {
gameSessions.get(userPlayerMap.get(userId)).sendPlayerUUID(data);
}
public void sendPlayerString(String sessionId, String data) {
gameSessions.get(sessionPlayerMap.get(sessionId)).sendPlayerString(data);
public void sendPlayerString(UUID userId, String data) {
gameSessions.get(userPlayerMap.get(userId)).sendPlayerString(data);
}
public void sendPlayerBoolean(String sessionId, Boolean data) {
gameSessions.get(sessionPlayerMap.get(sessionId)).sendPlayerBoolean(data);
public void sendPlayerBoolean(UUID userId, Boolean data) {
gameSessions.get(userPlayerMap.get(userId)).sendPlayerBoolean(data);
}
public void sendPlayerInteger(String sessionId, Integer data) {
gameSessions.get(sessionPlayerMap.get(sessionId)).sendPlayerInteger(data);
public void sendPlayerInteger(UUID userId, Integer data) {
gameSessions.get(userPlayerMap.get(userId)).sendPlayerInteger(data);
}
private synchronized void updateGame() {

View file

@ -32,7 +32,6 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import mage.cards.decks.DeckCardLists;
import mage.game.Game;
import mage.MageException;
import mage.view.GameView;
/**
@ -50,15 +49,15 @@ public class GameManager {
private ConcurrentHashMap<UUID, GameController> gameControllers = new ConcurrentHashMap<UUID, GameController>();
public UUID createGameSession(Game game, ConcurrentHashMap<String, UUID> sessionPlayerMap, UUID tableId, UUID choosingPlayerId) {
GameController gameController = new GameController(game, sessionPlayerMap, tableId, choosingPlayerId);
public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId, UUID choosingPlayerId) {
GameController gameController = new GameController(game, userPlayerMap, tableId, choosingPlayerId);
gameControllers.put(game.getId(), gameController);
return gameController.getSessionId();
}
public void joinGame(UUID gameId, String sessionId) {
public void joinGame(UUID gameId, UUID userId) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).join(sessionId);
gameControllers.get(gameId).join(userId);
}
public void destroyChatSession(UUID gameId) {
@ -71,66 +70,66 @@ public class GameManager {
return null;
}
public void sendPlayerUUID(UUID gameId, String sessionId, UUID data) {
public void sendPlayerUUID(UUID gameId, UUID userId, UUID data) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).sendPlayerUUID(sessionId, data);
gameControllers.get(gameId).sendPlayerUUID(userId, data);
}
public void sendPlayerString(UUID gameId, String sessionId, String data) {
public void sendPlayerString(UUID gameId, UUID userId, String data) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).sendPlayerString(sessionId, data);
gameControllers.get(gameId).sendPlayerString(userId, data);
}
public void sendPlayerBoolean(UUID gameId, String sessionId, Boolean data) {
public void sendPlayerBoolean(UUID gameId, UUID userId, Boolean data) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).sendPlayerBoolean(sessionId, data);
gameControllers.get(gameId).sendPlayerBoolean(userId, data);
}
public void sendPlayerInteger(UUID gameId, String sessionId, Integer data) {
public void sendPlayerInteger(UUID gameId, UUID userId, Integer data) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).sendPlayerInteger(sessionId, data);
gameControllers.get(gameId).sendPlayerInteger(userId, data);
}
public void concedeGame(UUID gameId, String sessionId) {
public void concedeGame(UUID gameId, UUID userId) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).concede(sessionId);
gameControllers.get(gameId).concede(userId);
}
public void watchGame(UUID gameId, String sessionId) {
public void watchGame(UUID gameId, UUID userId) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).watch(sessionId);
gameControllers.get(gameId).watch(userId);
}
public void stopWatching(UUID gameId, String sessionId) {
public void stopWatching(UUID gameId, UUID userId) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).stopWatching(sessionId);
gameControllers.get(gameId).stopWatching(userId);
}
public void removeSession(String sessionId) {
public void removeSession(UUID userId) {
for (GameController controller: gameControllers.values()) {
controller.kill(sessionId);
controller.kill(userId);
}
}
public void kill(UUID gameId, String sessionId) {
public void kill(UUID gameId, UUID userId) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).kill(sessionId);
gameControllers.get(gameId).kill(userId);
}
public void cheat(UUID gameId, String sessionId, UUID playerId, DeckCardLists deckList) {
public void cheat(UUID gameId, UUID userId, UUID playerId, DeckCardLists deckList) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).cheat(sessionId, playerId, deckList);
gameControllers.get(gameId).cheat(userId, playerId, deckList);
}
public boolean cheat(UUID gameId, String sessionId, UUID playerId, String cardName) {
public boolean cheat(UUID gameId, UUID userId, UUID playerId, String cardName) {
if (gameControllers.containsKey(gameId))
return gameControllers.get(gameId).cheat(sessionId, playerId, cardName);
return gameControllers.get(gameId).cheat(userId, playerId, cardName);
return false;
}
public void timeout(UUID gameId, String sessionId) {
public void timeout(UUID gameId, UUID userId) {
if (gameControllers.containsKey(gameId))
gameControllers.get(gameId).timeout(sessionId);
gameControllers.get(gameId).timeout(userId);
}
public void removeGame(UUID gameId) {
@ -142,7 +141,7 @@ public class GameManager {
gameControllers.get(gameId).saveGame();
}
public GameView getGameView(UUID gameId, String sessionId, UUID playerId) {
public GameView getGameView(UUID gameId, UUID userId, UUID playerId) {
if (gameControllers.containsKey(gameId))
return gameControllers.get(gameId).getGameView(playerId);
return null;

View file

@ -37,8 +37,8 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import mage.game.Game;
import mage.interfaces.callback.ClientCallback;
import mage.server.Session;
import mage.server.SessionManager;
import mage.server.User;
import mage.server.UserManager;
import mage.server.util.ConfigSettings;
import mage.server.util.ThreadExecutor;
import mage.view.AbilityPickerView;
@ -58,8 +58,8 @@ public class GameSession extends GameWatcher {
private ScheduledFuture<?> futureTimeout;
protected static ScheduledExecutorService timeoutExecutor = ThreadExecutor.getInstance().getTimeoutExecutor();
public GameSession(Game game, String sessionId, UUID playerId) {
super(sessionId, game.getId());
public GameSession(Game game, UUID userId, UUID playerId) {
super(userId, game.getId());
this.game = game;
this.playerId = playerId;
}
@ -67,9 +67,9 @@ public class GameSession extends GameWatcher {
public void ask(final String question, final GameView gameView) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameAsk", game.getId(), new GameClientMessage(gameView, question)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameAsk", game.getId(), new GameClientMessage(gameView, question)));
}
}
}
@ -77,9 +77,9 @@ public class GameSession extends GameWatcher {
public void target(final String question, final CardsView cardView, final Set<UUID> targets, final boolean required, final GameView gameView, final Map<String, Serializable> options) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameTarget", game.getId(), new GameClientMessage(gameView, question, cardView, targets, required, options)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameTarget", game.getId(), new GameClientMessage(gameView, question, cardView, targets, required, options)));
}
}
}
@ -87,9 +87,9 @@ public class GameSession extends GameWatcher {
public void select(final String message, final GameView gameView) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameSelect", game.getId(), new GameClientMessage(gameView, message)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameSelect", game.getId(), new GameClientMessage(gameView, message)));
}
}
}
@ -97,9 +97,9 @@ public class GameSession extends GameWatcher {
public void chooseAbility(final AbilityPickerView abilities) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameChooseAbility", game.getId(), abilities));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameChooseAbility", game.getId(), abilities));
}
}
}
@ -107,9 +107,9 @@ public class GameSession extends GameWatcher {
public void choose(final String message, final Set<String> choices) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameChoose", game.getId(), new GameClientMessage(choices.toArray(new String[0]), message)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameChoose", game.getId(), new GameClientMessage(choices.toArray(new String[0]), message)));
}
}
}
@ -117,9 +117,9 @@ public class GameSession extends GameWatcher {
public void playMana(final String message, final GameView gameView) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gamePlayMana", game.getId(), new GameClientMessage(gameView, message)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gamePlayMana", game.getId(), new GameClientMessage(gameView, message)));
}
}
}
@ -127,9 +127,9 @@ public class GameSession extends GameWatcher {
public void playXMana(final String message, final GameView gameView) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gamePlayXMana", game.getId(), new GameClientMessage(gameView, message)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gamePlayXMana", game.getId(), new GameClientMessage(gameView, message)));
}
}
}
@ -137,18 +137,18 @@ public class GameSession extends GameWatcher {
public void getAmount(final String message, final int min, final int max) {
if (!killed) {
setupTimeout();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameSelectAmount", game.getId(), new GameClientMessage(message, min, max)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameSelectAmount", game.getId(), new GameClientMessage(message, min, max)));
}
}
}
public void revealCards(final String name, final CardsView cardView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameReveal", game.getId(), new GameClientMessage(cardView, name)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameReveal", game.getId(), new GameClientMessage(cardView, name)));
}
}
}
@ -160,7 +160,7 @@ public class GameSession extends GameWatcher {
new Runnable() {
@Override
public void run() {
GameManager.getInstance().timeout(gameId, sessionId);
GameManager.getInstance().timeout(gameId, userId);
}
},
ConfigSettings.getInstance().getMaxSecondsIdle(), TimeUnit.SECONDS

View file

@ -31,8 +31,8 @@ package mage.server.game;
import java.rmi.RemoteException;
import java.util.UUID;
import mage.interfaces.callback.ClientCallback;
import mage.server.Session;
import mage.server.SessionManager;
import mage.server.User;
import mage.server.UserManager;
import mage.view.GameClientMessage;
import mage.view.GameView;
import org.apache.log4j.Logger;
@ -45,20 +45,20 @@ public class GameWatcher {
protected final static Logger logger = Logger.getLogger(GameWatcher.class);
protected String sessionId;
protected UUID userId;
protected UUID gameId;
protected boolean killed = false;
public GameWatcher(String sessionId, UUID gameId) {
this.sessionId = sessionId;
public GameWatcher(UUID userId, UUID gameId) {
this.userId = userId;
this.gameId = gameId;
}
public boolean init(final GameView gameView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameInit", gameId, gameView));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameInit", gameId, gameView));
return true;
}
}
@ -67,43 +67,43 @@ public class GameWatcher {
public void update(final GameView gameView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameUpdate", gameId, gameView));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameUpdate", gameId, gameView));
}
}
}
public void inform(final String message, final GameView gameView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameInform", gameId, new GameClientMessage(gameView, message)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameInform", gameId, new GameClientMessage(gameView, message)));
}
}
}
public void gameOver(final String message) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameOver", gameId, message));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameOver", gameId, message));
}
}
}
public void gameError(final String message) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("gameError", gameId, message));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("gameError", gameId, message));
}
}
}
protected void handleRemoteException(RemoteException ex) {
logger.fatal("GameWatcher error", ex);
GameManager.getInstance().kill(gameId, sessionId);
GameManager.getInstance().kill(gameId, userId);
}
public void setKilled() {

View file

@ -45,14 +45,14 @@ import mage.view.TableView;
public interface GamesRoom extends Room {
public List<TableView> getTables();
public boolean joinTable(String sessionId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException;
public boolean joinTournamentTable(String sessionId, UUID tableId, String name, String playerType, int skill) throws GameException;
public TableView createTable(String sessionId, MatchOptions options);
public TableView createTournamentTable(String sessionId, TournamentOptions options);
public void removeTable(String sessionId, UUID tableId);
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException;
public boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill) throws GameException;
public TableView createTable(UUID userId, MatchOptions options);
public TableView createTournamentTable(UUID userId, TournamentOptions options);
public void removeTable(UUID userId, UUID tableId);
public void removeTable(UUID tableId);
public TableView getTable(UUID tableId);
public void leaveTable(String sessionId, UUID tableId);
public boolean watchTable(String sessionId, UUID tableId) throws MageException;
public void leaveTable(UUID userId, UUID tableId);
public boolean watchTable(UUID userId, UUID tableId) throws MageException;
}

View file

@ -64,33 +64,33 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
@Override
public boolean joinTable(String sessionId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
if (tables.containsKey(tableId)) {
return TableManager.getInstance().joinTable(sessionId, tableId, name, playerType, skill, deckList);
return TableManager.getInstance().joinTable(userId, tableId, name, playerType, skill, deckList);
} else {
return false;
}
}
@Override
public TableView createTable(String sessionId, MatchOptions options) {
Table table = TableManager.getInstance().createTable(this.getRoomId(), sessionId, options);
public TableView createTable(UUID userId, MatchOptions options) {
Table table = TableManager.getInstance().createTable(this.getRoomId(), userId, options);
tables.put(table.getId(), table);
return new TableView(table);
}
@Override
public boolean joinTournamentTable(String sessionId, UUID tableId, String name, String playerType, int skill) throws GameException {
public boolean joinTournamentTable(UUID userId, UUID tableId, String name, String playerType, int skill) throws GameException {
if (tables.containsKey(tableId)) {
return TableManager.getInstance().joinTournament(sessionId, tableId, name, playerType, skill);
return TableManager.getInstance().joinTournament(userId, tableId, name, playerType, skill);
} else {
return false;
}
}
@Override
public TableView createTournamentTable(String sessionId, TournamentOptions options) {
Table table = TableManager.getInstance().createTournamentTable(this.getRoomId(), sessionId, options);
public TableView createTournamentTable(UUID userId, TournamentOptions options) {
Table table = TableManager.getInstance().createTournamentTable(this.getRoomId(), userId, options);
tables.put(table.getId(), table);
return new TableView(table);
}
@ -103,7 +103,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
@Override
public void removeTable(String sessionId, UUID tableId) {
public void removeTable(UUID userId, UUID tableId) {
tables.remove(tableId);
}
@ -115,13 +115,13 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
@Override
public void leaveTable(String sessionId, UUID tableId) {
TableManager.getInstance().leaveTable(sessionId, tableId);
public void leaveTable(UUID userId, UUID tableId) {
TableManager.getInstance().leaveTable(userId, tableId);
}
@Override
public boolean watchTable(String sessionId, UUID tableId) throws MageException {
return TableManager.getInstance().watchTable(sessionId, tableId);
public boolean watchTable(UUID userId, UUID tableId) throws MageException {
return TableManager.getInstance().watchTable(userId, tableId);
}
}

View file

@ -30,8 +30,7 @@ package mage.server.game;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import mage.MageException;
import mage.server.SessionManager;
import mage.server.UserManager;
/**
*
@ -48,26 +47,26 @@ public class ReplayManager {
private ConcurrentHashMap<String, ReplaySession> replaySessions = new ConcurrentHashMap<String, ReplaySession>();
public void replayGame(UUID gameId, String sessionId) {
ReplaySession replaySession = new ReplaySession(gameId, sessionId);
replaySessions.put(gameId.toString() + sessionId.toString(), replaySession);
SessionManager.getInstance().getSession(sessionId).replayGame(gameId);
public void replayGame(UUID gameId, UUID userId) {
ReplaySession replaySession = new ReplaySession(gameId, userId);
replaySessions.put(gameId.toString() + userId.toString(), replaySession);
UserManager.getInstance().getUser(userId).replayGame(gameId);
}
public void startReplay(UUID gameId, String sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).replay();
public void startReplay(UUID gameId, UUID userId) {
replaySessions.get(gameId.toString() + userId.toString()).replay();
}
public void stopReplay(UUID gameId, String sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).stop();
public void stopReplay(UUID gameId, UUID userId) {
replaySessions.get(gameId.toString() + userId.toString()).stop();
}
public void nextPlay(UUID gameId, String sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).next();
public void nextPlay(UUID gameId, UUID userId) {
replaySessions.get(gameId.toString() + userId.toString()).next();
}
public void previousPlay(UUID gameId, String sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).previous();
public void previousPlay(UUID gameId, UUID userId) {
replaySessions.get(gameId.toString() + userId.toString()).previous();
}
}

View file

@ -32,8 +32,8 @@ import java.util.UUID;
import mage.game.Game;
import mage.game.GameState;
import mage.interfaces.callback.ClientCallback;
import mage.server.Session;
import mage.server.SessionManager;
import mage.server.User;
import mage.server.UserManager;
import mage.view.GameView;
import org.apache.log4j.Logger;
@ -45,18 +45,18 @@ public class ReplaySession implements GameCallback {
private final static Logger logger = Logger.getLogger(ReplaySession.class);
private GameReplay replay;
protected String sessionId;
protected UUID userId;
ReplaySession(UUID gameId, String sessionId) {
ReplaySession(UUID gameId, UUID userId) {
this.replay = new GameReplay(gameId);
this.sessionId = sessionId;
this.userId = userId;
}
public void replay() {
replay.start();
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("replayInit", replay.getGame().getId(), new GameView(replay.next(), replay.getGame())));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("replayInit", replay.getGame().getId(), new GameView(replay.next(), replay.getGame())));
}
}
@ -74,9 +74,9 @@ public class ReplaySession implements GameCallback {
@Override
public void gameResult(final String result) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("replayDone", replay.getGame().getId(), result));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("replayDone", replay.getGame().getId(), result));
}
}
@ -85,9 +85,9 @@ public class ReplaySession implements GameCallback {
gameResult("game ended");
}
else {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("replayUpdate", replay.getGame().getId(), new GameView(state, game)));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("replayUpdate", replay.getGame().getId(), new GameView(state, game)));
}
}
}

View file

@ -31,7 +31,6 @@ package mage.server.tournament;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import mage.cards.decks.Deck;
import mage.game.GameException;
import mage.game.Table;
@ -60,16 +59,14 @@ public class TournamentController {
private final static Logger logger = Logger.getLogger(TournamentController.class);
private UUID sessionId;
private UUID chatId;
private UUID tableId;
private Tournament tournament;
private ConcurrentHashMap<String, UUID> sessionPlayerMap = new ConcurrentHashMap<String, UUID>();
private ConcurrentHashMap<UUID, UUID> userPlayerMap = new ConcurrentHashMap<UUID, UUID>();
private ConcurrentHashMap<UUID, TournamentSession> tournamentSessions = new ConcurrentHashMap<UUID, TournamentSession>();
public TournamentController(Tournament tournament, ConcurrentHashMap<String, UUID> sessionPlayerMap, UUID tableId) {
sessionId = UUID.randomUUID();
this.sessionPlayerMap = sessionPlayerMap;
public TournamentController(Tournament tournament, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
this.userPlayerMap = userPlayerMap;
chatId = ChatManager.getInstance().createChatSession();
this.tournament = tournament;
this.tableId = tableId;
@ -128,9 +125,9 @@ public class TournamentController {
checkStart();
}
public synchronized void join(String sessionId) {
UUID playerId = sessionPlayerMap.get(sessionId);
TournamentSession tournamentSession = new TournamentSession(tournament, sessionId, tableId, playerId);
public synchronized void join(UUID userId) {
UUID playerId = userPlayerMap.get(userId);
TournamentSession tournamentSession = new TournamentSession(tournament, null, tableId, playerId);
tournamentSessions.put(playerId, tournamentSession);
TournamentPlayer player = tournament.getPlayer(playerId);
player.setJoined();
@ -205,40 +202,40 @@ public class TournamentController {
tournamentSessions.get(playerId).submitDeck(deck);
}
public void timeout(String sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) {
TournamentPlayer player = tournament.getPlayer(sessionPlayerMap.get(sessionId));
tournament.autoSubmit(sessionPlayerMap.get(sessionId), player.getDeck());
public void timeout(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
TournamentPlayer player = tournament.getPlayer(userPlayerMap.get(userId));
tournament.autoSubmit(userPlayerMap.get(userId), player.getDeck());
}
}
public UUID getSessionId() {
return this.sessionId;
}
// public UUID getSessionId() {
// return this.sessionId;
// }
public UUID getChatId() {
return chatId;
}
public void kill(String sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) {
tournamentSessions.get(sessionPlayerMap.get(sessionId)).setKilled();
tournamentSessions.remove(sessionPlayerMap.get(sessionId));
leave(sessionId);
sessionPlayerMap.remove(sessionId);
public void kill(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
tournamentSessions.get(userPlayerMap.get(userId)).setKilled();
tournamentSessions.remove(userPlayerMap.get(userId));
leave(userId);
userPlayerMap.remove(userId);
}
}
private void leave(String sessionId) {
tournament.leave(getPlayerId(sessionId));
private void leave(UUID userId) {
tournament.leave(getPlayerId(userId));
}
private UUID getPlayerId(String sessionId) {
return sessionPlayerMap.get(sessionId);
private UUID getPlayerId(UUID userId) {
return userPlayerMap.get(userId);
}
private String getPlayerSessionId(UUID playerId) {
for (Entry<String, UUID> entry: sessionPlayerMap.entrySet()) {
private UUID getPlayerSessionId(UUID playerId) {
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
if (entry.getValue().equals(playerId))
return entry.getKey();
}

View file

@ -48,22 +48,21 @@ public class TournamentManager {
return INSTANCE;
}
public UUID createTournamentSession(Tournament tournament, ConcurrentHashMap<String, UUID> sessionPlayerMap, UUID tableId) {
TournamentController tournamentController = new TournamentController(tournament, sessionPlayerMap, tableId);
public void createTournamentSession(Tournament tournament, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId) {
TournamentController tournamentController = new TournamentController(tournament, userPlayerMap, tableId);
controllers.put(tournament.getId(), tournamentController);
return tournamentController.getSessionId();
}
public void joinTournament(UUID tournamentId, String sessionId) {
controllers.get(tournamentId).join(sessionId);
public void joinTournament(UUID tournamentId, UUID userId) {
controllers.get(tournamentId).join(userId);
}
public void kill(UUID tournamentId, String sessionId) {
controllers.get(tournamentId).kill(sessionId);
public void kill(UUID tournamentId, UUID userId) {
controllers.get(tournamentId).kill(userId);
}
public void timeout(UUID tournamentId, String sessionId) {
controllers.get(tournamentId).timeout(sessionId);
public void timeout(UUID tournamentId, UUID userId) {
controllers.get(tournamentId).timeout(userId);
}
public void submitDeck(UUID tournamentId, UUID playerId, Deck deck) {

View file

@ -37,8 +37,8 @@ import mage.cards.decks.Deck;
import mage.game.tournament.Tournament;
import mage.MageException;
import mage.interfaces.callback.ClientCallback;
import mage.server.Session;
import mage.server.SessionManager;
import mage.server.User;
import mage.server.UserManager;
import mage.server.util.ThreadExecutor;
import mage.view.TournamentView;
import org.apache.log4j.Logger;
@ -50,7 +50,7 @@ import org.apache.log4j.Logger;
public class TournamentSession {
protected final static Logger logger = Logger.getLogger(TournamentSession.class);
protected String sessionId;
protected UUID userId;
protected UUID playerId;
protected UUID tableId;
protected Tournament tournament;
@ -59,8 +59,8 @@ public class TournamentSession {
private ScheduledFuture<?> futureTimeout;
protected static ScheduledExecutorService timeoutExecutor = ThreadExecutor.getInstance().getTimeoutExecutor();
public TournamentSession(Tournament tournament, String sessionId, UUID tableId, UUID playerId) {
this.sessionId = sessionId;
public TournamentSession(Tournament tournament, UUID userId, UUID tableId, UUID playerId) {
this.userId = userId;
this.tournament = tournament;
this.playerId = playerId;
this.tableId = tableId;
@ -68,37 +68,29 @@ public class TournamentSession {
public boolean init(final TournamentView tournamentView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("tournamentInit", tournament.getId(), tournamentView));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("tournamentInit", tournament.getId(), tournamentView));
return true;
}
}
return false;
}
// public boolean waitForAck(String message) {
// Session session = SessionManager.getInstance().getSession(sessionId);
// do {
// //TODO: add timeout
// } while (!session.getAckMessage().equals(message) && !killed);
// return true;
// }
public void update(final TournamentView tournamentView) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("tournamentUpdate", tournament.getId(), tournamentView));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("tournamentUpdate", tournament.getId(), tournamentView));
}
}
}
public void gameOver(final String message) {
if (!killed) {
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null) {
session.fireCallback(new ClientCallback("tournamentOver", tournament.getId(), message));
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("tournamentOver", tournament.getId(), message));
}
}
}
@ -106,9 +98,10 @@ public class TournamentSession {
public void construct(Deck deck, int timeout) throws MageException {
if (!killed) {
setupTimeout(timeout);
Session session = SessionManager.getInstance().getSession(sessionId);
if (session != null)
session.construct(deck, tableId, timeout);
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.construct(deck, tableId, timeout);
}
}
}
@ -119,7 +112,7 @@ public class TournamentSession {
protected void handleRemoteException(RemoteException ex) {
logger.fatal("TournamentSession error ", ex);
TournamentManager.getInstance().kill(tournament.getId(), sessionId);
TournamentManager.getInstance().kill(tournament.getId(), userId);
}
public void setKilled() {
@ -133,7 +126,7 @@ public class TournamentSession {
new Runnable() {
@Override
public void run() {
TournamentManager.getInstance().timeout(tournament.getId(), sessionId);
TournamentManager.getInstance().timeout(tournament.getId(), userId);
}
},
seconds, TimeUnit.SECONDS