diff --git a/docs/dts/pokemon.md b/docs/dts/pokemon.md index 2a180767..a05cbc6c 100644 --- a/docs/dts/pokemon.md +++ b/docs/dts/pokemon.md @@ -26,9 +26,7 @@ Use any of the following in your embeds file to structure how notifications will | moveset | Fast & Charge move names | Quick Attack/Thunder | type_1 | Pokemon type | Dark | type_2 | Pokemon type | Water -| type_1_emoji | Pokemon type emoji | <:00000:types_water> -| type_2_emoji | Pokemon type emoji | <:00000:types_rock> -| types | Both types (if 2nd exists) | Dark/Fire +| types | Both Pokemon types (if 2nd exists) | Dark/Fire | types_emoji | Type Discord emoji | <:00000:types_fire> <00001:types_dark> | atk_iv | Attack IV stat | 15 | def_iv | Defense IV stat | 7 diff --git a/docs/dts/raids.md b/docs/dts/raids.md index d3c0479f..7e594287 100644 --- a/docs/dts/raids.md +++ b/docs/dts/raids.md @@ -29,9 +29,7 @@ Use any of the following in your embeds file to structure how notifications will | moveset | Fast & Charge move names | Quick Attack/Thunder | type_1 | Pokemon type | Dark | type_2 | Pokemon type | Water -| type_1_emoji | Pokemon type emoji | <:00000:types_water> -| type_2_emoji | Pokemon type emoji | <:00000:types_rock> -| types | Both types (if 2nd exists) | Dark/Fire +| types | Both Pokemon types (if 2nd exists) | Dark/Fire | types_emoji | Type Discord emoji | <:00000:types_fire> <00001:types_dark> | weaknesses | Raid boss weaknesses | Rock, Ground, Dark | weaknesses_emoji | Emoji(s) of raid boss weaknesses | Rock Ground Dark diff --git a/examples/configs/config.example.json b/examples/configs/config.example.json index c8a8e5b8..922296f2 100644 --- a/examples/configs/config.example.json +++ b/examples/configs/config.example.json @@ -100,6 +100,7 @@ } }, "staticMaps": { + "enabled": false, "url": "http://tiles.example.com:8080", // StaticMap or MultiStaticMap "type": "StaticMap", diff --git a/examples/embeds/default.json b/examples/embeds/default.json index 94f9a927..f716342e 100644 --- a/examples/embeds/default.json +++ b/examples/embeds/default.json @@ -54,7 +54,7 @@ "{{#if power_up_level}}**Power Level**", "Level: {{power_up_level}} | Points: {{power_up_points}}", "Time Left: {{power_up_end_time_left}}", - "{{/if}}{{#if is_ex}}{{ex_gym_emoji}} Gym!", + "{{/if}}{{#if is_ex}}{{ex_emoji}} Gym!", "{{/if}}**[Google]({{gmaps_url}}) | [Apple]({{applemaps_url}}) | [Waze]({{wazemaps_url}}) | [Scanner]({{scanmaps_url}})**" ], "iconUrl": "{{gym_url}}", diff --git a/src/Commands/Discord/Nests.cs b/src/Commands/Discord/Nests.cs index cded75d5..1b6da372 100644 --- a/src/Commands/Discord/Nests.cs +++ b/src/Commands/Discord/Nests.cs @@ -24,6 +24,7 @@ using WhMgr.Services; using WhMgr.Services.Alarms.Embeds; using WhMgr.Services.Geofence; + using WhMgr.Services.Geofence.Geocoding; using WhMgr.Services.Icons; using WhMgr.Services.StaticMap; @@ -197,41 +198,36 @@ public DiscordEmbed GenerateEmbedMessage(ulong guildId, DiscordClient client, Ne public dynamic GetProperties(DiscordGuild guild, Nest nest, string pokemonImageUrl) { + var config = _config.Instance; var pkmnInfo = GameMaster.GetPokemon(nest.PokemonId); var pkmnImage = pokemonImageUrl; var nestName = nest.Name ?? "Unknown"; - var type1 = pkmnInfo?.Types?[0]; - var type2 = pkmnInfo?.Types?.Count > 1 ? pkmnInfo.Types?[1] : PokemonType.None; - var type1Emoji = pkmnInfo?.Types?[0].GetTypeEmojiIcons(); - var type2Emoji = pkmnInfo?.Types?.Count > 1 ? pkmnInfo?.Types?[1].GetTypeEmojiIcons() : string.Empty; - var typeEmojis = $"{type1Emoji} {type2Emoji}"; + var types = pkmnInfo?.Types; + var type1 = types?.Count >= 1 + ? types[0] + : PokemonType.None; + var type2 = types?.Count > 1 + ? types[1] + : PokemonType.None; + var typeEmojis = types?.GetTypeEmojiIcons() ?? string.Empty; var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, nest.Latitude, nest.Longitude); var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, nest.Latitude, nest.Longitude); var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, nest.Latitude, nest.Longitude); - var scannerMapsLink = string.Format(_config.Instance.Urls.ScannerMap, nest.Latitude, nest.Longitude); + var scannerMapsLink = string.Format(config.Urls.ScannerMap, nest.Latitude, nest.Longitude); + var address = ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(nest)).Result; - //pkmnImage, var osmNest = _osmManager.GetNest(nest.Name)?.FirstOrDefault(); var polygonPath = OsmManager.MultiPolygonToLatLng(osmNest?.Geometry?.Coordinates, true); - var staticMapConfig = _config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = StaticMapType.Nests, - TemplateType = staticMapConfig.Type == StaticMapTemplateType.StaticMap - ? StaticMapTemplateType.StaticMap - : StaticMapTemplateType.MultiStaticMap, - Latitude = nest.Latitude, - Longitude = nest.Longitude, - SecondaryImageUrl = pokemonImageUrl, - PolygonPath = polygonPath, - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var geofence = GeofenceService.GetGeofence(_config.Instance.Servers[guild.Id].Geofences, new Coordinate(nest)); + var staticMapLink = config.StaticMaps?.GenerateStaticMap( + StaticMapType.Nests, + nest, + pokemonImageUrl, + null, + null, + polygonPath + ); + var geofence = GeofenceService.GetGeofence(config.Servers[guild.Id].Geofences, new Coordinate(nest)); var city = geofence?.Name ?? "Unknown"; - //var address = new Coordinate(city, nest.Latitude, nest.Longitude).GetAddress(_config.Instance); var dict = new { @@ -244,8 +240,6 @@ public dynamic GetProperties(DiscordGuild guild, Nest nest, string pokemonImageU nest_name = nestName, type_1 = Convert.ToString(type1), type_2 = Convert.ToString(type2), - type_1_emoji = type1Emoji, - type_2_emoji = type2Emoji, types = $"{type1} | {type2}", types_emojis = typeEmojis, @@ -265,7 +259,7 @@ public dynamic GetProperties(DiscordGuild guild, Nest nest, string pokemonImageU wazemaps_url = wazeMapsLink, scanmaps_url = scannerMapsLink, - //address = address?.Address, + address = address ?? string.Empty, // Discord Guild properties guild_name = guild?.Name, diff --git a/src/Configuration/Config.cs b/src/Configuration/Config.cs index 73c4de6f..08735d29 100644 --- a/src/Configuration/Config.cs +++ b/src/Configuration/Config.cs @@ -11,7 +11,6 @@ using WhMgr.Data; using WhMgr.Extensions; using WhMgr.Services.Icons; - using WhMgr.Services.StaticMap; /// /// Configuration file class diff --git a/src/Configuration/StaticMapConfig.cs b/src/Configuration/StaticMapConfig.cs index 11528574..777a3d0b 100644 --- a/src/Configuration/StaticMapConfig.cs +++ b/src/Configuration/StaticMapConfig.cs @@ -6,6 +6,9 @@ public class StaticMapConfig { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = true; + [JsonPropertyName("url")] public string Url { get; set; } diff --git a/src/Defaults.cs b/src/Defaults.cs index 9a57f23a..8471035e 100644 --- a/src/Defaults.cs +++ b/src/Defaults.cs @@ -115,6 +115,9 @@ public Defaults() // Ex gym emoji "ex", + // AR fort emoji + "ar", + // Type emojis "types_fire", "types_grass", diff --git a/src/Extensions/DateTimeExtensions.cs b/src/Extensions/DateTimeExtensions.cs index a9f82076..55642b7d 100644 --- a/src/Extensions/DateTimeExtensions.cs +++ b/src/Extensions/DateTimeExtensions.cs @@ -5,6 +5,8 @@ using GeoTimeZone; using TimeZoneConverter; + using WhMgr.Services.Webhook.Models; + public static class DateTimeExtensions { public static TimeSpan GetTimeRemaining(this DateTime startTime, DateTime endTime) @@ -13,6 +15,11 @@ public static TimeSpan GetTimeRemaining(this DateTime startTime, DateTime endTim return remaining; } + public static DateTime ConvertTimeFromCoordinates(this DateTime date, IWebhookPoint coord) + { + return ConvertTimeFromCoordinates(date, coord.Latitude, coord.Longitude); + } + public static DateTime ConvertTimeFromCoordinates(this DateTime date, double lat, double lon) { var tzIana = TimeZoneLookup.GetTimeZone(lat, lon).Result; diff --git a/src/Extensions/PokemonExtensions.cs b/src/Extensions/PokemonExtensions.cs index 2614c72b..c502d237 100644 --- a/src/Extensions/PokemonExtensions.cs +++ b/src/Extensions/PokemonExtensions.cs @@ -100,19 +100,33 @@ public static string GetTypeEmojiIcons(this IEnumerable pokemonType return string.Join(" ", list); } - public static string GetEmojiIcon(this T type, string keyPrefix, bool asString) + public static string GetEmojiIcon(this T type, string keyPrefix, bool asString, string defaultValue = null) { var value = asString ? type.ToString().ToLower() : Convert.ToInt32(type).ToString(); var key = string.IsNullOrEmpty(keyPrefix) ? value : $"{keyPrefix}_{value}"; var emojiId = GameMaster.Instance.Emojis.ContainsKey(key) ? GameMaster.Instance.Emojis[key] : 0; - var emojiName = string.IsNullOrEmpty(GameMaster.Instance.CustomEmojis[key]) + + if (emojiId == 0) + { + Console.WriteLine($"Emoji '{key}' does not exist! Using fallback text."); + } + + // Check if custom emoji list contains specified emoji string or if custom emoji is not overwritten. + var emojiName = !GameMaster.Instance.CustomEmojis.ContainsKey(key) || string.IsNullOrEmpty(GameMaster.Instance.CustomEmojis[key]) + // Check if we retrieved Discord emoji successfully ? emojiId > 0 + // Construct Discord emoji string ? string.Format(Strings.Defaults.EmojiSchema, key, emojiId) + // Fallback to text instead of emoji : type.ToString() + // Custom emoji is set which overwrites Discord emojis : GameMaster.Instance.CustomEmojis[key] ?? type.ToString(); - return emojiName; + var result = !string.IsNullOrEmpty(emojiName) + ? emojiName + : defaultValue; + return result; } public static string GetWeaknessEmojiIcons(this List pokemonTypes) diff --git a/src/Extensions/StaticMapExtensions.cs b/src/Extensions/StaticMapExtensions.cs new file mode 100644 index 00000000..f4a941b7 --- /dev/null +++ b/src/Extensions/StaticMapExtensions.cs @@ -0,0 +1,58 @@ +namespace WhMgr.Extensions +{ + using System; + using System.Threading.Tasks; + + using WhMgr.Common; + using WhMgr.Configuration; + using WhMgr.Services.Cache; + using WhMgr.Services.StaticMap; + using WhMgr.Services.Webhook.Models; + + public static class StaticMapExtensions + { + public static string GenerateStaticMap(this StaticMapConfig config, StaticMapType mapType, IWebhookPoint coord, string imageUrl, IMapDataCache cache = null, PokemonTeam? team = null, string polygonPath = null) + { + var url = GenerateStaticMapAsync(config, mapType, coord, imageUrl, cache, team, polygonPath).Result; + return url; + } + + public static async Task GenerateStaticMapAsync(this StaticMapConfig config, StaticMapType mapType, IWebhookPoint coord, string imageUrl, IMapDataCache cache = null, PokemonTeam? team = null, string polygonPath = null) + { + if (!(config?.Enabled ?? false)) + { + return string.Empty; + } + + if (string.IsNullOrEmpty(config.Url)) + { + return string.Empty; + } + + var staticMap = new StaticMapGenerator(new StaticMapOptions + { + BaseUrl = config.Url, + MapType = mapType, + TemplateType = config.Type, + Latitude = coord.Latitude, + Longitude = coord.Longitude, + SecondaryImageUrl = imageUrl, + Gyms = config.IncludeNearbyGyms && cache != null + // Fetch nearby gyms from MapDataCache + ? await cache?.GetGymsNearby(coord.Latitude, coord.Longitude) + : new(), + Pokestops = config.IncludeNearbyPokestops && cache != null + // Fetch nearby pokestops from MapDataCache + ? await cache?.GetPokestopsNearby(coord.Latitude, coord.Longitude) + : new(), + Team = team, + PolygonPath = polygonPath, + Pregenerate = config.Pregenerate, + Regeneratable = true, + }); + + var url = staticMap.GenerateLink(); + return url ?? string.Empty; + } + } +} \ No newline at end of file diff --git a/src/Localization/Language.cs b/src/Localization/Language.cs index d0d747c3..05faed29 100644 --- a/src/Localization/Language.cs +++ b/src/Localization/Language.cs @@ -70,8 +70,7 @@ public Language() : this(default) { CurrentCulture = new CultureInfo(DefaultLanguage); - LocaleDirectory = Strings.LocaleFolder; - + LocaleDirectory = Path.Combine(Strings.BasePath, Strings.LocaleFolder); //_map = LoadCountry(CurrentCulture.TwoLetterISOLanguageName); } diff --git a/src/Services/Alarms/Embeds/EmbedMessage.cs b/src/Services/Alarms/Embeds/EmbedMessage.cs index 3bd050f2..12ea9f0f 100644 --- a/src/Services/Alarms/Embeds/EmbedMessage.cs +++ b/src/Services/Alarms/Embeds/EmbedMessage.cs @@ -21,12 +21,10 @@ public class EmbedMessage : Dictionary "{{/if}}{{#if is_ditto}}**Catch Pokemon:** {{original_pkmn_name}}", "{{/if}}{{#if has_capture_rates}}{{capture_1_emoji}} {{capture_1}}% {{capture_2_emoji}} {{capture_2}}% {{capture_3_emoji}} {{capture_3}}%", "{{/if}}{{#if is_event}}Go Fest Spawn", - "{{/if}}{{#if has_pvp}}", - "{{#each pvp}}**{{@key}}**", + "{{/if}}{{#if has_pvp}}{{#each pvp}}**{{@key}}**", "{{#each this}}", "#{{rank}} {{getPokemonName pokemonId}} {{getFormName formId}} {{cp}}CP @ L{{level}} {{formatPercentage percentage}}%", - "{{/each}}{{/each}}", - "{{/if}}**[Google]({{gmaps_url}}) | [Apple]({{applemaps_url}}) | [Waze]({{wazemaps_url}}) | [Scanner]({{scanmaps_url}})**", + "{{/each}}{{/each}}{{/if}}**[Google]({{gmaps_url}}) | [Apple]({{applemaps_url}}) | [Waze]({{wazemaps_url}}) | [Scanner]({{scanmaps_url}})**" }, IconUrl = "{{pkmn_img_url}}", Title = "{{geofence}}", diff --git a/src/Services/Discord/DiscordClientService.cs b/src/Services/Discord/DiscordClientService.cs index 0f384c42..22e98301 100644 --- a/src/Services/Discord/DiscordClientService.cs +++ b/src/Services/Discord/DiscordClientService.cs @@ -333,7 +333,7 @@ private async Task CreateEmojisAsync(ulong guildId) } // Create steam of emoji file data - var fs = new FileStream(emojiPath, FileMode.Open, FileAccess.Read); + using var fs = new FileStream(emojiPath, FileMode.Open, FileAccess.Read); // Create emoji for Discord guild await guild.CreateEmojiAsync(emoji, fs, null, $"Missing `{emoji}` emoji."); diff --git a/src/Services/Webhook/Models/GenericEmbedProperties.cs b/src/Services/Webhook/Models/GenericEmbedProperties.cs new file mode 100644 index 00000000..8ee92a47 --- /dev/null +++ b/src/Services/Webhook/Models/GenericEmbedProperties.cs @@ -0,0 +1,69 @@ +namespace WhMgr.Services.Webhook.Models +{ + using System; + using System.Collections.Generic; + using System.Threading.Tasks; + + using DSharpPlus.Entities; + + using WhMgr.Configuration; + using WhMgr.Extensions; + using WhMgr.Services.Geofence; + using WhMgr.Services.Geofence.Geocoding; + using WhMgr.Services.Yourls; + + public class GenericEmbedProperties + { + public string GoogleMapsLocationLink { get; set; } + + public string AppleMapsLocationLink { get; set; } + + public string WazeMapsLocationLink { get; set; } + + public string ScannerMapsLocationLink { get; set; } + + public string Address { get; set; } + + public DiscordGuild Guild { get; set; } + + public DateTime Now { get; set; } + + + public static GenericEmbedProperties Generate(Config config, IReadOnlyDictionary guilds, ulong guildId, IWebhookPoint coord) + { + var data = GenerateAsync(config, guilds, guildId, coord).Result; + return data; + } + + public static async Task GenerateAsync(Config config, IReadOnlyDictionary guilds, ulong guildId, IWebhookPoint coord) + { + var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, coord.Latitude, coord.Longitude); + var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, coord.Latitude, coord.Longitude); + var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, coord.Latitude, coord.Longitude); + var scannerMapsLink = string.Format(config.Urls.ScannerMap, coord.Latitude, coord.Longitude); + + var urlShortener = new UrlShortener(config.ShortUrlApi); + var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); + var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); + var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); + var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); + var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(coord)); + + var now = DateTime.UtcNow.ConvertTimeFromCoordinates(coord); + var guild = guilds?.ContainsKey(guildId) ?? false + ? guilds[guildId] + : null; + + return new GenericEmbedProperties + { + GoogleMapsLocationLink = gmapsLocationLink, + AppleMapsLocationLink = appleMapsLocationLink, + WazeMapsLocationLink = wazeMapsLocationLink, + ScannerMapsLocationLink = scannerMapsLocationLink, + Address = address ?? string.Empty, + Guild = guild, + Now = now, + }; + } + } +} \ No newline at end of file diff --git a/src/Services/Webhook/Models/GymDetailsData.cs b/src/Services/Webhook/Models/GymDetailsData.cs index 43013fb0..864b01c5 100644 --- a/src/Services/Webhook/Models/GymDetailsData.cs +++ b/src/Services/Webhook/Models/GymDetailsData.cs @@ -126,14 +126,16 @@ public void SetTimes() { PowerUpEndTime = PowerUpEndTimestamp .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); } public async Task GenerateEmbedMessageAsync(AlarmMessageSettings settings) { var server = settings.Config.Instance.Servers[settings.GuildId]; var embedType = EmbedMessageType.Gyms; - var embed = settings.Alarm?.Embeds[embedType] ?? server.Subscriptions?.Embeds?[embedType] ?? EmbedMessage.Defaults[embedType]; + var embed = settings.Alarm?.Embeds[embedType] + ?? server.Subscriptions?.Embeds?[embedType] + ?? EmbedMessage.Defaults[embedType]; var properties = await GetPropertiesAsync(settings).ConfigureAwait(false); var eb = new DiscordEmbedMessage { @@ -177,52 +179,23 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { + var config = properties.Config.Instance; // Get old gym from cache var oldGym = await properties.MapDataCache.GetGym(FortId).ConfigureAwait(false); - var exEmoji = "ex".GetEmojiIcon(null, true); + var exEmoji = Strings.EX.GetEmojiIcon(null, true); var teamEmoji = Team.GetEmojiIcon(null, true); var oldTeamEmoji = oldGym?.Team.GetEmojiIcon(null, true); - - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); - var gymImageUrl = UIconService.Instance.GetGymIcon(properties.Config.Instance.Servers[properties.GuildId].IconStyle, Team); - - var staticMapConfig = properties.Config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = StaticMapType.Gyms, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - Team = Team, - SecondaryImageUrl = gymImageUrl, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby pokestops from MapDataCache - ? await properties.MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); - - var now = DateTime.UtcNow.ConvertTimeFromCoordinates(Latitude, Longitude); - var powerUpEndTimeLeft = now.GetTimeRemaining(PowerUpEndTime).ToReadableStringNoSeconds(); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; + var gymImageUrl = UIconService.Instance.GetGymIcon(config.Servers[properties.GuildId].IconStyle, Team); + + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + StaticMapType.Gyms, + this, + gymImageUrl, + properties.MapDataCache, + Team + ); + var powerUpEndTimeLeft = locProperties.Now.GetTimeRemaining(PowerUpEndTime).ToReadableStringNoSeconds(); const string defaultMissingValue = "?"; var dict = new @@ -266,16 +239,16 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, - address = address ?? string.Empty, + address = locProperties.Address, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, // Misc properties date_time = DateTime.Now.ToString(), diff --git a/src/Services/Webhook/Models/IncidentData.cs b/src/Services/Webhook/Models/IncidentData.cs index 0c802ff7..e394dea8 100644 --- a/src/Services/Webhook/Models/IncidentData.cs +++ b/src/Services/Webhook/Models/IncidentData.cs @@ -160,15 +160,15 @@ public void SetTimes() { StartTime = Start .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); ExpirationTime = Expiration .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); UpdatedTime = Updated .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); } public async Task GenerateEmbedMessageAsync(AlarmMessageSettings settings) @@ -217,41 +217,17 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { - var server = properties.Config.Instance.Servers[properties.GuildId]; + var config = properties.Config.Instance; + var server = config.Servers[properties.GuildId]; var imageUrl = UIconService.Instance.GetInvasionIcon(server.IconStyle, Character); - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); - - var staticMapConfigType = StaticMapType.Invasions; - var staticMapConfig = properties.Config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = staticMapConfigType, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - SecondaryImageUrl = imageUrl, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby pokestops from MapDataCache - ? await properties.MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); + + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + StaticMapType.Invasions, + this, + imageUrl, + properties.MapDataCache + ); var invasion = GameMaster.Instance.GruntTypes.ContainsKey(Character) ? GameMaster.Instance.GruntTypes[Character] @@ -266,12 +242,7 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) var invasionEncounters = Character > 0 ? invasion.GetPossibleInvasionEncounters() : new List(); - - var now = DateTime.UtcNow.ConvertTimeFromCoordinates(Latitude, Longitude); - var invasionExpireTimeLeft = now.GetTimeRemaining(ExpirationTime).ToReadableStringNoSeconds(); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; + var invasionExpireTimeLeft = locProperties.Now.GetTimeRemaining(ExpirationTime).ToReadableStringNoSeconds(); const string defaultMissingValue = "?"; var dict = new @@ -301,10 +272,10 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, // Pokestop properties pokestop_id = PokestopId ?? defaultMissingValue, @@ -312,11 +283,11 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) pokestop_url = Url ?? defaultMissingValue, invasion_img_url = imageUrl, - address = address ?? string.Empty, + address = locProperties.Address, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, // Misc properties date_time = DateTime.Now.ToString(), diff --git a/src/Services/Webhook/Models/PokemonData.cs b/src/Services/Webhook/Models/PokemonData.cs index dba24b6a..37b39b6c 100644 --- a/src/Services/Webhook/Models/PokemonData.cs +++ b/src/Services/Webhook/Models/PokemonData.cs @@ -352,23 +352,23 @@ public void SetTimes() { DespawnTime = DisappearTime .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); SecondsLeft = DespawnTime .Subtract(DateTime.UtcNow - .ConvertTimeFromCoordinates(Latitude, Longitude)); + .ConvertTimeFromCoordinates(this)); FirstSeenTime = FirstSeen .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); LastModifiedTime = LastModified .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); UpdatedTime = Updated .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); } /// @@ -434,6 +434,7 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { + var config = properties.Config.Instance; var pkmnInfo = GameMaster.GetPokemon(PokemonId, FormId); var pkmnName = Translator.Instance.GetPokemonName(PokemonId); var form = Translator.Instance.GetFormName(FormId); @@ -473,46 +474,20 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) ? Math.Round(Weight ?? 0).ToString() : ""; - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); - var staticMapConfig = properties.Config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = StaticMapType.Pokemon, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - SecondaryImageUrl = properties.ImageUrl, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby pokestops from MapDataCache - ? await properties.MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + StaticMapType.Pokemon, + this, + properties.ImageUrl, + properties.MapDataCache + ); var pokestop = properties.MapDataCache.GetPokestop(PokestopId).ConfigureAwait(false) .GetAwaiter() .GetResult(); + // TODO: Make configurable for user to add individual league icons var greatLeagueEmoji = PvpLeague.Great.GetEmojiIcon("league", true); var ultraLeagueEmoji = PvpLeague.Ultra.GetEmojiIcon("league", true); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; const string defaultMissingValue = "?"; var dict = new @@ -609,12 +584,12 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, - address = address ?? string.Empty, + address = locProperties.Address, // Pokestop properties near_pokestop = pokestop != null, @@ -623,8 +598,8 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) pokestop_url = pokestop?.FortUrl ?? defaultMissingValue, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, // Event properties is_event = IsEvent.HasValue && IsEvent.Value, diff --git a/src/Services/Webhook/Models/PokestopData.cs b/src/Services/Webhook/Models/PokestopData.cs index a76dab30..b598c3a3 100644 --- a/src/Services/Webhook/Models/PokestopData.cs +++ b/src/Services/Webhook/Models/PokestopData.cs @@ -132,7 +132,7 @@ public sealed class PokestopData : IWebhookData, IWebhookFort, IWebhookPowerLeve JsonIgnore, NotMapped, ] - public bool HasLure => LureExpire > 0 && LureType != PokestopLureType.None && LureExpireTime > DateTime.UtcNow.ConvertTimeFromCoordinates(Latitude, Longitude); + public bool HasLure => LureExpire > 0 && LureType != PokestopLureType.None && LureExpireTime > DateTime.UtcNow.ConvertTimeFromCoordinates(this); [ JsonPropertyName("incidents"), @@ -163,11 +163,11 @@ public void SetTimes() { LureExpireTime = LureExpire .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); PowerUpEndTime = PowerUpEndTimestamp .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); } public async Task GenerateEmbedMessageAsync(AlarmMessageSettings settings) @@ -222,54 +222,22 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { - var server = properties.Config.Instance.Servers[properties.GuildId]; + var config = properties.Config.Instance; + var server = config.Servers[properties.GuildId]; var lureImageUrl = UIconService.Instance.GetPokestopIcon(server.IconStyle, LureType); var imageUrl = HasLure ? lureImageUrl : FortUrl; - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); - - var staticMapConfigType = StaticMapType.Lures; - /* - var staticMapConfigType = HasLure - ? StaticMapType.Lures - : StaticMapType.Pokestop; // TODO: StaticMapType.Pokestops - */ - var staticMapConfig = properties.Config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = staticMapConfigType, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - SecondaryImageUrl = imageUrl, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby pokestops from MapDataCache - ? await properties.MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); - - var now = DateTime.UtcNow.ConvertTimeFromCoordinates(Latitude, Longitude); - var lureExpireTimeLeft = now.GetTimeRemaining(LureExpireTime).ToReadableStringNoSeconds(); - var powerUpEndTimeLeft = now.GetTimeRemaining(PowerUpEndTime).ToReadableStringNoSeconds(); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; + + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + // TODO: HasLure ? StaticMapType.Lures : StaticMapType.Pokestops, + StaticMapType.Lures, + this, + imageUrl, + properties.MapDataCache + ); + + var lureExpireTimeLeft = locProperties.Now.GetTimeRemaining(LureExpireTime).ToReadableStringNoSeconds(); + var powerUpEndTimeLeft = locProperties.Now.GetTimeRemaining(PowerUpEndTime).ToReadableStringNoSeconds(); const string defaultMissingValue = "?"; var dict = new @@ -298,10 +266,10 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, // Pokestop properties pokestop_id = FortId ?? defaultMissingValue, @@ -309,11 +277,11 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) pokestop_url = FortUrl ?? defaultMissingValue, lure_img_url = lureImageUrl, - address = address ?? string.Empty, + address = locProperties.Address, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, // Misc properties date_time = DateTime.Now.ToString(), diff --git a/src/Services/Webhook/Models/QuestData.cs b/src/Services/Webhook/Models/QuestData.cs index 55ce26d1..8a442487 100644 --- a/src/Services/Webhook/Models/QuestData.cs +++ b/src/Services/Webhook/Models/QuestData.cs @@ -143,48 +143,19 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { + var config = properties.Config.Instance; var questMessage = this.GetQuestMessage(); var questConditions = this.GetConditions(); var questReward = this.GetReward(); - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); + var arEmoji = Strings.AR.GetEmojiIcon(null, true); - var staticMapConfig = properties.Config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = StaticMapType.Quests, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - SecondaryImageUrl = properties.ImageUrl, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var arEmojiId = GameMaster.Instance.Emojis.ContainsKey("ar") - ? GameMaster.Instance.Emojis["ar"] - : 0; - var arEmoji = arEmojiId > 0 ? $"<:ar:{arEmojiId}>" : "AR Quest"; - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + StaticMapType.Quests, + this, + properties.ImageUrl, + properties.MapDataCache + ); const string defaultMissingValue = "?"; var dict = new @@ -211,12 +182,12 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, - address = address ?? string.Empty, + address = locProperties.Address, // Pokestop properties pokestop_id = PokestopId ?? defaultMissingValue, @@ -224,8 +195,8 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) pokestop_url = PokestopUrl ?? defaultMissingValue, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, //M isc properties date_time = DateTime.Now.ToString(), diff --git a/src/Services/Webhook/Models/RaidData.cs b/src/Services/Webhook/Models/RaidData.cs index 1a5bcd0d..fc502b0c 100644 --- a/src/Services/Webhook/Models/RaidData.cs +++ b/src/Services/Webhook/Models/RaidData.cs @@ -5,9 +5,11 @@ using System.Text.Json.Serialization; using System.Threading.Tasks; + using DiscordGuild = DSharpPlus.Entities.DiscordGuild; using Gender = POGOProtos.Rpc.PokemonDisplayProto.Types.Gender; using WhMgr.Common; + using WhMgr.Configuration; using WhMgr.Data; using WhMgr.Extensions; using WhMgr.Localization; @@ -152,15 +154,15 @@ public void SetTimes() { StartTime = Start .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); EndTime = End .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); PowerUpEndTime = PowerUpEndTimestamp .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); } /// @@ -222,6 +224,7 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { + var config = properties.Config.Instance; var pkmnInfo = GameMaster.GetPokemon(PokemonId, FormId); var name = IsEgg ? Translator.Instance.Translate("EGG") @@ -234,19 +237,13 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) var move1 = Translator.Instance.GetMoveName(FastMove); var move2 = Translator.Instance.GetMoveName(ChargeMove); var types = pkmnInfo?.Types; - var type1 = pkmnInfo?.Types?.Count >= 1 - ? pkmnInfo.Types[0] + var type1 = types?.Count >= 1 + ? types[0] : PokemonType.None; - var type2 = pkmnInfo?.Types?.Count > 1 - ? pkmnInfo.Types[1] + var type2 = types?.Count > 1 + ? types[1] : PokemonType.None; - var type1Emoji = pkmnInfo?.Types?.Count >= 1 - ? type1.GetTypeEmojiIcons() - : string.Empty; - var type2Emoji = pkmnInfo?.Types?.Count > 1 - ? type2.GetTypeEmojiIcons() - : string.Empty; - var typeEmojis = $"{type1Emoji} {type2Emoji}"; + var typeEmojis = types?.GetTypeEmojiIcons() ?? string.Empty; var weaknesses = Weaknesses == null ? string.Empty : string.Join(", ", Weaknesses); @@ -255,56 +252,21 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) var boostedRange = PokemonId.GetCpAtLevel(25, 15); var worstRange = PokemonId.GetCpAtLevel(20, 10); var worstBoosted = PokemonId.GetCpAtLevel(25, 10); - var exEmojiId = GameMaster.Instance.Emojis.ContainsKey("ex") - ? GameMaster.Instance.Emojis["ex"] - : 0; - var exEmoji = exEmojiId > 0 ? $"<:ex:{exEmojiId}>" : "EX"; - var teamEmojiId = GameMaster.Instance.Emojis.ContainsKey(Team.ToString().ToLower()) - ? GameMaster.Instance.Emojis[Team.ToString().ToLower()] - : 0; - var teamEmoji = teamEmojiId > 0 ? $"<:{Team.ToString().ToLower()}:{teamEmojiId}>" : Team.ToString(); - - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); - - var staticMapConfig = properties.Config.Instance.StaticMaps; - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = StaticMapType.Raids, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - SecondaryImageUrl = properties.ImageUrl, - Team = Team, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby pokestops from MapDataCache - ? await properties .MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); - - var now = DateTime.UtcNow.ConvertTimeFromCoordinates(Latitude, Longitude); - var startTimeLeft = now.GetTimeRemaining(StartTime).ToReadableStringNoSeconds(); - var endTimeLeft = now.GetTimeRemaining(EndTime).ToReadableStringNoSeconds(); - var powerUpEndTimeLeft = now.GetTimeRemaining(PowerUpEndTime).ToReadableStringNoSeconds(); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; + var exEmoji = Strings.EX.GetEmojiIcon(null, true); + var teamEmoji = Team.GetEmojiIcon(null, true); + + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + StaticMapType.Raids, + this, + properties.ImageUrl, + properties.MapDataCache, + Team + ); + + var startTimeLeft = locProperties.Now.GetTimeRemaining(StartTime).ToReadableStringNoSeconds(); + var endTimeLeft = locProperties.Now.GetTimeRemaining(EndTime).ToReadableStringNoSeconds(); + var powerUpEndTimeLeft = locProperties.Now.GetTimeRemaining(PowerUpEndTime).ToReadableStringNoSeconds(); const string defaultMissingValue = "?"; var dict = new @@ -339,8 +301,6 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) moveset = $"{move1}/{move2}", type_1 = type1.ToString() ?? defaultMissingValue, type_2 = type2.ToString() ?? defaultMissingValue, - type_1_emoji = type1Emoji, - type_2_emoji = type2Emoji, types = $"{type1}/{type2}", types_emoji = typeEmojis, weaknesses, @@ -375,12 +335,12 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, - address = address ?? string.Empty, + address = locProperties.Address, // Gym properties gym_id = GymId, @@ -388,8 +348,8 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) gym_url = GymUrl, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, // Misc properties date_time = DateTime.Now.ToString(), diff --git a/src/Services/Webhook/Models/WeatherData.cs b/src/Services/Webhook/Models/WeatherData.cs index b640d9f3..f44f7847 100644 --- a/src/Services/Webhook/Models/WeatherData.cs +++ b/src/Services/Webhook/Models/WeatherData.cs @@ -144,7 +144,7 @@ public void SetTimes() { UpdatedTime = Updated .FromUnix() - .ConvertTimeFromCoordinates(Latitude, Longitude); + .ConvertTimeFromCoordinates(this); } public async Task GenerateEmbedMessageAsync(AlarmMessageSettings settings) @@ -195,46 +195,23 @@ public async Task GenerateEmbedMessageAsync(AlarmMessageS private async Task GetPropertiesAsync(AlarmMessageSettings properties) { + var config = properties.Config.Instance; var weather = Translator.Instance.GetWeather(GameplayCondition); - var weatherEmoji = GameplayCondition != WeatherCondition.None ? GameplayCondition.GetEmojiIcon("weather", false) : string.Empty; + var weatherEmoji = GameplayCondition != WeatherCondition.None + ? GameplayCondition.GetEmojiIcon("weather", false) + : string.Empty; var hasWeather = GameplayCondition != WeatherCondition.None; - var gmapsLink = string.Format(Strings.Defaults.GoogleMaps, Latitude, Longitude); - var appleMapsLink = string.Format(Strings.Defaults.AppleMaps, Latitude, Longitude); - var wazeMapsLink = string.Format(Strings.Defaults.WazeMaps, Latitude, Longitude); - var scannerMapsLink = string.Format(properties.Config.Instance.Urls.ScannerMap, Latitude, Longitude); - var staticMapConfig = properties.Config.Instance.StaticMaps; + var locProperties = await GenericEmbedProperties.GenerateAsync(config, properties.Client.Guilds, properties.GuildId, this); var polygonPath = OsmManager.MultiPolygonToLatLng(new List { Polygon }, false); - var staticMap = new StaticMapGenerator(new StaticMapOptions - { - BaseUrl = staticMapConfig.Url, - MapType = StaticMapType.Weather, - TemplateType = staticMapConfig.Type, - Latitude = Latitude, - Longitude = Longitude, - SecondaryImageUrl = properties.ImageUrl, - PolygonPath = polygonPath, - Gyms = staticMapConfig.IncludeNearbyGyms - // Fetch nearby gyms from MapDataCache - ? await properties.MapDataCache?.GetGymsNearby(Latitude, Longitude) - : new(), - Pokestops = staticMapConfig.IncludeNearbyPokestops - // Fetch nearby pokestops from MapDataCache - ? await properties.MapDataCache?.GetPokestopsNearby(Latitude, Longitude) - : new(), - Pregenerate = staticMapConfig.Pregenerate, - Regeneratable = true, - }); - var staticMapLink = staticMap.GenerateLink(); - var urlShortener = new UrlShortener(properties.Config.Instance.ShortUrlApi); - var gmapsLocationLink = await urlShortener.CreateAsync(gmapsLink); - var appleMapsLocationLink = await urlShortener.CreateAsync(appleMapsLink); - var wazeMapsLocationLink = await urlShortener.CreateAsync(wazeMapsLink); - var scannerMapsLocationLink = await urlShortener.CreateAsync(scannerMapsLink); - var address = await ReverseGeocodingLookup.Instance.GetAddressAsync(new Coordinate(Latitude, Longitude)); - var guild = properties.Client.Guilds.ContainsKey(properties.GuildId) - ? properties.Client.Guilds[properties.GuildId] - : null; + var staticMapLink = await config.StaticMaps?.GenerateStaticMapAsync( + StaticMapType.Weather, + this, + properties.ImageUrl, + properties.MapDataCache, + null, + polygonPath + ); const string defaultMissingValue = "?"; var dict = new @@ -266,16 +243,16 @@ private async Task GetPropertiesAsync(AlarmMessageSettings properties) // Location links tilemaps_url = staticMapLink, - gmaps_url = gmapsLocationLink, - applemaps_url = appleMapsLocationLink, - wazemaps_url = wazeMapsLocationLink, - scanmaps_url = scannerMapsLocationLink, + gmaps_url = locProperties.GoogleMapsLocationLink, + applemaps_url = locProperties.AppleMapsLocationLink, + wazemaps_url = locProperties.WazeMapsLocationLink, + scanmaps_url = locProperties.ScannerMapsLocationLink, - address = address ?? string.Empty, + address = locProperties.Address, // Discord Guild properties - guild_name = guild?.Name, - guild_img_url = guild?.IconUrl, + guild_name = locProperties.Guild?.Name, + guild_img_url = locProperties.Guild?.IconUrl, // Misc properties date_time = DateTime.Now.ToString(), diff --git a/src/Strings.cs b/src/Strings.cs index 6319f3bf..3bb598f9 100644 --- a/src/Strings.cs +++ b/src/Strings.cs @@ -19,22 +19,17 @@ public static class Strings public const string WwwRoot = BasePath + "wwwroot"; public const string BasePath = "../bin/"; - public const string ViewsFolder = "Views"; - public const string TemplateExt = ".hbs"; public const string ConfigsFolder = BasePath + "configs"; public const string GeofencesFolder = BasePath + "geofences"; public const string AlarmsFolder = BasePath + "alarms"; public const string EmbedsFolder = BasePath + "embeds"; public const string DiscordsFolder = BasePath + "discords"; public const string FiltersFolder = BasePath + "filters"; - public const string LibrariesFolder = "libs"; public const string StaticFolder = "static"; public const string TemplatesFolder = BasePath + "templates"; - public const string MigrationsFolder = "migrations"; - public static readonly string AppFolder = StaticFolder + Path.DirectorySeparatorChar + "app"; public static readonly string DataFolder = BasePath + StaticFolder + Path.DirectorySeparatorChar + "data"; public static readonly string LocaleFolder = StaticFolder + Path.DirectorySeparatorChar + "locales"; - public static readonly string EmojisFolder = StaticFolder + Path.DirectorySeparatorChar + "emojis"; + public static readonly string EmojisFolder = BasePath + StaticFolder + Path.DirectorySeparatorChar + "emojis"; public static readonly string OsmNestFilePath = StaticFolder + Path.DirectorySeparatorChar + OsmNestFileName; public const string DiscordAuthFilePath = BasePath + "discord_auth.json"; public const string DiscordAvatarUrlFormat = "https://cdn.discordapp.com/avatars/{0}/{1}.png"; @@ -49,5 +44,8 @@ public static class Strings public const string ErrorLogFileName = "error.log"; public const int DiscordMaximumMessageLength = 2048; + + public const string EX = "ex"; + public const string AR = "ar"; } } \ No newline at end of file diff --git a/static/data/emojis.json b/static/data/emojis.json index 33ba4736..398ac5ef 100644 --- a/static/data/emojis.json +++ b/static/data/emojis.json @@ -4,6 +4,7 @@ "capture_2": "", "capture_3": "", "ex": "", + "ar": "", "gender_female": "", "gender_less": "", "gender_male": "",