mirror of
https://github.com/correl/mage.git
synced 2024-11-28 11:09:54 +00:00
Draft stability improvements (#9435)
This commit is contained in:
parent
c8c663b976
commit
b9530e307d
13 changed files with 116 additions and 4 deletions
|
@ -274,6 +274,10 @@ public final class SessionHandler {
|
|||
session.sendCardMark(draftId, id);
|
||||
}
|
||||
|
||||
public static void setBoosterLoaded(UUID draftId) {
|
||||
session.setBoosterLoaded(draftId);
|
||||
}
|
||||
|
||||
public static Optional<UUID> getRoomChatId(UUID roomId) {
|
||||
return session.getRoomChatId(roomId);
|
||||
}
|
||||
|
|
|
@ -303,6 +303,8 @@
|
|||
if (timeout != 0) {
|
||||
countdown.start();
|
||||
}
|
||||
|
||||
SessionHandler.setBoosterLoaded(draftId); // confirm to the server that the booster has been loaded
|
||||
}
|
||||
|
||||
private void loadCardsToPickedCardsArea(SimpleCardsView pickedCards) {
|
||||
|
|
|
@ -149,6 +149,8 @@ public interface MageServer {
|
|||
|
||||
void sendCardMark(UUID draftId, String sessionId, UUID cardId) throws MageException;
|
||||
|
||||
void setBoosterLoaded(UUID draftId, String sessionId) throws MageException;
|
||||
|
||||
//challenge methods
|
||||
// void startChallenge(String sessionId, UUID roomId, UUID tableId, UUID challengeId) throws MageException;
|
||||
//replay methods
|
||||
|
|
|
@ -988,6 +988,20 @@ public class SessionImpl implements Session {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setBoosterLoaded(UUID draftId) {
|
||||
try {
|
||||
if (isConnected()) {
|
||||
server.setBoosterLoaded(draftId, sessionId);
|
||||
}
|
||||
} catch (MageException ex) {
|
||||
handleMageException(ex);
|
||||
} catch (Throwable t) {
|
||||
handleThrowable(t);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean joinChat(UUID chatId) {
|
||||
try {
|
||||
|
|
|
@ -39,6 +39,8 @@ public interface GamePlay {
|
|||
|
||||
boolean updateDeck(UUID tableId, DeckCardLists deck);
|
||||
|
||||
boolean setBoosterLoaded(UUID draftId);
|
||||
|
||||
DraftPickView sendCardPick(UUID draftId, UUID cardId, Set<UUID> hiddenCards);
|
||||
DraftPickView sendCardMark(UUID draftId, UUID cardId);
|
||||
|
||||
|
|
|
@ -737,6 +737,16 @@ public class MageServerImpl implements MageServer {
|
|||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoosterLoaded(final UUID draftId, final String sessionId) throws MageException {
|
||||
execute("setBoosterLoaded", sessionId, () -> {
|
||||
managerFactory.sessionManager().getSession(sessionId).ifPresent(session -> {
|
||||
UUID userId = session.getUserId();
|
||||
managerFactory.draftManager().setBoosterLoaded(draftId, userId);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void quitMatch(final UUID gameId, final String sessionId) throws MageException {
|
||||
execute("quitMatch", sessionId, () -> {
|
||||
|
|
|
@ -200,6 +200,10 @@ public class DraftController {
|
|||
draftSessions.get(userPlayerMap.get(userId)).setMarkedCard(cardId);
|
||||
}
|
||||
|
||||
public void setBoosterLoaded(UUID userId) {
|
||||
draftSessions.get(userPlayerMap.get(userId)).setBoosterLoaded();
|
||||
}
|
||||
|
||||
private synchronized void updateDraft() throws MageException {
|
||||
for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
|
||||
entry.getValue().update();
|
||||
|
|
|
@ -50,6 +50,11 @@ public class DraftManagerImpl implements DraftManager {
|
|||
draftControllers.get(draftId).sendCardMark(userId, cardId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoosterLoaded(UUID draftId, UUID userId) {
|
||||
draftControllers.get(draftId).setBoosterLoaded(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSession(UUID userId) {
|
||||
for (DraftController controller : draftControllers.values()) {
|
||||
|
|
|
@ -145,4 +145,8 @@ public class DraftSession {
|
|||
this.markedCard = markedCard;
|
||||
}
|
||||
|
||||
public void setBoosterLoaded() {
|
||||
draft.setBoosterLoaded(playerId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@ public interface DraftManager {
|
|||
|
||||
void sendCardMark(UUID draftId, UUID userId, UUID cardId);
|
||||
|
||||
void setBoosterLoaded(UUID draftId, UUID userId);
|
||||
|
||||
void removeSession(UUID userId);
|
||||
|
||||
void kill(UUID draftId, UUID userId);
|
||||
|
|
|
@ -30,6 +30,7 @@ public interface Draft extends MageItem, Serializable {
|
|||
int getBoosterNum();
|
||||
int getCardNum();
|
||||
boolean addPick(UUID playerId, UUID cardId, Set<UUID> hiddenCards);
|
||||
void setBoosterLoaded(UUID playerID);
|
||||
void start();
|
||||
boolean isStarted();
|
||||
void setStarted();
|
||||
|
|
|
@ -9,6 +9,11 @@ import mage.game.events.TableEvent.EventType;
|
|||
import mage.players.Player;
|
||||
import mage.players.PlayerList;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
|
@ -25,6 +30,8 @@ public abstract class DraftImpl implements Draft {
|
|||
protected int boosterNum = 1; // starts with booster 1
|
||||
protected int cardNum = 1; // starts with card number 1, increases by +1 after each picking
|
||||
protected TimingOption timing;
|
||||
protected int boosterLoadingCounter; // number of times the boosters have been sent to players until all are confirmed to have received them
|
||||
protected final int BOOSTER_LOADING_INTERVAL = 3; // interval in seconds
|
||||
|
||||
protected boolean abort = false;
|
||||
protected boolean started = false;
|
||||
|
@ -32,6 +39,9 @@ public abstract class DraftImpl implements Draft {
|
|||
protected transient TableEventSource tableEventSource = new TableEventSource();
|
||||
protected transient PlayerQueryEventSource playerQueryEventSource = new PlayerQueryEventSource();
|
||||
|
||||
protected ScheduledFuture<?> boosterLoadingHandle;
|
||||
protected final ScheduledExecutorService boosterLoadingExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
public DraftImpl(DraftOptions options, List<ExpansionSet> sets) {
|
||||
id = UUID.randomUUID();
|
||||
this.setCodes = options.getSetCodes();
|
||||
|
@ -209,9 +219,9 @@ public abstract class DraftImpl implements Draft {
|
|||
return false;
|
||||
}
|
||||
player.setPicking();
|
||||
player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this);
|
||||
player.setBoosterNotLoaded();
|
||||
}
|
||||
cardNum++;
|
||||
setupBoosterLoadingHandle();
|
||||
synchronized (this) {
|
||||
while (!donePicking()) {
|
||||
try {
|
||||
|
@ -220,9 +230,42 @@ public abstract class DraftImpl implements Draft {
|
|||
}
|
||||
}
|
||||
}
|
||||
cardNum++;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void setupBoosterLoadingHandle() {
|
||||
cancelBoosterLoadingHandle();
|
||||
boosterLoadingCounter = 0;
|
||||
boosterLoadingHandle = boosterLoadingExecutor.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
if (loadBoosters() == true) {
|
||||
cancelBoosterLoadingHandle();
|
||||
} else {
|
||||
boosterLoadingCounter++;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
}, 0, BOOSTER_LOADING_INTERVAL, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
protected void cancelBoosterLoadingHandle() {
|
||||
if (boosterLoadingHandle != null) {
|
||||
boosterLoadingHandle.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean loadBoosters () {
|
||||
boolean allBoostersLoaded = true;
|
||||
for (DraftPlayer player : players.values()) {
|
||||
if (player.isPicking() && !player.isBoosterLoaded()) {
|
||||
allBoostersLoaded = false;
|
||||
player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this);
|
||||
}
|
||||
}
|
||||
return allBoostersLoaded;
|
||||
}
|
||||
|
||||
protected boolean donePicking() {
|
||||
if (isAbort()) {
|
||||
return true;
|
||||
|
@ -263,7 +306,7 @@ public abstract class DraftImpl implements Draft {
|
|||
public void firePickCardEvent(UUID playerId) {
|
||||
DraftPlayer player = players.get(playerId);
|
||||
int cardNum = Math.min(15, this.cardNum);
|
||||
int time = timing.getPickTimeout(cardNum);
|
||||
int time = timing.getPickTimeout(cardNum) - boosterLoadingCounter * BOOSTER_LOADING_INTERVAL;
|
||||
playerQueryEventSource.pickCard(playerId, "Pick card", player.getBooster(), time);
|
||||
}
|
||||
|
||||
|
@ -285,6 +328,12 @@ public abstract class DraftImpl implements Draft {
|
|||
return !player.isPicking();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoosterLoaded(UUID playerId) {
|
||||
DraftPlayer player = players.get(playerId);
|
||||
player.setBoosterLoaded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAbort() {
|
||||
return abort;
|
||||
|
|
|
@ -21,6 +21,7 @@ public class DraftPlayer {
|
|||
protected Deck deck;
|
||||
protected List<Card> booster;
|
||||
protected boolean picking;
|
||||
protected boolean boosterLoaded;
|
||||
protected boolean joined = false;
|
||||
protected Set<UUID> hiddenCards;
|
||||
|
||||
|
@ -98,4 +99,16 @@ public class DraftPlayer {
|
|||
this.joined = true;
|
||||
}
|
||||
|
||||
public void setBoosterLoaded() {
|
||||
boosterLoaded = true;
|
||||
}
|
||||
|
||||
public void setBoosterNotLoaded() {
|
||||
boosterLoaded = false;
|
||||
}
|
||||
|
||||
public boolean isBoosterLoaded() {
|
||||
return boosterLoaded;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue