koishi-plugin-beatsaber-bot
Version:
一个 用于 BeatSaber 的 koishi Bot 插件
98 lines (97 loc) • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BeatLeaderService = void 0;
const sortScore_1 = require("../sortScore");
const bsorDecoder_1 = require("../../img-render/utils/bl/bsorDecoder");
class BeatLeaderService {
constructor(bsClient, blClient) {
this.refreshOAuthToken = (rk) => this.blClient.refreshOAuthToken(rk);
this.bsClient = bsClient;
this.blClient = blClient;
}
async getScoreByPlayerIdAndMapId(playerId, mapId, option) {
const map = await this.bsClient.searchMapById(mapId);
if (!map) {
throw Error('error.not.found');
}
const hash = map.versions[0].hash;
let reqs = map.versions[0].diffs.map(it => ({
diff: it.difficulty,
mode: it.characteristic,
hash: hash,
playerID: playerId,
leaderboardContext: 'general'
}));
if (option && option.difficulty) {
reqs = reqs.filter(item => item.diff == option.difficulty);
}
if (option && option.mode) {
reqs = reqs.filter(item => item.mode == option.mode);
}
const res = await Promise.all(reqs.map(item => {
return this.blClient.getPlayerScore(item);
}));
const scores = res.filter(item => item != null);
// todo sort score
if (scores.length < 1) {
throw Error('error.not.found');
}
scores.sort(sortScore_1.sortScore);
return scores[0];
}
getTokenInfo(ak) {
return this.blClient.getTokenInfo(ak);
}
async getPlayerScoresWithUserInfo(accountId, queryParams) {
const [userInfo, playerScores, pinnedScores] = await Promise.all([
this.blClient.getPlayerInfo(accountId),
this.blClient.getPlayerScores(accountId),
this.blClient.getPlayerPinnedScores(accountId)
]);
const filteredScores = playerScores.data.filter(item => !pinnedScores.some(pinned => pinned.id === item.id));
const scores = pinnedScores.concat(filteredScores).slice(0, 24);
return {
scores: scores,
userInfo: userInfo
};
// /api/render/beatleader/${uid}/pinnedScores
// if(!query['count']) {
// query['count'] = ["32"]
// }
// if(!query['sortBy']) {
// query['sortBy'] = ["pp"]
// }
// if(!query['order']) {
// query['order'] = ["desc"]
// }
// const q = Object.keys(query).map(item=> (query[item].map(it=>`${item}=${it}`).join("&"))).join("&")
// console.log('q',query)
// const url = `https://api.beatleader.xyz/player/${uid}/scores?` + q
// console.log(url)
// const res = await fetch(url,{cache:'no-store'})
// const res = await fetch(`https://api.beatleader.xyz/player/${uid}`,{
// next: {
// revalidate: config.constants.CACHE_TIMEOUT
// }
// })
}
async getScoreAndBSMapByScoreId(scoreId) {
const res = await this.blClient.getBeatScore(scoreId);
const bsorLink = res.replay;
const bsorContent = await fetch(bsorLink);
const bsor = await new Promise((resolve, reject) => {
bsorContent.arrayBuffer().then(res => (0, bsorDecoder_1.decode)(res, resolve));
setTimeout(() => reject("timeout exceed"), 5000);
});
const statistic = await fetch(`https://cdn.scorestats.beatleader.xyz/${scoreId}.json`).then(res => res.json());
const id = res.song.id.split('x')?.[0];
const bsMap = await this.bsClient.searchMapById(id);
return {
score: res,
bsMap: bsMap,
statistic: statistic,
bsor: bsor
};
}
}
exports.BeatLeaderService = BeatLeaderService;