Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add seasons infrastructure and basic display #9725

Merged
merged 50 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
c5b8836
seasons
venix12 Jan 10, 2023
6717ba9
Delete multiplayer-select-options.tsx
venix12 Jan 10, 2023
1a140cc
Delete user-multiplayer-history-json.ts
venix12 Jan 10, 2023
4423b88
Apply suggestions from code review
venix12 Jan 25, 2023
384bfaf
rebase (#12)
venix12 Jan 25, 2023
07347e8
Merge branch 'master' into seasons
venix12 Jan 25, 2023
172e84a
clean-up removes
venix12 Jan 25, 2023
452ad3a
remove unused interface
venix12 Jan 25, 2023
0a0a338
use string cursor functions
venix12 Jan 25, 2023
b683f94
unique index
venix12 Jan 25, 2023
ca57aec
use exists for hasRooms() instead
venix12 Jan 25, 2023
70faca2
remove hasRooms function
venix12 Jan 26, 2023
85300a9
Merge branch 'master' into seasons
venix12 Jan 29, 2023
869366c
Merge branch 'master' into seasons
venix12 Jan 31, 2023
43017c3
apply suggestions from code review
venix12 Jan 31, 2023
731a911
MultiplayerList -> RoomList
venix12 Jan 31, 2023
b732068
unify getJson for room controllers
venix12 Feb 1, 2023
ad7a127
unify seasons lookup
venix12 Feb 1, 2023
5360d81
clean-up responseJson
venix12 Feb 1, 2023
5f70687
make startDate and endDate into optional includes
venix12 Feb 1, 2023
e7791d9
remove excess newline
venix12 Feb 1, 2023
9288f21
make type_group optional part of response
venix12 Feb 1, 2023
53565f4
Merge branch 'master' into seasons
venix12 Feb 1, 2023
79051d1
lint
venix12 Feb 1, 2023
584e087
Merge branch 'seasons' of https://github.com/venix12/osu-web into sea…
venix12 Feb 1, 2023
cf9a0d8
exception instead of abort
venix12 Feb 1, 2023
75178d3
clean-up deprecated parameter
venix12 Feb 2, 2023
f1bca6c
don't require params for show
venix12 Feb 2, 2023
29474f7
support max_limit param for room search function
venix12 Feb 2, 2023
69885d8
pass typeGroup as props instead of store
venix12 Feb 2, 2023
45331c7
remove unused interface fields
venix12 Feb 2, 2023
7943959
move mode directly to ranking.index
venix12 Feb 2, 2023
8953edd
showMoreRoute -> showMoreUrl
venix12 Feb 2, 2023
acdaa64
fix syntax
venix12 Feb 2, 2023
9b23270
move select options json to separate transformer
venix12 Feb 2, 2023
848272f
don't lookup season for rooms endpoint
venix12 Feb 2, 2023
6f3c26d
lint
venix12 Feb 2, 2023
836970d
concluded -> finalised
venix12 Feb 2, 2023
62d3688
make latestOrId into static function instead
venix12 Feb 3, 2023
0427ec1
make maxlimit into search argument
venix12 Feb 3, 2023
34ac0f5
add very basic url workflow
venix12 Feb 3, 2023
263f706
more accurate name
venix12 Feb 3, 2023
6aef4be
remove maxLimit clamp
venix12 Feb 3, 2023
d5840ed
proper english
venix12 Feb 3, 2023
c0ab953
remove unused param
venix12 Feb 3, 2023
fef9f6c
add ranking select option interface
venix12 Feb 3, 2023
a32b1a7
add seasons to internalUrls
venix12 Feb 3, 2023
8626229
Merge branch 'master' into seasons
nanaya Feb 3, 2023
6ea0a3e
redate migrations
venix12 Feb 3, 2023
5307363
Merge branch 'seasons' of https://github.com/venix12/osu-web into sea…
venix12 Feb 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions app/Http/Controllers/SeasonsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. 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;

use App\Models\Multiplayer\Room;
use App\Models\Season;
use App\Transformers\RankingSelectOptionTransformer;
use App\Transformers\SeasonTransformer;

class SeasonsController extends Controller
{
public function rooms($id)
{
$params = $this->paramsForResponse($id, request()->all());
$roomsJson = Room::responseJson($params);

return $roomsJson;
}

public function show($id)
{
$season = Season::latestOrId($id);

$params = $this->paramsForResponse($season->getKey());
$roomsJson = Room::responseJson($params);

$seasonJson = json_item($season, new SeasonTransformer());

$seasons = Season::orderByDesc('id')->get();
$seasonsJson = json_collection($seasons, new RankingSelectOptionTransformer());

return ext_view('seasons.show', compact('roomsJson', 'season', 'seasonJson', 'seasonsJson'));
}

private function paramsForResponse(int $seasonId, ?array $rawParams = null)
{
return [
'cursor' => cursor_from_params($rawParams),
'limit' => get_int($rawParams['limit'] ?? null),
'mode' => 'all',
'season_id' => $seasonId,
];
}
}
39 changes: 6 additions & 33 deletions app/Http/Controllers/Users/MultiplayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use App\Http\Controllers\Controller;
use App\Libraries\User\FindForProfilePage;
use App\Models\Multiplayer\Room;
use App\Models\User;
use App\Transformers\Multiplayer\RoomTransformer;
use App\Transformers\UserTransformer;

class MultiplayerController extends Controller
Expand All @@ -25,10 +23,14 @@ public function index($userId, $typeGroup)
$rawParams = request()->all();
$params = [
'cursor' => cursor_from_params($rawParams),
'limit' => get_int($params['limit'] ?? null),
'limit' => get_int($rawParams['limit'] ?? null),
'mode' => 'participated',
'sort' => 'ended',
'type_group' => $typeGroup,
'user' => $user,
];

$json = $this->getJson($user, $params, $typeGroup);
$json = Room::responseJson($params);

if (is_json_request()) {
return $json;
Expand All @@ -42,33 +44,4 @@ public function index($userId, $typeGroup)

return ext_view('users.multiplayer.index', compact('json', 'jsonUser', 'user'));
}

private function getJson(User $user, array $params, string $typeGroup)
{
$limit = clamp($params['limit'] ?? 50, 1, 50);

$search = Room::search([
'cursor' => $params['cursor'],
'user' => $user,
'limit' => $limit,
'mode' => 'participated',
'sort' => 'ended',
'type_group' => $typeGroup,
]);

[$rooms, $hasMore] = $search['query']->with([
'playlist.beatmap',
'host',
])->getWithHasMore();
$rooms->each->findAndSetCurrentPlaylistItem();
$rooms->loadMissing('currentPlaylistItem.beatmap.beatmapset');

$nextCursor = $hasMore ? $search['cursorHelper']->next($rooms) : null;

return array_merge([
'rooms' => json_collection($rooms, new RoomTransformer(), ['current_playlist_item.beatmap.beatmapset', 'difficulty_range', 'host', 'playlist_item_stats']),
'search' => $search['search'],
'type_group' => $typeGroup,
], cursor_for_response($nextCursor));
}
}
3 changes: 3 additions & 0 deletions app/Libraries/RouteSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class RouteSection
'scores_controller' => [
'_' => 'beatmaps',
],
'seasons_controller' => [
'_' => 'rankings',
],
'sessions_controller' => [
'_' => 'user',
],
Expand Down
47 changes: 45 additions & 2 deletions app/Models/Multiplayer/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use App\Models\Beatmap;
use App\Models\Chat\Channel;
use App\Models\Model;
use App\Models\Season;
use App\Models\SeasonRoom;
use App\Models\Traits\WithDbCursorHelper;
use App\Models\User;
use App\Traits\Memoizes;
use App\Transformers\Multiplayer\RoomTransformer;
use Carbon\Carbon;
use Ds\Set;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -33,6 +36,7 @@
* @property int $participant_count
* @property \Illuminate\Database\Eloquent\Collection $playlist PlaylistItem
* @property \Illuminate\Database\Eloquent\Collection $scores Score
* @property-read Collection<\App\Models\Season> $seasons
* @property \Carbon\Carbon $starts_at
* @property \Carbon\Carbon|null $updated_at
* @property int $user_id
Expand Down Expand Up @@ -103,18 +107,46 @@ public static function preloadRecentParticipants(Collection $rooms)
}
}

public static function search(array $rawParams)
public static function responseJson(array $rawParams): array
{
$typeGroup = $rawParams['type_group'] ?? null;

$search = static::search($rawParams, 50);

[$rooms, $hasMore] = $search['query']->with([
'playlist.beatmap',
'host',
])->getWithHasMore();

$rooms->each->findAndSetCurrentPlaylistItem();
$rooms->loadMissing('currentPlaylistItem.beatmap.beatmapset');

$response['rooms'] = json_collection($rooms, new RoomTransformer(), ['current_playlist_item.beatmap.beatmapset', 'difficulty_range', 'host', 'playlist_item_stats']);

if ($typeGroup !== null) {
$response['type_group'] = $typeGroup;
}

$nextCursor = $hasMore ? $search['cursorHelper']->next($rooms) : null;

return array_merge($response, cursor_for_response($nextCursor));
}

public static function search(array $rawParams, ?int $maxLimit = null)
{
$params = get_params($rawParams, null, [
'category',
'limit:int',
'mode',
'season_id:int',
'sort',
'type_group',
'user:any',
], ['null_missing' => true]);

$maxLimit ??= 250;
$user = $params['user'];
$seasonId = $params['season_id'];
$sort = $params['sort'];
$category = $params['category'];
$typeGroup = $params['type_group'];
Expand All @@ -132,11 +164,17 @@ public static function search(array $rawParams)

$query = static::whereIn('type', static::TYPE_GROUPS[$typeGroup]);

if (isset($seasonId)) {
$query->whereRelation('seasons', 'seasons.id', $seasonId);
}

if (in_array($category, static::CATEGORIES, true)) {
$query->where('category', $category);
}

switch ($params['mode']) {
case 'all':
break;
case 'ended':
$query->ended();
$sort ??= 'ended';
Expand All @@ -154,7 +192,7 @@ public static function search(array $rawParams)
$cursorHelper = static::makeDbCursorHelper($sort);
$query->cursorSort($cursorHelper, cursor_from_params($rawParams));

$limit = clamp($params['limit'] ?? 250, 1, 250);
$limit = clamp($params['limit'] ?? $maxLimit, 1, $maxLimit);
$query->limit($limit);

return [
Expand Down Expand Up @@ -192,6 +230,11 @@ public function scores()
return $this->hasMany(Score::class);
}

public function seasons()
{
return $this->belongsToMany(Season::class, SeasonRoom::class);
}

public function userHighScores()
{
return $this->hasMany(UserScoreAggregate::class);
Expand Down
57 changes: 57 additions & 0 deletions app/Models/Season.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

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

declare(strict_types=1);

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* @property bool $finalised
* @property string $name
* @property-read Collection<Multiplayer\Room> $rooms
* @property string|null $url
*/
class Season extends Model
{
protected $casts = [
'finalised' => 'boolean',
];

public static function latestOrId($id)
{
if ($id === 'latest') {
$season = static::last();

if ($season === null) {
throw new ModelNotFoundException();
}
} else {
$season = static::findOrFail($id);
}

return $season;
}

public function endDate(): ?Carbon
{
return $this->finalised
? $this->rooms->max('ends_at')
: null;
}

public function startDate(): ?Carbon
{
return $this->rooms->min('starts_at');
}

public function rooms(): BelongsToMany
{
return $this->belongsToMany(Multiplayer\Room::class, SeasonRoom::class);
}
}
20 changes: 20 additions & 0 deletions app/Models/SeasonRoom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property int $room_id
* @property int $season_id
*/
class SeasonRoom extends Model
{
public $timestamps = false;
}
22 changes: 22 additions & 0 deletions app/Transformers/RankingSelectOptionTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

declare(strict_types=1);

namespace App\Transformers;

use App\Models\Multiplayer\Room;
use App\Models\Season;

class RankingSelectOptionTransformer extends TransformerAbstract
{
public function transform(Room|Season $item): array
{
return [
'id' => $item->getKey(),
'name' => $item->name,
];
}
}
24 changes: 24 additions & 0 deletions app/Transformers/SeasonTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

declare(strict_types=1);

namespace App\Transformers;

use App\Models\Season;

class SeasonTransformer extends TransformerAbstract
{
public function transform(Season $season): array
{
return [
'end_date' => $season->endDate(),
'id' => $season->getKey(),
'name' => $season->name,
'room_count' => $season->rooms()->count(),
venix12 marked this conversation as resolved.
Show resolved Hide resolved
'start_date' => $season->startDate(),
];
}
}
1 change: 1 addition & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ function nav_links()
'rankings.type.score' => route('rankings', ['mode' => $defaultMode, 'type' => 'score']),
'rankings.type.country' => route('rankings', ['mode' => $defaultMode, 'type' => 'country']),
'rankings.type.multiplayer' => route('multiplayer.rooms.show', ['room' => 'latest']),
'rankings.type.seasons' => route('seasons.show', ['season' => 'latest']),
'layout.menu.rankings.kudosu' => osu_url('rankings.kudosu'),
];
$links['community'] = [
Expand Down
28 changes: 28 additions & 0 deletions database/factories/SeasonFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Season;
use Illuminate\Database\Eloquent\Factories\Factory;

class SeasonFactory extends Factory
{
protected $model = Season::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition(): array
{
return [
'name' => $this->faker->sentence(),
];
}
}
Loading