forked from External/mage
reconnect to drafts and tournaments
This commit is contained in:
parent
bf2f4e3078
commit
7f312ed453
8 changed files with 132 additions and 42 deletions
|
|
@ -125,6 +125,7 @@ public class DraftController {
|
|||
UUID playerId = userPlayerMap.get(userId);
|
||||
DraftSession draftSession = new DraftSession(draft, userId, playerId);
|
||||
draftSessions.put(playerId, draftSession);
|
||||
UserManager.getInstance().getUser(userId).addDraft(playerId, draftSession);
|
||||
logger.info("User " + UserManager.getInstance().getUser(userId).getName() + " has joined draft " + draft.getId());
|
||||
draft.getPlayer(playerId).setJoined();
|
||||
checkStart();
|
||||
|
|
@ -132,7 +133,7 @@ public class DraftController {
|
|||
|
||||
private synchronized void startDraft() {
|
||||
for (final Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
|
||||
if (!entry.getValue().init(getDraftView())) {
|
||||
if (!entry.getValue().init()) {
|
||||
logger.fatal("Unable to initialize client");
|
||||
//TODO: generate client error message
|
||||
return;
|
||||
|
|
@ -171,6 +172,7 @@ public class DraftController {
|
|||
private void endDraft() throws MageException {
|
||||
for (final DraftSession draftSession: draftSessions.values()) {
|
||||
draftSession.draftOver();
|
||||
draftSession.removeDraft();
|
||||
}
|
||||
TableManager.getInstance().endDraft(tableId, draft);
|
||||
}
|
||||
|
|
@ -187,6 +189,7 @@ public class DraftController {
|
|||
public void timeout(UUID userId) {
|
||||
if (userPlayerMap.containsKey(userId)) {
|
||||
draft.autoPick(userPlayerMap.get(userId));
|
||||
logger.info("Draft pick timeout - autopick for player: " + userPlayerMap.get(userId));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -195,29 +198,18 @@ public class DraftController {
|
|||
}
|
||||
|
||||
public DraftPickView sendCardPick(UUID userId, UUID cardId) {
|
||||
if (draftSessions.get(userPlayerMap.get(userId)).sendCardPick(cardId)) {
|
||||
return getDraftPickView(userPlayerMap.get(userId), 0);
|
||||
}
|
||||
return null;
|
||||
return draftSessions.get(userPlayerMap.get(userId)).sendCardPick(cardId);
|
||||
}
|
||||
|
||||
private synchronized void updateDraft() throws MageException {
|
||||
for (final Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
|
||||
entry.getValue().update(getDraftView());
|
||||
entry.getValue().update();
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void pickCard(UUID playerId, int timeout) throws MageException {
|
||||
if (draftSessions.containsKey(playerId))
|
||||
draftSessions.get(playerId).pickCard(getDraftPickView(playerId, timeout), timeout);
|
||||
}
|
||||
|
||||
private DraftView getDraftView() {
|
||||
return new DraftView(draft);
|
||||
}
|
||||
|
||||
private DraftPickView getDraftPickView(UUID playerId, int timeout) {
|
||||
return new DraftPickView(draft.getPlayer(playerId), timeout);
|
||||
draftSessions.get(playerId).pickCard(timeout);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,39 +65,34 @@ public class DraftSession {
|
|||
this.playerId = playerId;
|
||||
}
|
||||
|
||||
public boolean init(final DraftView draftView) {
|
||||
public boolean init() {
|
||||
if (!killed) {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
user.fireCallback(new ClientCallback("draftInit", draft.getId(), draftView));
|
||||
if (futureTimeout != null && !futureTimeout.isDone()) {
|
||||
int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS);
|
||||
user.fireCallback(new ClientCallback("draftInit", draft.getId(), new DraftClientMessage(getDraftPickView(remaining))));
|
||||
}
|
||||
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 DraftView draftView) {
|
||||
public void update() {
|
||||
if (!killed) {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
user.fireCallback(new ClientCallback("draftUpdate", draft.getId(), draftView));
|
||||
user.fireCallback(new ClientCallback("draftUpdate", draft.getId(), getDraftView()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void inform(final String message, final DraftView draftView) {
|
||||
public void inform(final String message) {
|
||||
if (!killed) {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
user.fireCallback(new ClientCallback("draftInform", draft.getId(), new DraftClientMessage(draftView, message)));
|
||||
user.fireCallback(new ClientCallback("draftInform", draft.getId(), new DraftClientMessage(getDraftView(), message)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -111,12 +106,12 @@ public class DraftSession {
|
|||
}
|
||||
}
|
||||
|
||||
public void pickCard(final DraftPickView draftPickView, int timeout) {
|
||||
public void pickCard(int timeout) {
|
||||
if (!killed) {
|
||||
setupTimeout(timeout);
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
user.fireCallback(new ClientCallback("draftPick", draft.getId(), new DraftClientMessage(draftPickView)));
|
||||
user.fireCallback(new ClientCallback("draftPick", draft.getId(), new DraftClientMessage(getDraftPickView(timeout))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -151,10 +146,29 @@ public class DraftSession {
|
|||
killed = true;
|
||||
}
|
||||
|
||||
public boolean sendCardPick(UUID cardId) {
|
||||
public DraftPickView sendCardPick(UUID cardId) {
|
||||
cancelTimeout();
|
||||
return draft.addPick(playerId, cardId);
|
||||
if (draft.addPick(playerId, cardId))
|
||||
return getDraftPickView(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeDraft() {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null)
|
||||
user.removeDraft(playerId);
|
||||
}
|
||||
|
||||
private DraftView getDraftView() {
|
||||
return new DraftView(draft);
|
||||
}
|
||||
|
||||
private DraftPickView getDraftPickView(int timeout) {
|
||||
return new DraftPickView(draft.getPlayer(playerId), timeout);
|
||||
}
|
||||
|
||||
public UUID getDraftId() {
|
||||
return draft.getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue