this.goalValues = new ArrayList<Integer>();
}
public Match(String theJSON, Game theGame, String authToken) throws JSONException, SymbolFormatException, GdlFormatException {
JSONObject theMatchObject = new JSONObject(theJSON);
this.matchId = theMatchObject.getString("matchId");
this.startClock = theMatchObject.getInt("startClock");
this.playClock = theMatchObject.getInt("playClock");
if (theGame == null) {
this.theGame = RemoteGameRepository.loadSingleGame(theMatchObject.getString("gameMetaURL"));
if (this.theGame == null) {
throw new RuntimeException("Could not find metadata for game referenced in Match object: " + theMatchObject.getString("gameMetaURL"));
}
} else {
this.theGame = theGame;
}
if (theMatchObject.has("previewClock")) {
this.previewClock = theMatchObject.getInt("previewClock");
} else {
this.previewClock = -1;
}
this.startTime = new Date(theMatchObject.getLong("startTime"));
this.randomToken = theMatchObject.getString("randomToken");
this.spectatorAuthToken = authToken;
this.isCompleted = theMatchObject.getBoolean("isCompleted");
if (theMatchObject.has("isAborted")) {
this.isAborted = theMatchObject.getBoolean("isAborted");
} else {
this.isAborted = false;
}
this.numRoles = Role.computeRoles(this.theGame.getRules()).size();
this.moveHistory = new ArrayList<List<GdlTerm>>();
this.stateHistory = new ArrayList<Set<GdlSentence>>();
this.stateTimeHistory = new ArrayList<Date>();
this.errorHistory = new ArrayList<List<String>>();
JSONArray theMoves = theMatchObject.getJSONArray("moves");
for (int i = 0; i < theMoves.length(); i++) {
List<GdlTerm> theMove = new ArrayList<GdlTerm>();
JSONArray moveElements = theMoves.getJSONArray(i);
for (int j = 0; j < moveElements.length(); j++) {
theMove.add(GdlFactory.createTerm(moveElements.getString(j)));
}
moveHistory.add(theMove);
}
JSONArray theStates = theMatchObject.getJSONArray("states");
for (int i = 0; i < theStates.length(); i++) {
Set<GdlSentence> theState = new HashSet<GdlSentence>();
SymbolList stateElements = (SymbolList) SymbolFactory.create(theStates.getString(i));
for (int j = 0; j < stateElements.size(); j++)
{
theState.add((GdlSentence)GdlFactory.create("( true " + stateElements.get(j).toString() + " )"));
}
stateHistory.add(theState);
}
JSONArray theStateTimes = theMatchObject.getJSONArray("stateTimes");
for (int i = 0; i < theStateTimes.length(); i++) {
this.stateTimeHistory.add(new Date(theStateTimes.getLong(i)));
}
if (theMatchObject.has("errors")) {
JSONArray theErrors = theMatchObject.getJSONArray("errors");
for (int i = 0; i < theErrors.length(); i++) {
List<String> theMoveErrors = new ArrayList<String>();
JSONArray errorElements = theErrors.getJSONArray(i);
for (int j = 0; j < errorElements.length(); j++)
{
theMoveErrors.add(errorElements.getString(j));
}
errorHistory.add(theMoveErrors);
}
}
this.goalValues = new ArrayList<Integer>();
try {
JSONArray theGoalValues = theMatchObject.getJSONArray("goalValues");
for (int i = 0; i < theGoalValues.length(); i++) {
this.goalValues.add(theGoalValues.getInt(i));
}
} catch (JSONException e) {}
// TODO: Add a way to recover cryptographic public keys and signatures.
// Or, perhaps loading a match into memory for editing should strip those?
if (theMatchObject.has("playerNamesFromHost")) {
thePlayerNamesFromHost = new ArrayList<String>();
JSONArray thePlayerNames = theMatchObject.getJSONArray("playerNamesFromHost");
for (int i = 0; i < thePlayerNames.length(); i++) {
thePlayerNamesFromHost.add(thePlayerNames.getString(i));
}
}
if (theMatchObject.has("isPlayerHuman")) {
isPlayerHuman = new ArrayList<Boolean>();
JSONArray isPlayerHumanArray = theMatchObject.getJSONArray("isPlayerHuman");
for (int i = 0; i < isPlayerHumanArray.length(); i++) {
isPlayerHuman.add(isPlayerHumanArray.getBoolean(i));
}
}
}