add ai strength and life to firemind queue worker

add --self-terminate option to queue worker to shut down once the queue
is empty
master
Mike 2015-03-04 20:41:19 +01:00 committed by melvinzhang
parent 5ae7920236
commit f13a33e446
3 changed files with 85 additions and 64 deletions

View File

@ -41,6 +41,85 @@ public class FiremindQueueWorker {
private static MagicAIImpl ai1 = MagicAIImpl.MCTS;
private static MagicAIImpl ai2 = MagicAIImpl.MCTS;
private static Duel currentDuel;
public static boolean shutDownOnEmptyQueue = false;
public static void main(final String[] args) {
parseArguments(args);
FiremindClient.setHostByEnvironment();
boolean run = true;
while (run) {
Duel duel = FiremindClient.popDeckJob();
if (duel == null) {
if(shutDownOnEmptyQueue){
run = false;
}else{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Woken");
}
}
}else{
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);
loadCardsInDeck(duel.deck1_text);
loadCardsInDeck(duel.deck2_text);
currentDuel = duel;
games = duel.games_to_play;
seed = duel.seed;
life = duel.life;
str1 = duel.strAi1;
str2 = duel.strAi2;
try {
ai1 = MagicAIImpl.valueOf(duel.ai1);
} catch (final IllegalArgumentException ex) {
System.err.println("Error: " + duel.ai1 + " is not valid AI");
}
try {
ai2 = MagicAIImpl.valueOf(duel.ai2);
} catch (final IllegalArgumentException ex) {
System.err.println("Error: " + duel.ai2 + " is not valid AI");
}
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();
}
}
}
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)) {
shutDownOnEmptyQueue = true;
}
}
return validArgs;
}
private static MagicDuel setupDuel() throws InvalidDeckException {
// Set the random seed
@ -80,70 +159,6 @@ public class FiremindQueueWorker {
return testDuel;
}
public static void main(final String[] args) {
FiremindClient.setHostByEnvironment();
while (true) {
Duel duel = FiremindClient.popDeckJob();
if (duel != null) {
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);
loadCardsInDeck(duel.deck1_text);
loadCardsInDeck(duel.deck2_text);
currentDuel = duel;
games = duel.games_to_play;
if(duel.seed != null){
seed = duel.seed;
}
if(duel.ai1 != null){
try {
ai1 = MagicAIImpl.valueOf(duel.ai1);
} catch (final IllegalArgumentException ex) {
System.err.println("Error: " + duel.ai1 + " is not valid AI");
}
}
if(duel.ai2 != null){
try {
ai2 = MagicAIImpl.valueOf(duel.ai2);
} catch (final IllegalArgumentException ex) {
System.err.println("Error: " + duel.ai2 + " is not valid AI");
}
}
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();
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Woken");
}
}
}
}
public static void loadCardsInDeck(String decklist){
Pattern r = Pattern.compile("^(\\d+) (.*)$");

View File

@ -8,5 +8,8 @@ public class Duel {
public String deck2_text;
public String ai1;
public String ai2;
public Integer strAi1;
public Integer strAi2;
public Integer life;
}

View File

@ -50,6 +50,9 @@ public class FiremindClient {
d.deck1_text = obj.getString("deck1_text");
d.deck2_text = obj.getString("deck2_text");
d.seed = obj.getInt("seed");
d.strAi1 = obj.getInt("str_ai1");
d.strAi2 = obj.getInt("str_ai2");
d.life = obj.getInt("life");
d.ai1 = obj.getString("ai1");
d.ai2 = obj.getString("ai2");