query PWL stats only once per deck using PwlWorker.

master
lodici 2017-02-07 12:54:25 +00:00
parent b4bce924e4
commit 32557fdf64
11 changed files with 142 additions and 57 deletions

View File

@ -7,15 +7,18 @@ import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
import magic.ui.screen.deck.editor.DeckSideBar;
import magic.ui.widget.cards.table.CardTablePanelA;
import magic.ui.widget.deck.stats.IPwlWorkerListener;
import magic.ui.widget.deck.stats.PwlWorker;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
class DeckScreenPanel extends JPanel {
class DeckScreenPanel extends JPanel implements IPwlWorkerListener {
private MagicDeck deck;
private final MigLayout migLayout = new MigLayout();
private final DeckSideBar sideBarPanel;
private final CardTablePanelA deckTable;
private PwlWorker pwlWorker;
DeckScreenPanel(final MagicDeck aDeck, final MagicCardDefinition selectedCard) {
@ -86,6 +89,17 @@ class DeckScreenPanel extends JPanel {
void setDeck(MagicDeck aDeck) {
this.deck = aDeck;
doPWLStatsQuery(deck);
}
@Override
public void setPlayedWonLost(String pwl) {
sideBarPanel.setPlayedWonLost(pwl);
}
private void doPWLStatsQuery(MagicDeck deck) {
pwlWorker = new PwlWorker(deck);
pwlWorker.setListeners(sideBarPanel);
pwlWorker.execute();
}
}

View File

@ -7,6 +7,7 @@ import magic.model.MagicDeck;
import magic.model.MagicDeckConstructionRule;
import magic.translate.MText;
import magic.ui.ScreenController;
import magic.ui.widget.deck.stats.PwlWorker;
import magic.utility.MagicSystem;
import net.miginfocom.swing.MigLayout;
@ -24,6 +25,7 @@ class ContentPanel extends JPanel implements IDeckEditorListener {
private final MainViewsPanel viewsPanel; // RHS
private final IDeckEditorListener listener;
private boolean isStandalone = true;
private PwlWorker pwlWorker;
ContentPanel(IDeckEditorListener aListener) {
@ -96,6 +98,7 @@ class ContentPanel extends JPanel implements IDeckEditorListener {
CONFIG.save();
}
listener.deckUpdated(deck);
doPWLStatsQuery(deck);
}
@Override
@ -114,4 +117,10 @@ class ContentPanel extends JPanel implements IDeckEditorListener {
throw new UnsupportedOperationException("Not supported yet.");
}
private void doPWLStatsQuery(MagicDeck deck) {
pwlWorker = new PwlWorker(deck);
pwlWorker.setListeners(sideBarPanel, viewsPanel);
pwlWorker.execute();
}
}

View File

@ -6,14 +6,15 @@ import javax.swing.BorderFactory;
import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
import magic.ui.widget.M.MScrollPane;
import magic.ui.widget.duel.viewer.CardViewer;
import magic.ui.widget.duel.viewer.DeckEditorCardViewer;
import magic.ui.widget.TexturedPanel;
import magic.ui.widget.deck.DeckInfoPanel;
import magic.ui.widget.deck.stats.IPwlWorkerListener;
import magic.ui.widget.duel.viewer.CardViewer;
import magic.ui.widget.duel.viewer.DeckEditorCardViewer;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
public class DeckSideBar extends TexturedPanel {
public class DeckSideBar extends TexturedPanel implements IPwlWorkerListener {
private final MigLayout migLayout = new MigLayout();
private final MScrollPane cardScrollPane = new MScrollPane();
@ -68,4 +69,9 @@ public class DeckSideBar extends TexturedPanel {
return cardViewer;
}
@Override
public void setPlayedWonLost(String pwl) {
deckInfo.setPlayedWonLost(pwl);
}
}

View File

@ -6,7 +6,6 @@ import javax.swing.AbstractAction;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import magic.data.GeneralConfig;
import magic.data.stats.MagicStats;
import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
import magic.translate.MText;
@ -15,10 +14,12 @@ import magic.ui.ScreenController;
import magic.ui.helpers.MouseHelper;
import magic.ui.screen.deck.editor.stats.DeckStatsPanel;
import magic.ui.widget.cards.table.CardsJTable;
import magic.ui.widget.deck.stats.IPwlWorkerListener;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
class MainViewsPanel extends JPanel implements IDeckEditorListener {
class MainViewsPanel extends JPanel
implements IDeckEditorListener, IPwlWorkerListener {
// translatable strings
private static final String _S1 = "Deck";
@ -132,7 +133,7 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener {
});
}
statsToggleButton = toggleButtonsPanel.addToggleButton(getStatsTabCaption(), new AbstractAction() {
statsToggleButton = toggleButtonsPanel.addToggleButton("", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
MouseHelper.showBusyCursor((Component) e.getSource());
@ -146,10 +147,6 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener {
toggleButtonsPanel.refreshLayout();
}
private String getStatsTabCaption() {
return MagicStats.getPlayedWonLost(controller.getDeck());
}
private void addPropertyChangeListeners() {
// DeckPanel
@ -242,7 +239,6 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener {
deckPanel.doRefreshView();
legalityPanel.setDeck(controller.getDeck());
statsPanel.setDeck(controller.getDeck());
statsToggleButton.setText(getStatsTabCaption());
activeView.notifyShowing();
}
@ -267,4 +263,9 @@ class MainViewsPanel extends JPanel implements IDeckEditorListener {
doRefreshViews();
}
@Override
public void setPlayedWonLost(String pwl) {
statsToggleButton.setText(pwl);
}
}

View File

@ -9,6 +9,7 @@ import magic.model.MagicDeck;
import magic.ui.screen.interfaces.IDeckConsumer;
import magic.ui.widget.cards.table.CardTablePanelB;
import magic.ui.widget.deck.DeckStatusPanel;
import magic.ui.widget.deck.stats.PwlWorker;
import magic.ui.widget.duel.viewer.CardViewer;
import magic.utility.DeckUtils;
import net.miginfocom.swing.MigLayout;
@ -23,6 +24,7 @@ class DecksScreenPanel extends JPanel implements IDeckConsumer {
private final JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
private final DeckStatusPanel deckStatusPanel;
private final SidebarPanel sidebar;
private PwlWorker pwlWorker;
DecksScreenPanel(DeckStatusPanel deckStatusPanel) {
@ -74,7 +76,7 @@ class DecksScreenPanel extends JPanel implements IDeckConsumer {
@Override
public void setDeck(String deckName, DeckType deckType) {
System.out.println("setDeck(" + deckName + ", " + deckType + ")");
throw new UnsupportedOperationException("setDeck(" + deckName + ", " + deckType + ")");
}
@Override
@ -94,17 +96,19 @@ class DecksScreenPanel extends JPanel implements IDeckConsumer {
deckStatusPanel.setDeck(null, false);
splitter.setVisible(false);
}
doPWLStatsQuery(deck);
return true;
}
@Override
public void setDeck(MagicDeck deck) {
selectedDeck = deck;
deckFilePath = DeckUtils.getDeckPath(deck);
sidebar.setDeck(deck);
deckTable.setCards(deck);
deckStatusPanel.setDeck(deck, deck.isValid() || deck.size() > 0);
splitter.setVisible(deck.isValid() || deck.size() > 0);
setDeck(deck, DeckUtils.getDeckPath(deck));
}
private void doPWLStatsQuery(MagicDeck deck) {
pwlWorker = new PwlWorker(deck);
pwlWorker.setListeners(sidebar);
pwlWorker.execute();
}
}

View File

@ -4,16 +4,17 @@ import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import magic.model.MagicDeck;
import magic.ui.FontsAndBorders;
import magic.ui.screen.interfaces.IDeckConsumer;
import magic.ui.widget.TexturedPanel;
import magic.ui.widget.deck.DeckInfoPanel;
import magic.ui.widget.deck.DeckPicker;
import magic.ui.widget.deck.stats.IPwlWorkerListener;
import magic.ui.widget.duel.viewer.CardViewer;
import magic.ui.screen.interfaces.IDeckConsumer;
import magic.ui.FontsAndBorders;
import magic.ui.widget.TexturedPanel;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
class SidebarPanel extends TexturedPanel {
class SidebarPanel extends TexturedPanel implements IPwlWorkerListener {
private final DeckPicker deckPicker;
private final DeckInfoPanel deckInfo;
@ -53,4 +54,9 @@ class SidebarPanel extends TexturedPanel {
deckInfo.setDeck(deck);
}
@Override
public void setPlayedWonLost(String pwl) {
deckInfo.setPlayedWonLost(pwl);
}
}

View File

@ -30,10 +30,13 @@ import magic.ui.screen.widget.ActionBarButton;
import magic.ui.screen.widget.MenuButton;
import magic.ui.widget.TexturedPanel;
import magic.ui.widget.cards.table.CardTablePanelB;
import magic.ui.widget.deck.stats.IPwlWorkerListener;
import magic.ui.widget.deck.stats.PwlWorker;
import net.miginfocom.swing.MigLayout;
@SuppressWarnings("serial")
class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener {
class DuelDecksPanel extends TexturedPanel
implements IPlayerProfileListener, IPwlWorkerListener {
// translatable strings
private static final String _S7 = "Swap Decks";
@ -50,8 +53,8 @@ class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener {
private final CardTablePanelB[] cardTables;
private final DeckSideBar sidebar;
private final ActionBarButton newDeckButton;
private boolean isTabChanged = false;
private PwlWorker pwlWorker;
DuelDecksPanel(final MagicDuel duel) {
@ -128,6 +131,7 @@ class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener {
DuelPlayerConfig player = getSelectedPlayer();
MagicDeck deck = player.getDeck();
sidebar.setDeck(deck);
doPWLStatsQuery(deck);
sidebar.setCard(deck.get(0));
boolean isRandomDeck = player.getDeckProfile().getDeckType() == DeckType.Random;
newDeckButton.setEnabled(isRandomDeck && duel.getGamesPlayed() == 0);
@ -275,4 +279,15 @@ class DuelDecksPanel extends TexturedPanel implements IPlayerProfileListener {
cardTables[0].setStyle();
cardTables[1].setStyle();
}
@Override
public void setPlayedWonLost(String pwl) {
sidebar.setPlayedWonLost(pwl);
}
private void doPWLStatsQuery(MagicDeck deck) {
pwlWorker = new PwlWorker(deck);
pwlWorker.setListeners(sidebar);
pwlWorker.execute();
}
}

View File

@ -49,4 +49,8 @@ public class DeckInfoPanel extends TexturedPanel {
descViewer.setDeckDescription(aDeck);
}
public void setPlayedWonLost(String pwl) {
statsViewer.setPlayedWonLost(pwl);
}
}

View File

@ -3,21 +3,16 @@ package magic.ui.widget.deck.stats;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import magic.data.CardStatistics;
import magic.data.GeneralConfig;
import magic.data.MagicIcon;
import magic.data.stats.MagicStats;
import magic.model.DuelPlayerConfig;
import magic.model.MagicDeck;
import magic.translate.MText;
@ -36,13 +31,10 @@ public class DeckStatisticsViewer extends JPanel implements ChangeListener {
private static final String _S1 = "Deck Statistics";
private static final String _S2 = "%d card deck";
private static final Logger LOGGER = Logger.getLogger(DeckStatisticsViewer.class.getName());
private final ActionButtonTitleBar titleBar;
private final ManaCurvePanel manaCurvePanel;
private final ActionBarButton titlebarButton;
private final StatsTable statsTable;
private SwingWorker<String, Void> pwlWorker;
private String pwl = "";
private CardStatistics statistics;
private MagicDeck thisDeck;
@ -127,39 +119,17 @@ public class DeckStatisticsViewer extends JPanel implements ChangeListener {
manaCurvePanel.setStats(statistics);
if (isNewDeck(aDeck)) {
this.thisDeck = aDeck;
doStatsQueryPWL(aDeck);
}
setPlayedWinLost(pwl);
setPlayedWonLost(pwl);
}
private void setPlayedWinLost(String newPWL) {
public void setPlayedWonLost(String newPWL) {
pwl = newPWL;
titleBar.setText(MText.get(_S2, statistics.totalCards)
+ (!newPWL.isEmpty() ? " • " + newPWL : "")
);
}
private void doStatsQueryPWL(MagicDeck aDeck) {
final MagicDeck deckCopy = new MagicDeck(aDeck);
pwlWorker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
return MagicStats.getPlayedWonLost(deckCopy);
}
@Override
protected void done() {
try {
setPlayedWinLost(get());
} catch (CancellationException ex) {
LOGGER.log(Level.INFO, "pwlWorker cancelled.");
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
};
pwlWorker.execute();
}
@Override
public void stateChanged(final ChangeEvent event) {
setDeck(((DuelPlayerConfig)event.getSource()).getDeck());

View File

@ -0,0 +1,5 @@
package magic.ui.widget.deck.stats;
public interface IPwlWorkerListener {
public void setPlayedWonLost(String pwl);
}

View File

@ -0,0 +1,51 @@
package magic.ui.widget.deck.stats;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import magic.data.stats.MagicStats;
import magic.model.MagicDeck;
public class PwlWorker extends SwingWorker<String, Void> {
private static final Logger LOGGER = Logger.getLogger(PwlWorker.class.getName());
private final MagicDeck deckCopy;
private final List<IPwlWorkerListener> listeners = new ArrayList<>();
public PwlWorker(MagicDeck deck) {
deckCopy = new MagicDeck(deck);
}
public void setListeners(IPwlWorkerListener... newListeners) {
listeners.clear();
listeners.addAll(Arrays.asList(newListeners));
}
@Override
protected String doInBackground() throws Exception {
return MagicStats.getPlayedWonLost(deckCopy);
}
@Override
protected void done() {
try {
final String pwl = get();
for (IPwlWorkerListener listener : listeners) {
SwingUtilities.invokeLater(() -> {
listener.setPlayedWonLost(pwl);
});
}
System.out.println("PwlWorker.done() : " + pwl);
} catch (CancellationException ex) {
LOGGER.log(Level.INFO, "pwlWorker cancelled.");
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
}