Unverified Commit 7fef3024 authored by Mathieu Féry (Mathius)'s avatar Mathieu Féry (Mathius)
Browse files

Fix selection, update select with almostSelector and update DnDSheet Name Generation

parent bfecc4c2
Pipeline #28853 failed with stages
in 53 seconds
...@@ -17,6 +17,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; ...@@ -17,6 +17,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
...@@ -172,7 +173,7 @@ public class DnDSheet implements Serializable { ...@@ -172,7 +173,7 @@ public class DnDSheet implements Serializable {
public static String generateName(@NonNull Context ctx, @Nullable String appearance, public static String generateName(@NonNull Context ctx, @Nullable String appearance,
@Nullable String personality, @Nullable String personality,
@NonNull DnDClass caracterClass) { @NonNull DnDClass caracterClass) {
return Name.getRandomName(ctx, return Name.getRandomName(ctx, Name.allPeopleNames,
personality != null ? personality : "", personality != null ? personality : "",
appearance != null ? appearance : "", appearance != null ? appearance : "",
caracterClass.name caracterClass.name
......
...@@ -29,6 +29,7 @@ public class Name implements Serializable { ...@@ -29,6 +29,7 @@ public class Name implements Serializable {
public static final String apiProvider = "https://donjon.bin.sh/"; public static final String apiProvider = "https://donjon.bin.sh/";
public static final String nameDelimiter = "\n"; public static final String nameDelimiter = "\n";
public static final String regexUsed = "[,;| ]";
protected static final String TAG = "donjon.bin.sh.Name"; protected static final String TAG = "donjon.bin.sh.Name";
public final String specie; public final String specie;
...@@ -97,6 +98,24 @@ public class Name implements Serializable { ...@@ -97,6 +98,24 @@ public class Name implements Serializable {
return matchWithSpecie(keywords) && matchWithType(keywords); return matchWithSpecie(keywords) && matchWithType(keywords);
} }
/**
* For check if this Name Generator match almost with keywords given
* @param keywords : String represent keyword used
* @return matched
*/
public boolean matchAlmostWith(String... keywords) {
return matchAlmostWith(Arrays.asList(keywords));
}
/**
* For check if this Name Generator match almost with list of keywords given
* @param keywords : List of String represent keyword used
* @return matched
*/
public boolean matchAlmostWith(List<String> keywords) {
return matchWithSpecie(keywords) || type != null && matchWithType(keywords);
}
/** /**
* For check if this Name Generator specie match with list of keywords given * For check if this Name Generator specie match with list of keywords given
* @param keywords : List of String represent keyword used * @param keywords : List of String represent keyword used
...@@ -106,9 +125,8 @@ public class Name implements Serializable { ...@@ -106,9 +125,8 @@ public class Name implements Serializable {
boolean matched = false; boolean matched = false;
for (String keyword: keywords) { for (String keyword: keywords) {
String word = keyword.trim(); String word = keyword.trim();
if (word.contains(",")) { if (!matched && word.split(regexUsed).length > 1)
matched = matchWithType(Arrays.asList(word.split(","))); matched = matchWithSpecie(Arrays.asList(word.split(regexUsed)));
}
if (!matched && specie.equalsIgnoreCase(word)) if (!matched && specie.equalsIgnoreCase(word))
matched = true; matched = true;
} }
...@@ -125,9 +143,8 @@ public class Name implements Serializable { ...@@ -125,9 +143,8 @@ public class Name implements Serializable {
if (type != null) if (type != null)
for (String keyword: keywords) { for (String keyword: keywords) {
String word = keyword.trim(); String word = keyword.trim();
if (word.contains(",")) { if (!matched && word.split(regexUsed).length > 1)
matched = matchWithType(Arrays.asList(word.split(","))); matched = matchWithType(Arrays.asList(word.split(regexUsed)));
}
if (!matched && type.equalsIgnoreCase(word)) if (!matched && type.equalsIgnoreCase(word))
matched = true; matched = true;
} }
...@@ -253,16 +270,14 @@ public class Name implements Serializable { ...@@ -253,16 +270,14 @@ public class Name implements Serializable {
return out; return out;
} }
public static final String maleType = "Male"; public static final String maleType = "Male";
public static final String femaleType = "Female"; public static final String femaleType = "Female";
public static final String townType = "Town"; public static final String townType = "Town";
public static final String elementalType = "Elemental"; public static final String elementalType = "Elemental";
public static final String specificLocationType = "Location"; public static final String specificLocationType = "Location";
public static final String castleType = "Castle"; public static final String castleType = "Castle";
public static final List<String> genderTypes = Arrays.asList(maleType, femaleType); public static final List<String> genderTypes = Arrays.asList(maleType, femaleType);
public static final List<String> genderAndTownTypes = joinList(genderTypes, townType); public static final List<String> townAndCastleTypes = Arrays.asList(townType, castleType);
public static final List<String> genderTownAndCastleTypes = joinList(genderAndTownTypes,
castleType);
public static final List<String> commonFantasySpecies = Arrays.asList( public static final List<String> commonFantasySpecies = Arrays.asList(
"Human", "Dwarvish", "Elvish", "Halfling"); "Human", "Dwarvish", "Elvish", "Halfling");
public static final List<String> monstrousFantasySpecies = Arrays.asList( public static final List<String> monstrousFantasySpecies = Arrays.asList(
...@@ -295,10 +310,14 @@ public class Name implements Serializable { ...@@ -295,10 +310,14 @@ public class Name implements Serializable {
"Congolese", "Egyptian", "Ethiopian", "Malian"); "Congolese", "Egyptian", "Ethiopian", "Malian");
public static final List<String> newWorldPeople = Arrays.asList( public static final List<String> newWorldPeople = Arrays.asList(
"Algonquin", "Aztec", "Inkan", "Inuit", "Navajo", "Sioux"); "Algonquin", "Aztec", "Inkan", "Inuit", "Navajo", "Sioux");
public static final List<Name> commonFantasyNames = createAllCombination( public static final List<Name> commonFantasyNamesOnly = createAllCombination(
commonFantasySpecies, genderAndTownTypes); commonFantasySpecies, genderTypes);
public static final List<Name> monstrousFantasyNames = createAllCombination( public static final List<Name> commonFantasyNames = joinList(Arrays.asList(
monstrousFantasySpecies, genderAndTownTypes); commonFantasyNamesOnly, createAllCombination(commonFantasySpecies, townType)));
public static final List<Name> monstrousFantasyNamesOnly = createAllCombination(
monstrousFantasySpecies, genderTypes);
public static final List<Name> monstrousFantasyNames = joinList(Arrays.asList(
monstrousFantasyNamesOnly, createAllCombination(monstrousFantasySpecies, townType)));
public static final List<Name> outsiderFantasyNames = createAllCombination( public static final List<Name> outsiderFantasyNames = createAllCombination(
outsiderFantasySpecies); outsiderFantasySpecies);
public static final List<Name> elementalFantasyNames = createAllCombination( public static final List<Name> elementalFantasyNames = createAllCombination(
...@@ -310,24 +329,42 @@ public class Name implements Serializable { ...@@ -310,24 +329,42 @@ public class Name implements Serializable {
createAllCombination(fantasySettingsBaseLocation), createAllCombination(fantasySettingsBaseLocation),
createAllCombination(fantasySettingsSpecificLocation, specificLocationType))); createAllCombination(fantasySettingsSpecificLocation, specificLocationType)));
public static final List<Name> townFantasyNames = createAllCombination(fantasySettingsTown); public static final List<Name> townFantasyNames = createAllCombination(fantasySettingsTown);
public static final List<Name> allPeopleFantasyNames = joinList(Arrays.asList(
commonFantasyNamesOnly, monstrousFantasyNamesOnly, outsiderFantasyNames,
elementalFantasyNames));
public static final List<Name> allFantasyNames = joinList(Arrays.asList( public static final List<Name> allFantasyNames = joinList(Arrays.asList(
commonFantasyNames, monstrousFantasyNames, outsiderFantasyNames, elementalFantasyNames, commonFantasyNames, monstrousFantasyNames, outsiderFantasyNames, elementalFantasyNames,
entityFantasyNames, eventFantasyNames, itemFantasyNames, locationFantasyNames, entityFantasyNames, eventFantasyNames, itemFantasyNames, locationFantasyNames,
townFantasyNames)); townFantasyNames));
public static final List<Name> ancientWorldPeopleNames = createAllCombination( public static final List<Name> ancientWorldPeopleNamesOnly = createAllCombination(
ancientWorldPeople, genderAndTownTypes); ancientWorldPeople, genderTypes);
public static final List<Name> medievalPeopleNames = createAllCombination( public static final List<Name> ancientWorldPeopleNames = joinList(Arrays.asList(
medievalPeople, genderAndTownTypes); ancientWorldPeopleNamesOnly, createAllCombination(ancientWorldPeople, townType)));
public static final List<Name> medievalPeopleNamesOnly = createAllCombination(
medievalPeople, genderTypes);
public static final List<Name> medievalPeopleNames = joinList(Arrays.asList(
medievalPeopleNamesOnly, createAllCombination(medievalPeople, townType)));
public static final List<Name> asiaticPeopleNamesOnly = createAllCombination(
joinList(Arrays.asList(baseAsiaticPeople, specificAsiaticPeople)), genderTypes);
public static final List<Name> asiaticPeopleNames = joinList(Arrays.asList( public static final List<Name> asiaticPeopleNames = joinList(Arrays.asList(
createAllCombination(baseAsiaticPeople, genderAndTownTypes), asiaticPeopleNamesOnly, createAllCombination(baseAsiaticPeople, townType),
createAllCombination(specificAsiaticPeople, genderTownAndCastleTypes))); createAllCombination(specificAsiaticPeople, townAndCastleTypes)));
public static final List<Name> africaPeopleNames = createAllCombination( public static final List<Name> africaPeopleNamesOnly = createAllCombination(
africaPeople, genderAndTownTypes); africaPeople, genderTypes);
public static final List<Name> newWorldPeopleNames = createAllCombination( public static final List<Name> africaPeopleNames = joinList(Arrays.asList(
newWorldPeople, genderAndTownTypes); africaPeopleNamesOnly, createAllCombination(africaPeople, townType)));
public static final List<Name> newWorldPeopleNamesOnly = createAllCombination(
newWorldPeople, genderTypes);
public static final List<Name> newWorldPeopleNames = joinList(Arrays.asList(
newWorldPeopleNamesOnly, createAllCombination(newWorldPeople, townType)));
public static final List<Name> allQuasiHistoricalPeopleNames = joinList(Arrays.asList(
ancientWorldPeopleNamesOnly, medievalPeopleNamesOnly, asiaticPeopleNamesOnly,
newWorldPeopleNamesOnly));
public static final List<Name> allQuasiHistoricalNames = joinList(Arrays.asList( public static final List<Name> allQuasiHistoricalNames = joinList(Arrays.asList(
ancientWorldPeopleNames, medievalPeopleNames, asiaticPeopleNames, africaPeopleNames, ancientWorldPeopleNames, medievalPeopleNames, asiaticPeopleNames, africaPeopleNames,
newWorldPeopleNames)); newWorldPeopleNames));
public static final List<Name> allPeopleNames = joinList(Arrays.asList(allPeopleFantasyNames,
allQuasiHistoricalPeopleNames));
public static final List<Name> allNames = joinList(Arrays.asList(allFantasyNames, public static final List<Name> allNames = joinList(Arrays.asList(allFantasyNames,
allQuasiHistoricalNames)); allQuasiHistoricalNames));
public static final Name defaultName = commonFantasyNames.get(0); public static final Name defaultName = commonFantasyNames.get(0);
...@@ -396,10 +433,12 @@ public class Name implements Serializable { ...@@ -396,10 +433,12 @@ public class Name implements Serializable {
public static String getRandomName(Context ctx, List<Name> names, List<String> keywords) { public static String getRandomName(Context ctx, List<Name> names, List<String> keywords) {
List<Name> baseNames = getNamesMatches(names, keywords); List<Name> baseNames = getNamesMatches(names, keywords);
Name nameUsed; Name nameUsed;
if (baseNames.size() == 0) if (baseNames.isEmpty())
nameUsed = defaultName; baseNames = getNamesAlmostMatches(names, keywords);
else if (!baseNames.isEmpty())
nameUsed = getRandomName(baseNames); nameUsed = getRandomName(baseNames);
else
nameUsed = defaultName;
return nameUsed.getRandomName(ctx); return nameUsed.getRandomName(ctx);
} }
...@@ -448,4 +487,50 @@ public class Name implements Serializable { ...@@ -448,4 +487,50 @@ public class Name implements Serializable {
out.add(name); out.add(name);
return out; return out;
} }
/**
* For get name generator from keywords given
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesAlmostMatches(String... keywords) {
return getNamesAlmostMatches(Arrays.asList(keywords));
}
/**
* For get name generator from keywords given
* @param keywords : List of String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesAlmostMatches(List<String> keywords) {
return getNamesAlmostMatches(allNames, keywords);
}
/**
* For get name generator from keywords given
* @param names : List of Names used
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesAlmostMatches(List<Name> names, String... keywords) {
return getNamesAlmostMatches(names, Arrays.asList(keywords));
}
/**
* For get name generator from keywords given
* @param names : List of Names used
* @param keywords : String represent keyword used
* @return nameGenerated
*/
@NonNull
public static List<Name> getNamesAlmostMatches(List<Name> names, List<String> keywords) {
List<Name> out = new ArrayList<>();
for (Name name: names)
if (name.matchAlmostWith(keywords))
out.add(name);
return out;
}
} }
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
import static fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh.Name.createAllCombination; import static fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh.Name.createAllCombination;
import static fr.iutnantes.miar.dndgenerator.api.utils.ListUtils.joinList; import static fr.iutnantes.miar.dndgenerator.api.utils.ListUtils.joinList;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
...@@ -66,6 +67,7 @@ public class NameTest { ...@@ -66,6 +67,7 @@ public class NameTest {
assertTrue(exampleTruc.matchWith("ExAmple, Truc, Bidule")); assertTrue(exampleTruc.matchWith("ExAmple, Truc, Bidule"));
assertFalse(exampleTruc.matchWith("Truc", "Bidule")); assertFalse(exampleTruc.matchWith("Truc", "Bidule"));
assertFalse(exampleTruc.matchWith("Example", "Bidule")); assertFalse(exampleTruc.matchWith("Example", "Bidule"));
assertFalse(exampleTruc.matchWith("Bidule"));
} }
@Test @Test
public void getNamesMatchesTest() { public void getNamesMatchesTest() {
...@@ -74,4 +76,33 @@ public class NameTest { ...@@ -74,4 +76,33 @@ public class NameTest {
assertEquals(allSecondExampleArg, assertEquals(allSecondExampleArg,
Name.getNamesMatches(allExamples, "2nDexample", "truc", "bidule")); Name.getNamesMatches(allExamples, "2nDexample", "truc", "bidule"));
} }
@Test
public void matchAlmostWithTest() {
assertTrue(example.matchAlmostWith("ExAmple", "Truc", "Bidule"));
assertTrue(example.matchAlmostWith(" Example", "Truc", "Bidule"));
assertFalse(example.matchAlmostWith("Truc", "Bidule"));
assertTrue(exampleTruc.matchAlmostWith("ExAmple", "Truc", "Bidule"));
assertTrue(exampleTruc.matchAlmostWith(" Example", "truc ", "Bidule"));
assertTrue(exampleTruc.matchAlmostWith("ExAmple, Truc", "Bidule"));
assertTrue(exampleTruc.matchAlmostWith("ExAmple", "Truc, Bidule"));
assertTrue(exampleTruc.matchAlmostWith("ExAmple, Truc, Bidule"));
assertTrue(exampleTruc.matchAlmostWith("Truc", "Bidule"));
assertTrue(exampleTruc.matchAlmostWith("Example", "Bidule"));
assertFalse(exampleTruc.matchAlmostWith("Bidule"));
}
@Test
public void getNamesAlmostMatchesTest() {
assertNotEquals(allExampleArg,
Name.getNamesAlmostMatches(allExamples, "example", "truc", "bidule"));
assertNotEquals(allSecondExampleArg,
Name.getNamesAlmostMatches(allExamples, "2nDexample", "truc", "bidule"));
assertNotEquals(allExampleArg,
Name.getNamesAlmostMatches(allExamples, "example", "bidule"));
assertNotEquals(allSecondExampleArg,
Name.getNamesAlmostMatches(allExamples, "2nDexample", "truc"));
assertEquals(allExampleArg,
Name.getNamesAlmostMatches(allExamples, "example"));
assertEquals(allSecondExampleArg,
Name.getNamesAlmostMatches(allExamples, "2nDexample"));
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment