make calcPpBoundary WAYYYYYYYY faster
All checks were successful
deploy / deploy (push) Successful in 53s

This commit is contained in:
Lee 2023-10-28 00:17:40 +01:00
parent d7fe245036
commit 412c07a304

@ -112,6 +112,13 @@ function calcRawPpAtIdx(
); );
} }
/**
* Gets the amount of raw pp needed to gain the expected pp
*
* @param playerId the player id
* @param expectedPp the expected pp
* @returns the pp boundary (+ per raw pp)
*/
/** /**
* Gets the amount of raw pp needed to gain the expected pp * Gets the amount of raw pp needed to gain the expected pp
* *
@ -120,38 +127,45 @@ function calcRawPpAtIdx(
* @returns the pp boundary (+ per raw pp) * @returns the pp boundary (+ per raw pp)
*/ */
export function calcPpBoundary(playerId: string, expectedPp = 1) { export function calcPpBoundary(playerId: string, expectedPp = 1) {
const rankedScores = useScoresaberScoresStore const state = useScoresaberScoresStore.getState();
.getState() const player = state.players.find((p) => p.id === playerId);
.players.find((p) => p.id === playerId) if (!player || !player.scores) return null;
?.scores?.filter((s) => s.score.pp !== undefined);
if (!rankedScores) return null;
const rankedScorePps = rankedScores const rankedScorePps = player.scores
.filter((s) => s.score.pp !== undefined)
.map((s) => s.score.pp) .map((s) => s.score.pp)
.sort((a, b) => b - a); .sort((a, b) => b - a);
let idx = rankedScorePps.length - 1; let left = 0;
let right = rankedScorePps.length - 1;
let boundaryIdx = -1;
while (idx >= 0) { while (left <= right) {
const bottomSlice = rankedScorePps.slice(idx); const mid = Math.floor((left + right) / 2);
const bottomPp = getTotalPpFromSortedPps(bottomSlice, idx); const bottomSlice = rankedScorePps.slice(mid);
const bottomPp = getTotalPpFromSortedPps(bottomSlice, mid);
bottomSlice.unshift(rankedScorePps[idx]); bottomSlice.unshift(rankedScorePps[mid]);
const modifiedBottomPp = getTotalPpFromSortedPps(bottomSlice, idx); const modifiedBottomPp = getTotalPpFromSortedPps(bottomSlice, mid);
const diff = modifiedBottomPp - bottomPp; const diff = modifiedBottomPp - bottomPp;
if (diff > expectedPp) { if (diff > expectedPp) {
const ppBoundary = calcRawPpAtIdx( boundaryIdx = mid;
rankedScorePps.slice(idx + 1), left = mid + 1;
idx + 1, } else {
expectedPp, right = mid - 1;
);
return ppBoundary;
} }
idx--;
} }
return calcRawPpAtIdx(rankedScorePps, 0, expectedPp);
if (boundaryIdx === -1) {
return calcRawPpAtIdx(rankedScorePps, 0, expectedPp);
} else {
return calcRawPpAtIdx(
rankedScorePps.slice(boundaryIdx + 1),
boundaryIdx + 1,
expectedPp,
);
}
} }
/** /**