draft improves: add protection timer (2 sec) to avoid double pick (#11188)

This commit is contained in:
Susucre 2023-09-24 02:25:48 +02:00 committed by GitHub
parent 35710efa60
commit a58cac2fd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 13 deletions

View file

@ -1,15 +1,16 @@
package mage.client.cards; package mage.client.cards;
import mage.cards.CardDimensions;
import mage.abilities.icon.CardIconRenderSettings; import mage.abilities.icon.CardIconRenderSettings;
import mage.cards.CardDimensions;
import mage.cards.MageCard; import mage.cards.MageCard;
import mage.client.dialog.PreferencesDialog; import mage.client.dialog.PreferencesDialog;
import mage.client.draft.DraftPanel;
import mage.client.plugins.impl.Plugins; import mage.client.plugins.impl.Plugins;
import mage.client.util.comparators.CardViewRarityComparator;
import mage.client.util.ClientEventType; import mage.client.util.ClientEventType;
import mage.client.util.Event; import mage.client.util.Event;
import mage.client.util.Listener; import mage.client.util.Listener;
import mage.client.util.audio.AudioManager; import mage.client.util.audio.AudioManager;
import mage.client.util.comparators.CardViewRarityComparator;
import mage.constants.Constants; import mage.constants.Constants;
import mage.view.CardView; import mage.view.CardView;
import mage.view.CardsView; import mage.view.CardsView;
@ -28,6 +29,8 @@ public class DraftGrid extends javax.swing.JPanel implements CardEventProducer {
private static final Logger logger = Logger.getLogger(DraftGrid.class); private static final Logger logger = Logger.getLogger(DraftGrid.class);
private final DraftPanel parentPanel;
protected final CardEventSource cardEventSource = new CardEventSource(); protected final CardEventSource cardEventSource = new CardEventSource();
protected BigCard bigCard; protected BigCard bigCard;
protected MageCard markedCard; protected MageCard markedCard;
@ -36,22 +39,28 @@ public class DraftGrid extends javax.swing.JPanel implements CardEventProducer {
/** /**
* Creates new form DraftGrid * Creates new form DraftGrid
*/ */
public DraftGrid() { public DraftGrid(DraftPanel panel) {
initComponents(); initComponents();
parentPanel = panel;
markedCard = null; markedCard = null;
emptyGrid = true; emptyGrid = true;
// ENABLE picks and other actions // ENABLE picks and other actions
cardEventSource.addListener(new Listener<Event>() { cardEventSource.addListener(event -> {
@Override if (event.getEventType() == ClientEventType.CARD_DOUBLE_CLICK
public void event(Event event) { || event.getEventType() == ClientEventType.CARD_CLICK) {
if (event.getEventType() == ClientEventType.CARD_DOUBLE_CLICK) { // There is a protection against picking too early in DraftPanel logic.
CardView card = (CardView) event.getSource(); // So, when double clicking early, we do mark the card as selected like
// a single click would.
CardView card = (CardView) event.getSource();
if(event.getEventType() == ClientEventType.CARD_DOUBLE_CLICK
&& parentPanel.isAllowedToPick()
) {
cardEventSource.fireEvent(card, ClientEventType.DRAFT_PICK_CARD); cardEventSource.fireEvent(card, ClientEventType.DRAFT_PICK_CARD);
hidePopup(); hidePopup();
AudioManager.playOnDraftSelect(); AudioManager.playOnDraftSelect();
} else if (event.getEventType() == ClientEventType.CARD_CLICK) { } else {
CardView card = (CardView) event.getSource();
MageCard cardPanel = (MageCard) event.getComponent(); MageCard cardPanel = (MageCard) event.getComponent();
if (markedCard != null) { if (markedCard != null) {
markedCard.setSelected(false); markedCard.setSelected(false);

View file

@ -19,7 +19,6 @@
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.dnd.DragSourceEvent;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -42,6 +41,20 @@
private Timer countdown; private Timer countdown;
private int timeout; private int timeout;
/**
* ms delay between booster showing up and pick being allowed.
*/
private static final int protectionTime = 2000;
/**
* Timer starting at booster being displayed, to protect from early pick due to clicking
* a little too much on the last pick.
*/
private Timer protectionTimer;
/**
* Number of the latest card pick for which the protection timer has been set.
*/
private int protectionPickNo = 0;
// popup menu area picked cards // popup menu area picked cards
private final JPopupMenu popupMenuPickedArea; private final JPopupMenu popupMenuPickedArea;
// popup menu for a card // popup menu for a card
@ -108,6 +121,10 @@
} }
} }
); );
protectionTimer = new Timer(protectionTime, e -> {
protectionTimer.stop();
});
} }
public void cleanUp() { public void cleanUp() {
@ -120,6 +137,13 @@
countdown.removeActionListener(al); countdown.removeActionListener(al);
} }
} }
if (protectionTimer != null) {
protectionTimer.stop();
for (ActionListener al : protectionTimer.getActionListeners()) {
protectionTimer.removeActionListener(al);
}
}
} }
public void changeGUISize() { public void changeGUISize() {
@ -311,7 +335,13 @@
} }
if (!draftBooster.isEmptyGrid()) { if (!draftBooster.isEmptyGrid()) {
SessionHandler.setBoosterLoaded(draftId); // confirm to the server that the booster has been successfully loaded, otherwise the server will re-send the booster SessionHandler.setBoosterLoaded(draftId); // confirm to the server that the booster has been successfully loaded, otherwise the server will re-send the booster
if(pickNo != protectionPickNo && !protectionTimer.isRunning()) {
// Restart the protection timer.
protectionPickNo = pickNo;
protectionTimer.restart();
}
} }
} }
@ -345,6 +375,10 @@
} }
} }
public boolean isAllowedToPick() {
return !protectionTimer.isRunning();
}
public void hideDraft() { public void hideDraft() {
Component c = this.getParent(); Component c = this.getParent();
while (c != null && !(c instanceof DraftPane)) { while (c != null && !(c instanceof DraftPane)) {
@ -525,7 +559,7 @@
lblPlayer15 = new javax.swing.JLabel(); lblPlayer15 = new javax.swing.JLabel();
lblPlayer16 = new javax.swing.JLabel(); lblPlayer16 = new javax.swing.JLabel();
draftPicks = new mage.client.cards.CardsList(); draftPicks = new mage.client.cards.CardsList();
draftBooster = new mage.client.cards.DraftGrid(); draftBooster = new mage.client.cards.DraftGrid(this);
draftLeftPane.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); draftLeftPane.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
draftLeftPane.setFocusable(false); draftLeftPane.setFocusable(false);