package magic; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.function.Function; import magic.ai.MagicAI; import magic.ai.MagicAIImpl; import magic.data.DuelConfig; import magic.utility.MagicSystem; class CommandLineArgs { private static Map> argMap = new HashMap<>(); private static Map flagMap = new HashMap<>(); // set player 1 deck (see wiki page for value syntax) private Arg deck1 = new Arg<>("--deck1", "", x -> { MagicSystem.setAiVersusAi(true); return x; }); // set player 2 deck (see wiki page for value syntax) private Arg deck2 = new Arg<>("--deck2", "", x -> { MagicSystem.setAiVersusAi(true); return x; }); // HeadlessAiGame-only settings private Flag headless = new Flag("--headless"); private Flag cardTest = new Flag("--cardtest"); // displays refresh rate in hertz for each screen device. private Flag showFps = new Flag("--showfps"); // turns off gameplay animations for session (does not change preferences). private Flag isAnimationsDisabled = new Flag("--nofx"); // enables access to additional functionality. private Flag isDevMode = new Flag("--devmode"); // shows wiki help page in browser. private Flag showHelp = new Flag("--help"); // sets the desired frames-per-second for the Trident animation library. private Arg fps = new Arg<>("--fps", 0, x -> Integer.parseInt(x)); // limits the number of threads AI uses. (eg. --threads 2) private Arg maxAiThreads = new Arg<>("--threads", MagicAI.getMaxThreads(), x -> Integer.parseInt(x)); // Runs specified test game, equivalent to -DtestGame VM arg. (eg. --test TestAura) private Arg testGame = new Arg<>("--test", "", x -> { System.setProperty("testGame", x); return x; }); // Setting any of the following arguments will automatically run an AI vs AI game. // MagicAIImpl class to load as player 1. (eg. --ai1 MMABFast) private Arg ai1 = new Arg<>("--ai1", MagicAIImpl.MMABFast, x -> { MagicSystem.setAiVersusAi(true); return MagicAIImpl.valueOf(x); }); // MagicAIImpl class to load as player 2. (eg. --ai2 MMABFast) private Arg ai2 = new Arg<>("--ai2", MagicAIImpl.MMABFast, x -> { MagicSystem.setAiVersusAi(true); return MagicAIImpl.valueOf(x); }); // level of AI player #1. (eg. --str1 8) private Arg ai1Level = new Arg<>("--str1", 6, x -> { MagicSystem.setAiVersusAi(true); return Integer.parseInt(x); }); // level of AI player #2. (eg. --str2 8) private Arg ai2Level = new Arg<>("--str2", 6, x -> { MagicSystem.setAiVersusAi(true); return Integer.parseInt(x); }); // "best of..." games total in AI v AI game. (eg. --games 3 = best of 3 games) private Arg games = new Arg<>("--games", DuelConfig.DEFAULT_GAMES, x -> { MagicSystem.setAiVersusAi(true); return Integer.parseInt(x); }); // initial life for each AI player. (eg. --life 10) private Arg startLife = new Arg<>("--life", DuelConfig.DEFAULT_LIFE, x -> { MagicSystem.setAiVersusAi(true); return Integer.parseInt(x); }); // the number of duels to play [--duels 1]. private Arg duels = new Arg<>("--duels", 1, x -> Integer.parseInt(x)); // Skiplist for card test private Arg skipList = new Arg<>("--skip", null, x -> x); /** * Commandline argument requiring a value * * @param type of internally stored value */ private static class Arg { private String argName; public V value; private Function setter; public Arg(String argName, V defaultValue, Function setter) { this.argName = argName; this.value = defaultValue; this.setter = setter; argMap.put(argName, this); } public void set(String arg) { value = setter.apply(arg); } } /** * Commandline argument that is a flag that enables something. */ private static class Flag { public boolean value = false; public Flag(String argName) { flagMap.put(argName, this); } } CommandLineArgs(final String[] args) { isDevMode.value = MagicSystem.isDevMode(); for (int i = 0; i < args.length; i++) { final String arg = args[i].toLowerCase(Locale.ENGLISH); Arg argValue = argMap.get(arg); if (argValue != null) { String arg0 = nextArg(args, i); if (arg0 == null) { throw new IllegalArgumentException("Argument " + argValue.argName + " requires a value"); } else { argValue.set(arg0); } i++; } Flag flagValue = flagMap.get(arg); if (flagValue != null) { flagValue.value = true; } } } private String nextArg(String[] args, int i) { if (i + 1 >= args.length) { return null; } return args[i + 1].trim(); } MagicAIImpl getAi1() { return ai1.value; } MagicAIImpl getAi2() { return ai2.value; } int getAi1Level() { return ai1Level.value; } int getAi2Level() { return ai2Level.value; } boolean isAnimationsEnabled() { return !isAnimationsDisabled.value; } int getMaxThreads() { return maxAiThreads.value; } boolean isDevMode() { return isDevMode.value; } int getGames() { return games.value; } int getLife() { return startLife.value; } boolean showHelp() { return showHelp.value; } String getDeck1() { return deck1.value; } String getDeck2() { return deck2.value; } boolean showFPS() { return showFps.value; } boolean isHeadless() { return headless.value; } boolean isCardTest() { return cardTest.value; } int getFPS() { return fps.value; } int getDuels() { return duels.value; } String getSkipList() { return skipList.value; } }