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

Implement base name generation (Thanks donjon.bin.sh)

parent b8c791ff
Pipeline #28820 failed with stages
in 1 minute and 8 seconds
package fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh;
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(AndroidJUnit4.class)
public class NameTest {
public Context getMockContext() {
return InstrumentationRegistry.getInstrumentation().getTargetContext()
.getApplicationContext();
}
//Mock
public Context ctx = getMockContext();
public static int n = 25;
public void testNameGenerated(List<String> namesGenerated, int nb) {
assertEquals(nb, namesGenerated.size());
for(String nameGenerated: namesGenerated)
testNameGenerated(nameGenerated);
}
public void testNameGenerated(String nameGenerated) {
assertNotNull(nameGenerated);
}
@Test
public void getName() {
testNameGenerated(Name.getRandomName().getRandomName(ctx));
}
@Test
public void getNames() {
testNameGenerated(Name.getRandomName().getRandomNames(ctx, n), n);
}
@Test
public void getAllNames() {
for (Name name: Name.allNames)
testNameGenerated(name.getRandomNames(ctx, n), n);
}
}
package fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.ion.Ion;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static fr.iutnantes.miar.dndgenerator.api.utils.ListUtils.joinList;
/**
* Name is a class represent Name Generator from donjon.bin.sh
*/
public class Name implements Serializable {
public static final String apiProvider = "https://donjon.bin.sh/";
public static final String nameDelimiter = "\n";
protected static final String TAG = "donjon.bin.sh.Name";
public final String specie;
public final String type;
/**
* Basic Constructor for name
* @param specie Specie related to this name generator
* @param type Type related to this name generator
*/
protected Name(@NonNull String specie, @Nullable String type) {
this.specie = specie;
this.type = type;
}
/**
* For write name.
* @return toString
*/
@Override
@NonNull
public String toString() {
return String.format(
"%s of %s with %s", TAG, specie, type
);
}
/**
* For identify name.
* @return hashCode
*/
@Override
public int hashCode() {
return new HashCodeBuilder(65, 11)
.append(specie).append(type != null ? type.hashCode() : 1).toHashCode();
}
/**
* For compare Any Object with this name.
* @param obj : Object for comparison
* @return equals
*/
@Override
public boolean equals(@Nullable Object obj) {
if (obj != null && obj.getClass() == this.getClass())
return this.hashCode() == obj.hashCode();
else
return false;
}
/**
* Method for obtaining a random name from this name generator
* @param ctx : Context for make request for name
* @return name
*/
@NonNull
public String getRandomName(@NonNull Context ctx) {
return getRandomNames(ctx, 1).get(0);
}
/**
* Method for obtaining a list of random name from this name generator
* @param ctx : Context for make request for name
* @param nb : Integer represent number of name you want
* @return names
*/
@NonNull
public List<String> getRandomNames(@NonNull Context ctx, @NonNull Integer nb) {
try {
return getRandomFutureNames(ctx, nb).get();
} catch (ExecutionException | InterruptedException e) {
return getRandomNames(ctx, nb);
}
}
/**
* Method for obtaining a list of future random name from this name generator
* @param ctx : Context for make request for name
* @param nb : Integer represent number of name you want
* @return futures
*/
@NonNull
public Future<List<String>> getRandomFutureNames(@NonNull Context ctx, @NonNull Integer nb) {
SimpleFuture<List<String>> future = new SimpleFuture<>();
getRandomFutureNames(ctx, nb, future);
return future;
}
/**
* Method performing the api query for the given system and will place the result in the future
* past as argument.
* @param ctx : Context for make request for name
* @param nb : Integer represent number of name you want
* @param future : SimpleFuture represents the object in which the result will be stored
**/
public void getRandomFutureNames(@NonNull Context ctx, @NonNull Integer nb,
@NonNull SimpleFuture<List<String>> future) {
String target = String.format("%sname/rpc-name.fcgi?type=%s%s&n=%d",
apiProvider, specie, type != null ? String.format("+%s", type) : "", nb);
Ion.with(ctx).load(target).noCache()
.setLogging(String.format("%s-Ion", TAG), Log.DEBUG)
.asString().withResponse().setCallback((e, response) -> {
Log.d(TAG, String.format("Request submit and response receveid !%nProcessing..."));
if (response != null && response.getHeaders().code() == 200) {
List<String> names = new ArrayList<>();
for (String name: response.getResult().split(nameDelimiter))
names.add(name);
int diff = nb - names.size();
if (diff > 0)
names.addAll(getRandomNames(ctx, nb));
Log.d(TAG, "Result set in OwnFuture");
future.setComplete(names);
} else {
Log.wtf(TAG, response != null ?
String.format("Error code %d for %s!, retrying...",
response.getHeaders().code(), target) :
String.format("Null response provided for %s", target)
);
getRandomFutureNames(ctx, nb, future);
}
}
);
}
/**
* Method for create all combination related to species given in argument
* @param species Species used for creates names
* @return names
*/
public static List<Name> createAllCombination(String... species) {
return createAllCombination(Arrays.asList(species));
}
/**
* Method for create all combination related to species given in argument
* @param species List of species used for creates names
* @return names
*/
public static List<Name> createAllCombination(List<String> species) {
List<Name> out = new ArrayList<>();
for (String specie : species)
out.add(new Name(specie, null));
return out;
}
/**
* Method for create all combination related to species and types given in argument
* @param species List of species used for creates names
* @param types Types used for creates names
* @return names
*/
public static List<Name> createAllCombination(List<String> species, String... types) {
return createAllCombination(species, Arrays.asList(types));
}
/**
* Method for create all combination related to species given in argument
* @param species List of Species used for creates names
* @param types List of Types used for creates names
* @return names
*/
public static List<Name> createAllCombination(List<String> species, List<String> types) {
List<Name> out = new ArrayList<>();
for (String specie : species)
for (String type : types)
out.add(new Name(specie, type));
return out;
}
public static final String maleType = "Male";
public static final String femaleType = "Female";
public static final String townType = "Town";
public static final String elementalType = "Elemental";
public static final List<String> genderTypes = Arrays.asList(maleType, femaleType);
public static final List<String> genderAndTownTypes = joinList(genderTypes, townType);
public static final List<String> commonFantasySpecies = Arrays.asList(
"Human", "Dwarvish", "Elvish", "Halfling");
public static final List<String> monstrousFantasySpecies = Arrays.asList(
"Draconic", "Drow", "Orcish");
public static final List<String> outsiderFantasySpecies = Arrays.asList(
"Celestial", "Fiendish", "Modron");
public static final List<String> elementalFantasySpecies = Arrays.asList(
"Air", "Earth", "Fire", "Water");
public static final List<Name> commonFantasyNames = createAllCombination(
commonFantasySpecies, genderAndTownTypes);
public static final List<Name> monstrousFantasyNames = createAllCombination(
monstrousFantasySpecies, genderAndTownTypes);
public static final List<Name> outsiderFantasyNames = createAllCombination(
outsiderFantasySpecies);
public static final List<Name> elementalFantasyNames = createAllCombination(
elementalFantasySpecies, elementalType);
public static final List<Name> allFantasyNames = joinList(Arrays.asList(
commonFantasyNames, monstrousFantasyNames, outsiderFantasyNames, elementalFantasyNames));
public static final List<Name> allNames = allFantasyNames;
/**
* For get random name from the API.
* @return name
*/
@NonNull
public static Name getRandomName() {
return getRandomName(allNames);
}
/**
* For get random name from list provided.
* @param names : List where we’re going to shoot randomly
* @return name
*/
@NonNull
public static Name getRandomName(List<Name> names) {
return names.get(new Random().nextInt(names.size()));
}
}
package fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static fr.iutnantes.miar.dndgenerator.api.donjon.bin.sh.Name.createAllCombination;
import static org.junit.Assert.assertEquals;
public class NameTest {
@Test
public void createAllCombinationTest() {
Name example = new Name("example", null);
Name secondExample = new Name("2ndExample", null);
List<Name> twoExamples = Arrays.asList(example, secondExample);
Name exampleTruc = new Name("example", "truc");
Name secondExampleTruc = new Name("2ndExample", "truc");
List<Name> twoExamplesTruc = Arrays.asList(exampleTruc, secondExampleTruc);
Name exampleBidule = new Name("example", "bidule");
Name secondExampleBidule = new Name("2ndExample", "bidule");
List<Name> fourExamples = Arrays.asList(
exampleTruc, exampleBidule, secondExampleTruc, secondExampleBidule);
assertEquals(
Collections.singletonList(example),
createAllCombination("example")
);
assertEquals(
twoExamples, createAllCombination("example", "2ndExample")
);
assertEquals(
twoExamples, createAllCombination(Arrays.asList("example", "2ndExample"))
);
assertEquals(
twoExamplesTruc, createAllCombination(Arrays.asList("example", "2ndExample"),
"truc")
);
assertEquals(
fourExamples, createAllCombination(Arrays.asList("example", "2ndExample"),
"truc", "bidule")
);
assertEquals(
fourExamples, createAllCombination(Arrays.asList("example", "2ndExample"),
Arrays.asList("truc", "bidule"))
);
}
}
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