Long mementoId = getMementoId(parameters);
json.append("\"table\":");
if (tableId != null) {
ScriptableObject table = null;
try {
table = getTable(tableId, mementoId);
// final ScriptableObject excludedKeys =
// ge.execute("[\"actions\"]");
// make sure any mementos we looked up and added
// get sent too
json.append(table.get("json"));
// json.append(stringify(table, excludedKeys));
} catch (GameEngineException gee) {
json.append("{}");
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
} else {
json.append("{}");
}
final ScriptableObject player = getPlayer(userName);
if (player != null) {
json.append(",\"player\":");
json.append(stringify(player));
}
break;
}
case JOIN_TABLE: {
// JOIN_TABLE
final Long tableId = getTableId(parameters);
final String armyName = (String) API.getParameter("army",
parameters);
if (userName == null || userName == "") {
try {
response.sendRedirect("/login.html?return_to=battledisks%252Fapi%253Faction%253DJOIN_TABLE%2526id%253D"
+ tableId + "%2526army%253D" + armyName);
return null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
ScriptableObject table = null;
try {
// make sure the player is allowed to join another table
String tablesStringJson = getOpenAndActiveTables(userName);
NativeArray tables = (NativeArray) ge
.execute(tablesStringJson);
if (tables.size() >= MAX_TABLES) {
// tell the player they are not allowed to join another
// table
json.append("\"messages\":[\"You are already in "
+ MAX_TABLES + " active tables.\"],");
} else {
ScriptableObject player = getPlayer(userName);
final Long mementoId = getMementoId(parameters);
table = getTable(tableId, mementoId);
ScriptableObject joinResult = join(table, player,
armyName);
if (!(Boolean) joinResult.get("success")) {
// TODO give joinResult to the user
}
}
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
//
json.append("\"id\":");
json.append(tableId);
}
break;
case CREATE_TABLE: {
if (userName != null && !userName.equals("")) {
ScriptableObject player = getPlayer(userName);
final String maxPlayers = (String) getParameter(
"maxPlayers", parameters);
final String armyName = (String) getParameter("armyName",
parameters);
final String activations = (String) getParameter(
"activations", parameters);
final String startingDisks = (String) getParameter(
"startingDisks", parameters);
final String reinforcements = (String) getParameter(
"reinforcements", parameters);
final String alignmentRestriction = (String) getParameter(
"alignmentRestriction", parameters);
final String maxPoints = (String) getParameter("maxPoints",
parameters);
final String scenario = (String) getParameter("scenario",
parameters);
ScriptableObject table = null;
try {
// make sure the player is allowed to create a new table
String tablesStringJson = getOpenAndActiveTables(userName);
NativeArray tables = (NativeArray) ge
.execute(tablesStringJson);
if (tables.size() >= 4) {
// tell the player they are not allowed to create
// more
// tables
json.append("\"messages\":[\"You are already in 4 active tables.\"]");
} else {
table = createTable(maxPlayers, maxPoints,
activations, startingDisks, reinforcements,
alignmentRestriction, scenario);
ScriptableObject joinResult = join(table, player,
armyName);
Object success = joinResult.get("success");
if ((Boolean) success) {
final Long tableId = getId(table);
json.append("\"id\":");
json.append(tableId);
} else {
json.append(",\"messages\":[");
// TODO give joinResult to the user
NativeArray messages = (NativeArray) joinResult
.get("messages");
for (Object message : messages) {
json.append("\"" + message + "\",");
}
json.append("]");
}
}
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
}
}
break;
case GET_ALL_TABLES: {
json.append("\"openTables\":");
// only get open tables
String fd = getOpenTables();
json.append(fd);
json.append(",\"activeTables\":");
// only get open tables
fd = getActiveTables();
json.append(fd);
final ScriptableObject player = getPlayer(userName);
if (player != null) {
json.append(",\"player\":");
json.append(stringify(player));
}
}
break;
case CREATE_DISK: {
final String name = (String) getParameter("name", parameters);
if (name == null || name.equals("")) {
break;
}
final String type = (String) getParameter("type", parameters);
final Integer attack = Integer.parseInt((String) getParameter(
"attack", parameters));
final Integer defense = Integer.parseInt((String) getParameter(
"defense", parameters));
final Integer toughness = Integer
.parseInt((String) getParameter("toughness", parameters));
final Integer movement = Integer
.parseInt((String) getParameter("movement", parameters));
final Integer wounds = Integer.parseInt((String) getParameter(
"wounds", parameters));
final Boolean flying = getParameter("flying", parameters) == null ? false
: Boolean.parseBoolean((String) getParameter("flying",
parameters));
final Boolean swashbuckler = getParameter("swashbuckler",
parameters) == null ? false : Boolean
.parseBoolean((String) getParameter("swashbuckler",
parameters));
final Boolean archer = getParameter("archer", parameters) == null ? false
: Boolean.parseBoolean((String) getParameter("archer",
parameters));
final Integer arrows = getParameter("arrows", parameters) == null ? 0
: Integer.parseInt((String) getParameter("arrows",
parameters));
final Integer bolts = getParameter("bolts", parameters) == null ? 0
: Integer.parseInt((String) getParameter("bolts",
parameters));
final Integer fireballs = getParameter("fireballs", parameters) == null ? 0
: Integer.parseInt((String) getParameter("fireballs",
parameters));
final Integer boulders = getParameter("boulders", parameters) == null ? 0
: Integer.parseInt((String) getParameter("boulders",
parameters));
final Integer spellcaster = Integer
.parseInt((String) getParameter("spellcaster",
parameters));
final Integer limit = Integer.parseInt((String) getParameter(
"limit", parameters));
String parameter = (String) getParameter("missileImmunity",
parameters);
final Boolean missileImmunity = getParameter("missileImmunity",
parameters) == null ? false : Boolean
.parseBoolean(parameter);
final Boolean firstblow = getParameter("firstblow", parameters) == null ? false
: Boolean.parseBoolean((String) getParameter(
"firstblow", parameters));
final Integer cost = Integer.parseInt((String) getParameter(
"cost", parameters));
final String faction = (String) getParameter("faction",
parameters);
final String alignment = (String) getParameter("alignment",
parameters);
final String diameter = (String) getParameter("diameter",
parameters);
final String description = (String) getParameter("description",
parameters);
final String price = (String) getParameter("price", parameters);
// name, attack, defense, toughness, movement, wounds, flying,
// swashbuckler, archer, arrows, bolts, fireballs, boulders,
// missileImmunity, firstblow, spellcaster, cost, faction,
// alignment, diameter, description, price
try {
createDisk(name, type, attack, defense, toughness,
movement, wounds, flying, swashbuckler, archer,
arrows, bolts, fireballs, boulders,
missileImmunity, firstblow, spellcaster, limit,
cost, faction, alignment, diameter, description,
price);
// redirect to a better place
response.sendRedirect("/battledisks/disk.jsp#!" + name);
} catch (GameEngineException gee) {
// GameEngineException CREATE_DISK
throw new APIException(gee);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case LOG_IN: {
try {
final ScriptableObject player = getPlayer(userName);
if (player == null) {
createPlayer(userName);
}
response.sendRedirect("/battledisks/profile.html");
return null;
} catch (GameEngineException gee) {
throw new APIException(gee);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case PROFILE: {
try {
final ScriptableObject player = getPlayer(userName);
if (player != null) {
json.append("\"player\":");
String f = getOpenAndActiveTables(userName);
NativeArray na = (NativeArray) ge.execute(f);
player.put("activeTables", player, na);
json.append(stringify(player));
json.append(",");
}
json.replace(json.length() - 1, json.length(), "");
} catch (GameEngineException gee) {
// GameEngineException GET_MY_DISKS
throw new APIException(gee);
}
break;
}
case SAVE_ARMY: {
// SAVE_ARMY
final ScriptableObject player = getPlayer(userName);
if (player != null) {
final String armyName = (String) getParameter("armyName",
parameters);
final String disks = (String) getParameter("disks",
parameters);
ge.invoke(player, "saveArmy",
new Object[] { armyName, ge.execute(disks) });
savePlayer(player);
json.append("\"player\":");
json.append(player.get("json"));
json.append(",");
}
json.replace(json.length() - 1, json.length(), "");
break;
}
case DELETE_ARMY: {
final ScriptableObject player = getPlayer(userName);
if (player != null) {
final String armyName = (String) getParameter("armyName",
parameters);
ge.invoke(player, "deleteArmy", new Object[] { armyName });
savePlayer(player);
json.append("\"player\":");
json.append(player.get("json"));
json.append(",");
}
json.replace(json.length() - 1, json.length(), "");
break;
}
case SAVE_REINFORCEMENT: {
ScriptableObject table = null;
try {
Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
final String pointString = (String) getParameter("point",
parameters);
final String diskNumber = (String) getParameter(
"diskNumber", parameters);
saveReinforcement(table, userName, diskNumber, pointString);
json.append("\"table\":");
// final ScriptableObject excludedKeys = ge
// .execute("[\"actions\"]");
json.append(table.get("json"));
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
}
break;
case END_REINFORCEMENTS: {
ScriptableObject table = null;
try {
final Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
endReinforcements(table, userName);
json.append("\"table\":");
json.append(table.get("json"));
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
break;
}
case END_MISSILES: {
ScriptableObject table = null;
try {
final Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
endMissiles(table, userName);
json.append("\"table\":");
json.append(table.get("json"));
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
break;
}
case FIRE_MISSILES: {
// FIRE_MISSILES
ScriptableObject table = null;
try {
final Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
final String diskNumber = (String) getParameter(
"diskNumber", parameters);
final String point = (String) getParameter("point",
parameters);
final String missileName = (String) getParameter("missile",
parameters);
fireMissiles(table, userName, diskNumber, point,
missileName);
json.append("\"table\":");
json.append(table.get("json"));
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
break;
}
case MOVE_DISK: {
final String diskNumber = (String) getParameter("diskNumber",
parameters);
final Long mementoId = getMementoId(parameters);
final ScriptableObject table = getTable(getTableId(parameters),
mementoId);
// "diskNumber" : movedDiskNumber,
// "point" : tableClickPoint
final String point = (String) getParameter("point", parameters);
move(table, userName, diskNumber, point);
json.append("\"table\":");
// final ScriptableObject excludedKeys = ge
// .execute("[\"actions\"]");
// json.append(stringify(table, excludedKeys));
json.append(table.get("json"));
}
break;
case JWT: {
try {
final ScriptableObject player = getPlayer(userName);
if (player == null) {
json.append("\"jwt\":\"" + "\"");
break;
}
final String s = saveCart(
(String) getParameter("disks", parameters), player);
json.append("\"jwt\":\"" + s + "\"");
} catch (GameEngineException gee) {
// GameEngineException JWT
throw new APIException(gee);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (SignatureException e) {
e.printStackTrace();
}
}
break;
case CONFIRM: {
final String jwt = (String) getParameter("jwt", parameters);
try {
return confirm(jwt, userName);
} catch (GameEngineException gee) {
// GameEngineException CONFIRM
throw new APIException(gee);
}
}
case GET_ALL_DISKS: {
json.append("\"disks\":{");
final HashMap<Object, HashMap<String, Object>> disks = ge.persistence
.getAll("Disk");
for (HashMap<String, Object> disk : disks.values()) {
final String diskJson = (String) disk.get("json");
final Object diskName = disk.get("name");
json.append("\"" + diskName + "\":" + diskJson + ",");
}
if (disks.size() > 0) {
// json = json.substring(0, json.length() - 1);
json.replace(json.length() - 1, json.length(), "");
}
json.append("},");
json.append("\"player\":");
try {
final ScriptableObject player = getPlayer(userName);
json.append(stringify(player));
} catch (GameEngineException gee) {
json.append("{}");
// GameEngineException GET_ALL_DISKS
throw new APIException(gee);
}
}
break;
case GET_DISK: {
final String diskName = (String) getParameter("diskName",
parameters);
json.append("\"disk\":");
final HashMap<String, Object> disk = ge.persistence.get("Disk",
diskName);
if (disk != null && disk.get("json") != null) {
final String text = (String) disk.get("json");
json.append(text);
} else {
json.append("{}");
}
}
break;
case ACTIVATE_DISK: {
ScriptableObject table = null;
try {
final Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
// "diskNumber" : movedDiskNumber,
final String diskNumber = (String) getParameter(
"diskNumber", parameters);
activateDisk(table, diskNumber, userName);
json.append("\"table\":");
json.append(table.get("json"));
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
}
break;
case END_ACTIVATIONS: {
ScriptableObject table = null;
try {
final Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
endActivations(table, userName);
json.append("\"table\":");
json.append(table.get("json"));
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
}
break;
case UPLOAD_DISKS: {
uploadDisks((BlobKey) parameters.get("csv"));
try {
response.sendRedirect("/battledisks/disk.jsp");
return "";
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
case DOWNLOAD_DISKS: {
downloadDisks(response);
return "";
}
case UNIT_TEST:
// get the unit test code
{
ScriptableObject table = null;
try {
final Long mementoId = getMementoId(parameters);
table = getTable(getTableId(parameters), mementoId);
return createUnitTest(table);
} catch (GameEngineException gee) {
// GameEngineException
mailUnitTest(createUnitTest(table));
throw gee;
}
}
case SET_ATTACKEE:
//
{
final Long mementoId = getMementoId(parameters);
final ScriptableObject table = getTable(getTableId(parameters),
mementoId);
final String attacker = (String) getParameter("attacker",
parameters);
final String attackee = (String) getParameter("attackee",
parameters);
setAttackee(table, userName, attacker, attackee);
json.append("\"table\":");
json.append(table.get("json"));
}
break;
case SET_DEFENDEE:
//
{
final Long mementoId = getMementoId(parameters);
final ScriptableObject table = getTable(getTableId(parameters),
mementoId);
final String defender = (String) getParameter("defender",
parameters);
final String defendee = (String) getParameter("defendee",
parameters);
setDefendee(table, userName, defender, defendee);
json.append("\"table\":");
json.append(table.get("json"));
}
break;
case LEADERBOARD: {
json.append("\"players\":[");