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

View File

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