| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Api.Utilities; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Generates memorable invitation codes in XXXX-XXXX format. |
| | | 7 | | /// Uses word-like patterns that are easy to read aloud in Discord. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class InvitationCodeGenerator |
| | | 10 | | { |
| | | 11 | | // Consonants and vowels for pronounceable codes |
| | 1 | 12 | | private static readonly char[] Consonants = "BCDFGHJKLMNPRSTVWXZ".ToCharArray(); |
| | 1 | 13 | | private static readonly char[] Vowels = "AEIOU".ToCharArray(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Generate a memorable code like "FROG-AXLE" or "MINT-RUBY" |
| | | 17 | | /// Pattern: CVCC-VCCV or similar pronounceable combinations |
| | | 18 | | /// </summary> |
| | | 19 | | public static string GenerateCode() |
| | | 20 | | { |
| | 16 | 21 | | var part1 = GenerateWordLikePart(); |
| | 16 | 22 | | var part2 = GenerateWordLikePart(); |
| | 16 | 23 | | return $"{part1}-{part2}"; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private static string GenerateWordLikePart() |
| | | 27 | | { |
| | | 28 | | // Generate a 4-character pronounceable part |
| | | 29 | | // Pattern alternates to create word-like strings |
| | 32 | 30 | | var chars = new char[4]; |
| | | 31 | | |
| | | 32 | | // Random pattern: start with consonant or vowel |
| | 32 | 33 | | bool startWithConsonant = RandomNumberGenerator.GetInt32(2) == 0; |
| | | 34 | | |
| | 320 | 35 | | for (int i = 0; i < 4; i++) |
| | | 36 | | { |
| | 128 | 37 | | bool useConsonant = (i % 2 == 0) ? startWithConsonant : !startWithConsonant; |
| | | 38 | | |
| | 128 | 39 | | if (useConsonant) |
| | | 40 | | { |
| | 64 | 41 | | chars[i] = Consonants[RandomNumberGenerator.GetInt32(Consonants.Length)]; |
| | | 42 | | } |
| | | 43 | | else |
| | | 44 | | { |
| | 64 | 45 | | chars[i] = Vowels[RandomNumberGenerator.GetInt32(Vowels.Length)]; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | 32 | 49 | | return new string(chars); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Normalize a code for comparison (uppercase, with hyphen) |
| | | 54 | | /// </summary> |
| | | 55 | | public static string NormalizeCode(string code) |
| | | 56 | | { |
| | 22 | 57 | | if (string.IsNullOrWhiteSpace(code)) |
| | 0 | 58 | | return string.Empty; |
| | | 59 | | |
| | | 60 | | // Remove spaces, uppercase |
| | 22 | 61 | | var cleaned = code.Trim().ToUpperInvariant().Replace(" ", ""); |
| | | 62 | | |
| | | 63 | | // If no hyphen and 8 chars, insert hyphen |
| | 22 | 64 | | if (!cleaned.Contains('-') && cleaned.Length == 8) |
| | | 65 | | { |
| | 1 | 66 | | cleaned = $"{cleaned[..4]}-{cleaned[4..]}"; |
| | | 67 | | } |
| | | 68 | | |
| | 22 | 69 | | return cleaned; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Validate code format (XXXX-XXXX) |
| | | 74 | | /// </summary> |
| | | 75 | | public static bool IsValidFormat(string code) |
| | | 76 | | { |
| | 11 | 77 | | var normalized = NormalizeCode(code); |
| | | 78 | | |
| | 11 | 79 | | if (normalized.Length != 9) // XXXX-XXXX |
| | 1 | 80 | | return false; |
| | | 81 | | |
| | 10 | 82 | | if (normalized[4] != '-') |
| | 0 | 83 | | return false; |
| | | 84 | | |
| | | 85 | | // Check all other chars are letters |
| | 200 | 86 | | for (int i = 0; i < 9; i++) |
| | | 87 | | { |
| | 90 | 88 | | if (i == 4) |
| | | 89 | | continue; |
| | 80 | 90 | | if (!char.IsLetter(normalized[i])) |
| | 0 | 91 | | return false; |
| | | 92 | | } |
| | | 93 | | |
| | 10 | 94 | | return true; |
| | | 95 | | } |
| | | 96 | | } |