forked from External/mage
Replaced Session with User - needed for reconnect
This commit is contained in:
parent
3d540160a8
commit
1a43757ed0
24 changed files with 610 additions and 525 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue