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
6namespace App\Libraries\Score;
7
8use App\Exceptions\InvariantException;
9use App\Libraries\Search\ScoreSearch;
10use App\Libraries\Search\ScoreSearchParams;
11
12class UserRank
13{
14 public static function getRank(ScoreSearchParams $params): int
15 {
16 if ($params->beforeTotalScore === null && $params->beforeScore === null) {
17 throw new InvariantException('beforeScore or beforeTotalScore must be specified');
18 }
19
20 $search = new ScoreSearch($params);
21
22 $search->size(0);
23 static $aggName = 'by_user';
24 $search->setAggregations([$aggName => ['cardinality' => [
25 'field' => 'user_id',
26 ]]]);
27 $response = $search->response();
28 $search->assertNoError();
29
30 return 1 + $response->aggregations($aggName)['value'];
31 }
32}