From 99fe6cca02a5820673dde4ebe5ed9169f024b8e0 Mon Sep 17 00:00:00 2001 From: "J. D" Date: Wed, 23 Aug 2023 14:56:02 -0500 Subject: [PATCH] Implement name suggestions (#24) --- src/Controllers/Common/ContentController.cs | 65 +++++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/src/Controllers/Common/ContentController.cs b/src/Controllers/Common/ContentController.cs index 03d7ab8..cd00346 100644 --- a/src/Controllers/Common/ContentController.cs +++ b/src/Controllers/Common/ContentController.cs @@ -28,12 +28,52 @@ public class ContentController : Controller { [HttpPost] [Produces("application/xml")] [Route("ContentWebService.asmx/GetDefaultNameSuggestion")] - public IActionResult GetDefaultNameSuggestion() { - // TODO: generate random names, and ensure they aren't already taken - string[] suggestions = new string[] { "dragon1", "dragon2", "dragon3" }; + public IActionResult GetDefaultNameSuggestion([FromForm] string apiToken) { + string[] adjs = { //Adjectives used to generate suggested names + "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 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 suggestions = new(); + AddSuggestion(choice, uname, suggestions); + + for (int i = 0; i < 5; i++) + AddSuggestion(choice, GetNameSuggestion(choice, uname, adjs), suggestions); + return Ok(new DisplayNameUniqueResponse { Suggestions = new SuggestionResult { - Suggestion = suggestions + Suggestion = suggestions.ToArray() } }); } @@ -1028,4 +1068,21 @@ public class ContentController : Controller { } return false; } + + private void AddSuggestion(Random rand, string name, List 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; + } }