<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers\Multiplayer\Rooms\Playlist;

use App\Exceptions\InvariantException;
use App\Http\Controllers\Controller as BaseController;
use App\Libraries\DbCursorHelper;
use App\Libraries\Multiplayer\Mod;
use App\Models\Multiplayer\PlaylistItem;
use App\Models\Multiplayer\PlaylistItemUserHighScore;
use App\Models\Multiplayer\Room;
use App\Transformers\Multiplayer\ScoreTransformer;
use Carbon\Carbon;

class ScoresController extends BaseController
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index($roomId, $playlistId)
    {
        $playlist = PlaylistItem::where('room_id', $roomId)->where('id', $playlistId)->firstOrFail();
        $params = request()->all();
        $cursorHelper = new DbCursorHelper(
            PlaylistItemUserHighScore::SORTS,
            PlaylistItemUserHighScore::DEFAULT_SORT,
            get_string($params['sort'] ?? null)
        );

        $sort = $cursorHelper->getSort();
        $cursor = $cursorHelper->prepare($params['cursor'] ?? null);
        $limit = clamp(get_int($params['limit'] ?? null) ?? 50, 1, 50);

        $highScores = $playlist
            ->highScores()
            ->cursorSort($sort, $cursor)
            ->with(ScoreTransformer::BASE_PRELOAD)
            ->limit($limit + 1) // an extra to check for pagination
            ->get();

        $hasMore = count($highScores) === $limit + 1;
        if ($hasMore) {
            $highScores->pop();
        }
        $scoresJson = json_collection($highScores->pluck('score'),
            'Multiplayer\Score',
            ScoreTransformer::BASE_INCLUDES
        );
        $total = $playlist->highScores()->count();

        $user = auth()->user();

        if ($user !== null) {
            $userHighScore = $playlist->highScores()->where('user_id', $user->getKey())->first();

            if ($userHighScore !== null) {
                $userScoreJson = json_item($userHighScore->score, 'Multiplayer\Score', ScoreTransformer::BASE_INCLUDES);
            }
        }

        return [
            'scores' => $scoresJson,
            'total' => $total,
            'user_score' => $userScoreJson ?? null,
            'cursor' => $hasMore ? $cursorHelper->next($highScores) : null,
        ];
    }

    public function show($roomId, $playlistId, $id)
    {
        $room = Room::find($roomId) ?? abort(404, 'Invalid room id');
        $playlistItem = $room->playlist()->find($playlistId) ?? abort(404, 'Invalid playlist id');
        $score = $playlistItem->scores()->findOrFail($id);

        return json_item(
            $score,
            'Multiplayer\Score',
            ['position', 'scores_around', ...ScoreTransformer::BASE_INCLUDES]
        );<commit_msg>Add params to response<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers\Multiplayer\Rooms\Playlist;

use App\Exceptions\InvariantException;
use App\Http\Controllers\Controller as BaseController;
use App\Libraries\DbCursorHelper;
use App\Libraries\Multiplayer\Mod;
use App\Models\Multiplayer\PlaylistItem;
use App\Models\Multiplayer\PlaylistItemUserHighScore;
use App\Models\Multiplayer\Room;
use App\Transformers\Multiplayer\ScoreTransformer;
use Carbon\Carbon;

class ScoresController extends BaseController
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index($roomId, $playlistId)
    {
        $playlist = PlaylistItem::where('room_id', $roomId)->where('id', $playlistId)->firstOrFail();
        $params = request()->all();
        $cursorHelper = new DbCursorHelper(
            PlaylistItemUserHighScore::SORTS,
            PlaylistItemUserHighScore::DEFAULT_SORT,
            get_string($params['sort'] ?? null)
        );

        $sort = $cursorHelper->getSort();
        $cursor = $cursorHelper->prepare($params['cursor'] ?? null);
        $limit = clamp(get_int($params['limit'] ?? null) ?? 50, 1, 50);

        $highScores = $playlist
            ->highScores()
            ->cursorSort($sort, $cursor)
            ->with(ScoreTransformer::BASE_PRELOAD)
            ->limit($limit + 1) // an extra to check for pagination
            ->get();

        $hasMore = count($highScores) === $limit + 1;
        if ($hasMore) {
            $highScores->pop();
        }
        $scoresJson = json_collection($highScores->pluck('score'),
            'Multiplayer\Score',
            ScoreTransformer::BASE_INCLUDES
        );
        $total = $playlist->highScores()->count();

        $user = auth()->user();

        if ($user !== null) {
            $userHighScore = $playlist->highScores()->where('user_id', $user->getKey())->first();

            if ($userHighScore !== null) {
                $userScoreJson = json_item($userHighScore->score, 'Multiplayer\Score', ScoreTransformer::BASE_INCLUDES);
            }
        }

        return [
            'cursor' => $hasMore ? $cursorHelper->next($highScores) : null,
            'params' => ['limit' => $limit, 'sort' => $cursorHelper->getSortName()],
            'scores' => $scoresJson,
            'total' => $total,
            'user_score' => $userScoreJson ?? null,
        ];
    }

    public function show($roomId, $playlistId, $id)
    {
        $room = Room::find($roomId) ?? abort(404, 'Invalid room id');
        $playlistItem = $room->playlist()->find($playlistId) ?? abort(404, 'Invalid playlist id');
        $score = $playlistItem->scores()->findOrFail($id);

        return json_item(
            $score,
            'Multiplayer\Score',
            ['position', 'scores_around', ...ScoreTransformer::BASE_INCLUDES]
        );
{'fraction_non_alphanumeric': 0.14936369725385132, 'fraction_numerical': 0.005023442732752847, 'mean_word_length': 2.657685241886099, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228613', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1725, 'n_words': 342}