mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
get maps details from bsaver api
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
import { BsvMapDetail } from "shared/models/maps";
|
||||
|
||||
export class BeatSaverService {
|
||||
|
||||
private static instance: BeatSaverService;
|
||||
|
||||
public static getInstance(): BeatSaverService{
|
||||
if(!BeatSaverService.instance){ BeatSaverService.instance = new BeatSaverService(); }
|
||||
return BeatSaverService.instance;
|
||||
}
|
||||
|
||||
private constructor(){}
|
||||
|
||||
public getMapDetailsFromHashs(hashs: string[]): BsvMapDetail{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ApiResult } from "renderer/models/api/api.model";
|
||||
import { BsvMapDetail } from "shared/models/maps";
|
||||
|
||||
export class BeatSaverApiService {
|
||||
|
||||
private static instance: BeatSaverApiService;
|
||||
|
||||
public static getInstance(): BeatSaverApiService{
|
||||
if(!BeatSaverApiService.instance){ BeatSaverApiService.instance = new BeatSaverApiService(); }
|
||||
return BeatSaverApiService.instance;
|
||||
}
|
||||
|
||||
private readonly bsaverApiUrl = "https://beatsaver.com/api"
|
||||
|
||||
private constructor(){}
|
||||
|
||||
public async getMapsDetailsByHashs<T extends string>(hashs: T[]): Promise<ApiResult<Record<Lowercase<T>, BsvMapDetail>>>{
|
||||
|
||||
if(hashs.length > 50){ throw "too musch map hashs"; }
|
||||
|
||||
const paramsHashs = hashs.join(",");
|
||||
|
||||
const resp = await fetch(`${this.bsaverApiUrl}/maps/hash/${paramsHashs}`);
|
||||
|
||||
const data = await resp.json() as Record<Lowercase<T>, BsvMapDetail>;
|
||||
|
||||
return {status: resp.status, data: data};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { splitIntoChunk } from "renderer/helpers/array-tools";
|
||||
import { Observable } from "rxjs";
|
||||
import { BsvMapDetail } from "shared/models/maps";
|
||||
import { BeatSaverApiService } from "./beat-saver-api.service";
|
||||
|
||||
export class BeatSaverService {
|
||||
|
||||
private static instance: BeatSaverService;
|
||||
|
||||
public static getInstance(): BeatSaverService{
|
||||
if(!BeatSaverService.instance){ BeatSaverService.instance = new BeatSaverService(); }
|
||||
return BeatSaverService.instance;
|
||||
}
|
||||
|
||||
private readonly bsaverApi: BeatSaverApiService;
|
||||
|
||||
private constructor(){
|
||||
this.bsaverApi = BeatSaverApiService.getInstance();
|
||||
}
|
||||
|
||||
public getMapDetailsFromHashs(hashs: string[]): Observable<BsvMapDetail[]>{
|
||||
const chunkHash = splitIntoChunk(hashs, 50);
|
||||
|
||||
return new Observable(observer => {
|
||||
(async () => {
|
||||
|
||||
const mapDetails: BsvMapDetail[] = [];
|
||||
|
||||
for(const hashs of chunkHash){
|
||||
const res = await this.bsaverApi.getMapsDetailsByHashs(hashs);
|
||||
if(res.status === 200){
|
||||
mapDetails.push(...Object.values<BsvMapDetail>(res.data));
|
||||
observer.next(mapDetails);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
observer.complete();
|
||||
})()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Observable } from "rxjs";
|
||||
import { BSVersion } from "shared/bs-version.interface";
|
||||
import { BsmLocalMap } from "shared/models/maps/bsm-local-map.interface";
|
||||
import { BeatSaverService } from "./beat-saver/beat-saver.service";
|
||||
import { IpcService } from "./ipc.service";
|
||||
|
||||
export class MapsManagerService {
|
||||
@@ -13,9 +14,11 @@ export class MapsManagerService {
|
||||
}
|
||||
|
||||
private readonly ipcService: IpcService;
|
||||
private readonly bsaver: BeatSaverService;
|
||||
|
||||
private constructor(){
|
||||
this.ipcService = IpcService.getInstance();
|
||||
this.bsaver = BeatSaverService.getInstance();
|
||||
}
|
||||
|
||||
public getMaps(version?: BSVersion): Observable<BsmLocalMap[]>{
|
||||
@@ -24,6 +27,13 @@ export class MapsManagerService {
|
||||
this.ipcService.send<BsmLocalMap[], BSVersion>("get-version-maps", {args: version}).then(res => {
|
||||
if(!res.success){ return obs.next(null);}
|
||||
obs.next(res.data);
|
||||
|
||||
this.bsaver.getMapDetailsFromHashs(res.data.map(localMap => localMap.hash)).subscribe(mapsDetails => {
|
||||
mapsDetails.forEach(details => {
|
||||
res.data.find(localMap => localMap.hash === details.versions.find(details => details?.hash === localMap.hash)?.hash).bsaverInfo = details
|
||||
});
|
||||
obs.next(res.data);
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user