From 6c5bd246ddd8cd7a7af7eb4c1199df4e41f15fdc Mon Sep 17 00:00:00 2001 From: silentrald Date: Wed, 16 Oct 2024 09:03:44 +0800 Subject: [PATCH] [feat-216] aligned with change with v4 schema maps * linted/auto-formatted sorter.service.ts files * changed generics.type to comparator.type --- .../local-playlist-filter-panel.component.tsx | 2 +- src/renderer/services/maps/sorter.service.ts | 45 ++++++++----------- .../services/playlists/sorter.service.ts | 35 +++++---------- src/shared/models/comparator.type.ts | 8 ++++ src/shared/models/generics.type.ts | 3 -- src/shared/models/maps/beat-saver.model.ts | 2 +- 6 files changed, 40 insertions(+), 55 deletions(-) create mode 100644 src/shared/models/comparator.type.ts delete mode 100644 src/shared/models/generics.type.ts diff --git a/src/renderer/components/maps-playlists-panel/playlists/local-playlist-filter-panel.component.tsx b/src/renderer/components/maps-playlists-panel/playlists/local-playlist-filter-panel.component.tsx index 630b6e385..355cb4527 100644 --- a/src/renderer/components/maps-playlists-panel/playlists/local-playlist-filter-panel.component.tsx +++ b/src/renderer/components/maps-playlists-panel/playlists/local-playlist-filter-panel.component.tsx @@ -7,7 +7,7 @@ import { hourToS, sToMs } from "shared/helpers/time.helpers"; import { useOnUpdate } from "renderer/hooks/use-on-update.hook"; import formatDuration from "format-duration"; import { LocalBPListsDetails } from "shared/models/playlists/local-playlist.models"; -import { Comparator } from "shared/models/generics.type"; +import { Comparator } from "shared/models/comparator.type"; type Props = { className?: string; diff --git a/src/renderer/services/maps/sorter.service.ts b/src/renderer/services/maps/sorter.service.ts index bcf759c4c..a73529b34 100644 --- a/src/renderer/services/maps/sorter.service.ts +++ b/src/renderer/services/maps/sorter.service.ts @@ -1,7 +1,6 @@ -import { Comparator } from "shared/models/generics.type"; +import { Comparator, Comparison } from "shared/models/comparator.type"; import { BsmLocalMap } from "shared/models/maps/bsm-local-map.interface"; - export class MapsSorterService { private static instance: MapsSorterService; @@ -15,44 +14,39 @@ export class MapsSorterService { private readonly comparators: { [key: string]: Comparator; } = { - name: (map1, map2) => - map1.rawInfo._songName.localeCompare(map2.rawInfo._songName), - "song-author": (map1, map2) => - map1.rawInfo._songAuthorName.localeCompare(map2.rawInfo._songAuthorName), - "map-author": (map1, map2) => - map1.rawInfo._levelAuthorName.localeCompare(map2.rawInfo._levelAuthorName), - bpm: (map1, map2) => - map1.rawInfo._beatsPerMinute - map2.rawInfo._beatsPerMinute, + name: (map1, map2) => map1.mapInfo.songName.localeCompare(map2.mapInfo.songName), + "song-author": (map1, map2) => map1.mapInfo.songAuthorName.localeCompare(map2.mapInfo.songAuthorName), + "map-author": (map1, map2) => { + // Compare the number of mappers, else compare the first mapper + const difference = map1.mapInfo.levelMappers.length - map2.mapInfo.levelMappers.length; + return difference || map1.mapInfo.levelMappers[0].localeCompare(map2.mapInfo.levelMappers[0]); + }, + bpm: (map1, map2) => map1.mapInfo.beatsPerMinute - map2.mapInfo.beatsPerMinute, duration: (map1, map2) => { if (!map1.songDetails) { - return map2.songDetails ? -1 : 0; + return map2.songDetails ? Comparison.LESSER : Comparison.EQUAL; } - return !map2.songDetails ? -1 - : map1.songDetails.duration - map2.songDetails.duration; + return !map2.songDetails ? Comparison.GREATER : map1.songDetails.duration - map2.songDetails.duration; }, likes: (map1, map2) => { if (!map1.songDetails) { - return map2.songDetails ? -1 : 0; + return map2.songDetails ? Comparison.LESSER : Comparison.EQUAL; } - return !map2.songDetails ? -1 - : map1.songDetails.upVotes - map2.songDetails.upVotes; + return !map2.songDetails ? Comparison.GREATER : map1.songDetails.upVotes - map2.songDetails.upVotes; }, "date-uploaded": (map1, map2) => { if (!map1.songDetails) { - return map2.songDetails ? -1 : 0; + return map2.songDetails ? Comparison.LESSER : Comparison.EQUAL; } - return !map2.songDetails ? -1 - : map1.songDetails.uploadedAt - map2.songDetails.uploadedAt; - } + return !map2.songDetails ? Comparison.GREATER : map1.songDetails.uploadedAt - map2.songDetails.uploadedAt; + }, }; private addTiebreak(comparator: Comparator): Comparator { - return (map1, map2) => - comparator(map1, map2) - || this.getDefaultComparator()(map1, map2); + return (map1, map2) => comparator(map1, map2) || this.getDefaultComparator()(map1, map2); } public getComparatorKeys(): string[] { @@ -69,9 +63,6 @@ export class MapsSorterService { public getComparator(key: string): Comparator { const comparator = this.comparators[key]; - return comparator - ? this.addTiebreak(comparator) - : this.getDefaultComparator(); + return comparator ? this.addTiebreak(comparator) : this.getDefaultComparator(); } - } diff --git a/src/renderer/services/playlists/sorter.service.ts b/src/renderer/services/playlists/sorter.service.ts index a194be6b7..f073ecaad 100644 --- a/src/renderer/services/playlists/sorter.service.ts +++ b/src/renderer/services/playlists/sorter.service.ts @@ -1,4 +1,4 @@ -import { Comparator } from "shared/models/generics.type"; +import { Comparator, Comparison } from "shared/models/comparator.type"; import { LocalBPListsDetails } from "shared/models/playlists/local-playlist.models"; export class PlaylistsSorterService { @@ -14,42 +14,34 @@ export class PlaylistsSorterService { private readonly comparators: { [key: string]: Comparator; } = { - title: (playlist1, playlist2) => - playlist1.playlistTitle.localeCompare(playlist2.playlistTitle), - author: (playlist1, playlist2) => - playlist1.playlistAuthor.localeCompare(playlist2.playlistAuthor), - "number-of-maps": (playlist1, playlist2) => - playlist1.nbMaps - playlist2.nbMaps, + title: (playlist1, playlist2) => playlist1.playlistTitle.localeCompare(playlist2.playlistTitle), + author: (playlist1, playlist2) => playlist1.playlistAuthor.localeCompare(playlist2.playlistAuthor), + "number-of-maps": (playlist1, playlist2) => playlist1.nbMaps - playlist2.nbMaps, duration: (playlist1, playlist2) => { if (!playlist1.duration) { - return playlist2.duration ? -1 : 0; + return playlist2.duration ? Comparison.LESSER : Comparison.EQUAL; } - return !playlist2.duration ? -1 - : playlist1.duration - playlist2.duration; + return !playlist2.duration ? Comparison.GREATER : playlist1.duration - playlist2.duration; }, "min-notes-per-second": (playlist1, playlist2) => { if (!playlist1.minNps) { - return playlist2.minNps ? -1 : 0; + return playlist2.minNps ? Comparison.LESSER : Comparison.EQUAL; } - return !playlist2.minNps ? -1 - : playlist1.minNps - playlist2.minNps; + return !playlist2.minNps ? Comparison.GREATER : playlist1.minNps - playlist2.minNps; }, "max-notes-per-second": (playlist1, playlist2) => { if (!playlist1.maxNps) { - return playlist2.maxNps ? -1 : 0; + return playlist2.maxNps ? Comparison.LESSER : Comparison.EQUAL; } - return !playlist2.maxNps ? -1 - : playlist1.maxNps - playlist2.maxNps; + return !playlist2.maxNps ? Comparison.GREATER : playlist1.maxNps - playlist2.maxNps; }, }; private addTiebreak(comparator: Comparator): Comparator { - return (playlist1, playlist2) => - comparator(playlist1, playlist2) - || this.getDefaultComparator()(playlist1, playlist2); + return (playlist1, playlist2) => comparator(playlist1, playlist2) || this.getDefaultComparator()(playlist1, playlist2); } public getComparatorKeys(): string[] { @@ -66,9 +58,6 @@ export class PlaylistsSorterService { public getComparator(key: string): Comparator { const comparator = this.comparators[key]; - return comparator - ? this.addTiebreak(comparator) - : this.getDefaultComparator(); + return comparator ? this.addTiebreak(comparator) : this.getDefaultComparator(); } - } diff --git a/src/shared/models/comparator.type.ts b/src/shared/models/comparator.type.ts new file mode 100644 index 000000000..c6c69dfcc --- /dev/null +++ b/src/shared/models/comparator.type.ts @@ -0,0 +1,8 @@ + +export enum Comparison { + EQUAL = 0, + GREATER = 1, + LESSER = -1, +}; + +export type Comparator = (object1: T, object2: T) => number; diff --git a/src/shared/models/generics.type.ts b/src/shared/models/generics.type.ts deleted file mode 100644 index 33ad62f4d..000000000 --- a/src/shared/models/generics.type.ts +++ /dev/null @@ -1,3 +0,0 @@ - -export type Comparator = (object1: T, object2: T) => number; - diff --git a/src/shared/models/maps/beat-saver.model.ts b/src/shared/models/maps/beat-saver.model.ts index c6588f840..8f571db52 100644 --- a/src/shared/models/maps/beat-saver.model.ts +++ b/src/shared/models/maps/beat-saver.model.ts @@ -1,7 +1,7 @@ import { ObjectValues } from "shared/helpers/type.helpers"; import { SongDetailDiffCharactertistic, SongDiffName } from "./song-details-cache/song-details-cache.model"; import { BsmLocalMap } from "./bsm-local-map.interface"; -import { Comparator } from "../generics.type"; +import { Comparator } from "../comparator.type"; export interface BsvMapDetail { automapper: boolean;