-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented roster statistics. (#10)
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 56 additions & 2 deletions
58
src/PokeData.Application/Roster/Queries/ReadPokemonRosterQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,73 @@ | ||
using MediatR; | ||
using PokeData.Application.Regions; | ||
using PokeData.Application.Types; | ||
using PokeData.Contracts.Regions; | ||
using PokeData.Contracts.Roster; | ||
using PokeData.Contracts.Search; | ||
using PokeData.Contracts.Types; | ||
|
||
namespace PokeData.Application.Roster.Queries; | ||
|
||
internal class ReadPokemonRosterQueryHandler : IRequestHandler<ReadPokemonRosterQuery, PokemonRoster> | ||
{ | ||
private readonly IRegionQuerier _regionQuerier; | ||
private readonly IPokemonRosterQuerier _rosterQuerier; | ||
private readonly IPokemonTypeQuerier _typeQuerier; | ||
|
||
public ReadPokemonRosterQueryHandler(IPokemonRosterQuerier rosterQuerier) | ||
public ReadPokemonRosterQueryHandler(IRegionQuerier regionQuerier, IPokemonRosterQuerier rosterQuerier, IPokemonTypeQuerier typeQuerier) | ||
{ | ||
_regionQuerier = regionQuerier; | ||
_rosterQuerier = rosterQuerier; | ||
_typeQuerier = typeQuerier; | ||
} | ||
|
||
public async Task<PokemonRoster> Handle(ReadPokemonRosterQuery _, CancellationToken cancellationToken) | ||
{ | ||
return await _rosterQuerier.ReadAsync(cancellationToken); | ||
PokemonRoster roster = await _rosterQuerier.ReadAsync(cancellationToken); | ||
|
||
SearchResults<Region> regions = await _regionQuerier.SearchAsync(new SearchRegionsPayload(), cancellationToken); | ||
Dictionary<string, int> regionCount = new(capacity: regions.Items.Count); | ||
foreach (Region region in regions.Items) | ||
{ | ||
string key = region.DisplayName ?? region.UniqueName; | ||
regionCount[key] = 0; | ||
} | ||
|
||
SearchResults<PokemonType> types = await _typeQuerier.SearchAsync(new SearchPokemonTypesPayload(), cancellationToken); | ||
Dictionary<string, int> typeCount = new(capacity: types.Items.Count); | ||
foreach (PokemonType type in types.Items) | ||
{ | ||
string key = type.DisplayName ?? type.UniqueName; | ||
typeCount[key] = 0; | ||
} | ||
|
||
ushort selected = 0; | ||
ushort selectedTypes = 0; | ||
foreach (RosterItem item in roster.Items) | ||
{ | ||
if (item.Destination != null) | ||
{ | ||
selected++; | ||
selectedTypes++; | ||
|
||
regionCount[item.Destination.Region]++; | ||
typeCount[item.Destination.PrimaryType]++; | ||
|
||
if (item.Destination.SecondaryType != null) | ||
{ | ||
selectedTypes++; | ||
typeCount[item.Destination.SecondaryType]++; | ||
} | ||
} | ||
} | ||
|
||
roster.Stats = new RosterStatistics | ||
{ | ||
Count = selected, | ||
Regions = regionCount.Select(pair => RosterStatistic.Create(pair.Key, pair.Value, selected)).ToList(), | ||
Types = typeCount.Select(pair => RosterStatistic.Create(pair.Key, pair.Value, selectedTypes)).ToList() | ||
}; | ||
|
||
return roster; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace PokeData.Contracts.Roster; | ||
|
||
public record RosterStatistic | ||
{ | ||
public string Key { get; set; } = string.Empty; | ||
public int Count { get; set; } | ||
public double Percentage { get; set; } | ||
|
||
public static RosterStatistic Create(string key, int count, int total) => new() | ||
{ | ||
Key = key, | ||
Count = count, | ||
Percentage = count / (double)total | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace PokeData.Contracts.Roster; | ||
|
||
public record RosterStatistics | ||
{ | ||
public ushort Count { get; set; } | ||
public List<RosterStatistic> Regions { get; set; } = []; | ||
public List<RosterStatistic> Types { get; set; } = []; | ||
} |