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;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;
......@@ -172,7 +173,7 @@ public class DnDSheet implements Serializable {
public static String generateName(@NonNull Context ctx, @Nullable String appearance,
@Nullable String personality,
@NonNull DnDClass caracterClass) {
return Name.getRandomName(ctx,
return Name.getRandomName(ctx, Name.allPeopleNames,
personality != null ? personality : "",
appearance != null ? appearance : "",
caracterClass.name
......
......@@ -29,6 +29,7 @@ public class Name implements Serializable {
public static final String apiProvider = "https://donjon.bin.sh/";
public static final String nameDelimiter = "\n";
public static final String regexUsed = "[,;| ]";
protected static final String TAG = "donjon.bin.sh.Name";
public final String specie;
......@@ -97,6 +98,24 @@ public class Name implements Serializable {
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
* @param keywords : List of String represent keyword used
......@@ -106,9 +125,8 @@ public class Name implements Serializable {
boolean matched = false;
for (String keyword: keywords) {
String word = keyword.trim();
if (word.contains(",")) {
matched = matchWithType(Arrays.asList(word.split(",")));
}
if (!matched && word.split(regexUsed).length > 1)
matched = matchWithSpecie(Arrays.asList(word.split(regexUsed)));
if (!matched && specie.equalsIgnoreCase(word))
matched = true;
}
......@@ -125,9 +143,8 @@ public class Name implements Serializable {
if (type != null)
for (String keyword: keywords) {
String word = keyword.trim();
if (word.contains(",")) {
matched = matchWithType(Arrays.asList(word.split(",")));
}
if (!matched && word.split(regexUsed).length > 1)
matched = matchWithType(Arrays.asList(word.split(regexUsed)));
if (!matched && type.equalsIgnoreCase(word))
matched = true;
}
......@@ -260,9 +277,7 @@ public class Name implements Serializable {
public static final String specificLocationType = "Location";
public static final String castleType = "Castle";
public static final List<String> genderTypes = Arrays.asList(maleType, femaleType);
public static final List<String> genderAndTownTypes = joinList(genderTypes, townType);
public static final List<String> genderTownAndCastleTypes = joinList(genderAndTownTypes,
castleType);
public static final List<String> townAndCastleTypes = Arrays.asList(townType, castleType);
public static final List<String> commonFantasySpecies = Arrays.asList(
"Human", "Dwarvish", "Elvish", "Halfling");
public static final List<String> monstrousFantasySpecies = Arrays.asList(
......@@ -295,10 +310,14 @@ public class Name implements Serializable {
"Congolese", "Egyptian", "Ethiopian", "Malian");
public static final List<String> newWorldPeople = Arrays.asList(
"Algonquin", "Aztec", "Inkan", "Inuit", "Navajo", "Sioux");
public static final List<Name> commonFantasyNames = createAllCombination(
commonFantasySpecies, genderAndTownTypes);
public static final List<Name> monstrousFantasyNames = createAllCombination(
monstrousFantasySpecies, genderAndTownTypes);
public static final List<Name> commonFantasyNamesOnly = createAllCombination(
commonFantasySpecies, genderTypes);
public static final List<Name> commonFantasyNames = joinList(Arrays.asList(
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(
outsiderFantasySpecies);
public static final List<Name> elementalFantasyNames = createAllCombination(
......@@ -310,24 +329,42 @@ public class Name implements Serializable {
createAllCombination(fantasySettingsBaseLocation),
createAllCombination(fantasySettingsSpecificLocation, specificLocationType)));
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(
commonFantasyNames, monstrousFantasyNames, outsiderFantasyNames, elementalFantasyNames,
entityFantasyNames, eventFantasyNames, itemFantasyNames, locationFantasyNames,
townFantasyNames));
public static final List<Name> ancientWorldPeopleNames = createAllCombination(
ancientWorldPeople, genderAndTownTypes);
public static final List<Name> medievalPeopleNames = createAllCombination(
medievalPeople, genderAndTownTypes);
public static final List<Name> ancientWorldPeopleNamesOnly = createAllCombination(
ancientWorldPeople, genderTypes);
public static final List<Name> ancientWorldPeopleNames = joinList(Arrays.asList(
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(
createAllCombination(baseAsiaticPeople, genderAndTownTypes),
createAllCombination(specificAsiaticPeople, genderTownAndCastleTypes)));
public static final List<Name> africaPeopleNames = createAllCombination(
africaPeople, genderAndTownTypes);
public static final List<Name> newWorldPeopleNames = createAllCombination(
newWorldPeople, genderAndTownTypes);
asiaticPeopleNamesOnly, createAllCombination(baseAsiaticPeople, townType),
createAllCombination(specificAsiaticPeople, townAndCastleTypes)));
public static final List<Name> africaPeopleNamesOnly = createAllCombination(
africaPeople, genderTypes);
public static final List<Name> africaPeopleNames = joinList(Arrays.asList(
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(
ancientWorldPeopleNames, medievalPeopleNames, asiaticPeopleNames, africaPeopleNames,
newWorldPeopleNames));
public static final List<Name> allPeopleNames = joinList(Arrays.asList(allPeopleFantasyNames,
allQuasiHistoricalPeopleNames));
public static final List<Name> allNames = joinList(Arrays.asList(allFantasyNames,
allQuasiHistoricalNames));
public static final Name defaultName = commonFantasyNames.get(0);
......@@ -396,10 +433,12 @@ public class Name implements Serializable {
public static String getRandomName(Context ctx, List<Name> names, List<String> keywords) {
List<Name> baseNames = getNamesMatches(names, keywords);
Name nameUsed;
if (baseNames.size() == 0)
nameUsed = defaultName;
else
if (baseNames.isEmpty())
baseNames = getNamesAlmostMatches(names, keywords);
if (!baseNames.isEmpty())
nameUsed = getRandomName(baseNames);
else
nameUsed = defaultName;
return nameUsed.getRandomName(ctx);
}
......@@ -448,4 +487,50 @@ public class Name implements Serializable {
out.add(name);
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;
import static fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh.Name.createAllCombination;
import static fr.iutnantes.miar.dndgenerator.api.utils.ListUtils.joinList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
......@@ -66,6 +67,7 @@ public class NameTest {
assertTrue(exampleTruc.matchWith("ExAmple, Truc, Bidule"));
assertFalse(exampleTruc.matchWith("Truc", "Bidule"));
assertFalse(exampleTruc.matchWith("Example", "Bidule"));
assertFalse(exampleTruc.matchWith("Bidule"));
}
@Test
public void getNamesMatchesTest() {
......@@ -74,4 +76,33 @@ public class NameTest {
assertEquals(allSecondExampleArg,
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