Skip to content

Commit b9b0e1e

Browse files
authored
Configurable StaticMaps Toggle (#290)
* - Add static map toggle to config. - Add reusable location links generator. - Update internal default embeds example. - Let user know if emoji does not exist. - Fix locales directory path. * Update docs
1 parent 6e5cfef commit b9b0e1e

File tree

24 files changed

+352
-417
lines changed

24 files changed

+352
-417
lines changed

docs/dts/pokemon.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ Use any of the following in your embeds file to structure how notifications will
2626
| moveset | Fast & Charge move names | Quick Attack/Thunder
2727
| type_1 | Pokemon type | Dark
2828
| type_2 | Pokemon type | Water
29-
| type_1_emoji | Pokemon type emoji | <:00000:types_water>
30-
| type_2_emoji | Pokemon type emoji | <:00000:types_rock>
31-
| types | Both types (if 2nd exists) | Dark/Fire
29+
| types | Both Pokemon types (if 2nd exists) | Dark/Fire
3230
| types_emoji | Type Discord emoji | <:00000:types_fire> <00001:types_dark>
3331
| atk_iv | Attack IV stat | 15
3432
| def_iv | Defense IV stat | 7

docs/dts/raids.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ Use any of the following in your embeds file to structure how notifications will
2929
| moveset | Fast & Charge move names | Quick Attack/Thunder
3030
| type_1 | Pokemon type | Dark
3131
| type_2 | Pokemon type | Water
32-
| type_1_emoji | Pokemon type emoji | <:00000:types_water>
33-
| type_2_emoji | Pokemon type emoji | <:00000:types_rock>
34-
| types | Both types (if 2nd exists) | Dark/Fire
32+
| types | Both Pokemon types (if 2nd exists) | Dark/Fire
3533
| types_emoji | Type Discord emoji | <:00000:types_fire> <00001:types_dark>
3634
| weaknesses | Raid boss weaknesses | Rock, Ground, Dark
3735
| weaknesses_emoji | Emoji(s) of raid boss weaknesses | Rock Ground Dark

examples/configs/config.example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
}
101101
},
102102
"staticMaps": {
103+
"enabled": false,
103104
"url": "http://tiles.example.com:8080",
104105
// StaticMap or MultiStaticMap
105106
"type": "StaticMap",

examples/embeds/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"{{#if power_up_level}}**Power Level**",
5555
"Level: {{power_up_level}} | Points: {{power_up_points}}",
5656
"Time Left: {{power_up_end_time_left}}",
57-
"{{/if}}{{#if is_ex}}{{ex_gym_emoji}} Gym!",
57+
"{{/if}}{{#if is_ex}}{{ex_emoji}} Gym!",
5858
"{{/if}}**[Google]({{gmaps_url}}) | [Apple]({{applemaps_url}}) | [Waze]({{wazemaps_url}}) | [Scanner]({{scanmaps_url}})**"
5959
],
6060
"iconUrl": "{{gym_url}}",

src/Commands/Discord/Nests.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using WhMgr.Services;
2525
using WhMgr.Services.Alarms.Embeds;
2626
using WhMgr.Services.Geofence;
27+
using WhMgr.Services.Geofence.Geocoding;
2728
using WhMgr.Services.Icons;
2829
using WhMgr.Services.StaticMap;
2930

@@ -197,41 +198,36 @@ public DiscordEmbed GenerateEmbedMessage(ulong guildId, DiscordClient client, Ne
197198

198199
public dynamic GetProperties(DiscordGuild guild, Nest nest, string pokemonImageUrl)
199200
{
201+
var config = _config.Instance;
200202
var pkmnInfo = GameMaster.GetPokemon(nest.PokemonId);
201203
var pkmnImage = pokemonImageUrl;
202204
var nestName = nest.Name ?? "Unknown";
203-
var type1 = pkmnInfo?.Types?[0];
204-
var type2 = pkmnInfo?.Types?.Count > 1 ? pkmnInfo.Types?[1] : PokemonType.None;
205-
var type1Emoji = pkmnInfo?.Types?[0].GetTypeEmojiIcons();
206-
var type2Emoji = pkmnInfo?.Types?.Count > 1 ? pkmnInfo?.Types?[1].GetTypeEmojiIcons() : string.Empty;
207-
var typeEmojis = $"{type1Emoji} {type2Emoji}";
205+
var types = pkmnInfo?.Types;
206+
var type1 = types?.Count >= 1
207+
? types[0]
208+
: PokemonType.None;
209+
var type2 = types?.Count > 1
210+
? types[1]
211+
: PokemonType.None;
212+
var typeEmojis = types?.GetTypeEmojiIcons() ?? string.Empty;
208213
var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, nest.Latitude, nest.Longitude);
209214
var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, nest.Latitude, nest.Longitude);
210215
var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, nest.Latitude, nest.Longitude);
211-
var scannerMapsLink = string.Format(_config.Instance.Urls.ScannerMap, nest.Latitude, nest.Longitude);
216+
var scannerMapsLink = string.Format(config.Urls.ScannerMap, nest.Latitude, nest.Longitude);
217+
var address = ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(nest)).Result;
212218

213-
//pkmnImage,
214219
var osmNest = _osmManager.GetNest(nest.Name)?.FirstOrDefault();
215220
var polygonPath = OsmManager.MultiPolygonToLatLng(osmNest?.Geometry?.Coordinates, true);
216-
var staticMapConfig = _config.Instance.StaticMaps;
217-
var staticMap = new StaticMapGenerator(new StaticMapOptions
218-
{
219-
BaseUrl = staticMapConfig.Url,
220-
MapType = StaticMapType.Nests,
221-
TemplateType = staticMapConfig.Type == StaticMapTemplateType.StaticMap
222-
? StaticMapTemplateType.StaticMap
223-
: StaticMapTemplateType.MultiStaticMap,
224-
Latitude = nest.Latitude,
225-
Longitude = nest.Longitude,
226-
SecondaryImageUrl = pokemonImageUrl,
227-
PolygonPath = polygonPath,
228-
Pregenerate = staticMapConfig.Pregenerate,
229-
Regeneratable = true,
230-
});
231-
var staticMapLink = staticMap.GenerateLink();
232-
var geofence = GeofenceService.GetGeofence(_config.Instance.Servers[guild.Id].Geofences, new Coordinate(nest));
221+
var staticMapLink = config.StaticMaps?.GenerateStaticMap(
222+
StaticMapType.Nests,
223+
nest,
224+
pokemonImageUrl,
225+
null,
226+
null,
227+
polygonPath
228+
);
229+
var geofence = GeofenceService.GetGeofence(config.Servers[guild.Id].Geofences, new Coordinate(nest));
233230
var city = geofence?.Name ?? "Unknown";
234-
//var address = new Coordinate(city, nest.Latitude, nest.Longitude).GetAddress(_config.Instance);
235231

236232
var dict = new
237233
{
@@ -244,8 +240,6 @@ public dynamic GetProperties(DiscordGuild guild, Nest nest, string pokemonImageU
244240
nest_name = nestName,
245241
type_1 = Convert.ToString(type1),
246242
type_2 = Convert.ToString(type2),
247-
type_1_emoji = type1Emoji,
248-
type_2_emoji = type2Emoji,
249243
types = $"{type1} | {type2}",
250244
types_emojis = typeEmojis,
251245

@@ -265,7 +259,7 @@ public dynamic GetProperties(DiscordGuild guild, Nest nest, string pokemonImageU
265259
wazemaps_url = wazeMapsLink,
266260
scanmaps_url = scannerMapsLink,
267261

268-
//address = address?.Address,
262+
address = address ?? string.Empty,
269263

270264
// Discord Guild properties
271265
guild_name = guild?.Name,

src/Configuration/Config.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using WhMgr.Data;
1212
using WhMgr.Extensions;
1313
using WhMgr.Services.Icons;
14-
using WhMgr.Services.StaticMap;
1514

1615
/// <summary>
1716
/// Configuration file class

src/Configuration/StaticMapConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
public class StaticMapConfig
88
{
9+
[JsonPropertyName("enabled")]
10+
public bool Enabled { get; set; } = true;
11+
912
[JsonPropertyName("url")]
1013
public string Url { get; set; }
1114

src/Defaults.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public Defaults()
115115
// Ex gym emoji
116116
"ex",
117117

118+
// AR fort emoji
119+
"ar",
120+
118121
// Type emojis
119122
"types_fire",
120123
"types_grass",

src/Extensions/DateTimeExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using GeoTimeZone;
66
using TimeZoneConverter;
77

8+
using WhMgr.Services.Webhook.Models;
9+
810
public static class DateTimeExtensions
911
{
1012
public static TimeSpan GetTimeRemaining(this DateTime startTime, DateTime endTime)
@@ -13,6 +15,11 @@ public static TimeSpan GetTimeRemaining(this DateTime startTime, DateTime endTim
1315
return remaining;
1416
}
1517

18+
public static DateTime ConvertTimeFromCoordinates(this DateTime date, IWebhookPoint coord)
19+
{
20+
return ConvertTimeFromCoordinates(date, coord.Latitude, coord.Longitude);
21+
}
22+
1623
public static DateTime ConvertTimeFromCoordinates(this DateTime date, double lat, double lon)
1724
{
1825
var tzIana = TimeZoneLookup.GetTimeZone(lat, lon).Result;

src/Extensions/PokemonExtensions.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,33 @@ public static string GetTypeEmojiIcons(this IEnumerable<PokemonType> pokemonType
100100
return string.Join(" ", list);
101101
}
102102

103-
public static string GetEmojiIcon<T>(this T type, string keyPrefix, bool asString)
103+
public static string GetEmojiIcon<T>(this T type, string keyPrefix, bool asString, string defaultValue = null)
104104
{
105105
var value = asString ? type.ToString().ToLower() : Convert.ToInt32(type).ToString();
106106
var key = string.IsNullOrEmpty(keyPrefix) ? value : $"{keyPrefix}_{value}";
107107
var emojiId = GameMaster.Instance.Emojis.ContainsKey(key)
108108
? GameMaster.Instance.Emojis[key]
109109
: 0;
110-
var emojiName = string.IsNullOrEmpty(GameMaster.Instance.CustomEmojis[key])
110+
111+
if (emojiId == 0)
112+
{
113+
Console.WriteLine($"Emoji '{key}' does not exist! Using fallback text.");
114+
}
115+
116+
// Check if custom emoji list contains specified emoji string or if custom emoji is not overwritten.
117+
var emojiName = !GameMaster.Instance.CustomEmojis.ContainsKey(key) || string.IsNullOrEmpty(GameMaster.Instance.CustomEmojis[key])
118+
// Check if we retrieved Discord emoji successfully
111119
? emojiId > 0
120+
// Construct Discord emoji string
112121
? string.Format(Strings.Defaults.EmojiSchema, key, emojiId)
122+
// Fallback to text instead of emoji
113123
: type.ToString()
124+
// Custom emoji is set which overwrites Discord emojis
114125
: GameMaster.Instance.CustomEmojis[key] ?? type.ToString();
115-
return emojiName;
126+
var result = !string.IsNullOrEmpty(emojiName)
127+
? emojiName
128+
: defaultValue;
129+
return result;
116130
}
117131

118132
public static string GetWeaknessEmojiIcons(this List<PokemonType> pokemonTypes)

0 commit comments

Comments
 (0)