From ebe22a39e9d86bed0659fd3fd558bf5cd65f73a1 Mon Sep 17 00:00:00 2001 From: Geomitron <22552797+Geomitron@users.noreply.github.com> Date: Mon, 18 May 2020 18:10:51 -0400 Subject: [PATCH] Improved version selection with chart packs --- .../chart-sidebar/chart-sidebar.component.ts | 16 ++++++++++++-- .../browse/status-bar/status-bar.component.ts | 22 +++++++++++-------- src/app/core/services/search.service.ts | 22 +++++++++++++++++++ 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/app/components/browse/chart-sidebar/chart-sidebar.component.ts b/src/app/components/browse/chart-sidebar/chart-sidebar.component.ts index 6a987c2..c7ca223 100644 --- a/src/app/components/browse/chart-sidebar/chart-sidebar.component.ts +++ b/src/app/components/browse/chart-sidebar/chart-sidebar.component.ts @@ -43,8 +43,7 @@ export class ChartSidebarComponent implements OnInit { const albumArt = this.albumArtService.getImage(result.id) const results = await this.electronService.invoke('song-details', result.id) this.charts = groupBy(results, 'chartID').sort((v1, v2) => v1[0].avTagName.length - v2[0].avTagName.length) - // This sorting is very inefficient, but there's rarely more than two or three in this list, so it's fine - this.charts.forEach(chart => chart.sort((v1, v2) => new Date(v2.lastModified).getTime() - new Date(v1.lastModified).getTime())) + this.sortCharts() await this.selectChart(0) this.initChartDropdown() @@ -52,6 +51,19 @@ export class ChartSidebarComponent implements OnInit { } } + /** + * Sorts `this.charts` and its subarrays in the correct order. + * The chart dropdown should display in a random order, but verified charters are prioritized. + * The version dropdown should be ordered by lastModified date. + * (but prefer the non-pack version if it's only a few days older) + */ + private sortCharts() { + for (const chart of this.charts) { + // TODO: sort by verified charter + this.searchService.sortChart(chart) + } + } + albumArtSrc: SafeUrl = '' /** * Updates the sidebar to display the album art. diff --git a/src/app/components/browse/status-bar/status-bar.component.ts b/src/app/components/browse/status-bar/status-bar.component.ts index c5d4775..fbee7b2 100644 --- a/src/app/components/browse/status-bar/status-bar.component.ts +++ b/src/app/components/browse/status-bar/status-bar.component.ts @@ -64,7 +64,8 @@ export class StatusBarComponent { if (this.chartGroups.length == 0) { for (const versions of versionGroups) { - const downloadVersion = versions.find(version => version.versionID == version.latestVersionID) + this.searchService.sortChart(versions) + const downloadVersion = versions[0] const downloadSong = this.selectedResults.find(song => song.id == downloadVersion.songID) this.downloadService.addDownload( downloadVersion.versionID, { @@ -81,16 +82,19 @@ export class StatusBarComponent { } downloadAllCharts() { - for (const version of this.batchResults) { - if (version.versionID != version.latestVersionID) { continue } - const downloadSong = this.selectedResults.find(song => song.id == version.songID) + const chartGroups = groupBy(this.batchResults, 'chartID') + for (const chart of chartGroups) { + this.searchService.sortChart(chart) + const downloadVersion = chart[0] + const downloadSong = this.selectedResults.find(song => song.id == downloadVersion.songID) this.downloadService.addDownload( - version.versionID, { - avTagName: version.avTagName, + downloadVersion.versionID, { + avTagName: downloadVersion.avTagName, artist: downloadSong.artist, - charter: version.charters, - driveData: version.driveData - }) + charter: downloadVersion.charters, + driveData: downloadVersion.driveData + } + ) } } diff --git a/src/app/core/services/search.service.ts b/src/app/core/services/search.service.ts index d337eec..c57c434 100644 --- a/src/app/core/services/search.service.ts +++ b/src/app/core/services/search.service.ts @@ -1,6 +1,7 @@ import { Injectable, EventEmitter } from '@angular/core' import { ElectronService } from './electron.service' import { SearchType, SongResult, SongSearch } from 'src/electron/shared/interfaces/search.interface' +import { VersionResult } from 'src/electron/shared/interfaces/songDetails.interface' @Injectable({ providedIn: 'root' @@ -72,4 +73,25 @@ export class SearchService { get allResultsVisible() { return this._allResultsVisible } + + /** + * Orders `versionResults` by lastModified date, but prefer the + * non-pack version if it's only a few days older. + */ + sortChart(versionResults: VersionResult[]) { + const dates: { [versionID: number]: number } = {} + versionResults.forEach(version => dates[version.versionID] = new Date(version.lastModified).getTime()) + versionResults.sort((v1, v2) => { + const diff = dates[v1.versionID] - dates[v2.versionID] + if (Math.abs(diff) < 6.048e+8 && v1.driveData.inChartPack != v2.driveData.inChartPack) { + if (v1.driveData.inChartPack) { + return 1 // prioritize v2 + } else { + return -1 // prioritize v1 + } + } else { + return diff + } + }) + } } \ No newline at end of file