Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Jan 26, 2025
1 parent 3b4901f commit b4beaa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use App\Platform\Data\GameSuggestionContextData;
use App\Platform\Enums\GameSuggestionReason;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class CommonPlayersStrategy implements GameSuggestionStrategy
{
Expand Down Expand Up @@ -76,44 +75,14 @@ public function reasonContext(): ?GameSuggestionContextData
* @return Collection<int, int>
*/
private function getMasterUserIds(): Collection
{
$connection = DB::connection()->getDriverName();

return match ($connection) {
'sqlite' => $this->getMasterUserIdsSQLite(),
default => $this->getMasterUserIdsMariaDB(),
};
}

/**
* @return Collection<int, int>
*/
private function getMasterUserIdsMariaDB(): Collection
{
// Use the optimized index hint for MariaDB.
// This is unfortunately not supported by SQLite.
return PlayerGame::from(DB::raw('`player_games` FORCE INDEX (player_games_completion_sample_idx)'))
->where('game_id', $this->sourceGame->id)
->where('user_id', '!=', $this->user->id)
->whereAllAchievementsUnlocked()
->withTrashed()
->whereRaw('id % 100 < 5')
->limit(5)
->pluck('user_id');
}

/**
* @return Collection<int, int>
*/
private function getMasterUserIdsSQLite(): Collection
{
// For SQLite, we'll use a different approach to get a stable random sample.
// We use the rowid (which SQLite automatically provides) for sampling.
return PlayerGame::where('game_id', $this->sourceGame->id)
->where('user_id', '!=', $this->user->id)
->whereAllAchievementsUnlocked()
->withTrashed()
->orderByRaw('rowid % 100')
->whereRaw('id % 100 < 5')
->limit(5)
->pluck('user_id');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,32 @@
public function up(): void
{
Schema::table('player_games', function (Blueprint $table) {
$table->dropIndex('player_games_completion_sample_idx');

$table->index([
'user_id',
'achievements_unlocked',
'achievements_total',
'game_id',
], 'idx_player_games_suggestions'); // custom name needed because the auto-generated one is too long
], 'player_games_suggestions_index'); // custom name needed because the auto-generated one is too long
});
}

public function down(): void
{
Schema::table('player_games', function (Blueprint $table) {
$table->dropIndex('idx_player_games_suggestions');
$table->dropIndex('player_games_suggestions_index');

$table->index(
[
'game_id',
'achievements_unlocked',
'achievements_total',
'user_id',
'id',
],
'player_games_completion_sample_idx' // custom name needed because the auto-generated one is too long
);
});
}
};

0 comments on commit b4beaa3

Please sign in to comment.