the browser-facing portion of osu!
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\Libraries\Search;
9
10use App\Libraries\Elasticsearch\SearchParams;
11use App\Libraries\Elasticsearch\Sort;
12use App\Models\Solo\Score;
13use App\Models\User;
14use App\Models\UserProfileCustomization;
15
16class ScoreSearchParams extends SearchParams
17{
18 const VALID_TYPES = ['global', 'country', 'friend'];
19 const DEFAULT_TYPE = 'global';
20
21 public ?array $beatmapIds = null;
22 public ?Score $beforeScore = null;
23 public ?int $beforeTotalScore = null;
24 public bool $excludeConverts = false;
25 public ?array $excludeMods = null;
26 public ?bool $isLegacy = null;
27 public bool $excludeWithoutPp = false;
28 public ?array $mods = null;
29 public ?int $rulesetId = null;
30 public $size = 50;
31 public ?User $user = null;
32 public ?int $userId = null;
33
34 private ?string $countryCode = null;
35 private string $type = self::DEFAULT_TYPE;
36
37 public static function fromArray(array $rawParams): static
38 {
39 $params = new static();
40 $params->beatmapIds = $rawParams['beatmap_ids'] ?? null;
41 $params->excludeConverts = $rawParams['exclude_converts'] ?? $params->excludeConverts;
42 $params->excludeMods = $rawParams['exclude_mods'] ?? null;
43 $params->excludeWithoutPp = $rawParams['exclude_without_pp'] ?? $params->excludeWithoutPp;
44 $params->isLegacy = $rawParams['is_legacy'] ?? null;
45 $params->mods = $rawParams['mods'] ?? null;
46 $params->rulesetId = $rawParams['ruleset_id'] ?? null;
47 $params->userId = $rawParams['user_id'] ?? null;
48
49 $params->setCountryCode($rawParams['country_code'] ?? null);
50 $params->setType($rawParams['type'] ?? null);
51
52 $params->beforeScore = $rawParams['before_score'] ?? null;
53 $params->beforeTotalScore = $rawParams['before_total_score'] ?? null;
54 $params->size = $rawParams['limit'] ?? $params->size;
55 $params->user = $rawParams['user'] ?? null;
56 $params->setSort($rawParams['sort'] ?? null);
57
58 return $params;
59 }
60
61 /**
62 * This returns value for isLegacy based on user preference, request type, and `legacy_only` parameter
63 */
64 public static function showLegacyForUser(
65 ?User $user = null,
66 ?bool $legacyOnly = null,
67 ?bool $isApiRequest = null
68 ): null | true {
69 $isApiRequest ??= is_api_request();
70 // `null` is actual parameter value for the other two parameters so
71 // only try filling them up if not passed at all.
72 $argLen = func_num_args();
73 if ($argLen < 2) {
74 $legacyOnly = get_bool(Request('legacy_only'));
75
76 if ($argLen < 1) {
77 $user = \Auth::user();
78 }
79 }
80
81 if ($legacyOnly !== null) {
82 return $legacyOnly ? true : null;
83 }
84
85 if ($isApiRequest) {
86 return null;
87 }
88
89 $profileCustomization = UserProfileCustomization::forUser($user);
90
91 return $profileCustomization['legacy_score_only']
92 ? true
93 : null;
94 }
95
96 public function getCountryCode(): string
97 {
98 return $this->countryCode ?? $this->user->country_acronym;
99 }
100
101 public function getFriendIds(): array
102 {
103 return [...$this->user->friends()->allRelatedIds(), $this->user->getKey()];
104 }
105
106 public function getType(): string
107 {
108 return $this->type;
109 }
110
111 public function isCacheable(): bool
112 {
113 return false;
114 }
115
116 public function setCountryCode(?string $countryCode): void
117 {
118 $this->countryCode = $countryCode;
119 }
120
121 public function setSort(?string $sort): void
122 {
123 switch ($sort) {
124 case 'score_desc':
125 $sortColumn = $this->isLegacy ? 'legacy_total_score' : 'total_score';
126 $this->sorts = [
127 new Sort($sortColumn, 'desc'),
128 new Sort('id', 'asc'),
129 ];
130 break;
131 case 'pp_desc':
132 $this->sorts = [
133 new Sort('pp', 'desc'),
134 new Sort('id', 'asc'),
135 ];
136 break;
137 case null:
138 $this->sorts = [];
139 break;
140 }
141 }
142
143 public function setType(?string $type): void
144 {
145 if (in_array($type, static::VALID_TYPES, true)) {
146 $this->type = $type;
147 }
148 }
149}