Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 2s protection timer during draft #11188

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions Mage.Client/src/main/java/mage/client/cards/DraftGrid.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package mage.client.cards;

import mage.cards.CardDimensions;
import mage.abilities.icon.CardIconRenderSettings;
import mage.cards.CardDimensions;
import mage.cards.MageCard;
import mage.client.dialog.PreferencesDialog;
import mage.client.draft.DraftPanel;
import mage.client.plugins.impl.Plugins;
import mage.client.util.comparators.CardViewRarityComparator;
import mage.client.util.ClientEventType;
import mage.client.util.Event;
import mage.client.util.Listener;
import mage.client.util.audio.AudioManager;
import mage.client.util.comparators.CardViewRarityComparator;
import mage.constants.Constants;
import mage.view.CardView;
import mage.view.CardsView;
Expand All @@ -28,6 +29,8 @@ public class DraftGrid extends javax.swing.JPanel implements CardEventProducer {

private static final Logger logger = Logger.getLogger(DraftGrid.class);

private final DraftPanel parentPanel;

protected final CardEventSource cardEventSource = new CardEventSource();
protected BigCard bigCard;
protected MageCard markedCard;
Expand All @@ -36,22 +39,28 @@ public class DraftGrid extends javax.swing.JPanel implements CardEventProducer {
/**
* Creates new form DraftGrid
*/
public DraftGrid() {
public DraftGrid(DraftPanel panel) {
initComponents();
parentPanel = panel;
markedCard = null;
emptyGrid = true;

// ENABLE picks and other actions
cardEventSource.addListener(new Listener<Event>() {
@Override
public void event(Event event) {
if (event.getEventType() == ClientEventType.CARD_DOUBLE_CLICK) {
CardView card = (CardView) event.getSource();
cardEventSource.addListener(event -> {
if (event.getEventType() == ClientEventType.CARD_DOUBLE_CLICK
|| event.getEventType() == ClientEventType.CARD_CLICK) {
// There is a protection against picking too early in DraftPanel logic.
// 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);
hidePopup();
AudioManager.playOnDraftSelect();
} else if (event.getEventType() == ClientEventType.CARD_CLICK) {
CardView card = (CardView) event.getSource();
} else {
MageCard cardPanel = (MageCard) event.getComponent();
if (markedCard != null) {
markedCard.setSelected(false);
Expand Down
40 changes: 37 additions & 3 deletions Mage.Client/src/main/java/mage/client/draft/DraftPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import javax.swing.Timer;
import javax.swing.*;
import java.awt.*;
import java.awt.dnd.DragSourceEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
Expand All @@ -42,6 +41,20 @@ public class DraftPanel extends javax.swing.JPanel {
private Timer countdown;
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
private final JPopupMenu popupMenuPickedArea;
// popup menu for a card
Expand Down Expand Up @@ -108,6 +121,10 @@ public DraftPanel() {
}
}
);

protectionTimer = new Timer(protectionTime, e -> {
protectionTimer.stop();
});
}

public void cleanUp() {
Expand All @@ -120,6 +137,13 @@ public void cleanUp() {
countdown.removeActionListener(al);
}
}

if (protectionTimer != null) {
protectionTimer.stop();
for (ActionListener al : protectionTimer.getActionListeners()) {
protectionTimer.removeActionListener(al);
}
}
}

public void changeGUISize() {
Expand Down Expand Up @@ -311,7 +335,13 @@ public void loadBooster(DraftPickView draftPickView) {
}

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

Expand Down Expand Up @@ -345,6 +375,10 @@ private void setTimeout(int s) {
}
}

public boolean isAllowedToPick() {
return !protectionTimer.isRunning();
}

public void hideDraft() {
Component c = this.getParent();
while (c != null && !(c instanceof DraftPane)) {
Expand Down Expand Up @@ -525,7 +559,7 @@ private void initComponents() {
lblPlayer15 = new javax.swing.JLabel();
lblPlayer16 = new javax.swing.JLabel();
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.setFocusable(false);
Expand Down