reconnect to drafts and tournaments

This commit is contained in:
BetaSteward 2011-07-12 23:13:50 -04:00
parent bf2f4e3078
commit 7f312ed453
8 changed files with 132 additions and 42 deletions

View file

@ -347,7 +347,9 @@ public class TableController {
private void sideboard(UUID playerId, Deck deck, int timeout) throws MageException {
for (Entry<UUID, UUID> entry: userPlayerMap.entrySet()) {
if (entry.getValue().equals(playerId)) {
UserManager.getInstance().getUser(entry.getKey()).sideboard(deck, table.getId(), timeout);
User user = UserManager.getInstance().getUser(entry.getKey());
if (user != null)
user.sideboard(deck, table.getId(), timeout);
break;
}
}
@ -384,6 +386,10 @@ public class TableController {
tournament.nextStep();
}
public void endTournament(Tournament tournament) {
//TODO: implement this
}
public void swapSeats(int seatNum1, int seatNum2) {
if (table.getState() == TableState.STARTING) {
if (seatNum1 >= 0 && seatNum2 >= 0 && seatNum1 < table.getSeats().length && seatNum2 < table.getSeats().length) {

View file

@ -38,6 +38,7 @@ import mage.game.GameException;
import mage.game.draft.Draft;
import mage.game.match.Match;
import mage.game.match.MatchOptions;
import mage.game.tournament.Tournament;
import mage.game.tournament.TournamentOptions;
import mage.MageException;
import mage.players.Player;
@ -191,6 +192,11 @@ public class TableManager {
controllers.get(tableId).endDraft(draft);
}
public void endTournament(UUID tableId, Tournament tournament) {
if (controllers.containsKey(tableId))
controllers.get(tableId).endTournament(tournament);
}
public void swapSeats(UUID tableId, UUID userId, int seatNum1, int seatNum2) {
if (controllers.containsKey(tableId) && isTableOwner(tableId, userId)) {
controllers.get(tableId).swapSeats(seatNum1, seatNum2);

View file

@ -34,8 +34,10 @@ import java.util.Map.Entry;
import java.util.UUID;
import mage.cards.decks.Deck;
import mage.interfaces.callback.ClientCallback;
import mage.server.draft.DraftSession;
import mage.server.game.GameManager;
import mage.server.game.GameSession;
import mage.server.tournament.TournamentSession;
import mage.view.TableClientMessage;
/**
@ -56,6 +58,8 @@ public class User {
private Date lastActivity = new Date();
private UserState userState;
private Map<UUID, GameSession> gameSessions = new HashMap<UUID, GameSession>();
private Map<UUID, DraftSession> draftSessions = new HashMap<UUID, DraftSession>();
private Map<UUID, TournamentSession> tournamentSessions = new HashMap<UUID, TournamentSession>();
public User(String userName, String host) {
this.userName = userName;
@ -164,6 +168,16 @@ public class User {
entry.getValue().init();
GameManager.getInstance().sendPlayerString(entry.getValue().getGameId(), userId, "");
}
for (Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
draftStarted(entry.getValue().getDraftId(), entry.getKey());
entry.getValue().init();
entry.getValue().update();
}
for (Entry<UUID, TournamentSession> entry: tournamentSessions.entrySet()) {
tournamentStarted(entry.getValue().getTournamentId(), entry.getKey());
entry.getValue().init();
entry.getValue().update();
}
}
public void addGame(UUID playerId, GameSession gameSession) {
@ -174,10 +188,32 @@ public class User {
gameSessions.remove(playerId);
}
public void addDraft(UUID playerId, DraftSession draftSession) {
draftSessions.put(playerId, draftSession);
}
public void removeDraft(UUID playerId) {
draftSessions.remove(playerId);
}
public void addTournament(UUID playerId, TournamentSession tournamentSession) {
tournamentSessions.put(playerId, tournamentSession);
}
public void removeTournament(UUID playerId) {
tournamentSessions.remove(playerId);
}
public void kill() {
for (Entry<UUID, GameSession> entry: gameSessions.entrySet()) {
entry.getValue().kill();
}
for (Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
entry.getValue().setKilled();
}
for (Entry<UUID, TournamentSession> entry: tournamentSessions.entrySet()) {
entry.getValue().setKilled();
}
}
}

View file

@ -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);
}
}

View file

@ -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();
}
}

View file

@ -45,6 +45,7 @@ import mage.game.tournament.TournamentPlayer;
import mage.MageException;
import mage.server.ChatManager;
import mage.server.TableManager;
import mage.server.UserManager;
import mage.server.game.GamesRoomManager;
import mage.server.util.ThreadExecutor;
import mage.view.ChatMessage.MessageColor;
@ -61,6 +62,7 @@ public class TournamentController {
private UUID chatId;
private UUID tableId;
private boolean started = false;
private Tournament tournament;
private ConcurrentHashMap<UUID, UUID> userPlayerMap = new ConcurrentHashMap<UUID, UUID>();
private ConcurrentHashMap<UUID, TournamentSession> tournamentSessions = new ConcurrentHashMap<UUID, TournamentSession>();
@ -95,6 +97,9 @@ public class TournamentController {
case CONSTRUCT:
construct();
break;
case END:
endTournament();
break;
}
}
}
@ -127,8 +132,9 @@ public class TournamentController {
public synchronized void join(UUID userId) {
UUID playerId = userPlayerMap.get(userId);
TournamentSession tournamentSession = new TournamentSession(tournament, null, tableId, playerId);
TournamentSession tournamentSession = new TournamentSession(tournament, userId, tableId, playerId);
tournamentSessions.put(playerId, tournamentSession);
UserManager.getInstance().getUser(userId).addTournament(playerId, tournamentSession);
TournamentPlayer player = tournament.getPlayer(playerId);
player.setJoined();
logger.info("player " + playerId + " has joined tournament " + tournament.getId());
@ -137,7 +143,7 @@ public class TournamentController {
}
private void checkStart() {
if (allJoined()) {
if (!started && allJoined()) {
ThreadExecutor.getInstance().getCallExecutor().execute(
new Runnable() {
@Override
@ -161,15 +167,24 @@ public class TournamentController {
private synchronized void startTournament() {
for (final Entry<UUID, TournamentSession> entry: tournamentSessions.entrySet()) {
if (!entry.getValue().init(getTournamentView())) {
if (!entry.getValue().init()) {
logger.fatal("Unable to initialize client");
//TODO: generate client error message
return;
}
}
started = true;
tournament.nextStep();
}
private void endTournament() {
for (final TournamentSession tournamentSession: tournamentSessions.values()) {
tournamentSession.tournamentOver();
tournamentSession.removeTournament();
}
TableManager.getInstance().endTournament(tableId, tournament);
}
private void startMatch(TournamentPairing pair, MatchOptions matchOptions) {
try {
TableManager tableManager = TableManager.getInstance();

View file

@ -66,22 +66,22 @@ public class TournamentSession {
this.tableId = tableId;
}
public boolean init(final TournamentView tournamentView) {
public boolean init() {
if (!killed) {
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("tournamentInit", tournament.getId(), tournamentView));
user.fireCallback(new ClientCallback("tournamentInit", tournament.getId(), getTournamentView()));
return true;
}
}
return false;
}
public void update(final TournamentView tournamentView) {
public void update() {
if (!killed) {
User user = UserManager.getInstance().getUser(userId);
if (user != null) {
user.fireCallback(new ClientCallback("tournamentUpdate", tournament.getId(), tournamentView));
user.fireCallback(new ClientCallback("tournamentUpdate", tournament.getId(), getTournamentView()));
}
}
}
@ -140,4 +140,22 @@ public class TournamentSession {
}
}
public void removeTournament() {
User user = UserManager.getInstance().getUser(userId);
if (user != null)
user.removeTournament(playerId);
}
private TournamentView getTournamentView() {
return new TournamentView(tournament);
}
public UUID getTournamentId() {
return tournament.getId();
}
void tournamentOver() {
//TODO: implement this
}
}