From 22420aba2b9acf959b1a7ac2ff62a4bbab8761c6 Mon Sep 17 00:00:00 2001 From: versx Date: Sat, 26 Jun 2021 19:24:02 -0700 Subject: [PATCH] Move discord embed colors config to separate file (#175) --- examples/discords/discord1.example.json | 43 ------------------- examples/discords/discord2.example.json | 45 +------------------- src/Configuration/DiscordServerConfig.cs | 9 +--- src/Data/MasterFile.cs | 11 ++++- src/Extensions/DiscordExtensions.cs | 54 ++++++++++++------------ src/Net/Models/PokemonData.cs | 8 ++-- src/Net/Models/PokestopData.cs | 4 +- src/Net/Models/QuestData.cs | 3 +- src/Net/Models/RaidData.cs | 2 +- src/Net/Models/WeatherData.cs | 2 +- static/data/embedColors.json | 43 +++++++++++++++++++ 11 files changed, 92 insertions(+), 132 deletions(-) create mode 100644 static/data/embedColors.json diff --git a/examples/discords/discord1.example.json b/examples/discords/discord1.example.json index 7f1bc6d1..63ad3d1a 100644 --- a/examples/discords/discord1.example.json +++ b/examples/discords/discord1.example.json @@ -38,47 +38,4 @@ "botChannelIds": [], "status": null, "dmAlertsFile": "default.json", - "embedColors": { - "pokemon": { - "iv": [ - { "min": 0, "max": 0, "color": "#ffffff" }, - { "min": 1, "max": 89, "color": "#ffff00" }, - { "min": 90, "max": 99, "color": "#ffa500" }, - { "min": 100, "max": 100, "color": "#00ff00" } - ], - "pvp": [ - { "min": 1, "max": 1, "color": "#000080" }, - { "min": 6, "max": 25, "color": "#800080" }, - { "min": 25, "max": 100, "color": "#aa2299" } - ] - }, - "raids": { - "1": "#ff69b4", - "2": "#ff69b4", - "3": "#ffff00", - "4": "#ffff00", - "5": "#800080", - "6": "#a52a2a", - "ex": "#2c2f33" - }, - "pokestops": { - "quests": "#ffa500", - "lures": { - "normal": "#ff69b4", - "glacial": "#6495ed", - "mossy": "#507d2a", - "magnetic": "#808080" - }, - "invasions": "#ff0000" - }, - "weather": { - "clear": "#ffff00", - "cloudy": "#99aab5", - "fog": "#9a9a9a", - "partlyCloudy": "#808080", - "rain": "#0000ff", - "snow": "#ffffff", - "windy": "#800080" - } - } } diff --git a/examples/discords/discord2.example.json b/examples/discords/discord2.example.json index f35c5e18..a093d46e 100644 --- a/examples/discords/discord2.example.json +++ b/examples/discords/discord2.example.json @@ -37,48 +37,5 @@ "iconStyle": "Default", "botChannelIds": [], "status": "Test #2", - "dmAlertsFile": "default.json", - "embedColors": { - "pokemon": { - "iv": [ - { "min": 0, "max": 0, "color": "#ffffff" }, - { "min": 1, "max": 89, "color": "#ffff00" }, - { "min": 90, "max": 99, "color": "#ffa500" }, - { "min": 100, "max": 100, "color": "#00ff00" } - ], - "pvp": [ - { "min": 1, "max": 1, "color": "#000080" }, - { "min": 6, "max": 25, "color": "#800080" }, - { "min": 25, "max": 100, "color": "#aa2299" } - ] - }, - "raids": { - "1": "#ff69b4", - "2": "#ff69b4", - "3": "#ffff00", - "4": "#ffff00", - "5": "#800080", - "6": "#a52a2a", - "ex": "#2c2f33" - }, - "pokestops": { - "quests": "#ffa500", - "lures": { - "normal": "#ff69b4", - "glacial": "#6495ed", - "mossy": "#507d2a", - "magnetic": "#808080" - }, - "invasions": "#ff0000" - }, - "weather": { - "clear": "#ffff00", - "cloudy": "#99aab5", - "fog": "#9a9a9a", - "partlyCloudy": "#808080", - "rain": "#0000ff", - "snow": "#ffffff", - "windy": "#800080" - } - } + "dmAlertsFile": "default.json" } diff --git a/src/Configuration/DiscordServerConfig.cs b/src/Configuration/DiscordServerConfig.cs index c03a791a..130e5515 100644 --- a/src/Configuration/DiscordServerConfig.cs +++ b/src/Configuration/DiscordServerConfig.cs @@ -68,7 +68,7 @@ public class DiscordServerConfig /// Gets or sets the list of Geofence files to use for the Discord server (in addition to the common ones) /// [JsonProperty("geofences")] - public string[] GeofenceFiles { get; set; } + public List GeofenceFiles { get; set; } [JsonIgnore] public List Geofences { get; } = new List(); @@ -151,12 +151,6 @@ public class DiscordServerConfig [JsonProperty("dmAlertsFile")] public string DmAlertsFile { get; set; } - /// - /// Gets or sets the Discord embed colors to use for each message type - /// - [JsonProperty("embedColors")] - public DiscordEmbedColorConfig DiscordEmbedColors { get; set; } - /// /// Gets or sets the direct message alerts class to use for subscriptions /// @@ -176,7 +170,6 @@ public DiscordServerConfig() Subscriptions = new SubscriptionsConfig(); NestsMinimumPerHour = 1; DmAlertsFile = "default.json"; - DiscordEmbedColors = new DiscordEmbedColorConfig(); LoadDmAlerts(); } diff --git a/src/Data/MasterFile.cs b/src/Data/MasterFile.cs index 6ca952df..76d611ca 100644 --- a/src/Data/MasterFile.cs +++ b/src/Data/MasterFile.cs @@ -9,6 +9,7 @@ using POGOProtos.Rpc; using InvasionCharacter = POGOProtos.Rpc.EnumWrapper.Types.InvasionCharacter; + using WhMgr.Configuration; using WhMgr.Data.Models; using WhMgr.Diagnostics; using WhMgr.Net.Models; @@ -19,6 +20,7 @@ public class MasterFile const string CpMultipliersFileName = "cpMultipliers.json"; const string EmojisFileName = "emojis.json"; const string RarityFileName = "rarity.json"; + const string EmbedColorsFileName = "embedColors.json"; private static readonly IEventLogger _logger = EventLogger.GetLogger("MASTER", Program.LogLevel); @@ -63,6 +65,12 @@ public class MasterFile [JsonIgnore] public IReadOnlyDictionary> PokemonRarity { get; set; } + /// + /// Gets or sets the Discord embed colors to use for each message type + /// + [JsonIgnore] + public DiscordEmbedColorConfig DiscordEmbedColors { get; set; } + #region Singletons private static MasterFile _instance; @@ -89,6 +97,7 @@ public MasterFile() PokemonRarity = LoadInit>>(Path.Combine(Strings.DataFolder, RarityFileName)); Emojis = new Dictionary(); CustomEmojis = LoadInit>(Path.Combine(Strings.DataFolder, EmojisFileName)); + DiscordEmbedColors = LoadInit(Path.Combine(Strings.DataFolder, EmbedColorsFileName)); } public static PokedexPokemon GetPokemon(uint pokemonId, int formId) @@ -97,7 +106,7 @@ public static PokedexPokemon GetPokemon(uint pokemonId, int formId) return null; var pkmn = Instance.Pokedex[pokemonId]; - var useForm = !pkmn.Attack.HasValue && formId > 0 && pkmn.Forms.ContainsKey(formId); + var useForm = !pkmn.Attack.HasValue && formId > 0 && (pkmn.Forms?.ContainsKey(formId) ?? true); var pkmnForm = useForm ? pkmn.Forms[formId] : pkmn; pkmnForm.Name = pkmn.Name; return pkmnForm; diff --git a/src/Extensions/DiscordExtensions.cs b/src/Extensions/DiscordExtensions.cs index 186fd676..cee42eb2 100644 --- a/src/Extensions/DiscordExtensions.cs +++ b/src/Extensions/DiscordExtensions.cs @@ -437,28 +437,28 @@ public static async Task Confirm(this CommandContext ctx, string message) #region Colors - public static DiscordColor BuildPokemonIVColor(this string iv, DiscordServerConfig server) + public static DiscordColor BuildPokemonIVColor(this string iv, DiscordEmbedColorConfig config) { if (!double.TryParse(iv.Substring(0, iv.Length - 1), out var result)) { return DiscordColor.White; } - var color = server.DiscordEmbedColors.Pokemon.IV.FirstOrDefault(x => result >= x.Minimum && result <= x.Maximum); + var color = config.Pokemon.IV.FirstOrDefault(x => result >= x.Minimum && result <= x.Maximum); return new DiscordColor(color.Color); } - public static DiscordColor BuildPokemonPvPColor(this int rank, DiscordServerConfig server) + public static DiscordColor BuildPokemonPvPColor(this int rank, DiscordEmbedColorConfig config) { if (rank <= 0) { return DiscordColor.White; } - var color = server.DiscordEmbedColors.Pokemon.PvP.FirstOrDefault(x => rank >= x.Minimum && rank <= x.Maximum); + var color = config.Pokemon.PvP.FirstOrDefault(x => rank >= x.Minimum && rank <= x.Maximum); return new DiscordColor(color.Color); } - public static DiscordColor BuildRaidColor(this int level, DiscordServerConfig server) - { + public static DiscordColor BuildRaidColor(this int level, DiscordEmbedColorConfig config) +{ if (level == 0) { return DiscordColor.White; @@ -467,49 +467,49 @@ public static DiscordColor BuildRaidColor(this int level, DiscordServerConfig se switch (level) { case 1: - color = server.DiscordEmbedColors.Raids.Level1; + color = config.Raids.Level1; break; case 2: - color = server.DiscordEmbedColors.Raids.Level2; + color = config.Raids.Level2; break; case 3: - color = server.DiscordEmbedColors.Raids.Level3; + color = config.Raids.Level3; break; case 4: - color = server.DiscordEmbedColors.Raids.Level4; + color = config.Raids.Level4; break; case 5: - color = server.DiscordEmbedColors.Raids.Level5; + color = config.Raids.Level5; break; case 6: - color = server.DiscordEmbedColors.Raids.Level6; + color = config.Raids.Level6; break; default: - color = server.DiscordEmbedColors.Raids.Ex; + color = config.Raids.Ex; break; } return new DiscordColor(color); } - public static DiscordColor BuildLureColor(this PokestopLureType lureType, DiscordServerConfig server) + public static DiscordColor BuildLureColor(this PokestopLureType lureType, DiscordEmbedColorConfig config) { string color; switch (lureType) { case PokestopLureType.Normal: - color = server.DiscordEmbedColors.Pokestops.Lures.Normal; + color = config.Pokestops.Lures.Normal; break; case PokestopLureType.Glacial: - color = server.DiscordEmbedColors.Pokestops.Lures.Glacial; + color = config.Pokestops.Lures.Glacial; break; case PokestopLureType.Mossy: - color = server.DiscordEmbedColors.Pokestops.Lures.Mossy; + color = config.Pokestops.Lures.Mossy; break; case PokestopLureType.Magnetic: - color = server.DiscordEmbedColors.Pokestops.Lures.Magnetic; + color = config.Pokestops.Lures.Magnetic; break; case PokestopLureType.Rainy: - color = server.DiscordEmbedColors.Pokestops.Lures.Rainy; + color = config.Pokestops.Lures.Rainy; break; default: return DiscordColor.White; @@ -517,31 +517,31 @@ public static DiscordColor BuildLureColor(this PokestopLureType lureType, Discor return new DiscordColor(color); } - public static DiscordColor BuildWeatherColor(this WeatherCondition weather, DiscordServerConfig server) + public static DiscordColor BuildWeatherColor(this WeatherCondition weather, DiscordEmbedColorConfig config) { var color = "#808080"; switch (weather) { case WeatherCondition.Clear: - color = server.DiscordEmbedColors.Weather.Clear; + color = config.Weather.Clear; break; case WeatherCondition.Overcast: - color = server.DiscordEmbedColors.Weather.Cloudy; + color = config.Weather.Cloudy; break; case WeatherCondition.Fog: - color = server.DiscordEmbedColors.Weather.Fog; + color = config.Weather.Fog; break; case WeatherCondition.PartlyCloudy: - color = server.DiscordEmbedColors.Weather.PartlyCloudy; + color = config.Weather.PartlyCloudy; break; case WeatherCondition.Rainy: - color = server.DiscordEmbedColors.Weather.Rain; + color = config.Weather.Rain; break; case WeatherCondition.Snow: - color = server.DiscordEmbedColors.Weather.Snow; + color = config.Weather.Snow; break; case WeatherCondition.Windy: - color = server.DiscordEmbedColors.Weather.Windy; + color = config.Weather.Windy; break; } return new DiscordColor(color); diff --git a/src/Net/Models/PokemonData.cs b/src/Net/Models/PokemonData.cs index 52aafb82..153ca8c3 100644 --- a/src/Net/Models/PokemonData.cs +++ b/src/Net/Models/PokemonData.cs @@ -443,8 +443,8 @@ public DiscordEmbedNotification GeneratePokemonMessage(ulong guildId, DiscordCli ThumbnailUrl = DynamicReplacementEngine.ReplaceText(alert.IconUrl, properties), Description = DynamicReplacementEngine.ReplaceText(alert.Content, properties), Color = MatchesGreatLeague || MatchesUltraLeague - ? GetPvPColor(GreatLeague, UltraLeague, server) - : IV.BuildPokemonIVColor(server), + ? GetPvPColor(GreatLeague, UltraLeague, MasterFile.Instance.DiscordEmbedColors) + : IV.BuildPokemonIVColor(MasterFile.Instance.DiscordEmbedColors), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = DynamicReplacementEngine.ReplaceText(alert.Footer?.Text, properties), @@ -629,7 +629,7 @@ private IReadOnlyDictionary GetProperties(MessageProperties prop return dict; } - private DiscordColor GetPvPColor(List greatLeague, List ultraLeague, DiscordServerConfig server) + private DiscordColor GetPvPColor(List greatLeague, List ultraLeague, DiscordEmbedColorConfig config) { if (greatLeague != null) greatLeague.Sort((x, y) => (x.Rank ?? 0).CompareTo(y.Rank ?? 0)); @@ -639,7 +639,7 @@ private DiscordColor GetPvPColor(List greatLeague, List ultraL var greatRank = greatLeague.FirstOrDefault(x => x.Rank > 0 && x.Rank <= 25 && x.CP >= Strings.MinimumGreatLeagueCP && x.CP <= Strings.MaximumGreatLeagueCP); var ultraRank = ultraLeague.FirstOrDefault(x => x.Rank > 0 && x.Rank <= 25 && x.CP >= Strings.MinimumUltraLeagueCP && x.CP <= Strings.MaximumUltraLeagueCP); - var color = server.DiscordEmbedColors.Pokemon.PvP.FirstOrDefault(x => ((greatRank?.Rank ?? 0) >= x.Minimum && (greatRank?.Rank ?? 0) <= x.Maximum) || ((ultraRank?.Rank ?? 0) >= x.Minimum && (ultraRank?.Rank ?? 0) <= x.Maximum)); + var color = config.Pokemon.PvP.FirstOrDefault(x => ((greatRank?.Rank ?? 0) >= x.Minimum && (greatRank?.Rank ?? 0) <= x.Maximum) || ((ultraRank?.Rank ?? 0) >= x.Minimum && (ultraRank?.Rank ?? 0) <= x.Maximum)); if (color == null) { return DiscordColor.White; diff --git a/src/Net/Models/PokestopData.cs b/src/Net/Models/PokestopData.cs index 689f57c1..6585fb42 100644 --- a/src/Net/Models/PokestopData.cs +++ b/src/Net/Models/PokestopData.cs @@ -122,9 +122,9 @@ public DiscordEmbedNotification GeneratePokestopMessage(ulong guildId, DiscordCl ThumbnailUrl = DynamicReplacementEngine.ReplaceText(alert.IconUrl, properties), Description = DynamicReplacementEngine.ReplaceText(alert.Content, properties), Color = useInvasion - ? new DiscordColor(server.DiscordEmbedColors.Pokestops.Invasions) + ? new DiscordColor(MasterFile.Instance.DiscordEmbedColors.Pokestops.Invasions) : useLure - ? LureType.BuildLureColor(server) + ? LureType.BuildLureColor(MasterFile.Instance.DiscordEmbedColors) : DiscordColor.CornflowerBlue, Footer = new DiscordEmbedBuilder.EmbedFooter { diff --git a/src/Net/Models/QuestData.cs b/src/Net/Models/QuestData.cs index 32fe7aa1..e848061c 100644 --- a/src/Net/Models/QuestData.cs +++ b/src/Net/Models/QuestData.cs @@ -15,6 +15,7 @@ using WhMgr.Alarms.Alerts; using WhMgr.Alarms.Models; using WhMgr.Configuration; + using WhMgr.Data; using WhMgr.Extensions; using WhMgr.Geofence; using WhMgr.Services; @@ -103,7 +104,7 @@ public DiscordEmbedNotification GenerateQuestMessage(ulong guildId, DiscordClien ImageUrl = DynamicReplacementEngine.ReplaceText(alert.ImageUrl, properties), ThumbnailUrl = DynamicReplacementEngine.ReplaceText(alert.IconUrl, properties), Description = DynamicReplacementEngine.ReplaceText(alert.Content, properties), - Color = new DiscordColor(server.DiscordEmbedColors.Pokestops.Quests), + Color = new DiscordColor(MasterFile.Instance.DiscordEmbedColors.Pokestops.Quests), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = DynamicReplacementEngine.ReplaceText(alert.Footer?.Text, properties), diff --git a/src/Net/Models/RaidData.cs b/src/Net/Models/RaidData.cs index 4254cd37..5d7c2235 100644 --- a/src/Net/Models/RaidData.cs +++ b/src/Net/Models/RaidData.cs @@ -166,7 +166,7 @@ public DiscordEmbedNotification GenerateRaidMessage(ulong guildId, DiscordClient ImageUrl = DynamicReplacementEngine.ReplaceText(alert.ImageUrl, properties), ThumbnailUrl = DynamicReplacementEngine.ReplaceText(alert.IconUrl, properties), Description = DynamicReplacementEngine.ReplaceText(alert.Content, properties), - Color = (IsExEligible ? 0 /*ex*/ : int.Parse(Level)).BuildRaidColor(server), + Color = (IsExEligible ? 0 /*ex*/ : int.Parse(Level)).BuildRaidColor(MasterFile.Instance.DiscordEmbedColors), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = DynamicReplacementEngine.ReplaceText(alert.Footer?.Text, properties), diff --git a/src/Net/Models/WeatherData.cs b/src/Net/Models/WeatherData.cs index e7654d9c..ffa53b5f 100644 --- a/src/Net/Models/WeatherData.cs +++ b/src/Net/Models/WeatherData.cs @@ -123,7 +123,7 @@ public DiscordEmbedNotification GenerateWeatherMessage(ulong guildId, DiscordCli ImageUrl = DynamicReplacementEngine.ReplaceText(alert.ImageUrl, properties), ThumbnailUrl = DynamicReplacementEngine.ReplaceText(alert.IconUrl, properties), Description = DynamicReplacementEngine.ReplaceText(alert.Content, properties), - Color = GameplayCondition.BuildWeatherColor(server), + Color = GameplayCondition.BuildWeatherColor(MasterFile.Instance.DiscordEmbedColors), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = DynamicReplacementEngine.ReplaceText(alert.Footer?.Text, properties), diff --git a/static/data/embedColors.json b/static/data/embedColors.json new file mode 100644 index 00000000..09975464 --- /dev/null +++ b/static/data/embedColors.json @@ -0,0 +1,43 @@ +{ + "pokemon": { + "iv": [ + { "min": 0, "max": 0, "color": "#ffffff" }, + { "min": 1, "max": 89, "color": "#ffff00" }, + { "min": 90, "max": 99, "color": "#ffa500" }, + { "min": 100, "max": 100, "color": "#00ff00" } + ], + "pvp": [ + { "min": 1, "max": 1, "color": "#000080" }, + { "min": 2, "max": 3, "color": "#800080" }, + { "min": 4, "max": 100, "color": "#aa2299" } + ] + }, + "raids": { + "1": "#ff69b4", + "2": "#ff69b4", + "3": "#ffff00", + "4": "#ffff00", + "5": "#800080", + "6": "#a52a2a", + "ex": "#2c2f33" + }, + "pokestops": { + "quests": "#ffa500", + "lures": { + "normal": "#ff69b4", + "glacial": "#6495ed", + "mossy": "#507d2a", + "magnetic": "#808080" + }, + "invasions": "#ff0000" + }, + "weather": { + "clear": "#ffff00", + "cloudy": "#99aab5", + "fog": "#9a9a9a", + "partlyCloudy": "#808080", + "rain": "#0000ff", + "snow": "#ffffff", + "windy": "#800080" + } +} \ No newline at end of file