Merge pull request #5 from PassiveModding/main

Avoid multiple instantiations of Random
This commit is contained in:
rootdarkarchon
2022-08-03 19:10:02 +02:00
committed by GitHub
2 changed files with 4 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ namespace MareSynchronosServer.Discord
private readonly IServiceProvider services; private readonly IServiceProvider services;
private readonly IConfiguration configuration; private readonly IConfiguration configuration;
private readonly ILogger<DiscordBot> logger; private readonly ILogger<DiscordBot> logger;
private readonly Random random;
private string authToken = string.Empty; private string authToken = string.Empty;
DiscordSocketClient discordClient; DiscordSocketClient discordClient;
ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new(); ConcurrentDictionary<ulong, string> DiscordLodestoneMapping = new();
@@ -39,6 +40,7 @@ namespace MareSynchronosServer.Discord
this.configuration = configuration; this.configuration = configuration;
this.logger = logger; this.logger = logger;
random = new();
authToken = configuration.GetValue<string>("DiscordBotToken"); authToken = configuration.GetValue<string>("DiscordBotToken");
discordClient = new(new DiscordSocketConfig() discordClient = new(new DiscordSocketConfig()
@@ -131,8 +133,7 @@ namespace MareSynchronosServer.Discord
var lodestoneAuth = db.LodeStoneAuth.SingleOrDefault(u => u.DiscordId == id); var lodestoneAuth = db.LodeStoneAuth.SingleOrDefault(u => u.DiscordId == id);
if (lodestoneAuth != null && DiscordLodestoneMapping.ContainsKey(id)) if (lodestoneAuth != null && DiscordLodestoneMapping.ContainsKey(id))
{ {
Random rand = new(); var randomServer = LodestoneServers[random.Next(LodestoneServers.Length)];
var randomServer = LodestoneServers[rand.Next(LodestoneServers.Length)];
var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{DiscordLodestoneMapping[id]}"); var response = await req.GetAsync($"https://{randomServer}.finalfantasyxiv.com/lodestone/character/{DiscordLodestoneMapping[id]}");
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {

View File

@@ -133,9 +133,7 @@ namespace MareSynchronosServer.Hubs
allowableChars = @"ABCDEFGHJKLMNPQRSTUVWXYZ0123456789"; allowableChars = @"ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
// Generate random data // Generate random data
var rnd = new byte[length]; var rnd = RandomNumberGenerator.GetBytes(length);
using (var rng = new RNGCryptoServiceProvider())
rng.GetBytes(rnd);
// Generate the output string // Generate the output string
var allowable = allowableChars.ToCharArray(); var allowable = allowableChars.ToCharArray();