Skip to content

Commit

Permalink
Implemented roster statistics. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 authored Jan 1, 2024
1 parent 1a55de8 commit 9bafb06
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implemented species extraction to JSON and CSV.
- Implemented a Web API.
- Implemented roster management.
- Implemented roster statistics.
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;
}
}
2 changes: 1 addition & 1 deletion src/PokeData.Contracts/Roster/PokemonRoster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public record PokemonRoster
{
public List<RosterItem> Items { get; set; } = [];
// TODO(fpion): Stats
public RosterStatistics Stats { get; set; } = new();
}
15 changes: 15 additions & 0 deletions src/PokeData.Contracts/Roster/RosterStatistic.cs
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
};
}
8 changes: 8 additions & 0 deletions src/PokeData.Contracts/Roster/RosterStatistics.cs
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; } = [];
}

0 comments on commit 9bafb06

Please sign in to comment.