Implement name suggestions (#24)

This commit is contained in:
J. D 2023-08-23 14:56:02 -05:00 committed by GitHub
parent d7e62ffbc3
commit 99fe6cca02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,12 +28,52 @@ public class ContentController : Controller {
[HttpPost] [HttpPost]
[Produces("application/xml")] [Produces("application/xml")]
[Route("ContentWebService.asmx/GetDefaultNameSuggestion")] [Route("ContentWebService.asmx/GetDefaultNameSuggestion")]
public IActionResult GetDefaultNameSuggestion() { public IActionResult GetDefaultNameSuggestion([FromForm] string apiToken) {
// TODO: generate random names, and ensure they aren't already taken string[] adjs = { //Adjectives used to generate suggested names
string[] suggestions = new string[] { "dragon1", "dragon2", "dragon3" }; "Adventurous", "Active", "Alert", "Attentive",
"Beautiful", "Berkian", "Berserker", "Bold", "Brave",
"Caring", "Cautious", "Creative", "Curious",
"Dangerous", "Daring", "Defender",
"Elder", "Exceptional", "Exquisite",
"Fearless", "Fighter", "Friendly",
"Gentle", "Grateful", "Great",
"Happy", "Honorable", "Hunter",
"Insightful", "Intelligent",
"Jolly", "Joyful", "Joyous",
"Kind", "Kindly",
"Legendary", "Lovable", "Lovely",
"Marvelous", "Magnificent",
"Noble", "Nifty", "Neat",
"Outcast", "Outgoing", "Organized",
"Planner", "Playful", "Pretty",
"Quick", "Quiet",
"Racer", "Random", "Resilient",
"Scientist", "Seafarer", "Smart", "Sweet",
"Thinker", "Thoughtful",
"Unafraid", "Unique",
"Valiant", "Valorous", "Victor", "Victorious", "Viking",
"Winner", "Warrior", "Wise",
"Young", "Youthful",
"Zealous", "Zealot"
};
//Get Username
User? user = ctx.Sessions.FirstOrDefault(e => e.ApiToken == apiToken)?.User;
string uname = user.Username;
string name = uname; //Variable for name processing
List<string> tnames = ctx.Vikings.Select(e => e.Name).ToList(); //Get a list of names from the table
Random choice = new Random(); //Randomizer for selecting random adjectives
List<string> suggestions = new();
AddSuggestion(choice, uname, suggestions);
for (int i = 0; i < 5; i++)
AddSuggestion(choice, GetNameSuggestion(choice, uname, adjs), suggestions);
return Ok(new DisplayNameUniqueResponse { return Ok(new DisplayNameUniqueResponse {
Suggestions = new SuggestionResult { Suggestions = new SuggestionResult {
Suggestion = suggestions Suggestion = suggestions.ToArray()
} }
}); });
} }
@ -1028,4 +1068,21 @@ public class ContentController : Controller {
} }
return false; return false;
} }
private void AddSuggestion(Random rand, string name, List<string> suggestions) {
if (ctx.Vikings.Any(x => x.Name == name) || suggestions.Contains(name)) {
name += rand.Next(1, 5000);
if (ctx.Vikings.Any(x => x.Name == name) || suggestions.Contains(name)) return;
}
suggestions.Add(name);
}
private string GetNameSuggestion(Random rand, string username, string[] adjectives) {
string name = username;
if (rand.NextDouble() >= 0.5)
name = username + "The" + adjectives[rand.Next(adjectives.Length)];
if (name == username || rand.NextDouble() >= 0.5)
return adjectives[rand.Next(adjectives.Length)] + name;
return name;
}
} }