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

fix(home): dont show duplicate user+game pairs in new sets/revisions #1536

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -1,38 +1,104 @@
<?php

declare(strict_types=1);

use LegacyApp\Community\Enums\ClaimFilters;
use LegacyApp\Community\Enums\ClaimSorting;

$desiredClaimCount = $count;
/**
* Creates a unique key based on the given game title and user.
* This key can then be used for convenient comparison of potential duplicates.
*/
$createClaimKey = function (string $gameTitle, string $user): string {
return $gameTitle . '_' . $user;
};

/**
* Compares the created timestamps of two claims to
* determine their chronological order.
*/
$isOlderClaim = function (array $claim1, array $claim2): bool {
return strtotime($claim1['Created']) < strtotime($claim2['Created']);
};

/**
* Replaces an existing claim in the claim data array that is to
* be rendered in the UI with a new claim. This function is used when
* a new claim is found to be older than the existing claim (ie- a duplicate).
*/
$updateClaimData = function (array &$claimData, array $existingClaim, array $newClaim): void {
foreach ($claimData as $index => $claim) {
if ($claim['ID'] === $existingClaim['ID']) {
$claimData[$index] = $newClaim;
break;
}
}
};

/**
* Processes a batch of claims and updates the unique claims and claim
* data arrays. This function is responsible for filtering out duplicate
* claims and claims belonging to consoles that haven't been rolled out yet.
* Ideally, if duplicates are detected, we want to keep the oldest duplicate.
*/
$processClaimsBatch = function (
array &$uniqueClaims,
array &$claimData,
array $claimsBatch,
int &$remainingClaimsToFetch
) use (
$createClaimKey,
$isOlderClaim,
$updateClaimData
): void {
foreach ($claimsBatch as $batchedClaim) {
if (isValidConsoleId($batchedClaim['ConsoleID'])) {
$claimKey = $createClaimKey($batchedClaim['GameTitle'], $batchedClaim['User']);

if (!isset($uniqueClaims[$claimKey])) {
$uniqueClaims[$claimKey] = $batchedClaim;
$claimData[] = $batchedClaim;
$remainingClaimsToFetch--;
} else {
$existingClaim = $uniqueClaims[$claimKey];

if ($isOlderClaim($batchedClaim, $existingClaim)) {
$uniqueClaims[$claimKey] = $batchedClaim;
$updateClaimData($claimData, $existingClaim, $batchedClaim);
}
}

if ($remainingClaimsToFetch === 0) {
break;
}
}
}
};

$desiredClaimCount = (int) $count;
$claimData = [];
$remainingClaimsToFetch = $desiredClaimCount;
$remainingClaimsToFetch = (int) $desiredClaimCount;
$currentOffset = 0;

// Continue fetching claims until the desired number of claims
// have been added to `$claimData`.
$uniqueClaims = [];

/**
* Continue fetching and processing claims until the desired number of
* unique claims is reached or no more claims are available.
*/
while ($remainingClaimsToFetch > 0) {
$currentClaimsBatch = getFilteredClaims(
claimFilter: ClaimFilters::AllCompletedPrimaryClaims,
sortType: ClaimSorting::FinishedDateDescending,
offset: $currentOffset,
limit: $count,
limit: (int) $count,
);

if ($currentClaimsBatch->isEmpty()) {
break;
}

foreach ($currentClaimsBatch as $batchedClaim) {
if (isValidConsoleId($batchedClaim['ConsoleID'])) {
$claimData[] = $batchedClaim;
$remainingClaimsToFetch--;

if ($remainingClaimsToFetch === 0) {
break;
}
}
}

$processClaimsBatch($uniqueClaims, $claimData, $currentClaimsBatch->toArray(), $remainingClaimsToFetch);
$currentOffset += $count;
}

Expand Down