Skip to content

Commit

Permalink
Merge pull request #24 from versx/timezones
Browse files Browse the repository at this point in the history
Despawn/Expire times based on TimeZone from coordinates
  • Loading branch information
versx authored Sep 7, 2020
2 parents 7e4b7ee + 5c82f90 commit ef54e61
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
using DSharpPlus.Interactivity;

// TODO: Subscriptions, Pokemon, Raid, Quest, Invasion, Gym, Weather alarm statistics by day. date/pokemonId/count
// TODO: Specific timezone per Discord
// TODO: Account status alarms
// TODO: List all subscriptions with info command
// TODO: Manage subscriptions via DM again
// TODO: Multiple discord bot tokens per server
// TODO: Only start database migrator if subscriptions are enabled
// TODO: Check nests again
Expand Down
16 changes: 16 additions & 0 deletions src/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{
using System;

using GeoTimeZone;
using TimeZoneConverter;

public static class DateTimeExtensions
{
public static TimeSpan GetTimeRemaining(this DateTime endTime)
Expand Down Expand Up @@ -37,5 +40,18 @@ public static DateTime ConvertTime(this DateTime timeUtc, string timeZoneId = "P
}
return timeUtc;
}

public static DateTime ConvertTimeFromCoordinates(this DateTime date, double lat, double lon)
{
var tzIana = TimeZoneLookup.GetTimeZone(lat, lon).Result;
#if Windows
// Convert to Windows acceptable TimeZone
tzIana = TZConvert.IanaToWindows(tzIana);
#endif
var tzInfo = TimeZoneInfo.FindSystemTimeZoneById(tzIana);
var dt = DateTime.SpecifyKind(date, DateTimeKind.Utc);
var convertedTime = TimeZoneInfo.ConvertTimeFromUtc(dt, tzInfo);
return convertedTime;
}
}
}
2 changes: 1 addition & 1 deletion src/Extensions/IntegerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static char NumberToAlphabet(this int num)
public static DateTime FromUnix(this long unixSeconds)
{
var epochTime = new DateTime(1970,1,1,0,0,0,0,DateTimeKind.Utc);
var localDateTime = epochTime.AddSeconds(unixSeconds).ToLocalTime();
var localDateTime = epochTime.AddSeconds(unixSeconds);//.ToLocalTime();

return localDateTime;
}
Expand Down
13 changes: 9 additions & 4 deletions src/Net/Models/PokemonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,18 @@ public PokemonData()
/// </summary>
public void SetDespawnTime()
{
DespawnTime = DisappearTime.FromUnix();
DespawnTime = DisappearTime.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);

SecondsLeft = DespawnTime.Subtract(DateTime.Now);
SecondsLeft = DespawnTime.Subtract(DateTime.Now.ConvertTimeFromCoordinates(Latitude, Longitude));

FirstSeenTime = FirstSeen.FromUnix();
FirstSeenTime = FirstSeen
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);

LastModifiedTime = LastModified.FromUnix();
LastModifiedTime = LastModified
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions src/Net/Models/PokestopData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ public PokestopData()
/// </summary>
public void SetTimes()
{
LureExpireTime = LureExpire.FromUnix();
LureExpireTime = LureExpire
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);

InvasionExpireTime = IncidentExpire.FromUnix();
InvasionExpireTime = IncidentExpire
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);
}

public DiscordEmbedNotification GeneratePokestopMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city)
Expand Down
8 changes: 6 additions & 2 deletions src/Net/Models/RaidData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ public RaidData()
/// </summary>
public void SetTimes()
{
StartTime = Start.FromUnix();
StartTime = Start
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);

EndTime = End.FromUnix();
EndTime = End
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);
}

/// <summary>
Expand Down
8 changes: 3 additions & 5 deletions src/Net/Models/WeatherData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ public WeatherData()

public void SetTimes()
{
UpdatedTime = Updated.FromUnix();
//if (TimeZoneInfo.Local.IsDaylightSavingTime(Updated))
//{
// UpdatedTime = UpdatedTime.AddHours(1);
//}
UpdatedTime = Updated
.FromUnix()
.ConvertTimeFromCoordinates(Latitude, Longitude);
}

public DiscordEmbedNotification GenerateWeatherMessage(ulong guildId, DiscordClient client, WhConfig whConfig, AlarmObject alarm, string city)
Expand Down
4 changes: 2 additions & 2 deletions src/Net/Webhooks/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public WebhookController(WhConfig config)
private void Http_PokemonReceived(object sender, DataReceivedEventArgs<PokemonData> e)
{
var pkmn = e.Data;
if (DateTime.Now > pkmn.DespawnTime)
if (DateTime.Now.ConvertTimeFromCoordinates(pkmn.Latitude, pkmn.Longitude) > pkmn.DespawnTime)
{
_logger.Debug($"Pokemon {pkmn.Id} already despawned at {pkmn.DespawnTime}");
return;
Expand All @@ -251,7 +251,7 @@ private void Http_PokemonReceived(object sender, DataReceivedEventArgs<PokemonDa
private void Http_RaidReceived(object sender, DataReceivedEventArgs<RaidData> e)
{
var raid = e.Data;
if (DateTime.Now > raid.EndTime)
if (DateTime.Now.ConvertTimeFromCoordinates(raid.Latitude, raid.Longitude) > raid.EndTime)
{
_logger.Debug($"Raid boss {raid.PokemonId} already despawned at {raid.EndTime}");
return;
Expand Down
1 change: 1 addition & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using WhMgr.Diagnostics;

class Program
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static string GetStaticMapsUrl(string templateFileName, string staticMapU
quest_reward_img_url = markerImageUrl,
weather_img_url = markerImageUrl,
tilemaps_url = staticMapUrl
}); ;
});
StaticMapConfig staticMap = JsonConvert.DeserializeObject<StaticMapConfig>(staticMapData);

var url = string.Format(staticMapUrl, latitude, longitude, staticMapZoom);
Expand Down
6 changes: 4 additions & 2 deletions src/WhMgr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
<PackageReference Include="DSharpPlus" Version="3.2.3" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="3.2.3" />
<PackageReference Include="DSharpPlus.Interactivity" Version="3.2.3" />
<PackageReference Include="GeoTimeZone" Version="4.0.0" />
<PackageReference Include="Microsoft.Win32.SystemEvents" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="ServiceStack.OrmLite.MySql" Version="5.8.0" />
<PackageReference Include="Stripe.net" Version="37.14.0" />
<PackageReference Include="Twilio" Version="5.44.0" />
<PackageReference Include="Stripe.net" Version="37.31.0" />
<PackageReference Include="TimeZoneConverter" Version="3.2.0" />
<PackageReference Include="Twilio" Version="5.45.2" />
</ItemGroup>

</Project>

0 comments on commit ef54e61

Please sign in to comment.