1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace App\Http\Controllers\Ranking;
9
10use App\Http\Controllers\Controller;
11use App\Models\Multiplayer\Room;
12use Carbon\CarbonImmutable;
13use Carbon\Exceptions\InvalidFormatException;
14
15class DailyChallengeController extends Controller
16{
17 private const string DATE_FORMAT = 'Y-m-d';
18
19 public static function roomId(Room $room): string
20 {
21 return $room->starts_at->format(static::DATE_FORMAT);
22 }
23
24 public function index()
25 {
26 $room = Room::dailyChallenges()->last() ?? abort(404);
27
28 return ujs_redirect(route('daily-challenge.show', ['daily_challenge' => static::roomId($room)]));
29 }
30
31 public function show(string $dateString)
32 {
33 try {
34 $date = CarbonImmutable::createFromFormat(static::DATE_FORMAT, $dateString);
35 } catch (InvalidFormatException) {
36 abort(404, 'invalid date');
37 }
38
39 $room = Room::dailyChallengeFor($date) ?? abort(404);
40 $playlist = $room->playlist[0];
41
42 $currentRoomOption = [
43 'id' => $dateString,
44 'text' => $dateString,
45 ];
46 $roomOptions = Room::dailyChallenges()
47 ->orderByDesc('id')
48 ->get()
49 ->map(static::roomId(...))
50 ->map(fn (string $roomName): array => [
51 'id' => $roomName,
52 'text' => $roomName,
53 ]);
54
55 $scores = $room->topScores()->paginate();
56
57 $userScore = ($currentUser = \Auth::user()) === null
58 ? null
59 : $room->topScores()->whereBelongsTo($currentUser)->first();
60
61 return ext_view('rankings.daily_challenge', compact(
62 'currentRoomOption',
63 'playlist',
64 'room',
65 'roomOptions',
66 'scores',
67 'userScore',
68 ));
69 }
70}