-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAchievementsController.php
executable file
·73 lines (62 loc) · 2.36 KB
/
AchievementsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace App\Http\Controllers;
use App\Achievements\Contents\AuthorCommentInteraction;
use App\Achievements\Contents\AuthorHighCommentInteraction;
use App\Achievements\Contents\AuthorHighInteraction;
use App\Achievements\Contents\AuthorInteraction;
use App\Achievements\Contents\CommentInteraction;
use App\Achievements\Contents\HighCommentInteraction;
use App\Achievements\Events\OpeningWebSite;
use App\Achievements\Unique\BigMzungu;
use App\Achievements\Unique\Lipa;
use App\Achievements\Unique\Troll;
use App\Models\Achievement;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class AchievementsController extends Controller
{
/**
* @param \App\Models\CodeSnippet|null $snippet
*
* @return \Illuminate\Contracts\View\View
*/
public function index(Request $request)
{
$groups = collect([
'Контент и Дискуссии' => [
AuthorInteraction::class,
AuthorHighInteraction::class,
AuthorCommentInteraction::class,
AuthorHighCommentInteraction::class,
CommentInteraction::class,
HighCommentInteraction::class,
],
'Уникальные' => [
Troll::class,
Lipa::class,
BigMzungu::class,
],
'Событийные' => [
OpeningWebSite::class,
],
])->map(fn ($achievements) => $this->setDataForAchievement($achievements, $request->user()));
return view('achievements.index', [
'groups' => $groups,
]);
}
protected function setDataForAchievement(array $achievements, ?User $user): Collection
{
return collect($achievements)
->map(fn ($achievement) => app($achievement))
->map(function ($achievement) use ($user) {
$achievement->used = ! is_null($user) && $user->achievements()
->where('achievement_type', $achievement::class)
->exists();
$countAllUsers = User::count();
$countUses = Achievement::where('achievement_type', $achievement::class)->count();
$achievement->percent = $countUses == 0 ? 0 : round($countUses / $countAllUsers * 100);
return $achievement;
});
}
}