Skip to content

Commit

Permalink
Merge pull request #22 from edgiardina/tournament-event-type
Browse files Browse the repository at this point in the history
Tournament event type
  • Loading branch information
edgiardina authored Oct 6, 2024
2 parents c61111a + 560264a commit eaff667
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 22 deletions.
9 changes: 9 additions & 0 deletions PinballApi.Tests/PinballRankingApiTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ public async Task PinballRankingApi_TournamentSearch_GetSearchById()

Assert.That(result, Is.Not.Null);
Assert.That(result.TournamentId, Is.EqualTo(tourneyId));
}

[Test]
public async Task PinballRankingApi_TournamentSearch_GetSearchByEventType()
{
var result = await rankingApi.TournamentSearch(tournamentEventType: Models.WPPR.Universal.Tournaments.Search.TournamentEventType.League);

Assert.That(result, Is.Not.Null);
Assert.That(result.TotalResults, Is.GreaterThan(0));
Assert.That(result.SearchFilter.EventType, Is.EqualTo(Models.WPPR.Universal.Tournaments.Search.TournamentEventType.League));
}

[Test]
Expand Down
18 changes: 15 additions & 3 deletions PinballApi.Tests/PinballRankingApiV2TestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public async Task PinballRankingApiV2_GetPlayer_ShouldReturnCorrectPlayer()
Assert.That(player.PlayerStats.CurrentWpprValue, Is.GreaterThan(0));
}

[Test]
[Ignore("This test is failing due to a bug in the API")]
public async Task PinballRankingApiV2_GetPlayer_ShouldReturnCorrectPlayer2()
{
var lname = "Lapping";

var player = await rankingApi.GetPlayersBySearch(new PlayerSearchFilter { Name = lname });

Assert.That(player.Results.First().LastName == lname);
//For some reason my
}

[Test]
public async Task PinballRankingApiV2_GetPlayer_ShouldReturnPlayerWithWomensFlag()
{
Expand Down Expand Up @@ -263,7 +275,7 @@ public async Task PinballRankingApiV2_GetRankingYouth_ShouldReturnRanking(int st
Assert.That(ranking.Rankings, Is.Not.Null);
Assert.That(ranking.Rankings.First().CurrentRank, Is.EqualTo(startRank));
Assert.That(ranking.Rankings.First().WpprPoints, Is.Positive);
Assert.That(ranking.Rankings.First().CurrentWpprRank, Is.Positive);
Assert.That(ranking.Rankings.First().CurrentWpprRank, Is.Positive);
Assert.That(ranking.Rankings.First().EventCount, Is.Positive);
}

Expand Down Expand Up @@ -420,7 +432,7 @@ public async Task PinballRankingApiV2_GetTournamentBySearch_ShouldReturnTourname

[Test]
public async Task PinballRankingApiV2_GetNacsDirectors_ShouldReturnDirectors()
{
{
var directors = await rankingApi.GetNacsDirectors();

Assert.That(directors.Count, Is.Positive);
Expand All @@ -444,7 +456,7 @@ public async Task PinballRankingApiV2_GetOverallStatistics_ShouldReturnStats()
Assert.That(stats.TournamentCountLastMonth, Is.Positive);
Assert.That(stats.TournamentCountThisYear, Is.Positive);
Assert.That(stats.TournamentPlayerCount, Is.Positive);
Assert.That(stats.TournamentPlayerCountAverage, Is.Positive);
Assert.That(stats.TournamentPlayerCountAverage, Is.Positive);
}

[Test]
Expand Down
42 changes: 42 additions & 0 deletions PinballApi/Converters/CapitalizedEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Text.Json.Serialization;
using System.Text.Json;
using Humanizer;

namespace PinballApi.Converters
{
public class CapitalizedEnumConverter<T> : JsonConverter<T> where T : struct, Enum
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var enumValue = reader.GetString();

if (enumValue == null)
{
throw new JsonException($"Unable to convert null to enum {typeof(T)}.");
}

// Convert the snake_case string (START_DATE) to PascalCase (StartDate)
var pascalCaseValue = enumValue.Pascalize();

// Try parsing the enum from the PascalCase string
if (Enum.TryParse(pascalCaseValue, true, out T parsedEnum))
{
return parsedEnum;
}

throw new JsonException($"Unable to convert \"{enumValue}\" to enum {typeof(T)}.");
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
// Convert the enum value (PascalCase) to snake_case (START_DATE)
string enumString = value.ToString();
if (!string.IsNullOrEmpty(enumString))
{
string snakeCase = enumString.Underscore().ToUpper(); // Ensure it's uppercase for consistency
writer.WriteStringValue(snakeCase);
}
}
}
}
41 changes: 41 additions & 0 deletions PinballApi/Converters/TournamentSearchSortOrderConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using PinballApi.Models.WPPR.Universal.Tournaments.Search;
using System;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace PinballApi.Converters
{
public class TournamentSearchSortOrderConverter : JsonConverter<TournamentSearchSortOrder>
{
public override TournamentSearchSortOrder Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var enumValue = reader.GetString();

if (enumValue == null)
{
throw new JsonException($"Unable to convert null to enum {typeof(TournamentSearchSortOrder)}.");
}

// Convert the string to the corresponding enum value
return enumValue.ToUpper() switch
{
"ASC" => TournamentSearchSortOrder.Ascending,
"DESC" => TournamentSearchSortOrder.Descending,
_ => throw new JsonException($"Unknown value \"{enumValue}\" for enum {typeof(TournamentSearchSortOrder)}.")
};
}

public override void Write(Utf8JsonWriter writer, TournamentSearchSortOrder value, JsonSerializerOptions options)
{
// Convert the enum value to the corresponding string (ASC or DESC)
string stringValue = value switch
{
TournamentSearchSortOrder.Ascending => "ASC",
TournamentSearchSortOrder.Descending => "DESC",
_ => throw new JsonException($"Unknown enum value {value}.")
};

writer.WriteStringValue(stringValue);
}
}
}
2 changes: 1 addition & 1 deletion PinballApi/Interfaces/IPinballRankingApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IPinballRankingApi
{
Task<RankingSearch> RankingSearch(RankingType rankingType, RankingSystem rankingSystem = RankingSystem.Open, int count = 100, int startPosition = 1, string countryCode = null);
Task<Models.WPPR.Universal.Tournaments.Tournament> GetTournament(int tournamentId);
Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, TournamentType? rankingSystem = null, int? startPosition = null, int? totalReturn = null, TournamentSearchSortMode? tournamentSearchSortMode = null, TournamentSearchSortOrder? tournamentSearchSortOrder = null, string directorName = null, bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null);
Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, TournamentType? tournamentType = null, int? startPosition = null, int? totalReturn = null, TournamentSearchSortMode? tournamentSearchSortMode = null, TournamentSearchSortOrder? tournamentSearchSortOrder = null, string directorName = null, bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null, TournamentEventType? tournamentEventType = null);
Task<Player> GetPlayer(int playerId);
Task<RankingCountries> GetRankingCountries();
Task<ProRankingSearch> ProRankingSearch(TournamentType rankingSystem);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PinballApi.Models.WPPR.Universal.Tournaments.Search
{
public enum TournamentEventType
{
Tournament,
League
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
using PinballApi.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace PinballApi.Models.WPPR.Universal.Tournaments.Search
{
public class TournamentSearchFilter
{
[JsonPropertyName("sort_mode")]
public string SortMode { get; set; }
[JsonConverter(typeof(CapitalizedEnumConverter<TournamentSearchSortMode>))]
public TournamentSearchSortMode? SortMode { get; set; }

[JsonPropertyName("sort_order")]
public string SortOrder { get; set; }
[JsonConverter(typeof(TournamentSearchSortOrderConverter))]
public TournamentSearchSortOrder? SortOrder { get; set; }

[JsonPropertyName("distance_unit")]
public string DistanceUnit { get; set; }
Expand All @@ -27,5 +24,9 @@ public class TournamentSearchFilter

[JsonPropertyName("longitude")]
public double Longitude { get; set; }

[JsonPropertyName("event_type")]
[JsonConverter(typeof(CapitalizedEnumConverter<TournamentEventType>))]
public TournamentEventType? EventType { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PinballApi.Models.WPPR.Universal.Tournaments.Search
namespace PinballApi.Models.WPPR.Universal.Tournaments.Search
{
public enum TournamentSearchSortOrder
{
Expand Down
5 changes: 2 additions & 3 deletions PinballApi/PinballApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
<ItemGroup>
<PackageReference Include="Flurl" Version="4.0.0" />
<PackageReference Include="Flurl.Http" Version="4.0.2" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.61" />
<PackageReference Include="Polly" Version="8.4.1" />
<PackageReference Include="RestSharp" Version="111.4.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.67" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
<ItemGroup>
Expand Down
11 changes: 10 additions & 1 deletion PinballApi/PinballRankingApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public PinballRankingApi(string apiKey) : base(apiKey)

public async Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, TournamentType? tournamentType = null, int? startPosition = null,
int? totalReturn = null, TournamentSearchSortMode? tournamentSearchSortMode = null, TournamentSearchSortOrder? tournamentSearchSortOrder = null, string directorName = null,
bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null)
bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null, TournamentEventType? tournamentEventType = null)
{

var request = BaseRequest
Expand Down Expand Up @@ -71,7 +71,13 @@ public async Task<TournamentSearch> TournamentSearch(double? latitude = null, do
request = request.SetQueryParam("end_date", endDate.Value.ToString("yyyy-MM-dd"));

if (tournamentType.HasValue)
{
//Tournament type must be MAIN or WOMEN
if (tournamentType != TournamentType.Main && tournamentType != TournamentType.Women)
throw new ArgumentException("Tournament Type must be MAIN or WOMEN");

request = request.SetQueryParam("rank_type", tournamentType.Value.ToString().ToUpper());
}

if (radius.HasValue)
request = request.SetQueryParam("radius", radius);
Expand Down Expand Up @@ -126,6 +132,9 @@ public async Task<TournamentSearch> TournamentSearch(double? latitude = null, do
if (!string.IsNullOrEmpty(directorName))
request = request.SetQueryParam("director_name", directorName);

if (tournamentEventType.HasValue)
request = request.SetQueryParam("event_type", tournamentEventType.Value.ToString().ToUpper());

return await request.GetJsonAsync<TournamentSearch>();
}

Expand Down

0 comments on commit eaff667

Please sign in to comment.