}
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(",");
}