Skip to content

Commit

Permalink
Added multi option to spoiler
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSpicyBurrito committed Aug 30, 2024
1 parent b2b98d4 commit b28d9df
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/clients/i18n/commonText.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fetchI18n } from './fetch.js'

export type CommonTextI18n = {
music: string
character: string
rarity: string
attribute: string
Expand Down
4 changes: 2 additions & 2 deletions src/sonolus/background/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { hideSpoilers } from '../utils/spoiler.js'
import { backgroundSearches } from './search.js'

export const installBackgroundInfo = () => {
sonolus.background.infoHandler = ({ options }) => {
sonolus.background.infoHandler = ({ options: { spoilers } }) => {
const cardBackgrounds = hideSpoilers(
options.spoilers,
spoilers[1],
sonolus.background.items.filter(
(item): item is BackgroundItemModel & { meta: object } => item.meta !== undefined,
),
Expand Down
5 changes: 3 additions & 2 deletions src/sonolus/background/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ export const installBackgroundList = () => {
sonolus.background.listHandler = ({
search: { type, options },
page,
options: serverOptions,
options: { spoilers },
}) => {
const filteredBackgrounds = [
...hideSpoilers(
serverOptions.spoilers,
spoilers[1],
sonolus.background.items.filter(
(item): item is BackgroundItemModel & { meta: object } =>
item.meta !== undefined,
),
),
...sonolus.background.items.filter(({ meta }) => !meta),
]

if (type === 'quick')
return {
...paginateItems(filterBackgrounds(filteredBackgrounds, options.keywords), page),
Expand Down
10 changes: 7 additions & 3 deletions src/sonolus/configuration/option.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ServerOptionsModel } from '@sonolus/express'
import { ServerMultiOptionValueModel, ServerOptionsModel } from '@sonolus/express'
import { Repository } from '../../repository/index.js'

export const configurationOptions = {
spoilers: {
name: {},
required: false,
type: 'toggle',
def: false,
type: 'multi',
values: [] as ServerMultiOptionValueModel[],
},
} satisfies ServerOptionsModel

export const updateConfigurationOptions = (repository: Repository) => {
configurationOptions.spoilers.name = repository.commonTexts.spoilerContent
configurationOptions.spoilers.values = [
{ title: repository.commonTexts.music, def: false },
{ title: repository.commonTexts.card, def: false },
]
}
4 changes: 2 additions & 2 deletions src/sonolus/level/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { hideSpoilers } from '../utils/spoiler.js'
import { levelSearches } from './search.js'

export const installLevelInfo = () => {
sonolus.level.infoHandler = ({ options }) => {
sonolus.level.infoHandler = ({ options: { spoilers } }) => {
const randomLevels: Record<string, LevelItemModel> = {}

const newestMusicIds = new Set<number>()
const newestLevels: LevelItemModel[] = []

for (const level of hideSpoilers(options.spoilers, sonolus.level.items)) {
for (const level of hideSpoilers(spoilers[0], sonolus.level.items)) {
randomLevels[`${level.meta.musicId}-${level.meta.musicVocalId}`] ??= level

if (newestLevels.length >= 5) continue
Expand Down
5 changes: 3 additions & 2 deletions src/sonolus/level/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { hideSpoilers } from '../utils/spoiler.js'
import { levelSearches } from './search.js'

export const installLevelList = () => {
sonolus.level.listHandler = ({ search: { type, options }, page, options: serverOptions }) => {
const filteredLevels = hideSpoilers(serverOptions.spoilers, sonolus.level.items)
sonolus.level.listHandler = ({ search: { type, options }, page, options: { spoilers } }) => {
const filteredLevels = hideSpoilers(spoilers[0], sonolus.level.items)

if (type === 'quick')
return {
...paginateItems(filterLevels(filteredLevels, options.keywords), page),
Expand Down
6 changes: 3 additions & 3 deletions src/sonolus/playlist/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { nonEmpty } from '../utils/section.js'
import { hideSpoilers, hideSpoilersFromPlaylist } from '../utils/spoiler.js'

export const installPlaylistDetails = () => {
sonolus.playlist.detailsHandler = ({ itemName, options }) => {
sonolus.playlist.detailsHandler = ({ itemName, options: { spoilers } }) => {
if (itemName.startsWith(`${config.sonolus.prefix}-random-`)) {
const [, , , min, max] = itemName.split('-')
const minRating = +(min ?? '') || 0
Expand All @@ -23,7 +23,7 @@ export const installPlaylistDetails = () => {
author: databaseEngineItem.subtitle,
tags: [{ title: { en: Text.Random } }],
levels: randomize(
hideSpoilers(options.spoilers, sonolus.level.items)
hideSpoilers(spoilers[0], sonolus.level.items)
.filter(({ rating }) => rating >= minRating && rating <= maxRating)
.map(({ name }) => name),
20,
Expand All @@ -45,7 +45,7 @@ export const installPlaylistDetails = () => {
if (!item) return 404

return {
item: hideSpoilersFromPlaylist(options.spoilers, item),
item: hideSpoilersFromPlaylist(spoilers[0], item),
description: item.description,
actions: {},
hasCommunity: false,
Expand Down
8 changes: 3 additions & 5 deletions src/sonolus/playlist/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { hideSpoilersFromPlaylists } from '../utils/spoiler.js'
import { playlistSearches } from './search.js'

export const installPlaylistInfo = () => {
sonolus.playlist.infoHandler = ({ options }) => {
const filteredPlaylists = hideSpoilersFromPlaylists(
options.spoilers,
sonolus.playlist.items,
)
sonolus.playlist.infoHandler = ({ options: { spoilers } }) => {
const filteredPlaylists = hideSpoilersFromPlaylists(spoilers[0], sonolus.playlist.items)

return {
searches: playlistSearches,
sections: [
Expand Down
12 changes: 3 additions & 9 deletions src/sonolus/playlist/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ import { hideSpoilersFromPlaylists } from '../utils/spoiler.js'
import { playlistSearches } from './search.js'

export const installPlaylistList = () => {
sonolus.playlist.listHandler = ({
search: { type, options },
page,
options: serverOptions,
}) => {
const filteredPlaylists = hideSpoilersFromPlaylists(
serverOptions.spoilers,
sonolus.playlist.items,
)
sonolus.playlist.listHandler = ({ search: { type, options }, page, options: { spoilers } }) => {
const filteredPlaylists = hideSpoilersFromPlaylists(spoilers[0], sonolus.playlist.items)

if (type === 'quick')
return {
...paginateItems(filterPlaylists(filteredPlaylists, options.keywords), page),
Expand Down
29 changes: 16 additions & 13 deletions src/sonolus/utils/spoiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@ import { PlaylistItemModel } from '@sonolus/express'
import { sonolus } from '../index.js'

export const hideSpoilers = <T extends { meta: { publishedAt: number } }>(
passThrough: boolean,
passThrough: boolean | undefined,
items: T[],
): T[] => {
if (passThrough) {
return items
}
) => {
if (passThrough) return items

return items.filter((item) => item.meta.publishedAt <= Date.now())
}

export const hideSpoilersFromPlaylist = (
passThrough: boolean,
passThrough: boolean | undefined,
playlist: PlaylistItemModel,
): PlaylistItemModel => {
if (passThrough) {
return playlist
}
) => {
if (passThrough) return playlist

return {
...playlist,
levels: hideSpoilers(
false,
playlist.levels.map((levelNameOrItem) => {
if (typeof levelNameOrItem === 'object') return levelNameOrItem

const level = sonolus.level.items.find((level) => level.name === levelNameOrItem)
if (!level) throw new Error(`Level not found: ${levelNameOrItem}`)

return level
}),
),
}
}

export const hideSpoilersFromPlaylists = (
passThrough: boolean,
passThrough: boolean | undefined,
playlists: PlaylistItemModel[],
): PlaylistItemModel[] =>
playlists.map((playlist) => hideSpoilersFromPlaylist(passThrough, playlist))
) => {
if (passThrough) return playlists

return playlists.map((playlist) => hideSpoilersFromPlaylist(false, playlist))
}

0 comments on commit b28d9df

Please sign in to comment.