mirror of
https://github.com/bloxstraplabs/bloxstrap.git
synced 2025-04-21 10:01:27 -07:00
Merge pull request #1610 from Redusofficial/main
[.NET] ActivityWatcher.GetServerLocation() Http optimization
This commit is contained in:
commit
86fcbf6c1a
@ -280,13 +280,27 @@
|
|||||||
if (GeolocationCache.ContainsKey(ActivityMachineAddress))
|
if (GeolocationCache.ContainsKey(ActivityMachineAddress))
|
||||||
return GeolocationCache[ActivityMachineAddress];
|
return GeolocationCache[ActivityMachineAddress];
|
||||||
|
|
||||||
string location, locationCity, locationRegion, locationCountry = "";
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
locationCity = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{ActivityMachineAddress}/city");
|
string location = "";
|
||||||
locationRegion = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{ActivityMachineAddress}/region");
|
var ipInfo = await Http.GetJson<IPInfoResponse>($"https://ipinfo.io/{ActivityMachineAddress}/json");
|
||||||
locationCountry = await App.HttpClient.GetStringAsync($"https://ipinfo.io/{ActivityMachineAddress}/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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -295,24 +309,6 @@
|
|||||||
|
|
||||||
return $"N/A ({Resources.Strings.ActivityTracker_LookupFailed})";
|
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()
|
public void Dispose()
|
||||||
|
14
Bloxstrap/Models/IPInfoResponse.cs
Normal file
14
Bloxstrap/Models/IPInfoResponse.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
namespace Bloxstrap.Models
|
||||||
|
{
|
||||||
|
public class IPInfoResponse
|
||||||
|
{
|
||||||
|
[JsonPropertyName("city")]
|
||||||
|
public string City { get; set; } = null!;
|
||||||
|
|
||||||
|
[JsonPropertyName("country")]
|
||||||
|
public string Country { get; set; } = null!;
|
||||||
|
|
||||||
|
[JsonPropertyName("region")]
|
||||||
|
public string Region { get; set; } = null!;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user