Skip to content

Commit

Permalink
Added percentage values for songs without star rating
Browse files Browse the repository at this point in the history
for leaderboard: add percentage if it is not present in a score row
for profile page: add percentage if not already set on ScoreSaber, percentage value replace weighted pp value (it is always = 0.00pp on this maps)
  • Loading branch information
Karghoff committed Aug 8, 2020
1 parent 90ee3da commit 48254b5
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ The generated output file is always `scoresaber.user.js`.

# Changelog

1.8.8
- Added percentage info on Leaderboard and Profile pages

1.8.7
- Fixed graphjs throwing when loading in background tab.

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions scoresaber.user.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/api/beatsaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,27 @@ export interface IBeatSaverData {
};
metadata: {
duration: number;
characteristics: IBeatSaverSongCharacteristic[];
};
}

export interface IBeatSaverSongCharacteristic {
name: string;
difficulties: {
easy: {
notes: number;
},
normal: {
notes: number;
},
hard: {
notes: number;
},
expert: {
notes: number;
},
expertPlus: {
notes: number;
}
};
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ function on_load_body(): void {
log.logc("Loading body");

page_user.setup_dl_link_user_site();
page_user.add_percentage();
page_user.setup_user_rank_link_swap();
page_user.setup_song_rank_link_swap();
page_user.setup_wide_table_checkbox();
page_song.setup_dl_link_leaderboard();
page_song.setup_song_filter_tabs();
page_song.highlight_user();
page_song.add_percentage();
header.setup_self_pin_button();
header.setup_self_button();
compare.setup_user_compare();
Expand Down
48 changes: 47 additions & 1 deletion src/pages/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import g from "../global";
import { create, intor } from "../util/dom";
import { check } from "../util/err";
import { format_en, number_to_timespan, toggled_class } from "../util/format";
import { get_song_compare_value, get_song_hash_from_text } from "../util/song";
import { calculate_max_score, get_notes_count, get_song_compare_value, get_song_hash_from_text} from "../util/song";

export function setup_song_filter_tabs(): void {
if (!is_song_leaderboard_page()) { return; }
Expand Down Expand Up @@ -192,3 +192,49 @@ export function highlight_user(): void {
element.parentElement!.parentElement!.style.backgroundColor = "var(--color-highlight)";
}
}

export function add_percentage(): void {
if (!is_song_leaderboard_page()) {
return;
}

// find the element we want to modify
let details_box = check(document.querySelector(".content .title.is-5"));
details_box = check(details_box.parentElement);

const song_hash = get_song_hash_from_text(details_box.innerHTML);

if (!song_hash) {
return;
}

beatsaver.get_data_by_hash(song_hash)
.then(data => {
if (data) {
const active_tab = check(document.querySelector(`div.tabs`)).querySelector(`li.is-active`);
const diff_name = check(check(active_tab).querySelector("span")).innerText;
const standard_characteristic = data.metadata.characteristics.find(c => c.name === "Standard");
if (diff_name && standard_characteristic) {
const notes = get_notes_count(diff_name, standard_characteristic);
if (notes > 0) {
const max_score = calculate_max_score(notes);
const ranking_table = check(document.querySelector("table.ranking.global"));
const user_scores = ranking_table.querySelectorAll("tbody > tr");
for (const score_row of user_scores) {
const percentage_column = check(score_row.querySelector("td.percentage"));
const percentage_value = percentage_column.innerText;
if (percentage_value && percentage_value === "-") {
const score = check(score_row.querySelector("td.score")).innerText;
if (score) {
// remove any non digit characters
const score_num = +(score.replace(/\D/g, ""));
const calculated_percentage = (score_num * 100. / max_score).toFixed(2);
percentage_column.innerText = calculated_percentage + "%";
}
}
}
}
}
}
});
}
46 changes: 45 additions & 1 deletion src/pages/user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as beatsaver from "../api/beatsaver";
import * as buttons from "../components/buttons";
import { get_wide_table, is_user_page } from "../env";
import g from "../global";
import { create, into } from "../util/dom";
import { check } from "../util/err";
import { number_invariant } from "../util/format";
import { get_song_hash_from_text } from "../util/song";
import {calculate_max_score, get_notes_count, get_song_hash_from_text} from "../util/song";

export function setup_dl_link_user_site(): void {
if (!is_user_page()) { return; }
Expand Down Expand Up @@ -95,3 +96,46 @@ export function setup_song_rank_link_swap(): void {
function rank_to_page(rank: number, ranks_per_page: number): number {
return Math.floor((rank + ranks_per_page - 1) / ranks_per_page);
}

export function add_percentage() {
if (!is_user_page()) { return; }

// find the table we want to modify
const table = check(document.querySelector("table.ranking.songs"));
const table_row = table.querySelectorAll("tbody tr");
for (const row of table_row) {
const image_link = check(row.querySelector<HTMLImageElement>("th.song img")).src;
const song_hash = get_song_hash_from_text(image_link);

if (!song_hash) {
return;
}

const score_column = check(row.querySelector(`th.score`));
// skip rows with percentage from ScoreSaber
if (!score_column.innerText || score_column.innerText.includes("%")) { continue; }

beatsaver.get_data_by_hash(song_hash)
.then(data => {
if (data) {
const song_column = check(row.querySelector(`th.song`));
const diff_name = check(song_column.querySelector(`span > span`)).innerText;
const standard_characteristic = data.metadata.characteristics.find(c => c.name === "Standard");
if (diff_name && standard_characteristic) {
const notes = get_notes_count(diff_name, standard_characteristic);
if (notes > 0) {
const max_score = calculate_max_score(notes);
const user_score = check(score_column.querySelector(".scoreBottom"));

if (user_score.innerText) {
// remove all non digit character, also remove two zero from fractional part
const user_score_num = +(user_score.innerHTML.replace(/\D/g, "")) / 100;
const calculated_percentage = (user_score_num * 100. / max_score).toFixed(2);
check(score_column.querySelector(".ppWeightedValue")).innerHTML = `(${calculated_percentage}%)`;
}
}
}
}
});
}
}
46 changes: 46 additions & 0 deletions src/util/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,49 @@ export function parse_mods(mods: string): string[] | undefined {
if (modarr.length === 0) return undefined;
return modarr;
}

export function get_notes_count(diff_name: string, characteristic: beatsaver.IBeatSaverSongCharacteristic): number {
switch (diff_name) {
case "Easy":
return characteristic.difficulties.easy.notes;
case "Normal":
return characteristic.difficulties.normal.notes;
case "Hard":
return characteristic.difficulties.hard.notes;
case"Expert":
return characteristic.difficulties.expert.notes;
case"Expert+":
return characteristic.difficulties.expertPlus.notes;
}
return -1;
}

export function calculate_max_score(notes: number): number {
const note_score = 115;
let max_score = 0;
let counter = 1;
// first note, multiplier x1, check if notes still present
while (notes > 0 && counter > 0) {
notes--;
counter--;
max_score += note_score;
}
// four notes, multiplier x2
counter = 4;
while (notes > 0 && counter > 0) {
notes--;
counter--;
max_score += note_score * 2;
}
// eight notes, multiplier x4
counter = 8;
while (notes > 0 && counter > 0) {
notes--;
counter--;
max_score += note_score * 4;
}
// all other notes with multiplier x8
max_score += notes * note_score * 8;

return max_score;
}

0 comments on commit 48254b5

Please sign in to comment.