Skip to content

Commit

Permalink
Add additional fields to powers to expose miscellaneous attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ForgottenArbiter committed Jul 28, 2019
1 parent 40ef133 commit f01de16
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/main/java/communicationmod/GameStateConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,37 @@ private static HashMap<String, Object> convertPlayerToJson(AbstractPlayer player
return jsonPlayer;
}

/**
* Checks whether the given object has the specified field. If so, returns the field's value. Else returns null.
* @param object The object used to look for the specified field
* @param fieldName The field that we want to access
* @return The value of the field, if present, or else null.
*/
private static Object getFieldIfExists(Object object, String fieldName) {
Class objectClass = object.getClass();
for (Field field : objectClass.getDeclaredFields()) {
if (field.getName().equals(fieldName)) {
try {
field.setAccessible(true);
return field.get(object);
} catch(IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
}
return null;
}

/**
* Creates a GSON-compatible representation of the given creature's powers
* The power object contains:
* "id" (string): The id of the power
* "name" (string): The name of the power, in the currently selected language
* "amount" (int): The amount of the power
* "damage" (int, optional): The amount of damage the power does, if applicable
* "misc" (int, optional): Contains misc values that don't fit elsewhere (such as the base value for Flight)
* "card" (object, optional): The card associated with the power (for powers like Nightmare)
* @param creature The creature whose powers are to be converted
* @return A list of power objects
*/
Expand All @@ -719,6 +744,25 @@ private static ArrayList<Object> convertCreaturePowersToJson(AbstractCreature cr
json_power.put("id", power.ID);
json_power.put("name", power.name);
json_power.put("amount", power.amount);
Object damage = getFieldIfExists(power, "damage");
if (damage != null) {
json_power.put("daamge", (int)damage);
}
Object card = getFieldIfExists(power, "card");
if (card != null) {
json_power.put("card", convertCardToJson((AbstractCard)card));
}
String[] miscFieldNames = {
"basePower", "maxAmt", "storedAmount", "hpLoss"
};
Object misc = null;
for (String fieldName : miscFieldNames) {
misc = getFieldIfExists(power, fieldName);
if (misc != null) {
json_power.put("misc", (int)misc);
break;
}
}
powers.add(json_power);
}
return powers;
Expand Down

0 comments on commit f01de16

Please sign in to comment.