Search in sources :

Example 6 with CardShake

use of org.magic.api.beans.CardShake in project MtgDesktopCompanion by nicho92.

the class BestTrendingDashlet method init.

@Override
public void init() {
    ThreadManager.getInstance().execute(() -> {
        try {
            List<CardShake> shakes = new ArrayList<>();
            if (boxM.isSelected())
                shakes.addAll(MTGControler.getInstance().getEnabledDashBoard().getShakerFor(FORMAT.MODERN.toString()));
            if (boxS.isSelected())
                shakes.addAll(MTGControler.getInstance().getEnabledDashBoard().getShakerFor(FORMAT.STANDARD.toString()));
            if (boxL.isSelected())
                shakes.addAll(MTGControler.getInstance().getEnabledDashBoard().getShakerFor(FORMAT.LEGACY.toString()));
            if (boxV.isSelected())
                shakes.addAll(MTGControler.getInstance().getEnabledDashBoard().getShakerFor(FORMAT.VINTAGE.toString()));
            Collections.sort(shakes, (CardShake o1, CardShake o2) -> {
                if (o1.getPriceDayChange() > o2.getPriceDayChange())
                    return -1;
                if (o1.getPriceDayChange() < o2.getPriceDayChange())
                    return 1;
                return 0;
            });
            int val = (Integer) spinner.getValue();
            save("LIMIT", String.valueOf(val));
            save("STD", String.valueOf(boxS.isSelected()));
            save("MDN", String.valueOf(boxM.isSelected()));
            save("LEG", String.valueOf(boxL.isSelected()));
            save("VIN", String.valueOf(boxV.isSelected()));
            List<CardShake> ret = new ArrayList<>();
            // X first
            ret.addAll(shakes.subList(0, val));
            // x last
            ret.addAll(shakes.subList(shakes.size() - (val + 1), shakes.size()));
            modStandard.init(ret);
        } catch (IOException e) {
            MTGLogger.printStackTrace(e);
        }
        table.setModel(modStandard);
        table.setRowSorter(new TableRowSorter(modStandard));
        table.packAll();
        table.getColumnModel().getColumn(3).setCellRenderer(new CardShakeRenderer());
        modStandard.fireTableDataChanged();
    }, "Init best Dashlet");
}
Also used : CardShakeRenderer(org.magic.gui.renderer.CardShakeRenderer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CardShake(org.magic.api.beans.CardShake) TableRowSorter(javax.swing.table.TableRowSorter)

Example 7 with CardShake

use of org.magic.api.beans.CardShake in project MtgDesktopCompanion by nicho92.

the class BoosterBoxDashlet method initGUI.

@Override
public void initGUI() {
    JSpinner boxSizeSpinner;
    JXTable table;
    BoostersTableModel boostersModel;
    DefaultListModel<MagicCard> cardsModel;
    JTextPane txtDetailBox;
    JPanel panneauHaut = new JPanel();
    getContentPane().add(panneauHaut, BorderLayout.NORTH);
    List<MagicEdition> eds = new ArrayList<>();
    try {
        eds.addAll(MTGControler.getInstance().getEnabledProviders().loadEditions());
        Collections.sort(eds);
        eds.add(0, null);
    } catch (Exception e) {
        MTGLogger.printStackTrace(e);
    }
    JComboBox<MagicEdition> cboEditions = new JComboBox<>(new DefaultComboBoxModel<MagicEdition>(eds.toArray(new MagicEdition[eds.size()])));
    cboEditions.setRenderer(new MagicEditionListRenderer());
    panneauHaut.add(cboEditions);
    JLabel lblBoxSize = new JLabel("Box size: ");
    panneauHaut.add(lblBoxSize);
    boxSizeSpinner = new JSpinner();
    boxSizeSpinner.setModel(new SpinnerNumberModel(36, 0, null, 1));
    panneauHaut.add(boxSizeSpinner);
    JScrollPane scrollPane = new JScrollPane();
    getContentPane().add(scrollPane, BorderLayout.CENTER);
    boostersModel = new BoostersTableModel();
    cardsModel = new DefaultListModel<>();
    table = new JXTable(boostersModel);
    scrollPane.setViewportView(table);
    JPanel panneauBas = new JPanel();
    getContentPane().add(panneauBas, BorderLayout.SOUTH);
    JButton btnCalculate = new JButton("Open");
    panneauBas.add(btnCalculate);
    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    getContentPane().add(tabbedPane, BorderLayout.EAST);
    txtDetailBox = new JTextPane();
    txtDetailBox.setEditable(false);
    tabbedPane.addTab("Box", null, txtDetailBox, null);
    JScrollPane scrollPane1 = new JScrollPane();
    tabbedPane.addTab("Booster", null, scrollPane1, null);
    JList<MagicCard> list1 = new JList<>();
    list1.setModel(cardsModel);
    list1.setCellRenderer(new MagicCardListRenderer());
    scrollPane1.setViewportView(list1);
    btnCalculate.addActionListener(e -> ThreadManager.getInstance().execute(() -> {
        try {
            List<CardShake> prices = MTGControler.getInstance().getEnabledDashBoard().getShakeForEdition((MagicEdition) cboEditions.getSelectedItem());
            boostersModel.clear();
            double total = 0;
            Map<String, Double> priceRarity = new HashMap<>();
            for (int i = 0; i < (int) boxSizeSpinner.getValue(); i++) {
                Booster booster = MTGControler.getInstance().getEnabledProviders().generateBooster((MagicEdition) cboEditions.getSelectedItem());
                Collections.reverse(booster.getCards());
                booster.setBoosterNumber(String.valueOf(i + 1));
                double price = 0;
                for (MagicCard mc : booster.getCards()) {
                    for (CardShake cs : prices) if (cs.getName().equalsIgnoreCase(mc.getName())) {
                        price += cs.getPrice();
                        booster.setPrice(price);
                        cs.setCard(mc);
                        String rarity = mc.getEditions().get(0).getRarity();
                        if (priceRarity.get(rarity) != null)
                            priceRarity.put(rarity, priceRarity.get(rarity) + cs.getPrice());
                        else
                            priceRarity.put(rarity, cs.getPrice());
                    }
                }
                boostersModel.addLine(booster);
                total = total + booster.getPrice();
                StringBuilder temp = new StringBuilder();
                temp.append("TOTAL: ").append(doubleFormat.format(total)).append("\n");
                for (Entry<String, Double> s : priceRarity.entrySet()) temp.append(s.getKey()).append(": ").append(doubleFormat.format(priceRarity.get(s.getKey()))).append("\n");
                txtDetailBox.setText(temp.toString());
            }
        } catch (Exception e1) {
            logger.error(e1);
        }
    }, "Open Box"));
    table.getSelectionModel().addListSelectionListener(event -> {
        if (!event.getValueIsAdjusting()) {
            int viewRow = table.getSelectedRow();
            if (viewRow > -1) {
                int modelRow = table.convertRowIndexToModel(viewRow);
                List<MagicCard> list = ((Booster) table.getModel().getValueAt(modelRow, 0)).getCards();
                cardsModel.clear();
                for (MagicCard mc : list) cardsModel.addElement(mc);
            }
        }
    });
    if (getProperties().size() > 0) {
        Rectangle r = new Rectangle((int) Double.parseDouble(getProperty("x")), (int) Double.parseDouble(getProperty("y")), (int) Double.parseDouble(getProperty("w")), (int) Double.parseDouble(getProperty("h")));
        setBounds(r);
    }
    setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) JTabbedPane(javax.swing.JTabbedPane) MagicEdition(org.magic.api.beans.MagicEdition) ArrayList(java.util.ArrayList) JButton(javax.swing.JButton) Rectangle(java.awt.Rectangle) MagicCardListRenderer(org.magic.gui.renderer.MagicCardListRenderer) SpinnerNumberModel(javax.swing.SpinnerNumberModel) JTextPane(javax.swing.JTextPane) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) BoostersTableModel(org.magic.gui.models.BoostersTableModel) CardShake(org.magic.api.beans.CardShake) JScrollPane(javax.swing.JScrollPane) JXTable(org.jdesktop.swingx.JXTable) JComboBox(javax.swing.JComboBox) Booster(org.magic.api.beans.Booster) JLabel(javax.swing.JLabel) MagicCard(org.magic.api.beans.MagicCard) JSpinner(javax.swing.JSpinner) HashMap(java.util.HashMap) Map(java.util.Map) MagicEditionListRenderer(org.magic.gui.renderer.MagicEditionListRenderer) JList(javax.swing.JList)

Example 8 with CardShake

use of org.magic.api.beans.CardShake in project MtgDesktopCompanion by nicho92.

the class MTGoldFishDashBoard method getShakerFor.

public List<CardShake> getShakerFor(String gameFormat) throws IOException {
    List<CardShake> list = new ArrayList<>();
    String urlW = getString("URL_MOVERS") + getString("FORMAT") + "/" + gameFormat.toLowerCase() + "/winners/" + getString("DAILY_WEEKLY");
    String urlL = getString("URL_MOVERS") + getString("FORMAT") + "/" + gameFormat.toLowerCase() + "/losers/" + getString("DAILY_WEEKLY");
    logger.debug("Loding Shake " + urlW);
    logger.debug("Loding Shake " + urlL);
    Document doc = read(urlW);
    Document doc2 = read(urlL);
    try {
        updateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(doc.getElementsByClass("timeago").get(0).attr("title"));
    } catch (ParseException e1) {
        logger.error(e1);
    }
    Element table = null;
    try {
        // combine 2 results
        table = doc.select(MTGConstants.HTML_TAG_TABLE).get(0).getElementsByTag("tbody").get(0).appendChild(doc2.select(MTGConstants.HTML_TAG_TABLE).get(0).getElementsByTag(MTGConstants.HTML_TAG_TBODY).get(0));
        for (Element e : table.getElementsByTag(MTGConstants.HTML_TAG_TR)) {
            CardShake cs = new CardShake();
            cs.setName(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(3).text().replaceAll("\\(RL\\)", "").trim());
            cs.setImg(new URL("http://" + e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(3).getElementsByTag("a").get(0).attr("data-full-image")));
            cs.setPrice(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(4).text()));
            cs.setPriceDayChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(1).text()));
            cs.setPercentDayChange(parseDouble(e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(5).text()));
            String set = e.getElementsByTag(MTGConstants.HTML_TAG_TD).get(2).getElementsByTag("img").get(0).attr("alt");
            cs.setEd(replace(set, true));
            list.add(cs);
        }
    } catch (IndexOutOfBoundsException e) {
        logger.error(e);
    }
    return list;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) Document(org.jsoup.nodes.Document) SimpleDateFormat(java.text.SimpleDateFormat) CardShake(org.magic.api.beans.CardShake) URL(java.net.URL)

Example 9 with CardShake

use of org.magic.api.beans.CardShake in project MtgDesktopCompanion by nicho92.

the class Shake method run.

@Override
public void run(String[] array, IoSession session, MTGConsoleHandler mtgConsoleHandler) throws ParseException, IOException {
    CommandLine cl = parser.parse(opts, array);
    this.session = session;
    if (cl.hasOption("f")) {
        String att = cl.getOptionValue("f");
        List<CardShake> list = MTGControler.getInstance().getEnabledDashBoard().getShakerFor(att);
        session.write(showList(list, Arrays.asList(MTGConsoleHandler.getAttShake())));
    }
    if (cl.hasOption("?")) {
        usage();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) CardShake(org.magic.api.beans.CardShake)

Aggregations

CardShake (org.magic.api.beans.CardShake)9 ArrayList (java.util.ArrayList)8 JsonElement (com.google.gson.JsonElement)4 IOException (java.io.IOException)4 Document (org.jsoup.nodes.Document)4 Element (org.jsoup.nodes.Element)4 JsonArray (com.google.gson.JsonArray)3 JsonObject (com.google.gson.JsonObject)3 URL (java.net.URL)3 ParseException (java.text.ParseException)3 JsonParser (com.google.gson.JsonParser)2 JsonReader (com.google.gson.stream.JsonReader)2 InputStreamReader (java.io.InputStreamReader)2 HttpURLConnection (java.net.HttpURLConnection)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 MagicCard (org.magic.api.beans.MagicCard)2 MagicEdition (org.magic.api.beans.MagicEdition)2