Files
gamelog/app/Http/Controllers/CompareController.php
2026-05-30 23:47:32 +03:00

63 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
class CompareController extends Controller
{
public function show(User $user)
{
if (auth()->id() === $user->id) {
return redirect()->route('users.show', $user->slug);
}
$myGames = auth()->user()->library()->with('game')->get()->keyBy('game_id');
$theirGames = $user->library()->with('game')->get()->keyBy('game_id');
$myIds = $myGames->keys()->values();
$theirIds = $theirGames->keys()->values();
$bothIds = $myIds->intersect($theirIds)->values();
$theyHaveNotYouIds = $theirIds->diff($myIds)->values();
$youHaveNotThemIds = $myIds->diff($theirIds)->values();
$both = $bothIds->map(fn($id) => [
'game' => $myGames[$id]->game,
'myEntry' => $myGames[$id],
'theirEntry' => $theirGames[$id],
])->filter(fn($item) => $item['game'] !== null)->values();
$theyHaveNotYou = $theyHaveNotYouIds
->map(fn($id) => $theirGames[$id])
->filter(fn($ug) => $ug->game !== null)
->values();
$youHaveNotThem = $youHaveNotThemIds
->map(fn($id) => $myGames[$id])
->filter(fn($ug) => $ug->game !== null)
->values();
// Average rating diff on games both rated
$diffSum = 0;
$diffCount = 0;
foreach ($bothIds as $id) {
$myRating = $myGames[$id]->rating;
$theirRating = $theirGames[$id]->rating;
if ($myRating !== null && $theirRating !== null) {
$diffSum += abs($myRating - $theirRating);
$diffCount++;
}
}
$stats = [
'both_count' => $bothIds->count(),
'they_not_you' => $theyHaveNotYouIds->count(),
'you_not_them' => $youHaveNotThemIds->count(),
'avg_rating_diff' => $diffCount > 0 ? round($diffSum / $diffCount, 1) : null,
];
return view('compare.show', compact('user', 'both', 'theyHaveNotYou', 'youHaveNotThem', 'stats'));
}
}