Skip to content

Instantly share code, notes, and snippets.

@macalinao
Created October 18, 2016 16:45
Show Gist options
  • Save macalinao/d140a0dffeecf9e5c1fd180864cdebda to your computer and use it in GitHub Desktop.
Save macalinao/d140a0dffeecf9e5c1fd180864cdebda to your computer and use it in GitHub Desktop.
// Aggregate aggregates.
func (a *aggregatorImpl) Aggregate(
aChampionId uint32,
enemyChampionId int32,
aPatch *apb.PatchRange,
aTier *apb.TierRange,
aRegion apb.Region,
aRole apb.Role,
minPlayRate float64,
) (*apb.MatchAggregate, error) {
champs, err := a.MatchSumDAO.SumsOfChampions(
aPatch, enemyChampionId, aTier, aRegion, aRole,
)
if err != nil {
return nil, fmt.Errorf("error finding champion sums: %v", err)
}
rolesSums, err := a.MatchSumDAO.SumsOfRoles(
aPatch.Max, aChampionId, enemyChampionId, aTier, aRegion,
)
if err != nil {
return nil, fmt.Errorf("error finding role sums: %v", err)
}
patches := map[string]map[uint32]*apb.MatchQuotient{}
for id, champPatches := range champs {
for patch, sum := range champPatches {
if patches[patch] == nil {
patches[patch] = map[uint32]*apb.MatchQuotient{}
}
if sum == nil {
continue
}
patches[patch][id] = makeQuotient(sum)
}
}
champions := map[uint32]*apb.MatchQuotient{}
for _, id := range a.Vulgate.GetChampionIDs() {
sum := &apb.MatchSum{}
normalizeMatchSum(sum)
for _, patch := range a.Vulgate.FindPatches(aPatch) {
// Use existing fetched patches
patchSum := champs[id][patch]
// Retrieve patch if it does not exist
if patchSum == nil {
patchSum, err = a.MatchSumDAO.SumOfPatch(
patch, aChampionId, enemyChampionId, aTier, aRegion, aRole,
)
if err != nil {
return nil, err
}
if patchSum == nil {
continue
}
}
// Append sum
sum = addMatchSums(sum, patchSum)
}
if sum == nil {
continue
}
champions[id] = makeQuotient(sum)
}
roles := map[apb.Role]*apb.MatchQuotient{}
for role, sum := range rolesSums {
if sum == nil {
continue
}
roles[role] = makeQuotient(sum)
}
// now let us build the match aggregate
return a.Deriver.Derive(aRole, champions, roles, patches, aChampionId, minPlayRate)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment