Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(site-award): query for game_sets in SeparateAwards() #3066

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 52 additions & 21 deletions app/Helpers/render/site-award.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,69 @@
use App\Community\Enums\AwardType;
use App\Models\Event;
use App\Models\EventAward;
use App\Models\GameSet;
use App\Models\PlayerBadge;

function SeparateAwards(array $userAwards): array
{
$gameAwards = array_values(array_filter($userAwards, fn ($award) => $award['AwardType'] == AwardType::Mastery && $award['ConsoleName'] != 'Events'));
// Pre-calculate type checks and perform partitioning in a single pass.
$typeInfo = [];
$gameAwards = [];
$eventAwards = [];

// Process each award: store its type info for later O(1) lookups.
foreach ($userAwards as $key => $award) {
$type = (int) $award['AwardType'];
$typeInfo[$key] = ['isGame' => AwardType::isGame($type), 'isActive' => AwardType::isActive($type)];

if ($type === AwardType::Event || ($type === AwardType::Mastery && $award['ConsoleName'] === 'Events')) {
$eventAwards[] = $award;
} elseif ($type === AwardType::Mastery) {
$gameAwards[] = $award;
}
}

$eventAwards = array_filter($userAwards, fn ($award) => $award['AwardType'] == AwardType::Event || ($award['AwardType'] == AwardType::Mastery && $award['ConsoleName'] == 'Events'));
// If there are no event awards, don't even check for dev events. We're done.
if (empty($eventAwards)) {
return [
$gameAwards,
[],
array_values(array_filter($userAwards, fn ($award, $key) => !$typeInfo[$key]['isGame'] && $typeInfo[$key]['isActive'], ARRAY_FILTER_USE_BOTH)),
];
}

$devEventsPrefix = "[Dev Events - ";
$devEventsHub = "[Central - Developer Events]";
// Find dev event games with a single optimized query.
$devEventGamesLookup = array_flip(GameSet::query()
->whereHas('games', fn ($q) => $q->whereIn('game_id', array_column($eventAwards, 'AwardData')))
->where(fn ($q) => $q->where('id', 5)->orWhere('title', 'like', '[Dev Events - %'))
->with(['games' => fn ($q) => $q->select('GameData.ID as id')])
->get()
->pluck('games.*.id')
->flatten()
->unique()
->values()
->all());

// Separate dev event awards and regular event awards with O(1) lookups.
$devEventAwards = [];
foreach ($eventAwards as $eventAward) {
$related = getGameAlternatives($eventAward['AwardData']);
foreach ($related as $hub) {
if ($hub['Title'] == $devEventsHub || str_starts_with($hub['Title'], $devEventsPrefix)) {
$devEventAwards[] = $eventAward;
break;
}
$regularEventAwards = [];
foreach ($eventAwards as $award) {
if (isset($devEventGamesLookup[$award['AwardData']])) {
$devEventAwards[] = $award;
} else {
$regularEventAwards[] = $award;
}
}

$eventAwards = array_values(array_filter($eventAwards, fn ($award) => !in_array($award, $devEventAwards)));

$siteAwards = array_values(array_filter($userAwards, function ($userAward) use ($devEventAwards) {
$isNotMasteryOrGameBeaten = !AwardType::isGame((int) $userAward['AwardType']);
$isActiveAwardType = AwardType::isActive((int) $userAward['AwardType']);
$isDevEventAward = in_array($userAward, $devEventAwards);

return ($isNotMasteryOrGameBeaten && $isActiveAwardType) || $isDevEventAward;
}));
// Calculate site awards including dev event awards.
$devEventAwardsLookup = array_flip(array_map('serialize', $devEventAwards));

return [$gameAwards, $eventAwards, $siteAwards];
return [
$gameAwards,
$regularEventAwards,
array_values(array_filter($userAwards, fn ($award, $key) => isset($devEventAwardsLookup[serialize($award)])
|| (!$typeInfo[$key]['isGame'] && $typeInfo[$key]['isActive']), ARRAY_FILTER_USE_BOTH)),
];
}

function RenderSiteAwards(array $userAwards, string $awardsOwnerUsername): void
Expand Down