Skip to content

Commit

Permalink
return tags with counts with beatmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Dec 13, 2024
1 parent 6ca03d4 commit 93dc799
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/BeatmapsetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ private function showJson($beatmapset)
'beatmaps.failtimes',
'beatmaps.max_combo',
'beatmaps.owners',
'beatmaps.tags',
'converts',
'converts.failtimes',
'converts.owners',
Expand All @@ -417,7 +418,6 @@ private function showJson($beatmapset)
'recent_favourites',
'related_users',
'user',
'user_tags',
]);
}
}
6 changes: 6 additions & 0 deletions app/Transformers/BeatmapCompactTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BeatmapCompactTransformer extends TransformerAbstract
'failtimes',
'max_combo',
'owners',
'tags',
'user',
];

Expand Down Expand Up @@ -83,6 +84,11 @@ public function includeOwners(Beatmap $beatmap)
]);
}

public function includeTags(Beatmap $beatmap)
{
return $this->primitive($beatmap->topTagsJson());
}

public function includeUser(Beatmap $beatmap)
{
return $this->item(
Expand Down
18 changes: 18 additions & 0 deletions resources/js/beatmapsets-show/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

import BeatmapJson from 'interfaces/beatmap-json';
import { BeatmapsetJsonForShow } from 'interfaces/beatmapset-extended-json';
import UserJson from 'interfaces/user-json';
import { keyBy } from 'lodash';
Expand Down Expand Up @@ -70,6 +71,23 @@ export default class Controller {
return this.beatmaps.get(this.currentBeatmap.mode) ?? [];
}

@computed
get tags() {
const summedTags: Partial<Record<number, BeatmapJson['tags']>> = {};
for (const beatmap of this.beatmapset.beatmaps) {
if (beatmap.tags == null) continue;
for (const tag of beatmap.tags) {
if (summedTags[tag.id] != null) {
summedTags[tag.id].count += tag.count;
} else {
summedTags[tag.id] = tag;
}
}
}

return summedTags;
}

@computed
get usersById() {
return keyBy(this.beatmapset.related_users, 'id') as Partial<Record<number, UserJson>>;
Expand Down
9 changes: 7 additions & 2 deletions resources/js/beatmapsets-show/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ export default class Info extends React.Component<Props> {
}

private get tags() {
const sortedTags = Object.values(this.controller.tags).sort((a, b) => {
const diff = b.count - a.count;
return diff !== 0 ? diff : a.id - b.id;
});

return [
...this.controller.beatmapset.user_tags.map((tag) => tag.name),
...this.controller.beatmapset.tags
...sortedTags.map((tag) => tag.name),
...this.controller.beatmapset.tags // TODO: something about duplicate mapper tags
.split(' ')
.filter(present),
];
Expand Down
2 changes: 2 additions & 0 deletions resources/js/interfaces/beatmap-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import BeatmapOwnerJson from './beatmap-owner-json';
import BeatmapsetJson from './beatmapset-json';
import Ruleset from './ruleset';
import TagJson from './tag-json';
import UserJson from './user-json';

interface BeatmapFailTimesArray {
Expand All @@ -17,6 +18,7 @@ interface BeatmapJsonAvailableIncludes {
failtimes: BeatmapFailTimesArray;
max_combo: number;
owners: BeatmapOwnerJson[];
tags: (TagJson & Required<Pick<TagJson, 'count'>>)[];
user: UserJson;
}

Expand Down
1 change: 0 additions & 1 deletion resources/js/interfaces/beatmapset-extended-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type BeatmapsetJsonForShowIncludes = Required<Pick<BeatmapsetExtendedJson,
| 'recent_favourites'
| 'related_users'
| 'user'
| 'user_tags'
>>;

export type BeatmapsetJsonForShow =
Expand Down
1 change: 1 addition & 0 deletions resources/js/interfaces/tag-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

export default interface TagJson {
count?: number;
description: string;
id: number;
name: string;
Expand Down

0 comments on commit 93dc799

Please sign in to comment.