mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
[feat-216] aligned with change with v4 schema maps
* linted/auto-formatted sorter.service.ts files * changed generics.type to comparator.type
This commit is contained in:
+1
-1
@@ -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;
|
||||
|
||||
@@ -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<BsmLocalMap>;
|
||||
} = {
|
||||
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<BsmLocalMap>): Comparator<BsmLocalMap> {
|
||||
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<BsmLocalMap> {
|
||||
const comparator = this.comparators[key];
|
||||
return comparator
|
||||
? this.addTiebreak(comparator)
|
||||
: this.getDefaultComparator();
|
||||
return comparator ? this.addTiebreak(comparator) : this.getDefaultComparator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<LocalBPListsDetails>;
|
||||
} = {
|
||||
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<LocalBPListsDetails>): Comparator<LocalBPListsDetails> {
|
||||
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<LocalBPListsDetails> {
|
||||
const comparator = this.comparators[key];
|
||||
return comparator
|
||||
? this.addTiebreak(comparator)
|
||||
: this.getDefaultComparator();
|
||||
return comparator ? this.addTiebreak(comparator) : this.getDefaultComparator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
export enum Comparison {
|
||||
EQUAL = 0,
|
||||
GREATER = 1,
|
||||
LESSER = -1,
|
||||
};
|
||||
|
||||
export type Comparator<T> = (object1: T, object2: T) => number;
|
||||
@@ -1,3 +0,0 @@
|
||||
|
||||
export type Comparator<T> = (object1: T, object2: T) => number;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user