Drafts: refactor to use same pack/pick number logic (#8039);

This commit is contained in:
Oleg Agafonov 2021-07-23 16:18:56 +04:00
parent 400acae0c1
commit 06ae494c5b
10 changed files with 41 additions and 52 deletions

View file

@ -62,9 +62,11 @@
private List<String> setCodes; private List<String> setCodes;
// Number of the current booster (for draft log writing). // Number of the current booster (for draft log writing).
// starts with 1
private int packNo; private int packNo;
// Number of the current card pick (for draft log writing). // Number of the current card pick (for draft log writing).
// starts with 1
private int pickNo; private int pickNo;
// Cached booster data to be written into the log (see logLastPick). // Cached booster data to be written into the log (see logLastPick).
@ -155,9 +157,9 @@
this.txtPack2.setText(draftView.getSets().get(1)); this.txtPack2.setText(draftView.getSets().get(1));
this.txtPack3.setText(draftView.getSets().get(2)); this.txtPack3.setText(draftView.getSets().get(2));
} }
this.chkPack1.setSelected(draftView.getBoosterNum() > 0); this.chkPack1.setSelected(draftView.getBoosterNum() > 1);
this.chkPack2.setSelected(draftView.getBoosterNum() > 1); this.chkPack2.setSelected(draftView.getBoosterNum() > 2);
this.chkPack3.setSelected(draftView.getBoosterNum() > 2); this.chkPack3.setSelected(draftView.getBoosterNum() > 3);
this.txtCardNo.setText(Integer.toString(draftView.getCardNum())); this.txtCardNo.setText(Integer.toString(draftView.getCardNum()));
packNo = draftView.getBoosterNum(); packNo = draftView.getBoosterNum();
@ -169,7 +171,7 @@
int left = draftView.getPlayers().size() - right; int left = draftView.getPlayers().size() - right;
int height = left * 18; int height = left * 18;
lblTableImage.setSize(new Dimension(lblTableImage.getWidth(), height)); lblTableImage.setSize(new Dimension(lblTableImage.getWidth(), height));
Image tableImage = ImageHelper.getImageFromResources(draftView.getBoosterNum() == 2 ? "/draft/table_left.png" : "/draft/table_right.png"); Image tableImage = ImageHelper.getImageFromResources((draftView.getBoosterNum() + 1) % 2 == 1 ? "/draft/table_left.png" : "/draft/table_right.png");
BufferedImage resizedTable = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(tableImage, BufferedImage.TYPE_INT_ARGB), lblTableImage.getWidth(), lblTableImage.getHeight()); BufferedImage resizedTable = ImageHelper.getResizedImage(BufferedImageBuilder.bufferImage(tableImage, BufferedImage.TYPE_INT_ARGB), lblTableImage.getWidth(), lblTableImage.getHeight());
lblTableImage.setIcon(new ImageIcon(resizedTable)); lblTableImage.setIcon(new ImageIcon(resizedTable));
@ -414,7 +416,7 @@
if (currentBooster != null) { if (currentBooster != null) {
String lastPick = getCardName(getLastPick(pickView.getPicks().values())); String lastPick = getCardName(getLastPick(pickView.getPicks().values()));
if (lastPick != null && currentBooster.length > 1) { if (lastPick != null && currentBooster.length > 1) {
draftLogger.logPick(getCurrentSetCode(), packNo, pickNo - 1, lastPick, currentBooster); draftLogger.logPick(getCurrentSetCode(), packNo, pickNo - 1, lastPick, currentBooster); // wtf pickno need -1?
} }
currentBooster = null; currentBooster = null;
} }

View file

@ -177,7 +177,7 @@ public class TableView implements Serializable {
case DRAFTING: case DRAFTING:
Draft draft = table.getTournament().getDraft(); Draft draft = table.getTournament().getDraft();
if (draft != null) { if (draft != null) {
stateText.append(' ').append(draft.getBoosterNum()).append('/').append(draft.getCardNum() - 1); stateText.append(' ').append(draft.getBoosterNum()).append('/').append(draft.getCardNum());
} }
break; break;
default: default:

View file

@ -53,7 +53,7 @@ public class TournamentView implements Serializable {
tournamentState = tournament.getTournamentState(); tournamentState = tournament.getTournamentState();
if (tournament.getTournamentState().equals("Drafting") && tournament.getDraft() != null) { if (tournament.getTournamentState().equals("Drafting") && tournament.getDraft() != null) {
runningInfo = "booster/card: " + tournament.getDraft().getBoosterNum() + '/' + (tournament.getDraft().getCardNum() -1); runningInfo = "booster/card: " + tournament.getDraft().getBoosterNum() + '/' + (tournament.getDraft().getCardNum());
} else { } else {
runningInfo = ""; runningInfo = "";
} }

View file

@ -17,18 +17,21 @@ public class BoosterDraft extends DraftImpl {
@Override @Override
public void start() { public void start() {
cardNum = 0; cardNum = 1;
while (!isAbort() && boosterNum < numberBoosters) { boosterNum = 1;
while (!isAbort() && boosterNum <= numberBoosters) {
openBooster(); openBooster();
cardNum = 0; cardNum = 1;
fireUpdatePlayersEvent();
while (!isAbort() && pickCards()) { while (!isAbort() && pickCards()) {
if (boosterNum % 2 == 1) { if ((boosterNum + 1) % 2 == 1) {
passLeft(); passLeft();
} else { } else {
passRight(); passRight();
} }
fireUpdatePlayersEvent(); fireUpdatePlayersEvent();
} }
boosterNum++;
} }
resetBufferedCards(); resetBufferedCards();
this.fireEndDraftEvent(); this.fireEndDraftEvent();

View file

@ -22,8 +22,8 @@ public abstract class DraftImpl implements Draft {
protected DraftCube draftCube; protected DraftCube draftCube;
protected List<ExpansionSet> sets; protected List<ExpansionSet> sets;
protected List<String> setCodes; protected List<String> setCodes;
protected int boosterNum = 0; protected int boosterNum = 1; // starts with booster 1
protected int cardNum = 0; // increases +1 on first picking (so draft get 1 as first card number) protected int cardNum = 1; // starts with card number 1, increases by +1 after each picking
protected TimingOption timing; protected TimingOption timing;
protected boolean abort = false; protected boolean abort = false;
@ -192,21 +192,18 @@ public abstract class DraftImpl implements Draft {
} }
protected void openBooster() { protected void openBooster() {
if (boosterNum < numberBoosters) { if (boosterNum <= numberBoosters) {
for (DraftPlayer player : players.values()) { for (DraftPlayer player : players.values()) {
if (draftCube != null) { if (draftCube != null) {
player.setBooster(draftCube.createBooster()); player.setBooster(draftCube.createBooster());
} else { } else {
player.setBooster(sets.get(boosterNum).createBooster()); player.setBooster(sets.get(boosterNum - 1).createBooster());
} }
} }
} }
boosterNum++;
fireUpdatePlayersEvent();
} }
protected boolean pickCards() { protected boolean pickCards() {
cardNum++;
for (DraftPlayer player : players.values()) { for (DraftPlayer player : players.values()) {
if (player.getBooster().isEmpty()) { if (player.getBooster().isEmpty()) {
return false; return false;
@ -214,6 +211,7 @@ public abstract class DraftImpl implements Draft {
player.setPicking(); player.setPicking();
player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this); player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this);
} }
cardNum++;
synchronized (this) { synchronized (this) {
while (!donePicking()) { while (!donePicking()) {
try { try {
@ -264,9 +262,7 @@ public abstract class DraftImpl implements Draft {
@Override @Override
public void firePickCardEvent(UUID playerId) { public void firePickCardEvent(UUID playerId) {
DraftPlayer player = players.get(playerId); DraftPlayer player = players.get(playerId);
if (cardNum > 15) { int cardNum = Math.min(15, this.cardNum);
cardNum = 15;
}
int time = timing.getPickTimeout(cardNum); int time = timing.getPickTimeout(cardNum);
playerQueryEventSource.pickCard(playerId, "Pick card", player.getBooster(), time); playerQueryEventSource.pickCard(playerId, "Pick card", player.getBooster(), time);
} }

View file

@ -22,7 +22,7 @@ public class DraftOptions extends LimitedOptions implements Serializable {
REGULAR("x1.5", "Regular (x1.5)", 1.5, REGULAR("x1.5", "Regular (x1.5)", 1.5,
Arrays.asList(113, 105, 98, 90, 83, 75, 68, 60, 35, 30, 25, 20, 15, 10, 8) Arrays.asList(113, 105, 98, 90, 83, 75, 68, 60, 35, 30, 25, 20, 15, 10, 8)
), ),
PROFI("x1.0", "Profi (x1.0)", 1.0, PROFI("x1.0", "Professional (x1.0)", 1.0,
Arrays.asList(75, 70, 65, 60, 55, 50, 45, 40, 30, 25, 20, 15, 12, 10, 7) Arrays.asList(75, 70, 65, 60, 55, 50, 45, 40, 30, 25, 20, 15, 12, 10, 7)
), ),
NONE("ERROR", "", 0, NONE("ERROR", "", 0,

View file

@ -69,11 +69,6 @@ public class DraftPlayer {
picking = false; picking = false;
} }
// public void openBooster(ExpansionSet set) {
// synchronized(booster) {
// booster = set.createBooster();
// }
// }
public void setBooster(List<Card> booster) { public void setBooster(List<Card> booster) {
this.booster = booster; this.booster = booster;
} }

View file

@ -26,13 +26,11 @@ public class RandomBoosterDraft extends BoosterDraft {
@Override @Override
protected void openBooster() { protected void openBooster() {
if (boosterNum < numberBoosters) { if (boosterNum <= numberBoosters) {
for (DraftPlayer player: players.values()) { for (DraftPlayer player: players.values()) {
player.setBooster(getNextBooster().create15CardBooster()); player.setBooster(getNextBooster().create15CardBooster());
} }
} }
boosterNum++;
fireUpdatePlayersEvent();
} }
private ExpansionSet getNextBooster() { private ExpansionSet getNextBooster() {

View file

@ -25,14 +25,17 @@ public class RichManBoosterDraft extends DraftImpl {
@Override @Override
public void start() { public void start() {
cardNum = 0; cardNum = 1;
while (!isAbort() && cardNum < 36) { boosterNum = 1;
while (!isAbort() && cardNum <= 36) {
openBooster(); openBooster();
cardNum = 0; cardNum = 1;
fireUpdatePlayersEvent();
while (!isAbort() && pickCards()) { while (!isAbort() && pickCards()) {
passLeft(); passLeft();
fireUpdatePlayersEvent(); fireUpdatePlayersEvent();
} }
boosterNum++;
} }
resetBufferedCards(); resetBufferedCards();
this.fireEndDraftEvent(); this.fireEndDraftEvent();
@ -46,7 +49,7 @@ public class RichManBoosterDraft extends DraftImpl {
UUID nextId = table.getNext(); UUID nextId = table.getNext();
DraftPlayer next = players.get(nextId); DraftPlayer next = players.get(nextId);
while (true) { while (true) {
List<Card> nextBooster = sets.get(cardNum % sets.size()).createBooster(); List<Card> nextBooster = sets.get((cardNum - 1) % sets.size()).createBooster();
next.setBooster(nextBooster); next.setBooster(nextBooster);
if (Objects.equals(nextId, startId)) { if (Objects.equals(nextId, startId)) {
break; break;
@ -59,7 +62,6 @@ public class RichManBoosterDraft extends DraftImpl {
@Override @Override
protected boolean pickCards() { protected boolean pickCards() {
cardNum++;
for (DraftPlayer player : players.values()) { for (DraftPlayer player : players.values()) {
if (cardNum > 36) { if (cardNum > 36) {
return false; return false;
@ -67,6 +69,7 @@ public class RichManBoosterDraft extends DraftImpl {
player.setPicking(); player.setPicking();
player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this); player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this);
} }
cardNum++;
synchronized (this) { synchronized (this) {
while (!donePicking()) { while (!donePicking()) {
try { try {
@ -81,13 +84,7 @@ public class RichManBoosterDraft extends DraftImpl {
@Override @Override
public void firePickCardEvent(UUID playerId) { public void firePickCardEvent(UUID playerId) {
DraftPlayer player = players.get(playerId); DraftPlayer player = players.get(playerId);
if (cardNum > 36) { int cardNum = Math.min(36, this.cardNum);
cardNum = 36;
}
if (cardNum <= 0) {
cardNum = 1;
}
// richman uses custom times // richman uses custom times
int time = (int) Math.ceil(customProfiTimes[cardNum - 1] * timing.getCustomTimeoutFactor()); int time = (int) Math.ceil(customProfiTimes[cardNum - 1] * timing.getCustomTimeoutFactor());
playerQueryEventSource.pickCard(playerId, "Pick card", player.getBooster(), time); playerQueryEventSource.pickCard(playerId, "Pick card", player.getBooster(), time);

View file

@ -23,14 +23,17 @@ public class RichManCubeBoosterDraft extends DraftImpl {
@Override @Override
public void start() { public void start() {
cardNum = 0; cardNum = 1;
while (!isAbort() && cardNum < 36) { boosterNum = 1;
while (!isAbort() && cardNum <= 36) {
openBooster(); openBooster();
cardNum = 0; cardNum = 1;
fireUpdatePlayersEvent();
while (!isAbort() && pickCards()) { while (!isAbort() && pickCards()) {
passLeft(); passLeft();
fireUpdatePlayersEvent(); fireUpdatePlayersEvent();
} }
boosterNum++;
} }
resetBufferedCards(); resetBufferedCards();
this.fireEndDraftEvent(); this.fireEndDraftEvent();
@ -74,7 +77,6 @@ public class RichManCubeBoosterDraft extends DraftImpl {
@Override @Override
protected boolean pickCards() { protected boolean pickCards() {
cardNum++;
for (DraftPlayer player : players.values()) { for (DraftPlayer player : players.values()) {
if (cardNum > 36) { if (cardNum > 36) {
return false; return false;
@ -82,6 +84,7 @@ public class RichManCubeBoosterDraft extends DraftImpl {
player.setPicking(); player.setPicking();
player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this); player.getPlayer().pickCard(player.getBooster(), player.getDeck(), this);
} }
cardNum++;
synchronized (this) { synchronized (this) {
while (!donePicking()) { while (!donePicking()) {
try { try {
@ -96,12 +99,7 @@ public class RichManCubeBoosterDraft extends DraftImpl {
@Override @Override
public void firePickCardEvent(UUID playerId) { public void firePickCardEvent(UUID playerId) {
DraftPlayer player = players.get(playerId); DraftPlayer player = players.get(playerId);
if (cardNum > 36) { int cardNum = Math.min(36, this.cardNum);
cardNum = 36;
}
if (cardNum <= 0) {
cardNum = 1;
}
// richman uses custom times // richman uses custom times
int time = (int) Math.ceil(customProfiTimes[cardNum - 1] * timing.getCustomTimeoutFactor()); int time = (int) Math.ceil(customProfiTimes[cardNum - 1] * timing.getCustomTimeoutFactor());