mirror of
https://github.com/correl/mage.git
synced 2024-11-28 19:19:55 +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);
|
session.sendCardMark(draftId, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setBoosterLoaded(UUID draftId) {
|
||||||
|
session.setBoosterLoaded(draftId);
|
||||||
|
}
|
||||||
|
|
||||||
public static Optional<UUID> getRoomChatId(UUID roomId) {
|
public static Optional<UUID> getRoomChatId(UUID roomId) {
|
||||||
return session.getRoomChatId(roomId);
|
return session.getRoomChatId(roomId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -303,6 +303,8 @@
|
||||||
if (timeout != 0) {
|
if (timeout != 0) {
|
||||||
countdown.start();
|
countdown.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SessionHandler.setBoosterLoaded(draftId); // confirm to the server that the booster has been loaded
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadCardsToPickedCardsArea(SimpleCardsView pickedCards) {
|
private void loadCardsToPickedCardsArea(SimpleCardsView pickedCards) {
|
||||||
|
|
|
@ -149,6 +149,8 @@ public interface MageServer {
|
||||||
|
|
||||||
void sendCardMark(UUID draftId, String sessionId, UUID cardId) throws MageException;
|
void sendCardMark(UUID draftId, String sessionId, UUID cardId) throws MageException;
|
||||||
|
|
||||||
|
void setBoosterLoaded(UUID draftId, String sessionId) throws MageException;
|
||||||
|
|
||||||
//challenge methods
|
//challenge methods
|
||||||
// void startChallenge(String sessionId, UUID roomId, UUID tableId, UUID challengeId) throws MageException;
|
// void startChallenge(String sessionId, UUID roomId, UUID tableId, UUID challengeId) throws MageException;
|
||||||
//replay methods
|
//replay methods
|
||||||
|
|
|
@ -988,6 +988,20 @@ public class SessionImpl implements Session {
|
||||||
return null;
|
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
|
@Override
|
||||||
public boolean joinChat(UUID chatId) {
|
public boolean joinChat(UUID chatId) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -39,6 +39,8 @@ public interface GamePlay {
|
||||||
|
|
||||||
boolean updateDeck(UUID tableId, DeckCardLists deck);
|
boolean updateDeck(UUID tableId, DeckCardLists deck);
|
||||||
|
|
||||||
|
boolean setBoosterLoaded(UUID draftId);
|
||||||
|
|
||||||
DraftPickView sendCardPick(UUID draftId, UUID cardId, Set<UUID> hiddenCards);
|
DraftPickView sendCardPick(UUID draftId, UUID cardId, Set<UUID> hiddenCards);
|
||||||
DraftPickView sendCardMark(UUID draftId, UUID cardId);
|
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
|
@Override
|
||||||
public void quitMatch(final UUID gameId, final String sessionId) throws MageException {
|
public void quitMatch(final UUID gameId, final String sessionId) throws MageException {
|
||||||
execute("quitMatch", sessionId, () -> {
|
execute("quitMatch", sessionId, () -> {
|
||||||
|
|
|
@ -200,6 +200,10 @@ public class DraftController {
|
||||||
draftSessions.get(userPlayerMap.get(userId)).setMarkedCard(cardId);
|
draftSessions.get(userPlayerMap.get(userId)).setMarkedCard(cardId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBoosterLoaded(UUID userId) {
|
||||||
|
draftSessions.get(userPlayerMap.get(userId)).setBoosterLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized void updateDraft() throws MageException {
|
private synchronized void updateDraft() throws MageException {
|
||||||
for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
|
for (final Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
|
||||||
entry.getValue().update();
|
entry.getValue().update();
|
||||||
|
|
|
@ -50,6 +50,11 @@ public class DraftManagerImpl implements DraftManager {
|
||||||
draftControllers.get(draftId).sendCardMark(userId, cardId);
|
draftControllers.get(draftId).sendCardMark(userId, cardId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBoosterLoaded(UUID draftId, UUID userId) {
|
||||||
|
draftControllers.get(draftId).setBoosterLoaded(userId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeSession(UUID userId) {
|
public void removeSession(UUID userId) {
|
||||||
for (DraftController controller : draftControllers.values()) {
|
for (DraftController controller : draftControllers.values()) {
|
||||||
|
|
|
@ -145,4 +145,8 @@ public class DraftSession {
|
||||||
this.markedCard = markedCard;
|
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 sendCardMark(UUID draftId, UUID userId, UUID cardId);
|
||||||
|
|
||||||
|
void setBoosterLoaded(UUID draftId, UUID userId);
|
||||||
|
|
||||||
void removeSession(UUID userId);
|
void removeSession(UUID userId);
|
||||||
|
|
||||||
void kill(UUID draftId, UUID userId);
|
void kill(UUID draftId, UUID userId);
|
||||||
|
|
|
@ -30,6 +30,7 @@ public interface Draft extends MageItem, Serializable {
|
||||||
int getBoosterNum();
|
int getBoosterNum();
|
||||||
int getCardNum();
|
int getCardNum();
|
||||||
boolean addPick(UUID playerId, UUID cardId, Set<UUID> hiddenCards);
|
boolean addPick(UUID playerId, UUID cardId, Set<UUID> hiddenCards);
|
||||||
|
void setBoosterLoaded(UUID playerID);
|
||||||
void start();
|
void start();
|
||||||
boolean isStarted();
|
boolean isStarted();
|
||||||
void setStarted();
|
void setStarted();
|
||||||
|
|
|
@ -9,6 +9,11 @@ import mage.game.events.TableEvent.EventType;
|
||||||
import mage.players.Player;
|
import mage.players.Player;
|
||||||
import mage.players.PlayerList;
|
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
|
* @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 boosterNum = 1; // starts with booster 1
|
||||||
protected int cardNum = 1; // starts with card number 1, increases by +1 after each picking
|
protected int cardNum = 1; // starts with card number 1, increases by +1 after each picking
|
||||||
protected TimingOption timing;
|
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 abort = false;
|
||||||
protected boolean started = false;
|
protected boolean started = false;
|
||||||
|
@ -32,6 +39,9 @@ public abstract class DraftImpl implements Draft {
|
||||||
protected transient TableEventSource tableEventSource = new TableEventSource();
|
protected transient TableEventSource tableEventSource = new TableEventSource();
|
||||||
protected transient PlayerQueryEventSource playerQueryEventSource = new PlayerQueryEventSource();
|
protected transient PlayerQueryEventSource playerQueryEventSource = new PlayerQueryEventSource();
|
||||||
|
|
||||||
|
protected ScheduledFuture<?> boosterLoadingHandle;
|
||||||
|
protected final ScheduledExecutorService boosterLoadingExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
public DraftImpl(DraftOptions options, List<ExpansionSet> sets) {
|
public DraftImpl(DraftOptions options, List<ExpansionSet> sets) {
|
||||||
id = UUID.randomUUID();
|
id = UUID.randomUUID();
|
||||||
this.setCodes = options.getSetCodes();
|
this.setCodes = options.getSetCodes();
|
||||||
|
@ -209,9 +219,9 @@ public abstract class DraftImpl implements Draft {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
player.setPicking();
|
player.setPicking();
|
||||||
player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this);
|
player.setBoosterNotLoaded();
|
||||||
}
|
}
|
||||||
cardNum++;
|
setupBoosterLoadingHandle();
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
while (!donePicking()) {
|
while (!donePicking()) {
|
||||||
try {
|
try {
|
||||||
|
@ -220,9 +230,42 @@ public abstract class DraftImpl implements Draft {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cardNum++;
|
||||||
return true;
|
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() {
|
protected boolean donePicking() {
|
||||||
if (isAbort()) {
|
if (isAbort()) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -263,7 +306,7 @@ public abstract class DraftImpl implements Draft {
|
||||||
public void firePickCardEvent(UUID playerId) {
|
public void firePickCardEvent(UUID playerId) {
|
||||||
DraftPlayer player = players.get(playerId);
|
DraftPlayer player = players.get(playerId);
|
||||||
int cardNum = Math.min(15, this.cardNum);
|
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);
|
playerQueryEventSource.pickCard(playerId, "Pick card", player.getBooster(), time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,6 +328,12 @@ public abstract class DraftImpl implements Draft {
|
||||||
return !player.isPicking();
|
return !player.isPicking();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBoosterLoaded(UUID playerId) {
|
||||||
|
DraftPlayer player = players.get(playerId);
|
||||||
|
player.setBoosterLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAbort() {
|
public boolean isAbort() {
|
||||||
return abort;
|
return abort;
|
||||||
|
|
|
@ -21,6 +21,7 @@ public class DraftPlayer {
|
||||||
protected Deck deck;
|
protected Deck deck;
|
||||||
protected List<Card> booster;
|
protected List<Card> booster;
|
||||||
protected boolean picking;
|
protected boolean picking;
|
||||||
|
protected boolean boosterLoaded;
|
||||||
protected boolean joined = false;
|
protected boolean joined = false;
|
||||||
protected Set<UUID> hiddenCards;
|
protected Set<UUID> hiddenCards;
|
||||||
|
|
||||||
|
@ -98,4 +99,16 @@ public class DraftPlayer {
|
||||||
this.joined = true;
|
this.joined = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBoosterLoaded() {
|
||||||
|
boosterLoaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBoosterNotLoaded() {
|
||||||
|
boosterLoaded = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBoosterLoaded() {
|
||||||
|
return boosterLoaded;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue