Skip to content

Commit

Permalink
[feat-216] aligned with change with v4 schema maps
Browse files Browse the repository at this point in the history
* linted/auto-formatted sorter.service.ts files
* changed generics.type to comparator.type
  • Loading branch information
silentrald committed Dec 23, 2024
1 parent 1a223ef commit 6c5bd24
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 18 additions & 27 deletions src/renderer/services/maps/sorter.service.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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[] {
Expand All @@ -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();
}

}
35 changes: 12 additions & 23 deletions src/renderer/services/playlists/sorter.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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[] {
Expand All @@ -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();
}

}
8 changes: 8 additions & 0 deletions src/shared/models/comparator.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export enum Comparison {
EQUAL = 0,
GREATER = 1,
LESSER = -1,
};

export type Comparator<T> = (object1: T, object2: T) => number;
3 changes: 0 additions & 3 deletions src/shared/models/generics.type.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/shared/models/maps/beat-saver.model.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 6c5bd24

Please sign in to comment.