Skip to content

Commit

Permalink
chore: revert some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Jan 26, 2025
1 parent db40edf commit 7acb411
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Platform\Data\GameSuggestionContextData;
use App\Platform\Enums\GameSuggestionReason;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class CommonPlayersStrategy implements GameSuggestionStrategy
{
Expand All @@ -38,7 +39,7 @@ public function select(): ?Game
->whereAllAchievementsUnlocked()
->where('achievements_total', '>', 0)
->where('game_id', '!=', $this->sourceGame->id)
->withTrashed()
->whereNull('deleted_at')
->groupBy('game_id')
->orderByRaw('COUNT(*) DESC')
->limit(10)
Expand Down Expand Up @@ -75,15 +76,45 @@ 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::where('game_id', $this->sourceGame->id)
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()
->whereNull('deleted_at')
->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()
->whereNull('deleted_at')
->orderByRaw('rowid % 100')
->limit(5)
->pluck('user_id');
}
}

0 comments on commit 7acb411

Please sign in to comment.