Skip to content

Commit

Permalink
Move discord embed colors config to separate file (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
versx authored Jun 27, 2021
1 parent e5222d0 commit 22420ab
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 132 deletions.
43 changes: 0 additions & 43 deletions examples/discords/discord1.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
45 changes: 1 addition & 44 deletions examples/discords/discord2.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
9 changes: 1 addition & 8 deletions src/Configuration/DiscordServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// </summary>
[JsonProperty("geofences")]
public string[] GeofenceFiles { get; set; }
public List<string> GeofenceFiles { get; set; }

[JsonIgnore]
public List<GeofenceItem> Geofences { get; } = new List<GeofenceItem>();
Expand Down Expand Up @@ -151,12 +151,6 @@ public class DiscordServerConfig
[JsonProperty("dmAlertsFile")]
public string DmAlertsFile { get; set; }

/// <summary>
/// Gets or sets the Discord embed colors to use for each message type
/// </summary>
[JsonProperty("embedColors")]
public DiscordEmbedColorConfig DiscordEmbedColors { get; set; }

/// <summary>
/// Gets or sets the direct message alerts class to use for subscriptions
/// </summary>
Expand All @@ -176,7 +170,6 @@ public DiscordServerConfig()
Subscriptions = new SubscriptionsConfig();
NestsMinimumPerHour = 1;
DmAlertsFile = "default.json";
DiscordEmbedColors = new DiscordEmbedColorConfig();

LoadDmAlerts();
}
Expand Down
11 changes: 10 additions & 1 deletion src/Data/MasterFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -63,6 +65,12 @@ public class MasterFile
[JsonIgnore]
public IReadOnlyDictionary<PokemonRarity, List<uint>> PokemonRarity { get; set; }

/// <summary>
/// Gets or sets the Discord embed colors to use for each message type
/// </summary>
[JsonIgnore]
public DiscordEmbedColorConfig DiscordEmbedColors { get; set; }

#region Singletons

private static MasterFile _instance;
Expand All @@ -89,6 +97,7 @@ public MasterFile()
PokemonRarity = LoadInit<Dictionary<PokemonRarity, List<uint>>>(Path.Combine(Strings.DataFolder, RarityFileName));
Emojis = new Dictionary<string, ulong>();
CustomEmojis = LoadInit<Dictionary<string, string>>(Path.Combine(Strings.DataFolder, EmojisFileName));
DiscordEmbedColors = LoadInit<DiscordEmbedColorConfig>(Path.Combine(Strings.DataFolder, EmbedColorsFileName));
}

public static PokedexPokemon GetPokemon(uint pokemonId, int formId)
Expand All @@ -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;
Expand Down
54 changes: 27 additions & 27 deletions src/Extensions/DiscordExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,28 +437,28 @@ public static async Task<bool> 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;
Expand All @@ -467,81 +467,81 @@ 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;
}
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);
Expand Down
8 changes: 4 additions & 4 deletions src/Net/Models/PokemonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -629,7 +629,7 @@ private IReadOnlyDictionary<string, string> GetProperties(MessageProperties prop
return dict;
}

private DiscordColor GetPvPColor(List<PVPRank> greatLeague, List<PVPRank> ultraLeague, DiscordServerConfig server)
private DiscordColor GetPvPColor(List<PVPRank> greatLeague, List<PVPRank> ultraLeague, DiscordEmbedColorConfig config)
{
if (greatLeague != null)
greatLeague.Sort((x, y) => (x.Rank ?? 0).CompareTo(y.Rank ?? 0));
Expand All @@ -639,7 +639,7 @@ private DiscordColor GetPvPColor(List<PVPRank> greatLeague, List<PVPRank> 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;
Expand Down
4 changes: 2 additions & 2 deletions src/Net/Models/PokestopData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 2 additions & 1 deletion src/Net/Models/QuestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/Net/Models/RaidData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/Net/Models/WeatherData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading

0 comments on commit 22420ab

Please sign in to comment.