Skip to content

Commit

Permalink
Refactor for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
versx committed Dec 14, 2020
1 parent 130eec5 commit 4e129c2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 208 deletions.
16 changes: 4 additions & 12 deletions src/Alarms/Alerts/DynamicReplacementEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

public static class DynamicReplacementEngine
Expand All @@ -19,23 +18,16 @@ public static string ReplaceText(string alarmText, IReadOnlyDictionary<string, s
return string.Empty;

var placeHolder = alarmText;
var keys = pkmnInfo.Keys.ToList();

//Loop through all available keys, replace any place holders with values.
for (var i = 0; i < keys.Count; i++)
// Loop through all available keys, replace any place holders with values.
foreach (var (key, value) in pkmnInfo)
{
var key = keys[i];
var value = pkmnInfo[key];

placeHolder = placeHolder.Replace($"<{key}>", value);
}

//Replace IF statement blocks i.e. <#is_ditto>**Catch Pokemon:** <original_pkmn_name></is_ditto>. If value is true return value inside IF block, otherwise return an empty string.
for (var i = 0; i < keys.Count; i++)
// Replace IF statement blocks i.e. <#is_ditto>**Catch Pokemon:** <original_pkmn_name></is_ditto>. If value is true return value inside IF block, otherwise return an empty string.
foreach (var (key, value) in pkmnInfo)
{
var key = keys[i];
var value = pkmnInfo[key];

if (bool.TryParse(value, out var result))
{
placeHolder = ReplaceBlock(placeHolder, key, result);
Expand Down
74 changes: 22 additions & 52 deletions src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,15 @@ public Bot(WhConfig whConfig)
}

// Create a DiscordClient object per Discord server in config
var keys = _whConfig.Servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, serverConfig) in _whConfig.Servers)
{
var guildId = keys[i];
var server = _whConfig.Servers[guildId];
server.LoadDmAlerts();
serverConfig.LoadDmAlerts();
var client = new DiscordClient(new DiscordConfiguration
{
AutomaticGuildSync = true,
AutoReconnect = true,
EnableCompression = true,
Token = server.Token,
Token = serverConfig.Token,
TokenType = TokenType.Bot,
UseInternalLogHandler = true
});
Expand Down Expand Up @@ -162,10 +159,10 @@ public Bot(WhConfig whConfig)
(
new CommandsNextConfiguration
{
StringPrefix = server.CommandPrefix?.ToString(),
StringPrefix = serverConfig.CommandPrefix?.ToString(),
EnableDms = true,
// If command prefix is null, allow for mention prefix
EnableMentionPrefix = string.IsNullOrEmpty(server.CommandPrefix),
EnableMentionPrefix = string.IsNullOrEmpty(serverConfig.CommandPrefix),
// Use DSharpPlus's built-in help formatter
EnableDefaultHelp = true,
CaseSensitive = false,
Expand All @@ -183,11 +180,11 @@ public Bot(WhConfig whConfig)
commands.RegisterCommands<Gyms>();
commands.RegisterCommands<Quests>();
commands.RegisterCommands<Settings>();
if (server.Subscriptions.Enabled)
if (serverConfig.Subscriptions.Enabled)
{
commands.RegisterCommands<Notifications>();
}
if (server.EnableCities)
if (serverConfig.EnableCities)
{
commands.RegisterCommands<Feeds>();
}
Expand Down Expand Up @@ -219,12 +216,8 @@ public async Task Start()
_logger.Info("Connecting to Discord...");

// Loop through each Discord server and attempt initial connection
var keys = _servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, client) in _servers)
{
var guildId = keys[i];
var client = _servers[guildId];

_logger.Info($"Attempting connection to Discord server {guildId}");
await client.ConnectAsync();
await Task.Delay(1000);
Expand Down Expand Up @@ -262,13 +255,9 @@ public async Task Stop()
_logger.Info("Disconnecting from Discord...");

// Loop through each Discord server and terminate the connection
var keys = _servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, client) in _servers)
{
var guildId = keys[i];
var client = _servers[guildId];

_logger.Info($"Attempting connection to Discord server {guildId}");
_logger.Info($"Attempting disconnection from Discord server {guildId}");
await client.DisconnectAsync();
await Task.Delay(1000);
}
Expand Down Expand Up @@ -907,11 +896,9 @@ private async Task CacheGuildEmojisList()
{
_logger.Trace($"LoadEmojis");

var keys = _whConfig.Servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, serverConfig) in _whConfig.Servers)
{
var guildId = keys[i];
var emojiGuildId = _whConfig.Servers[guildId].EmojiGuildId;
var emojiGuildId = serverConfig.EmojiGuildId;
if (!_servers.ContainsKey(guildId))
continue;

Expand All @@ -921,9 +908,8 @@ private async Task CacheGuildEmojisList()

var emojiGuild = configGuild.Guilds[emojiGuildId];
var emojis = await emojiGuild.GetEmojisAsync();
for (var j = 0; j < Strings.EmojiList.Length; j++)
foreach (var name in Strings.EmojiList)
{
var name = Strings.EmojiList[j];
var emoji = emojis.FirstOrDefault(x => string.Compare(x.Name, name, true) == 0);
if (emoji == null)
continue;
Expand All @@ -944,32 +930,28 @@ private async Task OnMidnightTimer()
Statistics.WriteOut();
Statistics.Instance.Reset();

var keys = _whConfig.Servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, serverConfig) in _whConfig.Servers)
{
var guildId = keys[i];
var server = _whConfig.Servers[guildId];

if (!_servers.ContainsKey(guildId))
{
_logger.Warn($"{guildId} guild does not exist it Discord servers.");
continue;
}
var client = _servers[guildId];
if (server.ShinyStats.Enabled)
if (serverConfig.ShinyStats.Enabled)
{
_logger.Debug($"Starting Shiny Stat posting...");
await PostShinyStats(client, guildId, server);
await PostShinyStats(client, guildId, serverConfig);
}
else
{
_logger.Debug($"Shiny Stat posting not enabled...skipping");
}

if (server.PruneQuestChannels && server.QuestChannelIds.Count > 0)
if (serverConfig.PruneQuestChannels && serverConfig.QuestChannelIds.Count > 0)
{
_logger.Debug($"Starting automatic quest messages cleanup...");
await PruneQuestChannels(client, server);
await PruneQuestChannels(client, serverConfig);
}
else
{
Expand Down Expand Up @@ -1066,11 +1048,8 @@ private void CleanupDepartedMembers()
{
_logger.Trace("CleanupDepartedMembers");

var keys = _servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, client) in _servers)
{
var guildId = keys[i];
var client = _servers[guildId];
var server = _whConfig.Servers[guildId];
if (!server.Subscriptions.Enabled)
return;
Expand Down Expand Up @@ -1121,17 +1100,8 @@ private async void UnhandledExceptionHandler(object sender, UnhandledExceptionEv

if (e.IsTerminating)
{
var keys = _whConfig.Servers.Keys.ToList();
for (var i = 0; i < keys.Count; i++)
foreach (var (guildId, serverConfig) in _whConfig.Servers)
{
var guildId = keys[i];
if (!_whConfig.Servers.ContainsKey(guildId))
{
_logger.Error($"Unable to find guild id {guildId} in server config list.");
continue;
}
var server = _whConfig.Servers[guildId];

if (!_servers.ContainsKey(guildId))
{
_logger.Error($"Unable to find guild id {guildId} in Discord server client list.");
Expand All @@ -1140,10 +1110,10 @@ private async void UnhandledExceptionHandler(object sender, UnhandledExceptionEv
var client = _servers[guildId];
if (client != null)
{
var owner = await client.GetUserAsync(server.OwnerId);
var owner = await client.GetUserAsync(serverConfig.OwnerId);
if (owner == null)
{
_logger.Warn($"Unable to get owner from id {server.OwnerId}.");
_logger.Warn($"Unable to get owner from id {serverConfig.OwnerId}.");
return;
}

Expand Down
Loading

0 comments on commit 4e129c2

Please sign in to comment.