From cda660cc3e7f6206ff05720a4e307f1affd7d24b Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sun, 12 May 2024 21:20:00 +0100 Subject: [PATCH] Activity tracker, more efficient, fix city not IP --- Bloxstrap/Integrations/ActivityWatcher.cs | 45 +++++++++-------------- Bloxstrap/Models/IPInfoResponse.cs | 4 +- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Bloxstrap/Integrations/ActivityWatcher.cs b/Bloxstrap/Integrations/ActivityWatcher.cs index 3c4696c..ac0bea0 100644 --- a/Bloxstrap/Integrations/ActivityWatcher.cs +++ b/Bloxstrap/Integrations/ActivityWatcher.cs @@ -280,18 +280,27 @@ if (GeolocationCache.ContainsKey(ActivityMachineAddress)) return GeolocationCache[ActivityMachineAddress]; - string location = "", locationCity = "", locationRegion = "", locationCountry = ""; - try { - var locationInformation = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); + string location = ""; + var ipInfo = await Http.GetJson($"https://ipinfo.io/{ActivityMachineAddress}/json"); - if (locationInformation is not null) - { - locationCity = locationInformation.IP; - locationRegion = locationInformation.Region; - locationCountry = locationInformation.Country; - } + if (ipInfo is null) + return $"N/A ({Resources.Strings.ActivityTracker_LookupFailed})"; + + if (string.IsNullOrEmpty(ipInfo.Country)) + location = "N/A"; + else if (ipInfo.City == ipInfo.Region) + location = $"{ipInfo.Region}, {ipInfo.Country}"; + else + location = $"{ipInfo.City}, {ipInfo.Region}, {ipInfo.Country}"; + + if (!ActivityInGame) + return $"N/A ({Resources.Strings.ActivityTracker_LeftGame})"; + + GeolocationCache[ActivityMachineAddress] = location; + + return location; } catch (Exception ex) { @@ -300,24 +309,6 @@ return $"N/A ({Resources.Strings.ActivityTracker_LookupFailed})"; } - - locationCity = locationCity.ReplaceLineEndings(""); - locationRegion = locationRegion.ReplaceLineEndings(""); - locationCountry = locationCountry.ReplaceLineEndings(""); - - if (string.IsNullOrEmpty(locationCountry)) - location = "N/A"; - else if (locationCity == locationRegion) - location = $"{locationRegion}, {locationCountry}"; - else - location = $"{locationCity}, {locationRegion}, {locationCountry}"; - - if (!ActivityInGame) - return $"N/A ({Resources.Strings.ActivityTracker_LeftGame})"; - - GeolocationCache[ActivityMachineAddress] = location; - - return location; } public void Dispose() diff --git a/Bloxstrap/Models/IPInfoResponse.cs b/Bloxstrap/Models/IPInfoResponse.cs index 6deaf26..f8d3bc0 100644 --- a/Bloxstrap/Models/IPInfoResponse.cs +++ b/Bloxstrap/Models/IPInfoResponse.cs @@ -2,8 +2,8 @@ namespace Bloxstrap.Models { public class IPInfoResponse { - [JsonPropertyName("ip")] - public string IP { get; set; } = null!; + [JsonPropertyName("city")] + public string City { get; set; } = null!; [JsonPropertyName("country")] public string Country { get; set; } = null!;