diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchiveBlock.java b/forge-gui/src/main/java/forge/deck/NetDeckArchiveBlock.java index 383e77a52e3..487a8a5fff5 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchiveBlock.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchiveBlock.java @@ -1,122 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchiveBlock extends StorageBase { +public class NetDeckArchiveBlock extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_BLOCK_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchiveBlock(name, url)); - } - } - } - return categories; - } public static NetDeckArchiveBlock selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchiveBlock selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_BLOCK_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchiveBlock category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchiveBlock c = SGuiChoose.oneOrNone("Select a Net Deck Archive Block category", category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchiveBlock(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - - - public String getUrl() { - return url; + public static NetDeckArchiveBlock selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_BLOCK_DECKS_LIST_FILE, "Block", NetDeckArchiveBlock::new); } - public String getDeckType() { - return "Net Archive Block Decks - " + name; + public static NetDeckArchiveBlock selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_BLOCK_DECKS_LIST_FILE, "Block", NetDeckArchiveBlock::new); } - @Override - public String toString() { - return name; + private NetDeckArchiveBlock(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Block Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchiveLegacy.java b/forge-gui/src/main/java/forge/deck/NetDeckArchiveLegacy.java index 667fc26aed0..8ee9451e675 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchiveLegacy.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchiveLegacy.java @@ -1,122 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchiveLegacy extends StorageBase { +public class NetDeckArchiveLegacy extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_LEGACY_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchiveLegacy(name, url)); - } - } - } - return categories; - } public static NetDeckArchiveLegacy selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchiveLegacy selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_LEGACY_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchiveLegacy category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchiveLegacy c = SGuiChoose.oneOrNone("Select a Net Deck Archive Legacy category", category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchiveLegacy(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - - - public String getUrl() { - return url; + public static NetDeckArchiveLegacy selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_LEGACY_DECKS_LIST_FILE, "Legacy", NetDeckArchiveLegacy::new); } - public String getDeckType() { - return "Net Archive Legacy Decks - " + name; + public static NetDeckArchiveLegacy selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_LEGACY_DECKS_LIST_FILE, "Legacy", NetDeckArchiveLegacy::new); } - @Override - public String toString() { - return name; + private NetDeckArchiveLegacy(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Legacy Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchiveModern.java b/forge-gui/src/main/java/forge/deck/NetDeckArchiveModern.java index 2060490abc5..08723484b3e 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchiveModern.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchiveModern.java @@ -1,121 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchiveModern extends StorageBase { +public class NetDeckArchiveModern extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_MODERN_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchiveModern(name, url)); - } - } - } - return categories; - } public static NetDeckArchiveModern selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchiveModern selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_MODERN_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchiveModern category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchiveModern c = SGuiChoose.oneOrNone("Select a Net Deck Archive Modern category", category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchiveModern(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - public String getUrl() { - return url; + public static NetDeckArchiveModern selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_MODERN_DECKS_LIST_FILE, "Modern", NetDeckArchiveModern::new); } - public String getDeckType() { - return "Net Archive Modern Decks - " + name; + public static NetDeckArchiveModern selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_MODERN_DECKS_LIST_FILE, "Modern", NetDeckArchiveModern::new); } - @Override - public String toString() { - return name; + private NetDeckArchiveModern(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Modern Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchivePauper.java b/forge-gui/src/main/java/forge/deck/NetDeckArchivePauper.java index ec60dcb8658..b4bb346a6f9 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchivePauper.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchivePauper.java @@ -1,122 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchivePauper extends StorageBase { +public class NetDeckArchivePauper extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_PAUPER_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchivePauper(name, url)); - } - } - } - return categories; - } public static NetDeckArchivePauper selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchivePauper selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_PAUPER_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchivePauper category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchivePauper c = SGuiChoose.oneOrNone("Select a Net Deck Archive Pauper category", category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchivePauper(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - - - public String getUrl() { - return url; + public static NetDeckArchivePauper selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_PAUPER_DECKS_LIST_FILE, "Pauper", NetDeckArchivePauper::new); } - public String getDeckType() { - return "Net Archive Pauper Decks - " + name; + public static NetDeckArchivePauper selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_PAUPER_DECKS_LIST_FILE, "Pauper", NetDeckArchivePauper::new); } - @Override - public String toString() { - return name; + private NetDeckArchivePauper(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Pauper Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchivePioneer.java b/forge-gui/src/main/java/forge/deck/NetDeckArchivePioneer.java index 903d120da1e..44276e51df5 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchivePioneer.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchivePioneer.java @@ -1,121 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchivePioneer extends StorageBase { +public class NetDeckArchivePioneer extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_PIONEER_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchivePioneer(name, url)); - } - } - } - return categories; - } public static NetDeckArchivePioneer selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchivePioneer selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_PIONEER_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchivePioneer category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchivePioneer c = SGuiChoose.oneOrNone("Select a Net Deck Archive Pioneer category", category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchivePioneer(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - public String getUrl() { - return url; + public static NetDeckArchivePioneer selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_PIONEER_DECKS_LIST_FILE, "Pioneer", NetDeckArchivePioneer::new); } - public String getDeckType() { - return "Net Archive Pioneer Decks - " + name; + public static NetDeckArchivePioneer selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_PIONEER_DECKS_LIST_FILE, "Pioneer", NetDeckArchivePioneer::new); } - @Override - public String toString() { - return name; + private NetDeckArchivePioneer(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Pioneer Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchiveStandard.java b/forge-gui/src/main/java/forge/deck/NetDeckArchiveStandard.java index 62ae7f782a0..9d818226036 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchiveStandard.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchiveStandard.java @@ -1,122 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchiveStandard extends StorageBase { +public class NetDeckArchiveStandard extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_STANDARD_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchiveStandard(name, url)); - } - } - } - return categories; - } public static NetDeckArchiveStandard selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchiveStandard selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_STANDARD_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchiveStandard category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchiveStandard c = SGuiChoose.oneOrNone("Select a Net Deck Archive Standard category",category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchiveStandard(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - - - public String getUrl() { - return url; + public static NetDeckArchiveStandard selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_STANDARD_DECKS_LIST_FILE, "Standard", NetDeckArchiveStandard::new); } - public String getDeckType() { - return "Net Archive Standard Decks - " + name; + public static NetDeckArchiveStandard selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_STANDARD_DECKS_LIST_FILE, "Standard", NetDeckArchiveStandard::new); } - @Override - public String toString() { - return name; + private NetDeckArchiveStandard(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Standard Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckArchiveVintage.java b/forge-gui/src/main/java/forge/deck/NetDeckArchiveVintage.java index 97a917362f0..528e4a21b0f 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckArchiveVintage.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckArchiveVintage.java @@ -1,122 +1,26 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; - -public class NetDeckArchiveVintage extends StorageBase { +public class NetDeckArchiveVintage extends NetDeckStorageBase { public static final String PREFIX = "NET_ARCHIVE_VINTAGE_DECK"; - private static Map constructed, commander, brawl; - - private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckArchiveVintage(name, url)); - } - } - } - return categories; - } public static NetDeckArchiveVintage selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } - public static NetDeckArchiveVintage selectAndLoad(GameType gameType, String name) { - Map categories; - switch (gameType) { - case Constructed: - case Gauntlet: - if (constructed == null) { - constructed = loadCategories(ForgeConstants.NET_ARCHIVE_VINTAGE_DECKS_LIST_FILE); - } - categories = constructed; - break; - default: - return null; - } - - if (name != null) { - NetDeckArchiveVintage category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - List category = new ArrayList<>(categories.values()); - Collections.reverse(category); - - final NetDeckArchiveVintage c = SGuiChoose.oneOrNone("Select a Net Deck Archive Vintage category", category); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; - } - - private final String url; - - private NetDeckArchiveVintage(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, new HashMap<>()); - url = url0; - } - - - public String getUrl() { - return url; + public static NetDeckArchiveVintage selectAndLoad(GameType gameType, String name) { + return selectAndLoadArchive(gameType, name, + ForgeConstants.NET_ARCHIVE_VINTAGE_DECKS_LIST_FILE, "Vintage", NetDeckArchiveVintage::new); } - public String getDeckType() { - return "Net Archive Vintage Decks - " + name; + public static NetDeckArchiveVintage selectAndLoad(GameType gameType, String name, boolean forceDownload) { + return selectAndLoadArchive(gameType, name, forceDownload, + ForgeConstants.NET_ARCHIVE_VINTAGE_DECKS_LIST_FILE, "Vintage", NetDeckArchiveVintage::new); } - @Override - public String toString() { - return name; + private NetDeckArchiveVintage(String name0, String url0) { + super(name0, ForgeConstants.DECK_NET_ARCHIVE_DIR + name0, url0, "Net Archive Vintage Decks"); } } diff --git a/forge-gui/src/main/java/forge/deck/NetDeckCategory.java b/forge-gui/src/main/java/forge/deck/NetDeckCategory.java index a43d38c63ac..ddcb22729e1 100644 --- a/forge-gui/src/main/java/forge/deck/NetDeckCategory.java +++ b/forge-gui/src/main/java/forge/deck/NetDeckCategory.java @@ -1,48 +1,35 @@ package forge.deck; -import forge.deck.io.DeckSerializer; -import forge.deck.io.DeckStorage; import forge.game.GameType; -import forge.gui.GuiBase; -import forge.gui.download.GuiDownloadZipService; -import forge.gui.util.SGuiChoose; import forge.localinstance.properties.ForgeConstants; -import forge.util.FileUtil; -import forge.util.WaitCallback; -import forge.util.storage.StorageBase; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.TreeMap; -public class NetDeckCategory extends StorageBase { +public class NetDeckCategory extends NetDeckStorageBase { public static final String PREFIX = "NET_DECK_"; private static Map constructed, commander, brawl, oathbreaker, tinyleaders; private static Map loadCategories(String filename) { - Map categories = new TreeMap<>(); - if (FileUtil.doesFileExist(filename)) { - List lines = FileUtil.readFile(filename); - for (String line : lines) { - int idx = line.indexOf('|'); - if (idx != -1) { - String name = line.substring(0, idx).trim(); - String url = line.substring(idx + 1).trim(); - categories.put(name, new NetDeckCategory(name, url)); - } - } - } - return categories; + return loadCategories(filename, NetDeckCategory::new); } public static NetDeckCategory selectAndLoad(GameType gameType) { return selectAndLoad(gameType, null); } public static NetDeckCategory selectAndLoad(GameType gameType, String name) { + return selectAndLoad(gameType, name, false); + } + + public static Map getCategories(GameType gameType) { + return getCategoriesForGameType(gameType); + } + + public static NetDeckCategory selectAndLoad(GameType gameType, String name, boolean forceDownload) { + Map categories = getCategoriesForGameType(gameType); + return selectAndLoad(categories, name, forceDownload, "Select a Net Deck category"); + } + + private static Map getCategoriesForGameType(GameType gameType) { Map categories; switch (gameType) { case Constructed: @@ -79,59 +66,11 @@ public static NetDeckCategory selectAndLoad(GameType gameType, String name) { default: return null; } - - if (name != null) { - NetDeckCategory category = categories.get(name); - if (category != null && category.map.isEmpty()) { - //if name passed in, try to load decks from current cached files - File downloadDir = new File(category.getFullPath()); - if (downloadDir.exists()) { - for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { - Deck deck = DeckSerializer.fromFile(file); - if (deck != null) { - category.map.put(deck.getName(), deck); - } - } - } - } - return category; - } - - final NetDeckCategory c = SGuiChoose.oneOrNone("Select a Net Deck category", categories.values()); - if (c == null) { return null; } - - if (c.map.isEmpty()) { //only download decks once per session - WaitCallback callback = new WaitCallback() { - @Override - public void run() { - String downloadLoc = c.getFullPath(); - GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { - @Override - protected void copyInputStream(InputStream in, String outPath) throws IOException { - super.copyInputStream(in, outPath); - - Deck deck = DeckSerializer.fromFile(new File(outPath)); - if (deck != null) { - c.map.put(deck.getName(), deck); - } - } - }, this); - } - }; - if (!callback.invokeAndWait()) { return null; } //wait for download to finish - } - return c; + return categories; } - private final String url; - private NetDeckCategory(String name0, String url0) { - super(name0, ForgeConstants.DECK_NET_DIR + name0, new HashMap<>()); - url = url0; - } - - public String getUrl() { - return url; + super(name0, ForgeConstants.DECK_NET_DIR + name0, url0); } public String getDeckType() { diff --git a/forge-gui/src/main/java/forge/deck/NetDeckStorageBase.java b/forge-gui/src/main/java/forge/deck/NetDeckStorageBase.java new file mode 100644 index 00000000000..e8be53a2b8d --- /dev/null +++ b/forge-gui/src/main/java/forge/deck/NetDeckStorageBase.java @@ -0,0 +1,159 @@ +package forge.deck; + +import forge.deck.io.DeckSerializer; +import forge.deck.io.DeckStorage; +import forge.game.GameType; +import forge.gui.GuiBase; +import forge.gui.download.GuiDownloadZipService; +import forge.gui.util.SGuiChoose; +import forge.util.FileUtil; +import forge.util.WaitCallback; +import forge.util.storage.StorageBase; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public abstract class NetDeckStorageBase extends StorageBase { + private static final Map> categoryCache = new HashMap<>(); + + private final String url; + private final String deckTypePrefix; + + protected interface Factory { + T create(String name, String url); + } + + protected NetDeckStorageBase(final String name0, final String fullPath0, final String url0) { + this(name0, fullPath0, url0, null); + } + + protected NetDeckStorageBase(final String name0, final String fullPath0, final String url0, final String deckTypePrefix0) { + super(name0, fullPath0, new HashMap<>()); + url = url0; + deckTypePrefix = deckTypePrefix0; + } + + protected static Map loadCategories(final String filename, final Factory factory) { + Map categories = new TreeMap<>(); + if (FileUtil.doesFileExist(filename)) { + List lines = FileUtil.readFile(filename); + for (String line : lines) { + int idx = line.indexOf('|'); + if (idx != -1) { + String name = line.substring(0, idx).trim(); + String url = line.substring(idx + 1).trim(); + categories.put(name, factory.create(name, url)); + } + } + } + return categories; + } + + protected static T selectAndLoad(final Map categories, + final String name, final boolean forceDownload, final String chooserTitle) { + if (categories == null) { + return null; + } + + if (name != null) { + T category = categories.get(name); + if (category != null && (forceDownload || category.map.isEmpty())) { + category.map.clear(); + if (forceDownload) { + return download(category) ? category : null; + } + category.loadCachedDecks(); + } + return category; + } + + List category = new ArrayList<>(categories.values()); + Collections.reverse(category); + + final T c = SGuiChoose.oneOrNone(chooserTitle, category); + if (c == null) { return null; } + + if (c.map.isEmpty()) { //only load/download decks once per session + c.loadCachedDecks(); + if (c.map.isEmpty() && !download(c)) { + return null; + } + } + return c; + } + + protected static T selectAndLoadArchive(final GameType gameType, + final String name, final String listFile, final String formatName, final Factory factory) { + return selectAndLoadArchive(gameType, name, false, listFile, formatName, factory); + } + + @SuppressWarnings("unchecked") + protected static T selectAndLoadArchive(final GameType gameType, + final String name, final boolean forceDownload, final String listFile, final String formatName, + final Factory factory) { + switch (gameType) { + case Constructed: + case Gauntlet: + Map categories = (Map) categoryCache.computeIfAbsent(listFile, + file -> loadCategories(file, factory)); + return selectAndLoad(categories, name, forceDownload, + "Select a Net Deck Archive " + formatName + " category"); + default: + return null; + } + } + + protected final void loadCachedDecks() { + File downloadDir = new File(getFullPath()); + if (downloadDir.exists()) { + for (File file : getAllFilesList(downloadDir, DeckStorage.DCK_FILE_FILTER)) { + Deck deck = DeckSerializer.fromFile(file); + if (deck != null) { + map.put(deck.getName(), deck); + } + } + } + } + + private static boolean download(final NetDeckStorageBase c) { + WaitCallback callback = new WaitCallback() { + @Override + public void run() { + String downloadLoc = c.getFullPath(); + GuiBase.getInterface().download(new GuiDownloadZipService(c.getName(), "decks", c.getUrl(), downloadLoc, downloadLoc, null) { + @Override + protected void copyInputStream(InputStream in, String outPath) throws IOException { + super.copyInputStream(in, outPath); + + Deck deck = DeckSerializer.fromFile(new File(outPath)); + if (deck != null) { + c.map.put(deck.getName(), deck); + } + } + }, this); + } + }; + final Boolean downloaded = callback.invokeAndWait(); //wait for download to finish + return Boolean.TRUE.equals(downloaded) && !c.map.isEmpty(); + } + + public String getUrl() { + return url; + } + + public String getDeckType() { + return deckTypePrefix == null ? name : deckTypePrefix + " - " + name; + } + + @Override + public String toString() { + return name; + } +}