Skip to content

Commit

Permalink
* fixed a bug, which caused the tool to not work as intended in spect…
Browse files Browse the repository at this point in the history
…ator mode
  • Loading branch information
Johannes-Schneider committed Nov 4, 2020
1 parent 67c4507 commit 7f69130
Show file tree
Hide file tree
Showing 8 changed files with 1,484 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace GoldDiff.LeagueOfLegends.ClientApi.ActivePlayer
public class LoLClientActivePlayer
{
[JsonProperty("summonerName")]
public string SummonerName { get; set; }
public string SummonerName { get; set; } = string.Empty;
}
}
4 changes: 2 additions & 2 deletions GoldDiff.LeagueOfLegends.ClientApi/Player/LoLClientItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class LoLClientItem
{
[JsonProperty("itemID")]
public int Id { get; set; }

[JsonProperty("displayName")]
public string Name { get; set; }
public string Name { get; set; } = string.Empty;

[JsonProperty("count")]
public int Amount { get; set; }
Expand Down
20 changes: 10 additions & 10 deletions GoldDiff.LeagueOfLegends.ClientApi/Player/LoLClientPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ namespace GoldDiff.LeagueOfLegends.ClientApi.Player
public class LoLClientPlayer
{
[JsonProperty("summonerName")]
public string SummonerName { get; set; }
public string SummonerName { get; set; } = string.Empty;

[JsonProperty("championName")]
public string ChampionName { get; set; }
public string ChampionName { get; set; } = string.Empty;

[JsonProperty("isDead")]
public bool IsDead { get; set; }

[JsonProperty("respawnTimer")]
[JsonConverter(typeof(LoLTimeConverter))]
public TimeSpan RespawnTimeInSeconds { get; set; }
public TimeSpan RespawnTimeInSeconds { get; set; } = TimeSpan.Zero;

[JsonProperty("position")]
[JsonConverter(typeof(LoLPositionConverter))]
public LoLPositionType Position { get; set; }
public LoLPositionType Position { get; set; } = LoLPositionType.Undefined;

[JsonProperty("team")]
[JsonConverter(typeof(LoLTeamConverter))]
public LoLTeamType Team { get; set; }
public LoLTeamType Team { get; set; } = LoLTeamType.Undefined;

[JsonProperty("items")]
public List<LoLClientItem> Items { get; set; }
public List<LoLClientItem> Items { get; set; } = new List<LoLClientItem>();

[JsonProperty("scores")]
public LoLClientScore Score { get; set; }
public LoLClientScore Score { get; set; } = new LoLClientScore();
}
}
17 changes: 12 additions & 5 deletions GoldDiff.Shared/Http/RestRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ public RestRequester(HttpClientHandler clientHandler)

public async Task<TResultType> GetAsync<TResultType>(string url)
{
var response = await Client.GetAsync(url).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
try
{
var response = await Client.GetAsync(url).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
return default!;
}

var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<TResultType>(json);
}
catch (TaskCanceledException)
{
return default!;
}

var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<TResultType>(json);
}

#region IDisposable
Expand Down
8 changes: 6 additions & 2 deletions GoldDiff/LeagueOfLegends/Game/LoLClientDataPollService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using GoldDiff.LeagueOfLegends.ClientApi;
using log4net;

namespace GoldDiff.LeagueOfLegends.Game
{
public sealed class LoLClientDataPollService : IDisposable
{
private static ILog Log { get; } = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

public event EventHandler<LoLClientGameData>? GameDataReceived;

public TimeSpan PollInterval { get; set; }
Expand Down Expand Up @@ -45,9 +49,9 @@ private async Task PollGameData()
GameDataReceived?.Invoke(this, gameData);
await Task.Delay(PollInterval, CancellationTokenSource.Token);
}
catch
catch (Exception exception)
{
// ignore
Log.Error($"Exception while polling game data!", exception);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Test.GoldDiff.LeagueOfLegends.ClientApi
public class LoLClientGameDataTest
{
private static string FullGameAsJsonPath { get; } = Path.Combine(Environment.CurrentDirectory, "Resources", "full-game.json");
private static string SpectatorGameAsJsonPath { get; } = Path.Combine(Environment.CurrentDirectory, "Resources", "spectator-game.json");

[Test]
public void TestDeserializeFullGame()
Expand All @@ -27,5 +28,13 @@ public void TestDeserializeFullGame()
Assert.AreEqual(5, gameData.Players.Count(player => player.Team == LoLTeamType.BlueSide));
Assert.AreEqual(5, gameData.Players.Count(player => player.Team == LoLTeamType.RedSide));
}

[Test]
public void TestDeserializeSpectatorGame()
{
var gameData = JsonConvert.DeserializeObject<LoLClientGameData>(File.ReadAllText(SpectatorGameAsJsonPath));

Assert.AreEqual(string.Empty, gameData.ActivePlayer.SummonerName);
}
}
}
Loading

0 comments on commit 7f69130

Please sign in to comment.