Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Triky313 committed May 24, 2024
2 parents 38533b7 + 46791b5 commit 327bfbe
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
87 changes: 74 additions & 13 deletions src/StatisticsAnalysisTool/Common/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -58,19 +60,23 @@ public static async Task<List<MarketResponse>> GetCityItemPricesFromJsonAsync(st
using var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
using var client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));

try
{
client.Timeout = TimeSpan.FromSeconds(30);

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
if (response.StatusCode == (HttpStatusCode) 429)
{
throw new TooManyRequestsException();
}

using var content = response.Content;
var result = JsonSerializer.Deserialize<List<MarketResponse>>(await content.ReadAsStringAsync());
response.EnsureSuccessStatusCode();

Stream decompressedStream = await DecompressedStream(response);

var result = await JsonSerializer.DeserializeAsync<List<MarketResponse>>(decompressedStream);
return MergeMarketAndPortalLocations(result);
}
catch (TooManyRequestsException)
Expand Down Expand Up @@ -107,19 +113,33 @@ public static async Task<List<MarketHistoriesResponse>> GetHistoryItemPricesFrom
using var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
using var client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.Timeout = TimeSpan.FromSeconds(300);

try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;

if (response.StatusCode == (HttpStatusCode) 429)
{
throw new TooManyRequestsException();
}

var result = JsonSerializer.Deserialize<List<MarketHistoriesResponse>>(await content.ReadAsStringAsync());
response.EnsureSuccessStatusCode();

await using var responseStream = await response.Content.ReadAsStreamAsync();
using var memoryStream = new MemoryStream();
await responseStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;

Stream decompressedStream = memoryStream;
if (response.Content.Headers.ContentEncoding.Contains("gzip"))
{
decompressedStream = new GZipStream(memoryStream, CompressionMode.Decompress);
}

var result = await JsonSerializer.DeserializeAsync<List<MarketHistoriesResponse>>(decompressedStream);
return MergeCityAndPortalCity(result);
}
catch (Exception e)
Expand All @@ -145,6 +165,7 @@ public static async Task<GameInfoSearchResponse> GetGameInfoSearchFromJsonAsync(
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;

return JsonSerializer.Deserialize<GameInfoSearchResponse>(await content.ReadAsStringAsync()) ?? gameInfoSearchResponse;
}
catch (JsonException ex)
Expand Down Expand Up @@ -174,6 +195,7 @@ public static async Task<GameInfoPlayersResponse> GetGameInfoPlayersFromJsonAsyn
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;

return JsonSerializer.Deserialize<GameInfoPlayersResponse>(await content.ReadAsStringAsync()) ??
gameInfoPlayerResponse;
}
Expand Down Expand Up @@ -206,6 +228,7 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerKills
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;

return JsonSerializer.Deserialize<List<GameInfoPlayerKillsDeaths>>(await content.ReadAsStringAsync()) ?? values;
}
catch (Exception e)
Expand Down Expand Up @@ -246,6 +269,7 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerTopKi
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;

return JsonSerializer.Deserialize<List<GameInfoPlayerKillsDeaths>>(await content.ReadAsStringAsync()) ?? values;
}
catch (Exception e)
Expand Down Expand Up @@ -286,6 +310,7 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerSoloK
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;

return JsonSerializer.Deserialize<List<GameInfoPlayerKillsDeaths>>(await content.ReadAsStringAsync()) ?? values;
}
catch (Exception e)
Expand Down Expand Up @@ -322,24 +347,32 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerSoloK
// }
//}

public static async Task<List<GoldResponseModel>> GetGoldPricesFromJsonAsync(DateTime? dateTime, int count, int timeout = 300)
public static async Task<List<GoldResponseModel>> GetGoldPricesFromJsonAsync(int count, int timeout = 300)
{
var dateString = dateTime != null ? $"{dateTime:yyyy-MM-dd'T'HH:mm:ss}" : string.Empty;

var url = Path.Combine(GetAoDataProjectServerBaseUrlByCurrentServer(), "stats/Gold/");
url += $"?date={dateString}&count={count}";
var url = Path.Combine(GetAoDataProjectServerBaseUrlByCurrentServer(), "stats/");
url += $"gold?count={count}";

using var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
using var client = new HttpClient(clientHandler);
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.Timeout = TimeSpan.FromSeconds(timeout);
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
using var response = await client.GetAsync(url);
using var content = response.Content;
var contentString = await content.ReadAsStringAsync();
return string.IsNullOrEmpty(contentString) ? new List<GoldResponseModel>() : JsonSerializer.Deserialize<List<GoldResponseModel>>(contentString);

response.EnsureSuccessStatusCode();

await using var responseStream = await response.Content.ReadAsStreamAsync();
Stream decompressedStream = responseStream;
if (response.Content.Headers.ContentEncoding.Contains("gzip"))
{
decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress);
}

var result = await JsonSerializer.DeserializeAsync<List<GoldResponseModel>>(decompressedStream);
return result ?? new List<GoldResponseModel>();
}
catch (Exception e)
{
Expand Down Expand Up @@ -373,6 +406,34 @@ public static async Task<List<Donation>> GetDonationsFromJsonAsync()
}
}

//private static async Task<Stream> DecompressedStream(HttpResponseMessage response)
//{
// await using var responseStream = await response.Content.ReadAsStreamAsync();
// Stream decompressedStream = responseStream;
// if (response.Content.Headers.ContentEncoding.Contains("gzip"))
// {
// decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress);
// }

// return decompressedStream;
//}

private static async Task<Stream> DecompressedStream(HttpResponseMessage response)
{
var responseStream = await response.Content.ReadAsStreamAsync();
var memoryStream = new MemoryStream();
await responseStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;

if (response.Content.Headers.ContentEncoding.Contains("gzip"))
{
return new GZipStream(memoryStream, CompressionMode.Decompress);
}

memoryStream.Position = 0;
return memoryStream;
}

#region Helper methods

#region Merge history data
Expand Down
11 changes: 8 additions & 3 deletions src/StatisticsAnalysisTool/Common/Converters/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;

namespace StatisticsAnalysisTool.Common.Converters;

Expand All @@ -19,6 +20,8 @@ public static class Converter
private static DateTime _lastGoldUpdate = DateTime.MinValue;
private static int _currentGoldPrice = DefaultGoldPrice;

private static bool _getGoldPriceisRunning = false;

public static string GoldToDollar(ulong itemSilverPrice)
{
if (itemSilverPrice == 0 || _currentGoldPrice == 0)
Expand All @@ -36,14 +39,15 @@ public static string GoldToDollar(ulong itemSilverPrice)
return $"{minPrice:0.00} - {maxPrice:0.00} $";
}

private static async void GetCurrentGoldPriceAsync()
private static async Task GetCurrentGoldPriceAsync()
{
if (_lastGoldUpdate.Ticks > DateTime.UtcNow.AddHours(-1).Ticks)
if (_lastGoldUpdate.Ticks > DateTime.UtcNow.AddHours(-1).Ticks || _getGoldPriceisRunning)
{
return;
}

var response = await ApiController.GetGoldPricesFromJsonAsync(null, 1, 30);
_getGoldPriceisRunning = true;
var response = await ApiController.GetGoldPricesFromJsonAsync(1, 30);
if (response == null)
{
return;
Expand All @@ -52,5 +56,6 @@ private static async void GetCurrentGoldPriceAsync()
var price = response.FirstOrDefault()?.Price ?? DefaultGoldPrice;
_currentGoldPrice = price;
_lastGoldUpdate = DateTime.UtcNow;
_getGoldPriceisRunning = false;
}
}
4 changes: 2 additions & 2 deletions src/StatisticsAnalysisTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("7.3.2.*")]
[assembly: AssemblyFileVersion("7.3.2.0")]
[assembly: AssemblyVersion("7.3.3.*")]
[assembly: AssemblyFileVersion("7.3.3.0")]

0 comments on commit 327bfbe

Please sign in to comment.