Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #362 from aspriddell/modern-stats-updates
Browse files Browse the repository at this point in the history
Further Modern stats updates
  • Loading branch information
aspriddell authored Jan 12, 2023
2 parents 18bd297 + 1c3df36 commit c3fcd0a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 18 deletions.
18 changes: 14 additions & 4 deletions DragonFruit.Six.Api/Modern/ModernStatsDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Dragon6 API Copyright DragonFruit Network <inbox@dragonfruit.network>
// Licensed under Apache-2. Refer to the LICENSE file for more info

using DragonFruit.Six.Api.Accounts.Entities;
using DragonFruit.Six.Api.Modern.Containers;
using DragonFruit.Six.Api.Modern.Requests;
using DragonFruit.Six.Api.Modern.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -14,10 +14,20 @@ public static class ModernStatsDeserializer
/// <summary>
/// Abstracts a ubisoft stats response to present the data required
/// </summary>
public static ModernModeStatsContainer<T> ProcessData<T>(this JObject source, ModernStatsRequest request)
public static ModernModeStatsContainer<T> ProcessData<T>(this JObject source, UbisoftAccount account)
{
var platformKey = request.PlatformGroup?.ToString().ToUpperInvariant() ?? request.Account.Platform.ModernName();
return source?["platforms"]?[platformKey]?.ToObject<ModernModeStatsContainer<T>>(new JsonSerializer
var profileContainer = source?["profileData"].ToObject<JObject>();

if (profileContainer == null)
{
return null;
}

var platformContainer = profileContainer.Count == 1
? profileContainer.First.Value<JProperty>().Value.ToObject<JObject>()
: profileContainer[account.ProfileId];

return platformContainer?["platforms"]?.First.Value<JProperty>().Value.ToObject<ModernModeStatsContainer<T>>(new JsonSerializer
{
Converters = { new JsonPathConverter() }
});
Expand Down
8 changes: 5 additions & 3 deletions DragonFruit.Six.Api/Modern/ModernStatsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ModernStatsExtensions
public static async Task<ModernModeStatsContainer<TResponse>> GetModernStatsAsync<TResponse>(this Dragon6Client client, ModernStatsRequest request, CancellationToken token = default)
{
var response = await client.PerformAsync<JObject>(request, token).ConfigureAwait(false);
return response.ProcessData<TResponse>(request);
return response.ProcessData<TResponse>(request.Account);
}

/// <summary>
Expand Down Expand Up @@ -97,13 +97,15 @@ public static Task<ModernOperatorInfo[]> GetModernOperatorInfoAsync(this ApiClie
/// <param name="client">The <see cref="Dragon6Client"/> to use</param>
/// <param name="account">The <see cref="UbisoftAccount"/> to get stats for</param>
/// <param name="playlistType">The <see cref="PlaylistType"/> to get stats for</param>
/// <param name="crossPlatformGroup">Optional <see cref="PlatformGroup"/> to get modern cross-platform stats for.</param>
/// <param name="token">Optional <see cref="CancellationToken"/></param>
/// <returns>A container with all seasons tracked. Will return null if no stats found</returns>
public static Task<ModernModeStatsContainer<IEnumerable<ModernSeasonStats>>> GetModernSeasonStatsAsync(this Dragon6Client client, UbisoftAccount account, PlaylistType playlistType = PlaylistType.All, CancellationToken token = default)
public static Task<ModernModeStatsContainer<IEnumerable<ModernSeasonStats>>> GetModernSeasonStatsAsync(this Dragon6Client client, UbisoftAccount account, PlaylistType playlistType = PlaylistType.All, PlatformGroup? crossPlatformGroup = null, CancellationToken token = default)
{
var request = new ModernSeasonalStatsRequest(account)
{
Playlist = playlistType
Playlist = playlistType,
PlatformGroup = crossPlatformGroup
};

return client.GetModernStatsAsync<IEnumerable<ModernSeasonStats>>(request, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
// Licensed under Apache-2. Refer to the LICENSE file for more info

using System;
using System.Collections.Generic;
using DragonFruit.Data;
using DragonFruit.Data.Requests;
using DragonFruit.Six.Api.Accounts.Entities;
using DragonFruit.Six.Api.Enums;
using DragonFruit.Six.Api.Modern.Utils;

namespace DragonFruit.Six.Api.Modern.Requests
{
public class ModernSeasonalStatsRequest : ModernStatsRequest
public class ModernSeasonalStatsRequest : ModernStatsRequest, IRequestExecutingCallback
{
protected override string RequestCategory => "seasonal";
protected override string RequestType => "summary";
Expand Down Expand Up @@ -38,5 +42,15 @@ public override OperatorType OperatorType
protected override string FormattedStartDate => null;
protected override string FormattedEndDate => null;
protected override string OperatorTypeNames => null;

void IRequestExecutingCallback.OnRequestExecuting(ApiClient client)
{
var platformQuery = !PlatformGroup.HasValue
? new KeyValuePair<string, string>("platform", Account.Platform.ModernName())
: new KeyValuePair<string, string>("platformGroup", PlatformGroup.ToString());

Queries.Clear();
Queries.Add(platformQuery);
}
}
}
55 changes: 47 additions & 8 deletions DragonFruit.Six.Api/Modern/Requests/ModernStatsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@

using System;
using System.Collections.Generic;
using System.Linq;
using DragonFruit.Data;
using DragonFruit.Data.Parameters;
using DragonFruit.Data.Requests;
using DragonFruit.Six.Api.Accounts.Entities;
using DragonFruit.Six.Api.Accounts.Enums;
using DragonFruit.Six.Api.Enums;
using DragonFruit.Six.Api.Modern.Enums;
using DragonFruit.Six.Api.Modern.Utils;
using DragonFruit.Six.Api.Seasonal.Requests;
using JetBrains.Annotations;

namespace DragonFruit.Six.Api.Modern.Requests
{
public abstract class ModernStatsRequest : UbiApiRequest
public abstract class ModernStatsRequest : UbiApiRequest, IRequestExecutingCallback
{
private const string DateTimeFormat = "yyyyMMdd";
private const int DefaultStartWindow = 14;
Expand All @@ -22,11 +26,19 @@ public abstract class ModernStatsRequest : UbiApiRequest
private OperatorType? _operatorType;
private DateTimeOffset? _startDate, _endDate;

protected readonly IList<KeyValuePair<string, string>> Queries;

private static readonly DateTimeOffset CrossPlatformStartDate = new(2022, 12, 6, 0, 0, 0, TimeSpan.Zero);

public override string Path => $"https://prod.datadev.ubisoft.com/v1/users/{Account.UbisoftId}/playerstats";

protected override IEnumerable<KeyValuePair<string, string>> AdditionalQueries => Queries;

protected ModernStatsRequest(UbisoftAccount account)
{
Account = account ?? throw new NullReferenceException();

Queries = new List<KeyValuePair<string, string>>(1);
}

/// <summary>
Expand Down Expand Up @@ -92,12 +104,11 @@ public virtual DateTimeOffset EndDate
/// List of seasons to return stats for. Provides an alternative timespan compared to start-end dates.
/// </summary>
[QueryParameter("seasons", CollectionConversionMode.Concatenated)]
public virtual IEnumerable<string> Seasons { get; set; }
public virtual IEnumerable<ModernSeason> Seasons { get; set; }

/// <summary>
/// Optional <see cref="PlatformGroup"/> to override when getting cross-progression metrics
/// </summary>
[QueryParameter("platformGroup", EnumHandlingMode.StringUpper)]
public PlatformGroup? PlatformGroup { get; set; }

/// <summary>
Expand All @@ -117,11 +128,6 @@ public virtual DateTimeOffset EndDate
[QueryParameter("view")]
protected virtual string RequestCategory => "current";

[CanBeNull]
[UsedImplicitly]
[QueryParameter("platform")]
protected string PlatformName => PlatformGroup.HasValue ? null : Account.Platform.ModernName();

[UsedImplicitly]
[QueryParameter("spaceId")]
protected string GameSpaceId => Account.Platform.GameSpaceId();
Expand All @@ -143,5 +149,38 @@ public virtual DateTimeOffset EndDate
[UsedImplicitly]
[QueryParameter("teamRole")]
protected virtual string OperatorTypeNames => OperatorType.Expand();

void IRequestExecutingCallback.OnRequestExecuting(ApiClient client)
{
bool useCrossPlayQueries;

if (Seasons?.Any() != true)
{
useCrossPlayQueries = EndDate > CrossPlatformStartDate;
}
else
{
// check seasons to make sure old and new seasons aren't mixed in
if (Seasons.All(x => x.SeasonNumber < SeasonalStatsRecordRequest.CrossPlatformProgressionId))
{
useCrossPlayQueries = false;
}
else if (Seasons.All(x => x.SeasonNumber >= SeasonalStatsRecordRequest.CrossPlatformProgressionId))
{
useCrossPlayQueries = true;
}
else
{
throw new ArgumentException($"{nameof(Seasons)} combines both cross-platform and individual platform seasons.", nameof(Seasons));
}
}

var platformQuery = useCrossPlayQueries
? new KeyValuePair<string, string>("platform", Account.Platform.ModernName())
: new KeyValuePair<string, string>("platformGroup", (PlatformGroup ?? (Account.Platform == Platform.PC ? Api.Enums.PlatformGroup.PC : Api.Enums.PlatformGroup.Console)).ToString());

Queries.Clear();
Queries.Add(platformQuery);
}
}
}
4 changes: 2 additions & 2 deletions DragonFruit.Six.Api/Modern/Utils/ModernPlatformUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static class ModernPlatformUtils
public static string ModernName(this Platform platform) => platform switch
{
Platform.PC => "PC",
Platform.PSN => "PSN",
Platform.XB1 => "XONE",
Platform.PSN => "PLAYSTATION",
Platform.XB1 => "XBOX",

_ => throw new ArgumentOutOfRangeException()
};
Expand Down
23 changes: 23 additions & 0 deletions DragonFruit.Six.Api/Modern/Utils/ModernSeason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Dragon6 API Copyright DragonFruit Network <inbox@dragonfruit.network>
// Licensed under Apache-2. Refer to the LICENSE file for more info

using System;

namespace DragonFruit.Six.Api.Modern.Utils
{
public readonly struct ModernSeason
{
public ModernSeason(int year, int season)
{
Year = year;
Season = Math.Max(season, 4);
}

public int Year { get; }
public int Season { get; }

public int SeasonNumber => (Year - 1) * 4 + Season;

public override string ToString() => $"Y{Year}S{Season}";
}
}

0 comments on commit c3fcd0a

Please sign in to comment.