Improve Firemind worker

No longer shut down after fixed number of duels
Handle temp files for card script submission in a more resource
efficient way
Allow seed and AIs to be set dynamically
master
Mike 2015-03-03 22:56:04 +01:00
parent 6c48ce02c1
commit 9336ea5f8c
4 changed files with 33 additions and 13 deletions

View File

@ -25,6 +25,7 @@ import java.io.StringWriter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import magic.data.GeneralConfig;
import magic.exception.InvalidDeckException;
@ -40,7 +41,6 @@ public class FiremindQueueWorker {
private static MagicAIImpl ai1 = MagicAIImpl.MCTS;
private static MagicAIImpl ai2 = MagicAIImpl.MCTS;
private static Duel currentDuel;
private static int gameCount = 0;
private static MagicDuel setupDuel() throws InvalidDeckException {
// Set the random seed
@ -104,15 +104,26 @@ public class FiremindQueueWorker {
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);
if (gameCount > 25) {
System.out
.println("Exceeded max number of games. Shutting down.");
return;
}
} catch (Exception e) {
StringWriter sw = new StringWriter();
@ -178,7 +189,6 @@ public class FiremindQueueWorker {
controller.runGame();
if (testDuel.getGamesPlayed() > played) {
gameCount++;
played = testDuel.getGamesPlayed();
long diff = System.currentTimeMillis() - started;
String[] vers = GeneralConfig.VERSION.split("\\.");

View File

@ -3,8 +3,10 @@ public class Duel {
public Integer games_to_play;
public Integer id;
public Integer seed;
public String deck1_text;
public String deck2_text;
public String ai1;
public String ai2;
}

View File

@ -49,6 +49,9 @@ public class FiremindClient {
d.games_to_play = obj.getInt("games_to_play");
d.deck1_text = obj.getString("deck1_text");
d.deck2_text = obj.getString("deck2_text");
d.seed = obj.getInt("seed");
d.ai1 = obj.getString("ai1");
d.ai2 = obj.getString("ai2");
JSONArray scripts = obj.getJSONArray("card_scripts");
addedScripts = new ArrayList<String>();
@ -83,11 +86,13 @@ public class FiremindClient {
}
private static void saveScriptFile(String name, String extension, String content){
File scriptsDirectory = MagicFileSystem.getDataPath(DataPath.SCRIPTS).toFile();
MagicFileSystem.getDataPath(DataPath.SCRIPT_ORIGS).toFile().mkdirs();
File scriptsDirectory = MagicFileSystem.getDataPath(DataPath.SCRIPT_ORIGS).toFile();
File scriptOrigsDirectory = MagicFileSystem.getDataPath(DataPath.SCRIPT_ORIGS).toFile();
String filename = CardDefinitions.getCanonicalName(name)+"."+extension;
File f = new File(scriptsDirectory.getAbsolutePath()+"/"+filename);
if (f.exists()){
f.renameTo(new File(scriptsDirectory.getAbsolutePath()+"/"+filename+".orig"));
f.renameTo(new File(scriptOrigsDirectory.getAbsolutePath()+"/"+filename+".orig"));
}else{
addedScripts.add(f.getAbsolutePath());
}
@ -109,10 +114,12 @@ public class FiremindClient {
}
public static void resetChangedScripts(){
File scriptsDirectory = MagicFileSystem.getDataPath(DataPath.SCRIPT_ORIGS).toFile();
MagicFileSystem.getDataPath(DataPath.SCRIPT_ORIGS).toFile().mkdirs();
String[] ext = new String[]{"orig"};
List<File> files = (List<File>) FileUtils.listFiles(MagicFileSystem.getDataPath(DataPath.SCRIPTS).toFile(), ext, true);
List<File> files = (List<File>) FileUtils.listFiles(MagicFileSystem.getDataPath(DataPath.SCRIPT_ORIGS).toFile(), ext, false);
for(File f: files){
f.renameTo(new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().lastIndexOf("."))));
f.renameTo(new File(scriptsDirectory.getAbsolutePath()+"/"+f.getName().substring(0, f.getName().lastIndexOf("."))));
}
for(String path: addedScripts){
(new File(path)).delete();

View File

@ -66,6 +66,7 @@ public final class MagicFileSystem {
MODS("mods"),
SCRIPTS("scripts"),
SCRIPTS_MISSING("scripts_missing"),
SCRIPT_ORIGS("origs"),
SOUNDS("sounds"),
LOGS("logs"),
DUELS("duels"),