magarena/src/magic/CommandLineArgs.java

231 lines
6.3 KiB
Java
Raw Normal View History

package magic;
2019-12-06 18:36:02 -08:00
import java.util.HashMap;
import java.util.Locale;
2019-12-06 18:36:02 -08:00
import java.util.Map;
import java.util.function.Function;
2020-01-15 12:02:42 -08:00
import magic.ai.MagicAI;
import magic.ai.MagicAIImpl;
import magic.data.DuelConfig;
import magic.utility.MagicSystem;
class CommandLineArgs {
2019-12-06 18:36:02 -08:00
private static Map<String, Arg<?>> argMap = new HashMap<>();
private static Map<String, Flag> flagMap = new HashMap<>();
2019-12-06 18:36:02 -08:00
// set player 1 deck (see wiki page for value syntax)
private Arg<String> deck1 = new Arg<>("--deck1", "", x -> {
MagicSystem.setAiVersusAi(true);
return x;
});
2019-12-06 18:36:02 -08:00
// set player 2 deck (see wiki page for value syntax)
private Arg<String> deck2 = new Arg<>("--deck2", "", x -> {
MagicSystem.setAiVersusAi(true);
return x;
});
2019-12-06 18:36:02 -08:00
// HeadlessAiGame-only settings
private Flag headless = new Flag("--headless");
private Flag cardTest = new Flag("--cardtest");
2019-12-06 18:36:02 -08:00
// displays refresh rate in hertz for each screen device.
private Flag showFps = new Flag("--showfps");
2019-12-06 18:36:02 -08:00
// turns off gameplay animations for session (does not change preferences).
private Flag isAnimationsDisabled = new Flag("--nofx");
2019-12-06 18:36:02 -08:00
// 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<Integer> fps = new Arg<>("--fps", 0, x -> Integer.parseInt(x));
2019-12-06 18:36:02 -08:00
// limits the number of threads AI uses. (eg. --threads 2)
private Arg<Integer> maxAiThreads = new Arg<>("--threads", MagicAI.getMaxThreads(), x -> Integer.parseInt(x));
// Runs specified test game, equivalent to -DtestGame VM arg. (eg. --test TestAura)
private Arg<String> 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<MagicAIImpl> 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<MagicAIImpl> ai2 = new Arg<>("--ai2", MagicAIImpl.MMABFast, x -> {
MagicSystem.setAiVersusAi(true);
return MagicAIImpl.valueOf(x);
});
// level of AI player #1. (eg. --str1 8)
private Arg<Integer> ai1Level = new Arg<>("--str1", 6, x -> {
MagicSystem.setAiVersusAi(true);
2019-12-06 18:36:02 -08:00
return Integer.parseInt(x);
});
// level of AI player #2. (eg. --str2 8)
private Arg<Integer> 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<Integer> 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<Integer> 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<Integer> duels = new Arg<>("--duels", 1, x -> Integer.parseInt(x));
// Skiplist for card test
private Arg<String> skipList = new Arg<>("--skip", null, x -> x);
2019-12-06 18:36:02 -08:00
/**
* Commandline argument requiring a value
*
* @param <V> type of internally stored value
*/
private static class Arg<V> {
private String argName;
public V value;
private Function<String, V> setter;
public Arg(String argName, V defaultValue, Function<String, V> setter) {
this.argName = argName;
this.value = defaultValue;
this.setter = setter;
argMap.put(argName, this);
}
public void set(String arg) {
value = setter.apply(arg);
}
}
2019-12-06 18:36:02 -08:00
/**
* 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);
}
}
2019-12-06 18:36:02 -08:00
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;
}
}
}
2019-12-06 18:36:02 -08:00
private String nextArg(String[] args, int i) {
if (i + 1 >= args.length) {
return null;
}
return args[i + 1].trim();
}
2019-12-06 18:36:02 -08:00
MagicAIImpl getAi1() {
2019-12-06 18:36:02 -08:00
return ai1.value;
}
MagicAIImpl getAi2() {
2019-12-06 18:36:02 -08:00
return ai2.value;
}
int getAi1Level() {
2019-12-06 18:36:02 -08:00
return ai1Level.value;
}
int getAi2Level() {
2019-12-06 18:36:02 -08:00
return ai2Level.value;
}
boolean isAnimationsEnabled() {
2019-12-06 18:36:02 -08:00
return !isAnimationsDisabled.value;
}
int getMaxThreads() {
2019-12-06 18:36:02 -08:00
return maxAiThreads.value;
}
boolean isDevMode() {
2019-12-06 18:36:02 -08:00
return isDevMode.value;
}
int getGames() {
2019-12-06 18:36:02 -08:00
return games.value;
}
int getLife() {
2019-12-06 18:36:02 -08:00
return startLife.value;
}
boolean showHelp() {
2019-12-06 18:36:02 -08:00
return showHelp.value;
}
String getDeck1() {
2019-12-06 18:36:02 -08:00
return deck1.value;
}
String getDeck2() {
2019-12-06 18:36:02 -08:00
return deck2.value;
}
boolean showFPS() {
2019-12-06 18:36:02 -08:00
return showFps.value;
}
boolean isHeadless() {
2019-12-06 18:36:02 -08:00
return headless.value;
}
boolean isCardTest() {
2019-12-06 18:36:02 -08:00
return cardTest.value;
}
int getFPS() {
2019-12-06 18:36:02 -08:00
return fps.value;
}
2017-06-11 14:44:12 -07:00
int getDuels() {
2019-12-06 18:36:02 -08:00
return duels.value;
2017-06-11 14:44:12 -07:00
}
String getSkipList() {
return skipList.value;
}
}