Merge commit 'd1e3c3f5f313057e5a81a4333906ef5d79adea83' as 'Android'
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.majinnaibu.monstercards.helpers;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class ArrayHelper {
|
||||
public static int indexOf(@NonNull Object[] array, Object target) {
|
||||
for (int index = 0; index < array.length; index++) {
|
||||
if (Objects.equals(array[index], target)) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.majinnaibu.monstercards.helpers;
|
||||
|
||||
import org.commonmark.node.Document;
|
||||
import org.commonmark.node.Node;
|
||||
import org.commonmark.node.Paragraph;
|
||||
import org.commonmark.parser.Parser;
|
||||
import org.commonmark.renderer.html.HtmlRenderer;
|
||||
|
||||
public final class CommonMarkHelper {
|
||||
public static String toHtml(String rawCommonMark) {
|
||||
Parser parser = Parser.builder().build();
|
||||
Node document = parser.parse(rawCommonMark);
|
||||
Node parent1 = document.getFirstChild();
|
||||
Node parent2 = document.getLastChild();
|
||||
if (parent1 == parent2 && parent1 instanceof Paragraph) {
|
||||
document = new Document();
|
||||
Node child = parent1.getFirstChild();
|
||||
while (child != null) {
|
||||
Node nextChild = child.getNext();
|
||||
document.appendChild(child);
|
||||
child = nextChild;//child.getNext();
|
||||
}
|
||||
}
|
||||
HtmlRenderer renderer = HtmlRenderer.builder().build();
|
||||
return renderer.render(document);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
package com.majinnaibu.monstercards.helpers;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.majinnaibu.monstercards.data.converters.ArmorTypeConverter;
|
||||
import com.majinnaibu.monstercards.data.converters.ChallengeRatingConverter;
|
||||
import com.majinnaibu.monstercards.data.enums.AbilityScore;
|
||||
import com.majinnaibu.monstercards.data.enums.AdvantageType;
|
||||
import com.majinnaibu.monstercards.data.enums.ProficiencyType;
|
||||
import com.majinnaibu.monstercards.models.Language;
|
||||
import com.majinnaibu.monstercards.models.Monster;
|
||||
import com.majinnaibu.monstercards.models.Skill;
|
||||
import com.majinnaibu.monstercards.models.Trait;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
public class MonsterImportHelper {
|
||||
@NonNull
|
||||
public static Monster fromJSON(String json) {
|
||||
JsonObject rootDict = JsonParser.parseString(json).getAsJsonObject();
|
||||
|
||||
Monster monster = new Monster();
|
||||
monster.name = Helpers.getString(rootDict, "name");
|
||||
monster.size = Helpers.getString(rootDict, "size");
|
||||
monster.type = Helpers.getString(rootDict, "type");
|
||||
monster.subtype = Helpers.getString(rootDict, "tag");
|
||||
monster.alignment = Helpers.getString(rootDict, "alignment");
|
||||
monster.hitDice = Helpers.getInt(rootDict, "hitDice");
|
||||
monster.armorType = ArmorTypeConverter.armorTypeFromStringValue(Helpers.getString(rootDict, "armorName"));
|
||||
monster.shieldBonus = Helpers.getInt(rootDict, "shieldBonus");
|
||||
monster.naturalArmorBonus = Helpers.getInt(rootDict, "natArmorBonus");
|
||||
monster.otherArmorDescription = Helpers.getString(rootDict, "otherArmorDesc");
|
||||
monster.walkSpeed = Helpers.getInt(rootDict, "speed");
|
||||
monster.burrowSpeed = Helpers.getInt(rootDict, "burrowSpeed");
|
||||
monster.climbSpeed = Helpers.getInt(rootDict, "climbSpeed");
|
||||
monster.flySpeed = Helpers.getInt(rootDict, "flySpeed");
|
||||
monster.canHover = Helpers.getBool(rootDict, "hover");
|
||||
monster.swimSpeed = Helpers.getInt(rootDict, "swimSpeed");
|
||||
monster.hasCustomHP = Helpers.getBool(rootDict, "customHP");
|
||||
monster.hasCustomSpeed = Helpers.getBool(rootDict, "customSpeed");
|
||||
monster.customHPDescription = Helpers.getString(rootDict, "hpText");
|
||||
monster.customSpeedDescription = Helpers.getString(rootDict, "speedDesc");
|
||||
monster.strengthScore = Helpers.getInt(rootDict, "strPoints");
|
||||
monster.dexterityScore = Helpers.getInt(rootDict, "dexPoints");
|
||||
monster.constitutionScore = Helpers.getInt(rootDict, "conPoints");
|
||||
monster.intelligenceScore = Helpers.getInt(rootDict, "intPoints");
|
||||
monster.wisdomScore = Helpers.getInt(rootDict, "wisPoints");
|
||||
monster.charismaScore = Helpers.getInt(rootDict, "chaPoints");
|
||||
Helpers.addSense(monster, rootDict, "blindsight");
|
||||
// Helpers.getBool(rootDict, "blind");
|
||||
Helpers.addSense(monster, rootDict, "darkvision");
|
||||
Helpers.addSense(monster, rootDict, "tremorsense");
|
||||
Helpers.addSense(monster, rootDict, "truesight");
|
||||
monster.telepathyRange = Helpers.getInt(rootDict, "telepathy");
|
||||
monster.challengeRating = ChallengeRatingConverter.challengeRatingFromStringValue(Helpers.getString(rootDict, "cr"));
|
||||
monster.customChallengeRatingDescription = Helpers.getString(rootDict, "customCr");
|
||||
monster.customProficiencyBonus = Helpers.getInt(rootDict, "customProf");
|
||||
// Helpers.getBool(rootDict, "isLegendary");
|
||||
// Helpers.getString(rootDict, "legendariesDescription");
|
||||
// Helpers.getBool(rootDict, "isLair");
|
||||
// Helpers.getString(rootDict, "lairDescription");
|
||||
// Helpers.getString(rootDict, "lairDescriptionEnd");
|
||||
// Helpers.getBool(rootDict, "isRegional");
|
||||
// Helpers.getString(rootDict, "regionalDescription");
|
||||
// Helpers.getString(rootDict, "regionalDescriptionEnd");
|
||||
// properties: []
|
||||
monster.abilities = Helpers.getListOfTraits(rootDict, "abilities");
|
||||
monster.actions = Helpers.getListOfTraits(rootDict, "actions");
|
||||
monster.reactions = Helpers.getListOfTraits(rootDict, "reactions");
|
||||
monster.legendaryActions = Helpers.getListOfTraits(rootDict, "legendaries");
|
||||
monster.lairActions = Helpers.getListOfTraits(rootDict, "lairs");
|
||||
monster.regionalActions = Helpers.getListOfTraits(rootDict, "regionals");
|
||||
Helpers.addSavingThrows(monster, rootDict);
|
||||
// skills: []
|
||||
monster.skills = Helpers.getSetOfSkills(rootDict);
|
||||
// damagetypes: []
|
||||
// specialdamage: []
|
||||
monster.damageImmunities = Helpers.getSetOfDamageTypes(rootDict, "damageTypes", "i");
|
||||
monster.damageImmunities.addAll(Helpers.getSetOfDamageTypes(rootDict, "specialdamage", "i"));
|
||||
monster.damageResistances = Helpers.getSetOfDamageTypes(rootDict, "damageTypes", "r");
|
||||
monster.damageResistances.addAll(Helpers.getSetOfDamageTypes(rootDict, "specialdamage", "r"));
|
||||
monster.damageVulnerabilities = Helpers.getSetOfDamageTypes(rootDict, "damageTypes", "v");
|
||||
monster.damageVulnerabilities.addAll(Helpers.getSetOfDamageTypes(rootDict, "specialdamage", "v"));
|
||||
// conditions: []
|
||||
monster.conditionImmunities = Helpers.getSetOfDamageTypes(rootDict, "conditions");
|
||||
// languages: []
|
||||
monster.languages = Helpers.getSetOfLanguages(rootDict, "languages");
|
||||
// understandsBut: ""
|
||||
monster.understandsButDescription = Helpers.getString(rootDict, "understandsBut");
|
||||
// shortName: ""
|
||||
// doubleColumns: true
|
||||
// separationPoint: -1
|
||||
// damage: []
|
||||
// pluralName: ""
|
||||
|
||||
return monster;
|
||||
}
|
||||
|
||||
|
||||
public static class Helpers {
|
||||
public static String getString(JsonObject dict, String name) {
|
||||
return getString(dict, name, "");
|
||||
}
|
||||
|
||||
public static String getString(@NonNull JsonObject dict, String name, String defaultValue) {
|
||||
if (dict.has(name)) {
|
||||
return dict.get(name).getAsString();
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static int getInt(JsonObject dict, String name) {
|
||||
return getInt(dict, name, 0);
|
||||
}
|
||||
|
||||
public static int getInt(@NonNull JsonObject dict, String name, int defaultValue) {
|
||||
if (dict.has(name)) {
|
||||
JsonElement element = dict.get(name);
|
||||
if (element.isJsonPrimitive()) {
|
||||
JsonPrimitive rawValue = element.getAsJsonPrimitive();
|
||||
if (rawValue.isNumber()) {
|
||||
return rawValue.getAsInt();
|
||||
} else {
|
||||
try {
|
||||
return rawValue.getAsInt();
|
||||
} catch (Exception ex) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static boolean getBool(JsonObject dict, String name) {
|
||||
return getBool(dict, name, false);
|
||||
}
|
||||
|
||||
public static boolean getBool(@NonNull JsonObject dict, String name, boolean defaultValue) {
|
||||
if (dict.has(name)) {
|
||||
JsonElement element = dict.get(name);
|
||||
if (element.isJsonPrimitive()) {
|
||||
JsonPrimitive rawValue = element.getAsJsonPrimitive();
|
||||
if (rawValue.isBoolean()) {
|
||||
return rawValue.getAsBoolean();
|
||||
} else {
|
||||
try {
|
||||
return rawValue.getAsBoolean();
|
||||
} catch (Exception ex) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String formatDistance(String name, int distance) {
|
||||
// TODO: consider moving this to a string resource so it can be localized
|
||||
return String.format(Locale.getDefault(), "%s %d ft.", name, distance);
|
||||
}
|
||||
|
||||
public static void addSense(Monster monster, JsonObject root, String name) {
|
||||
int distance = Helpers.getInt(root, name);
|
||||
if (distance > 0) {
|
||||
monster.senses.add(Helpers.formatDistance(name, distance));
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static List<Trait> getListOfTraits(@NonNull JsonObject dict, String name) {
|
||||
ArrayList<Trait> traits = new ArrayList<>();
|
||||
if (dict.has(name)) {
|
||||
JsonElement arrayElement = dict.get(name);
|
||||
if (arrayElement.isJsonArray()) {
|
||||
JsonArray array = arrayElement.getAsJsonArray();
|
||||
int size = array.size();
|
||||
for (int index = 0; index < size; index++) {
|
||||
JsonElement jsonElement = array.get(index);
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
String traitName = Helpers.getString(jsonObject, "name");
|
||||
String description = Helpers.getString(jsonObject, "desc");
|
||||
Trait trait = new Trait(traitName, description);
|
||||
traits.add(trait);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return traits;
|
||||
}
|
||||
|
||||
public static void addSavingThrows(Monster monster, @NonNull JsonObject root) {
|
||||
if (root.has("sthrows")) {
|
||||
JsonElement arrayElement = root.get("sthrows");
|
||||
if (arrayElement.isJsonArray()) {
|
||||
JsonArray array = arrayElement.getAsJsonArray();
|
||||
int size = array.size();
|
||||
for (int index = 0; index < size; index++) {
|
||||
JsonElement jsonElement = array.get(index);
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
String name = Helpers.getString(jsonObject, "name");
|
||||
if ("str".equals(name)) {
|
||||
monster.strengthSavingThrowProficiency = ProficiencyType.PROFICIENT;
|
||||
} else if ("dex".equals(name)) {
|
||||
monster.dexteritySavingThrowProficiency = ProficiencyType.PROFICIENT;
|
||||
} else if ("con".equals(name)) {
|
||||
monster.constitutionSavingThrowProficiency = ProficiencyType.PROFICIENT;
|
||||
} else if ("int".equals(name)) {
|
||||
monster.intelligenceSavingThrowProficiency = ProficiencyType.PROFICIENT;
|
||||
} else if ("wis".equals(name)) {
|
||||
monster.wisdomSavingThrowProficiency = ProficiencyType.PROFICIENT;
|
||||
} else if ("cha".equals(name)) {
|
||||
monster.charismaSavingThrowProficiency = ProficiencyType.PROFICIENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Set<Skill> getSetOfSkills(@NonNull JsonObject root) {
|
||||
HashSet<Skill> skills = new HashSet<>();
|
||||
if (root.has("skills")) {
|
||||
JsonElement arrayElement = root.get("skills");
|
||||
if (arrayElement.isJsonArray()) {
|
||||
JsonArray array = arrayElement.getAsJsonArray();
|
||||
int size = array.size();
|
||||
for (int index = 0; index < size; index++) {
|
||||
JsonElement jsonElement = array.get(index);
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
String name = Helpers.getString(jsonObject, "name");
|
||||
String stat = Helpers.getString(jsonObject, "stat");
|
||||
String note = Helpers.getString(jsonObject, "note");
|
||||
|
||||
Skill skill = new Skill(name, AbilityScore.valueOfString(stat), AdvantageType.NONE, " (ex)".equals(note) ? ProficiencyType.EXPERTISE : ProficiencyType.PROFICIENT);
|
||||
skills.add(skill);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return skills;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Set<String> getSetOfDamageTypes(JsonObject rootDict, String name) {
|
||||
return getSetOfDamageTypes(rootDict, name, null);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Set<String> getSetOfDamageTypes(@NonNull JsonObject root, String name, String type) {
|
||||
HashSet<String> damageTypes = new HashSet<>();
|
||||
if (root.has(name)) {
|
||||
JsonElement arrayElement = root.get(name);
|
||||
if (arrayElement.isJsonArray()) {
|
||||
JsonArray array = arrayElement.getAsJsonArray();
|
||||
int size = array.size();
|
||||
for (int index = 0; index < size; index++) {
|
||||
JsonElement jsonElement = array.get(index);
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
String dtName = Helpers.getString(jsonObject, "name");
|
||||
String dtType = Helpers.getString(jsonObject, "type");
|
||||
if (type == null || type.equals(dtType)) {
|
||||
damageTypes.add(dtName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return damageTypes;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Set<Language> getSetOfLanguages(@NonNull JsonObject root, String name) {
|
||||
HashSet<Language> languages = new HashSet<>();
|
||||
if (root.has(name)) {
|
||||
JsonElement arrayElement = root.get(name);
|
||||
if (arrayElement.isJsonArray()) {
|
||||
JsonArray array = arrayElement.getAsJsonArray();
|
||||
int size = array.size();
|
||||
for (int index = 0; index < size; index++) {
|
||||
JsonElement jsonElement = array.get(index);
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
String languageName = Helpers.getString(jsonObject, "name");
|
||||
boolean canSpeak = Helpers.getBool(jsonObject, "speaks");
|
||||
Language language = new Language(languageName, canSpeak);
|
||||
languages.add(language);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return languages;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.majinnaibu.monstercards.helpers;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@SuppressWarnings({"RedundantIfStatement"})
|
||||
public final class StringHelper {
|
||||
public static boolean isNullOrEmpty(CharSequence value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ("".contentEquals(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static String join(String delimiter, @NonNull Collection<String> strings) {
|
||||
int length = strings.size();
|
||||
if (length < 1) {
|
||||
return "";
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean isFirst = true;
|
||||
for (String element : strings) {
|
||||
if (!isFirst) {
|
||||
sb.append(delimiter);
|
||||
}
|
||||
|
||||
sb.append(element);
|
||||
|
||||
isFirst = false;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static String oxfordJoin(String delimiter, String lastDelimiter, String onlyDelimiter, @NonNull Collection<String> strings) {
|
||||
int length = strings.size();
|
||||
if (length < 1) {
|
||||
return "";
|
||||
} else if (length == 2) {
|
||||
return join(onlyDelimiter, strings);
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int index = 0;
|
||||
int lastIndex = length - 1;
|
||||
for (String element : strings) {
|
||||
if (index > 0 && index < lastIndex) {
|
||||
sb.append(delimiter);
|
||||
} else if (index > 0 && index >= lastIndex) {
|
||||
sb.append(lastDelimiter);
|
||||
}
|
||||
sb.append(element);
|
||||
index++;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Integer parseInt(String s) {
|
||||
try {
|
||||
return Integer.parseInt(s);
|
||||
} catch (NumberFormatException _ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean containsCaseInsensitive(@NonNull String text, @NonNull String search) {
|
||||
// TODO: find a locale independent way to do this
|
||||
return text.toLowerCase().contains(search.toLowerCase());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user