2014-09-03 10:15:41 -07:00
|
|
|
package magic;
|
|
|
|
|
|
|
|
import magic.ai.MagicAI;
|
|
|
|
import magic.ai.MagicAIImpl;
|
2014-10-10 13:09:11 -07:00
|
|
|
import magic.data.CardDefinitions;
|
2014-09-03 10:15:41 -07:00
|
|
|
import magic.data.DeckUtils;
|
|
|
|
import magic.data.DuelConfig;
|
2014-09-05 18:32:43 -07:00
|
|
|
import magic.firemind.Duel;
|
|
|
|
import magic.firemind.FiremindClient;
|
2015-02-23 07:57:32 -08:00
|
|
|
import magic.firemind.FiremindGameReport;
|
2014-10-12 09:54:50 -07:00
|
|
|
import magic.model.MagicDeckProfile;
|
2014-09-03 10:15:41 -07:00
|
|
|
import magic.model.MagicDuel;
|
|
|
|
import magic.model.MagicGame;
|
|
|
|
import magic.model.MagicGameLog;
|
2014-10-12 09:54:50 -07:00
|
|
|
import magic.model.MagicPlayerDefinition;
|
2014-09-03 10:15:41 -07:00
|
|
|
import magic.model.MagicRandom;
|
2015-02-17 19:17:53 -08:00
|
|
|
import magic.headless.HeadlessGameController;
|
2014-09-03 10:15:41 -07:00
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
2014-09-05 18:32:43 -07:00
|
|
|
import java.io.File;
|
2014-09-03 10:15:41 -07:00
|
|
|
import java.io.FileWriter;
|
2014-09-05 18:32:43 -07:00
|
|
|
import java.io.IOException;
|
2014-09-23 01:33:02 -07:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2014-09-03 10:15:41 -07:00
|
|
|
import java.util.Date;
|
2014-10-12 09:54:50 -07:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2015-03-03 13:56:04 -08:00
|
|
|
|
2015-02-15 04:20:32 -08:00
|
|
|
import magic.data.GeneralConfig;
|
2015-02-22 09:18:36 -08:00
|
|
|
import magic.exception.InvalidDeckException;
|
2014-09-03 10:15:41 -07:00
|
|
|
|
|
|
|
public class FiremindQueueWorker {
|
|
|
|
|
2014-09-24 04:33:02 -07:00
|
|
|
private static int games;
|
|
|
|
private static int str1 = 2;
|
|
|
|
private static int str2 = 2;
|
|
|
|
private static int life = 20;
|
|
|
|
private static int seed;
|
|
|
|
private static String deck1 = "";
|
|
|
|
private static String deck2 = "";
|
|
|
|
private static MagicAIImpl ai1 = MagicAIImpl.MCTS;
|
|
|
|
private static MagicAIImpl ai2 = MagicAIImpl.MCTS;
|
|
|
|
private static Duel currentDuel;
|
2015-03-04 11:41:19 -08:00
|
|
|
public static boolean shutDownOnEmptyQueue = false;
|
2014-09-24 04:33:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
public static void main(final String[] args) {
|
2015-03-04 11:41:19 -08:00
|
|
|
parseArguments(args);
|
2014-09-24 04:33:02 -07:00
|
|
|
|
|
|
|
FiremindClient.setHostByEnvironment();
|
2015-03-04 11:41:19 -08:00
|
|
|
boolean run = true;
|
|
|
|
while (run) {
|
2014-09-24 04:33:02 -07:00
|
|
|
Duel duel = FiremindClient.popDeckJob();
|
2015-03-04 11:41:19 -08:00
|
|
|
if (duel == null) {
|
2015-03-04 11:41:19 -08:00
|
|
|
if(shutDownOnEmptyQueue){
|
|
|
|
run = false;
|
|
|
|
}else{
|
2015-03-04 11:41:19 -08:00
|
|
|
try {
|
|
|
|
Thread.sleep(5000);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
System.out.println("Woken");
|
|
|
|
}
|
2015-03-04 11:41:19 -08:00
|
|
|
}
|
2015-03-04 11:41:19 -08:00
|
|
|
}else{
|
2014-09-24 04:33:02 -07:00
|
|
|
try {
|
|
|
|
final FiremindGameReport reporter = new FiremindGameReport(
|
|
|
|
duel.id);
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(reporter);
|
|
|
|
System.out.println(duel.games_to_play + " Games to run");
|
|
|
|
File theDir = new File("duels/" + duel.id);
|
|
|
|
theDir.mkdir();
|
|
|
|
|
|
|
|
deck1 = saveDeckFile("duels/" + duel.id + "/" + "deck1",
|
|
|
|
duel.deck1_text);
|
|
|
|
deck2 = saveDeckFile("duels/" + duel.id + "/" + "deck2",
|
|
|
|
duel.deck2_text);
|
2014-10-12 09:54:50 -07:00
|
|
|
loadCardsInDeck(duel.deck1_text);
|
|
|
|
loadCardsInDeck(duel.deck2_text);
|
|
|
|
|
2014-09-24 04:33:02 -07:00
|
|
|
currentDuel = duel;
|
|
|
|
games = duel.games_to_play;
|
2015-03-04 11:41:19 -08:00
|
|
|
seed = duel.seed;
|
|
|
|
life = duel.life;
|
|
|
|
str1 = duel.strAi1;
|
|
|
|
str2 = duel.strAi2;
|
2015-03-04 11:41:19 -08:00
|
|
|
try {
|
2015-03-04 11:41:19 -08:00
|
|
|
ai1 = MagicAIImpl.valueOf(duel.ai1);
|
|
|
|
} catch (final IllegalArgumentException ex) {
|
|
|
|
System.err.println("Error: " + duel.ai1 + " is not valid AI");
|
2015-03-03 13:56:04 -08:00
|
|
|
}
|
2015-03-04 11:41:19 -08:00
|
|
|
try {
|
2015-03-04 11:41:19 -08:00
|
|
|
ai2 = MagicAIImpl.valueOf(duel.ai2);
|
|
|
|
} catch (final IllegalArgumentException ex) {
|
|
|
|
System.err.println("Error: " + duel.ai2 + " is not valid AI");
|
2015-03-03 13:56:04 -08:00
|
|
|
}
|
2014-09-24 04:33:02 -07:00
|
|
|
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
|
|
|
|
runDuel();
|
|
|
|
FiremindClient.postSuccess(duel.id);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
FiremindClient.postFailure(duel.id, sw.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
FiremindClient.resetChangedScripts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-10-12 09:54:50 -07:00
|
|
|
|
2015-03-04 11:41:19 -08:00
|
|
|
private static boolean parseArguments(final String[] args) {
|
|
|
|
boolean validArgs = true;
|
|
|
|
for (int i = 0; i < args.length; i += 1) {
|
|
|
|
final String curr = args[i];
|
|
|
|
if ("--self-terminate".equals(curr)) {
|
2015-03-04 11:41:19 -08:00
|
|
|
shutDownOnEmptyQueue = true;
|
2015-03-04 11:41:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return validArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MagicDuel setupDuel() throws InvalidDeckException {
|
|
|
|
// Set the random seed
|
|
|
|
if (seed != 0) {
|
|
|
|
MagicRandom.setRNGState(seed);
|
2015-03-04 11:41:19 -08:00
|
|
|
seed = MagicRandom.nextRNGInt(Integer.MAX_VALUE) + 1;
|
2015-03-04 11:41:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set number of games.
|
|
|
|
final DuelConfig config = new DuelConfig();
|
|
|
|
config.setNrOfGames(games);
|
|
|
|
config.setStartLife(life);
|
|
|
|
|
|
|
|
// Set difficulty.
|
|
|
|
final MagicDuel testDuel = new MagicDuel(config);
|
|
|
|
|
|
|
|
testDuel.initialize();
|
|
|
|
testDuel.setDifficulty(0, str1);
|
|
|
|
testDuel.setDifficulty(1, str2);
|
|
|
|
final MagicDeckProfile profile=new MagicDeckProfile("bgruw");
|
|
|
|
final MagicPlayerDefinition player1=new MagicPlayerDefinition("Player1",true, profile);
|
|
|
|
final MagicPlayerDefinition player2=new MagicPlayerDefinition("Player2",true, profile);
|
|
|
|
testDuel.setPlayers(new MagicPlayerDefinition[]{player1,player2});
|
|
|
|
|
|
|
|
// Set the AI
|
|
|
|
testDuel.setAIs(new MagicAI[] { ai1.getAI(), ai2.getAI() });
|
|
|
|
testDuel.getPlayer(0).setArtificial(true);
|
|
|
|
testDuel.getPlayer(1).setArtificial(true);
|
|
|
|
|
|
|
|
// Set the deck.
|
|
|
|
if (deck1.length() > 0) {
|
|
|
|
DeckUtils.loadAndSetPlayerDeck(deck1, testDuel.getPlayer(0));
|
|
|
|
}
|
|
|
|
if (deck2.length() > 0) {
|
|
|
|
DeckUtils.loadAndSetPlayerDeck(deck2, testDuel.getPlayer(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return testDuel;
|
|
|
|
}
|
|
|
|
|
2014-10-12 09:54:50 -07:00
|
|
|
public static void loadCardsInDeck(String decklist){
|
|
|
|
Pattern r = Pattern.compile("^(\\d+) (.*)$");
|
|
|
|
for(String line: decklist.split("\\r\\n")){
|
|
|
|
Matcher m = r.matcher(line);
|
|
|
|
if(m.find()){
|
2014-10-12 18:09:25 -07:00
|
|
|
CardDefinitions.loadCardDefinition(m.group(2));
|
|
|
|
}
|
2014-10-12 09:54:50 -07:00
|
|
|
}
|
|
|
|
}
|
2014-09-24 04:33:02 -07:00
|
|
|
|
|
|
|
private static String saveDeckFile(String name, String content) {
|
|
|
|
try {
|
|
|
|
File deckFile = new File(name + ".dec");
|
|
|
|
deckFile.getParentFile().mkdirs();
|
|
|
|
deckFile.createNewFile();
|
|
|
|
FileWriter fw = new FileWriter(deckFile.getAbsoluteFile());
|
|
|
|
BufferedWriter bw = new BufferedWriter(fw);
|
|
|
|
bw.write(content);
|
|
|
|
bw.close();
|
|
|
|
return deckFile.getPath();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 09:18:36 -08:00
|
|
|
private static void runDuel() throws InvalidDeckException {
|
2014-09-24 04:33:02 -07:00
|
|
|
int played = 0;
|
|
|
|
int wins = 0;
|
|
|
|
MagicGameLog.initialize();
|
|
|
|
final MagicDuel testDuel = setupDuel();
|
|
|
|
|
|
|
|
Date baseDate = new Date();
|
|
|
|
baseDate.setTime(0);
|
|
|
|
long started = System.currentTimeMillis();
|
|
|
|
while (testDuel.getGamesPlayed() < testDuel.getGamesTotal()) {
|
2014-11-29 04:58:07 -08:00
|
|
|
final MagicGame game = testDuel.nextGame();
|
2014-09-24 04:33:02 -07:00
|
|
|
game.setArtificial(true);
|
2015-02-17 20:54:33 -08:00
|
|
|
|
2014-09-24 04:33:02 -07:00
|
|
|
// maximum duration of a game is 60 minutes
|
2015-02-17 20:54:33 -08:00
|
|
|
final HeadlessGameController controller = new HeadlessGameController(game, 3600000);
|
2014-09-24 04:33:02 -07:00
|
|
|
|
|
|
|
controller.runGame();
|
|
|
|
if (testDuel.getGamesPlayed() > played) {
|
|
|
|
played = testDuel.getGamesPlayed();
|
|
|
|
long diff = System.currentTimeMillis() - started;
|
2015-02-15 04:20:32 -08:00
|
|
|
String[] vers = GeneralConfig.VERSION.split("\\.");
|
2014-09-24 04:33:02 -07:00
|
|
|
String log = MagicGameLog.getLogFileName();
|
|
|
|
FiremindClient.postGame(currentDuel.id, played, new Date(
|
|
|
|
baseDate.getTime() + diff),
|
|
|
|
testDuel.getGamesWon() > wins, Integer
|
|
|
|
.parseInt(vers[0]), Integer.parseInt(vers[1]),
|
|
|
|
log);
|
|
|
|
|
|
|
|
wins = testDuel.getGamesWon();
|
|
|
|
started = System.currentTimeMillis();
|
|
|
|
MagicGameLog.initialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("Duel finished " + played + " of "
|
|
|
|
+ testDuel.getGamesTotal() + " run");
|
|
|
|
|
|
|
|
}
|
2014-09-03 10:15:41 -07:00
|
|
|
|
|
|
|
}
|