52 Commits

Author SHA1 Message Date
59699f05f8 Update dependency lucide-react to ^0.454.0 2024-10-28 14:03:51 +00:00
ad568ddf5d fix player data tracking
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 40s
2024-10-28 14:00:12 +00:00
df297d0c99 maybe fix player data tracking idk
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-28 13:35:14 +00:00
c8fb08b192 add embeds to top and live scores pages
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s
2024-10-28 13:28:54 +00:00
981bc13a1f update player name
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-28 13:27:46 +00:00
8314cbcf2d bob the builder
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m21s
2024-10-28 13:22:23 +00:00
f52b62ba83 add top scores page
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Successful in 46s
Deploy Website / docker (ubuntu-latest) (push) Failing after 1m31s
2024-10-28 13:18:40 +00:00
0a5d42f6ac make player score fetching much faster
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-28 12:12:44 +00:00
ce65116db4 log this to see if its why some don't get tracked
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 1m12s
2024-10-28 11:46:03 +00:00
6c81316364 auto reload site stats
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m23s
2024-10-27 19:30:58 +00:00
b83fb6f3a8 cache statistics
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m20s
2024-10-27 15:17:54 +00:00
c58f24103f update lgo
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-27 15:13:43 +00:00
e146d20f4f log ss score tracking
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 40s
2024-10-27 15:11:00 +00:00
ffa4ab2b6c add PP to the acc chart for a score
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m23s
2024-10-27 15:08:20 +00:00
b889eee7ff change connection idle timeout
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 40s
2024-10-27 14:03:41 +00:00
28e8561020 cleanup and track player history when creating the player instead of waiting for the cron job
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-27 14:01:12 +00:00
de3768559f include today in seeding player history
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 42s
2024-10-27 13:35:45 +00:00
4be0b072b2 fix player embed image not updating (maybe?)
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m23s
2024-10-27 13:30:51 +00:00
d086e922c4 fix leaderboard id
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 48s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m22s
2024-10-27 11:20:08 +00:00
96ab9be79a maybe this will help the memory usage idk
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s
2024-10-27 10:59:41 +00:00
3357939071 maybe fix backend crash?
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 1m13s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s
2024-10-27 10:45:20 +00:00
f3737ce7a5 format previous score
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m20s
2024-10-26 18:44:57 +01:00
9626931b91 migrate some values to ssr data tracking so we don't need to rely on BL as much
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Successful in 49s
Deploy Website / docker (ubuntu-latest) (push) Has been cancelled
2024-10-26 18:41:51 +01:00
5ff0d11f5a show pause count for scores
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m21s
2024-10-26 18:02:04 +01:00
5f4d3829e2 no need to log this
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m19s
2024-10-26 15:45:40 +01:00
b3cd770724 move score feed button to the footer
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m18s
2024-10-26 15:38:24 +01:00
c3a75b139a minimize api calls to scoresaber
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s
2024-10-26 15:33:05 +01:00
ba80b9623b revert react
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m40s
2024-10-26 15:19:03 +01:00
e57e725639 optimize requests to scoresaber's api
Some checks failed
Deploy Website / docker (ubuntu-latest) (push) Failing after 1m57s
2024-10-26 15:16:50 +01:00
f8b0f7c6cd cleanup + bump next & react
Some checks failed
Deploy Website / docker (ubuntu-latest) (push) Failing after 2m12s
2024-10-26 14:58:44 +01:00
0d39a905f6 never refresh ranked leaderboards
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 40s
2024-10-26 13:27:53 +01:00
7be8c37779 move this to the cached leaderboard
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-26 13:22:43 +01:00
6bc2e09f43 meow
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 45s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m25s
2024-10-26 13:16:55 +01:00
da7f5f1c62 fix imports
Some checks failed
Deploy Website / docker (ubuntu-latest) (push) Failing after 8s
2024-10-26 13:14:25 +01:00
fe888d9fb6 cache scoresaber leaderboards
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Failing after 32s
Deploy Website / docker (ubuntu-latest) (push) Has been cancelled
2024-10-26 13:13:32 +01:00
a8eb2372cb fix imports
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s
2024-10-26 11:46:24 +01:00
e0c719eaba highlight correct player on leaderboard scores and make player name clickable on a leaderboard score
Some checks failed
Deploy Website / docker (ubuntu-latest) (push) Has been cancelled
2024-10-26 11:45:34 +01:00
413d72182d add per hand real accuracy (eg: 95%)
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m27s
2024-10-26 11:38:16 +01:00
d7929cc36a fix player daily score set
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 1m47s
2024-10-26 01:05:51 +01:00
dd162bf77c fix mobile score
All checks were successful
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m25s
2024-10-25 21:56:52 +01:00
5d7bdc17b1 fix score history icon
Some checks failed
Deploy Website / docker (ubuntu-latest) (push) Failing after 6s
2024-10-25 21:42:21 +01:00
ff287222f7 reset dropdown mode on dropdown close
Some checks failed
Deploy Website / docker (ubuntu-latest) (push) Has been cancelled
2024-10-25 21:40:07 +01:00
3abffec9cb oops
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m25s
2024-10-25 21:34:26 +01:00
f20d83a436 fix error
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Failing after 34s
Deploy Website / docker (ubuntu-latest) (push) Failing after 32s
2024-10-25 21:32:59 +01:00
97fba47fd8 add score history viewing
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Failing after 34s
Deploy Website / docker (ubuntu-latest) (push) Failing after 33s
2024-10-25 21:29:57 +01:00
9fb5317bc8 maybe it just needs time to work idk
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 43s
2024-10-25 18:29:57 +01:00
7e1d172b43 fix perms
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-25 18:01:44 +01:00
da950e08f2 bot stuff
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 47s
2024-10-25 18:00:15 +01:00
Lee
2b9a777506 Update projects/backend/src/bot/bot.ts
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 41s
2024-10-25 16:49:38 +00:00
90b0994524 add bot cmd
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Failing after 30s
2024-10-25 17:44:19 +01:00
53e0ce007d rename cron 2024-10-25 17:39:25 +01:00
a421243973 ensure scores are always up-to-date for players
All checks were successful
Deploy Backend / docker (ubuntu-latest) (push) Successful in 46s
Deploy Website / docker (ubuntu-latest) (push) Successful in 2m24s
2024-10-25 17:37:56 +01:00
82 changed files with 1679 additions and 975 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -2,14 +2,15 @@ import { Client, MetadataStorage } from "discordx";
import { ActivityType, EmbedBuilder } from "discord.js";
import { Config } from "@ssr/common/config";
export const guildId = "1295984874942894100";
export enum DiscordChannels {
trackedPlayerLogs = "1295985197262569512",
numberOneFeed = "1295988063817830430",
backendLogs = "1296524935237468250",
}
const DiscordBot = new Client({
intents: [],
const client = new Client({
intents: ["Guilds", "GuildMessages"],
presence: {
status: "online",
@ -23,15 +24,26 @@ const DiscordBot = new Client({
},
});
DiscordBot.once("ready", () => {
client.once("ready", () => {
console.log("Discord bot ready!");
});
export function initDiscordBot() {
export async function initDiscordBot() {
console.log("Initializing discord bot...");
// We will now build our application to load all the commands/events for both bots.
MetadataStorage.instance.build().then(async () => {
await DiscordBot.login(Config.discordBotToken!).then();
// Setup slash commands
client.once("ready", async () => {
await client.initApplicationCommands();
console.log(client.applicationCommands);
});
client.on("interactionCreate", interaction => {
client.executeInteraction(interaction);
});
// Login
await client.login(Config.discordBotToken!);
});
}
@ -42,13 +54,17 @@ export function initDiscordBot() {
* @param message the message to log
*/
export async function logToChannel(channelId: DiscordChannels, message: EmbedBuilder) {
const channel = await DiscordBot.channels.fetch(channelId);
if (channel == undefined) {
throw new Error(`Channel "${channelId}" not found`);
}
if (!channel.isSendable()) {
throw new Error(`Channel "${channelId}" is not sendable`);
}
try {
const channel = await client.channels.fetch(channelId);
if (channel == undefined) {
throw new Error(`Channel "${channelId}" not found`);
}
if (!channel.isSendable()) {
throw new Error(`Channel "${channelId}" is not sendable`);
}
channel.send({ embeds: [message] });
channel.send({ embeds: [message] });
} catch (error) {
console.error(error);
}
}

View File

@ -0,0 +1,20 @@
import { Discord, Guild, Slash } from "discordx";
import { CommandInteraction } from "discord.js";
import { PlayerService } from "../../service/player.service";
import { guildId } from "../bot";
@Discord()
export class RefreshPlayerScoresCommand {
@Guild(guildId)
@Slash({
description: "Refreshes scores for all tracked players",
name: "refresh-player-scores",
defaultMemberPermissions: ["Administrator"],
})
hello(interaction: CommandInteraction) {
interaction.reply("Updating player scores...").then(async response => {
await PlayerService.refreshPlayerScores();
await response.edit("Done!");
});
}
}

View File

@ -1,5 +1,5 @@
import { SSRCache } from "@ssr/common/cache";
import { InternalServerError } from "../error/internal-server-error";
import { InternalServerError } from "@ssr/common/error/internal-server-error";
import { isProduction } from "@ssr/common/utils/utils";
/**

View File

@ -0,0 +1,37 @@
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import { DiscordChannels, logToChannel } from "../bot/bot";
import { EmbedBuilder } from "discord.js";
import { formatPp } from "@ssr/common/utils/number-utils";
/**
* Logs that a new player is being tracked
*
* @param player the player being tracked
*/
export async function logNewTrackedPlayer(player: ScoreSaberPlayerToken) {
await logToChannel(
DiscordChannels.trackedPlayerLogs,
new EmbedBuilder()
.setTitle("New Player Tracked")
.setDescription(`https://ssr.fascinated.cc/player/${player.id}`)
.addFields([
{
name: "Username",
value: player.name,
inline: true,
},
{
name: "ID",
value: player.id,
inline: true,
},
{
name: "PP",
value: formatPp(player.pp) + "pp",
inline: true,
},
])
.setThumbnail(player.profilePicture)
.setColor("#00ff00")
);
}

View File

@ -1,39 +1,36 @@
import { Controller, Get } from "elysia-decorators";
import { t } from "elysia";
import { Leaderboards } from "@ssr/common/leaderboard";
import { TopScoresResponse } from "@ssr/common/response/top-scores-response";
import { ScoreService } from "../service/score.service";
import { ScoreSortType } from "@ssr/common/sorter/sort-type";
import { SortDirection } from "@ssr/common/sorter/sort-direction";
@Controller("/scores")
export default class ScoresController {
@Get("/player/:leaderboard/:id/:page/:sort/:direction", {
@Get("/player/:leaderboard/:id/:page/:sort", {
config: {},
params: t.Object({
leaderboard: t.String({ required: true }),
id: t.String({ required: true }),
page: t.Number({ required: true }),
sort: t.String({ required: true }),
direction: t.String({ required: true }),
}),
query: t.Object({
search: t.Optional(t.String()),
}),
})
public async getScores({
params: { leaderboard, id, page, sort, direction },
params: { leaderboard, id, page, sort },
query: { search },
}: {
params: {
leaderboard: Leaderboards;
id: string;
page: number;
sort: ScoreSortType;
direction: SortDirection;
sort: string;
};
query: { search?: string };
}): Promise<unknown> {
return await ScoreService.getPlayerScores(leaderboard, id, page, sort, direction, search);
return await ScoreService.getPlayerScores(leaderboard, id, page, sort, search);
}
@Get("/leaderboard/:leaderboard/:id/:page", {
@ -56,4 +53,35 @@ export default class ScoresController {
}): Promise<unknown> {
return await ScoreService.getLeaderboardScores(leaderboard, id, page);
}
@Get("/history/:playerId/:leaderboardId/:page", {
config: {},
params: t.Object({
playerId: t.String({ required: true }),
leaderboardId: t.String({ required: true }),
page: t.Number({ required: true }),
}),
})
public async getScoreHistory({
params: { playerId, leaderboardId, page },
}: {
params: {
playerId: string;
leaderboardId: string;
page: number;
};
query: { search?: string };
}): Promise<unknown> {
return (await ScoreService.getScoreHistory(playerId, leaderboardId, page)).toJSON();
}
@Get("/top", {
config: {},
})
public async getTopScores(): Promise<TopScoresResponse> {
const scores = await ScoreService.getTopScores();
return {
scores,
};
}
}

View File

@ -11,12 +11,10 @@ import mongoose from "mongoose";
import PlayerController from "./controller/player.controller";
import { PlayerService } from "./service/player.service";
import { cron } from "@elysiajs/cron";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { delay, isProduction } from "@ssr/common/utils/utils";
import { isProduction } from "@ssr/common/utils/utils";
import ImageController from "./controller/image.controller";
import { ScoreService } from "./service/score.service";
import { Config } from "@ssr/common/config";
import { PlayerDocument, PlayerModel } from "@ssr/common/model/player";
import ScoresController from "./controller/scores.controller";
import LeaderboardController from "./controller/leaderboard.controller";
import { getAppVersion } from "./common/app.util";
@ -24,7 +22,6 @@ import { connectScoresaberWebsocket } from "@ssr/common/websocket/scoresaber-web
import { connectBeatLeaderWebsocket } from "@ssr/common/websocket/beatleader-websocket";
import { DiscordChannels, initDiscordBot, logToChannel } from "./bot/bot";
import { EmbedBuilder } from "discord.js";
import { ScoreSort } from "@ssr/common/score/score-sort";
// Load .env file
dotenv.config({
@ -67,84 +64,22 @@ export const app = new Elysia();
app.use(
cron({
name: "player-statistics-tracker-cron",
pattern: "1 0 * * *", // Every day at 00:01
pattern: "0 1 * * * *", // Every day at 00:01
timezone: "Europe/London", // UTC time
protect: true,
run: async () => {
const pages = 20; // top 1000 players
const cooldown = 60_000 / 250; // 250 requests per minute
let toTrack: PlayerDocument[] = await PlayerModel.find({});
const toRemoveIds: string[] = [];
// loop through pages to fetch the top players
console.log(`Fetching ${pages} pages of players from ScoreSaber...`);
for (let i = 0; i < pages; i++) {
const pageNumber = i + 1;
console.log(`Fetching page ${pageNumber}...`);
const page = await scoresaberService.lookupPlayers(pageNumber);
if (page === undefined) {
console.log(`Failed to fetch players on page ${pageNumber}, skipping page...`);
await delay(cooldown);
continue;
}
for (const player of page.players) {
const foundPlayer = await PlayerService.getPlayer(player.id, true, player);
await PlayerService.trackScoreSaberPlayer(foundPlayer, player);
toRemoveIds.push(foundPlayer.id);
}
await delay(cooldown);
}
console.log(`Finished tracking player statistics for ${pages} pages, found ${toRemoveIds.length} players.`);
// remove all players that have been tracked
toTrack = toTrack.filter(player => !toRemoveIds.includes(player.id));
console.log(`Tracking ${toTrack.length} player statistics...`);
for (const player of toTrack) {
await PlayerService.trackScoreSaberPlayer(player);
await delay(cooldown);
}
console.log("Finished tracking player statistics.");
await PlayerService.updatePlayerStatistics();
},
})
);
app.use(
cron({
name: "scores-background-refresh",
pattern: "*/1 * * * *",
name: "player-scores-tracker-cron",
pattern: "0 4 * * *", // Every day at 04:00
timezone: "Europe/London", // UTC time
protect: true,
run: async () => {
console.log(`Refreshing player score data...`);
const players = await PlayerModel.find({});
console.log(`Found ${players.length} players to refresh.`);
for (const player of players) {
console.log(`Refreshing scores for ${player.id}...`);
let page = 1;
let hasMorePages = true;
while (hasMorePages) {
const scoresPage = await scoresaberService.lookupPlayerScores({
playerId: player.id,
page: page,
limit: 100,
sort: ScoreSort.recent,
});
if (!scoresPage) {
break;
}
if (scoresPage.metadata.total <= page * 100) {
hasMorePages = false;
}
page++;
for (const score of scoresPage.playerScores) {
await ScoreService.trackScoreSaberScore(score.score, score.leaderboard, player.id);
}
}
console.log(`Finished refreshing scores for ${player.id}, total pages refreshed: ${page - 1}.`);
}
await PlayerService.refreshPlayerScores();
},
})
);
@ -228,11 +163,14 @@ app.use(
})
);
app.onStart(() => {
app.onStart(async () => {
console.log("Listening on port http://localhost:8080");
if (isProduction()) {
initDiscordBot();
await initDiscordBot();
}
});
app.listen(8080);
app.listen({
port: 8080,
idleTimeout: 120, // 2 minutes
});

View File

@ -3,22 +3,36 @@ import { AppStatistics } from "@ssr/common/types/backend/app-statistics";
import { ScoreSaberScoreModel } from "@ssr/common/model/score/impl/scoresaber-score";
import { AdditionalScoreDataModel } from "@ssr/common/model/additional-score-data/additional-score-data";
import { BeatSaverMapModel } from "@ssr/common/model/beatsaver/map";
import { ScoreSaberLeaderboardModel } from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { SSRCache } from "@ssr/common/cache";
const statisticsCache = new SSRCache({
ttl: 120 * 1000, // 2 minutes
});
export class AppService {
/**
* Gets the app statistics.
*/
public static async getAppStatistics(): Promise<AppStatistics> {
if (statisticsCache.has("app-statistics")) {
return statisticsCache.get<AppStatistics>("app-statistics")!;
}
const trackedPlayers = await PlayerModel.countDocuments();
const trackedScores = await ScoreSaberScoreModel.countDocuments();
const additionalScoresData = await AdditionalScoreDataModel.countDocuments();
const cachedBeatSaverMaps = await BeatSaverMapModel.countDocuments();
const cachedScoreSaberLeaderboards = await ScoreSaberLeaderboardModel.countDocuments();
return {
const response = {
trackedPlayers,
trackedScores,
additionalScoresData,
cachedBeatSaverMaps,
cachedScoreSaberLeaderboards,
};
statisticsCache.set("app-statistics", response);
return response;
}
}

View File

@ -2,10 +2,8 @@ import { ImageResponse } from "@vercel/og";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import React from "react";
import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils";
import { getDifficultyFromScoreSaberDifficulty } from "@ssr/common/utils/scoresaber-utils";
import { StarIcon } from "../../components/star-icon";
import { GlobeIcon } from "../../components/globe-icon";
import ScoreSaberLeaderboardToken from "@ssr/common/types/token/scoresaber/score-saber-leaderboard-token";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
import { Jimp } from "jimp";
import { extractColors } from "extract-colors";
@ -13,6 +11,8 @@ import { Config } from "@ssr/common/config";
import { fetchWithCache } from "../common/cache.util";
import { SSRCache } from "@ssr/common/cache";
import { getScoreSaberPlayerFromToken } from "@ssr/common/token-creators";
import LeaderboardService from "./leaderboard.service";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
const cache = new SSRCache({
ttl: 1000 * 60 * 60, // 1 hour
@ -176,12 +176,11 @@ export class ImageService {
* @param id the leaderboard's id
*/
public static async generateLeaderboardImage(id: string) {
const leaderboard = await fetchWithCache<ScoreSaberLeaderboardToken>(cache, `leaderboard-${id}`, () =>
scoresaberService.lookupLeaderboard(id)
);
if (!leaderboard) {
const response = await LeaderboardService.getLeaderboard<ScoreSaberLeaderboard>("scoresaber", id);
if (!response) {
return undefined;
}
const { leaderboard } = response;
const ranked = leaderboard.stars > 0;
@ -189,7 +188,7 @@ export class ImageService {
(
<ImageService.BaseImage>
{/* Leaderboard Cover Image */}
<img src={leaderboard.coverImage} width={256} height={256} alt="Leaderboard Cover" tw="rounded-full mb-3" />
<img src={leaderboard.songArt} width={256} height={256} alt="Leaderboard Cover" tw="rounded-full mb-3" />
{/* Leaderboard Name */}
<p tw="font-bold text-6xl m-0">
@ -206,9 +205,7 @@ export class ImageService {
)}
{/* Leaderboard Difficulty */}
<p tw={"font-bold m-0 text-4xl" + (ranked ? " pl-3" : "")}>
{getDifficultyFromScoreSaberDifficulty(leaderboard.difficulty.difficulty)}
</p>
<p tw={"font-bold m-0 text-4xl" + (ranked ? " pl-3" : "")}>{leaderboard.difficulty.difficulty}</p>
</div>
{/* Leaderboard Author */}

View File

@ -1,17 +1,16 @@
import { Leaderboards } from "@ssr/common/leaderboard";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { SSRCache } from "@ssr/common/cache";
import { LeaderboardResponse } from "@ssr/common/response/leaderboard-response";
import Leaderboard from "@ssr/common/leaderboard/leaderboard";
import ScoreSaberLeaderboardToken from "@ssr/common/types/token/scoresaber/score-saber-leaderboard-token";
import { NotFoundError } from "elysia";
import BeatSaverService from "./beatsaver.service";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import { getScoreSaberLeaderboardFromToken } from "@ssr/common/token-creators";
const leaderboardCache = new SSRCache({
ttl: 1000 * 60 * 60 * 24,
});
import {
ScoreSaberLeaderboard,
ScoreSaberLeaderboardModel,
} from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import Leaderboard from "@ssr/common/model/leaderboard/leaderboard";
export default class LeaderboardService {
/**
@ -21,16 +20,9 @@ export default class LeaderboardService {
* @param id the id
*/
private static async getLeaderboardToken<T>(leaderboard: Leaderboards, id: string): Promise<T | undefined> {
const cacheKey = `${leaderboard}-${id}`;
if (leaderboardCache.has(cacheKey)) {
return leaderboardCache.get(cacheKey) as T;
}
switch (leaderboard) {
case "scoresaber": {
const leaderboard = (await scoresaberService.lookupLeaderboard(id)) as T;
leaderboardCache.set(cacheKey, leaderboard);
return leaderboard;
return (await scoresaberService.lookupLeaderboard(id)) as T;
}
default: {
return undefined;
@ -49,16 +41,45 @@ export default class LeaderboardService {
let leaderboard: Leaderboard | undefined;
let beatSaverMap: BeatSaverMap | undefined;
const now = new Date();
switch (leaderboardName) {
case "scoresaber": {
const leaderboardToken = await LeaderboardService.getLeaderboardToken<ScoreSaberLeaderboardToken>(
leaderboardName,
id
);
if (leaderboardToken == undefined) {
let foundLeaderboard = false;
const cachedLeaderboard = await ScoreSaberLeaderboardModel.findById(id);
if (cachedLeaderboard != null) {
leaderboard = cachedLeaderboard.toObject() as unknown as ScoreSaberLeaderboard;
if (
leaderboard &&
(leaderboard.ranked || // Never refresh ranked leaderboards (it will get refreshed every night)
leaderboard.lastRefreshed == undefined || // Refresh if it has never been refreshed
now.getTime() - leaderboard.lastRefreshed.getTime() > 1000 * 60 * 60 * 24) // Refresh every day
) {
foundLeaderboard = true;
}
}
if (!foundLeaderboard) {
const leaderboardToken = await LeaderboardService.getLeaderboardToken<ScoreSaberLeaderboardToken>(
leaderboardName,
id
);
if (leaderboardToken == undefined) {
throw new NotFoundError(`Leaderboard not found for "${id}"`);
}
leaderboard = getScoreSaberLeaderboardFromToken(leaderboardToken);
leaderboard.lastRefreshed = new Date();
await ScoreSaberLeaderboardModel.findOneAndUpdate({ _id: id }, leaderboard, {
upsert: true,
new: true,
setDefaultsOnInsert: true,
});
}
if (leaderboard == undefined) {
throw new NotFoundError(`Leaderboard not found for "${id}"`);
}
leaderboard = getScoreSaberLeaderboardFromToken(leaderboardToken);
beatSaverMap = await BeatSaverService.getMap(leaderboard.songHash);
break;
}

View File

@ -1,79 +1,82 @@
import { PlayerDocument, PlayerModel } from "@ssr/common/model/player";
import { NotFoundError } from "../error/not-found-error";
import { NotFoundError } from "@ssr/common/error/not-found-error";
import { getDaysAgoDate, getMidnightAlignedDate } from "@ssr/common/utils/time-utils";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import { InternalServerError } from "../error/internal-server-error";
import { formatPp } from "@ssr/common/utils/number-utils";
import { getPageFromRank, isProduction } from "@ssr/common/utils/utils";
import { DiscordChannels, logToChannel } from "../bot/bot";
import { EmbedBuilder } from "discord.js";
import { InternalServerError } from "@ssr/common/error/internal-server-error";
import { delay, getPageFromRank, isProduction } from "@ssr/common/utils/utils";
import { AroundPlayer } from "@ssr/common/types/around-player";
import { ScoreSort } from "@ssr/common/score/score-sort";
import { getScoreSaberLeaderboardFromToken } from "@ssr/common/token-creators";
import { ScoreService } from "./score.service";
import { logNewTrackedPlayer } from "../common/embds";
const SCORESABER_REQUEST_COOLDOWN = 60_000 / 250; // 250 requests per minute
const accountCreationLock: { [id: string]: Promise<PlayerDocument> } = {};
export class PlayerService {
/**
* Get a player from the database.
*
* @param id the player to fetch
* @param create if true, create the player if it doesn't exist
* @param playerToken an optional player token for the player
* @returns the player
* @throws NotFoundError if the player is not found
*/
public static async getPlayer(
id: string,
create: boolean = false,
playerToken?: ScoreSaberPlayerToken
): Promise<PlayerDocument> {
// Wait for the existing lock if it's in progress
if (accountCreationLock[id] !== undefined) {
await accountCreationLock[id];
}
let player: PlayerDocument | null = await PlayerModel.findById(id);
if (player === null) {
// If create is on, create the player, otherwise return unknown player
playerToken = create ? (playerToken ? playerToken : await scoresaberService.lookupPlayer(id)) : undefined;
if (playerToken === undefined) {
if (!create) {
throw new NotFoundError(`Player "${id}" not found`);
}
console.log(`Creating player "${id}"...`);
try {
player = (await PlayerModel.create({ _id: id })) as PlayerDocument;
player.trackedSince = new Date();
await this.seedPlayerHistory(player, playerToken);
playerToken = playerToken || (await scoresaberService.lookupPlayer(id));
// Only notify in production
if (isProduction()) {
await logToChannel(
DiscordChannels.trackedPlayerLogs,
new EmbedBuilder()
.setTitle("New Player Tracked")
.setDescription(`https://ssr.fascinated.cc/player/${playerToken.id}`)
.addFields([
{
name: "Username",
value: playerToken.name,
inline: true,
},
{
name: "ID",
value: playerToken.id,
inline: true,
},
{
name: "PP",
value: formatPp(playerToken.pp) + "pp",
inline: true,
},
])
.setThumbnail(playerToken.profilePicture)
.setColor("#00ff00")
);
if (!playerToken) {
throw new NotFoundError(`Player "${id}" not found`);
}
// Create a new lock promise and assign it
accountCreationLock[id] = (async () => {
let newPlayer: PlayerDocument;
try {
console.log(`Creating player "${id}"...`);
newPlayer = (await PlayerModel.create({ _id: id })) as PlayerDocument;
newPlayer.trackedSince = new Date();
await newPlayer.save();
await this.seedPlayerHistory(newPlayer, playerToken);
await this.refreshAllPlayerScores(newPlayer);
// Notify in production
if (isProduction()) {
await logNewTrackedPlayer(playerToken);
}
} catch (err) {
console.log(`Failed to create player document for "${id}"`, err);
throw new InternalServerError(`Failed to create player document for "${id}"`);
} finally {
// Ensure the lock is always removed
delete accountCreationLock[id];
}
} catch (err) {
const message = `Failed to create player document for "${id}"`;
console.log(message, err);
throw new InternalServerError(message);
return newPlayer;
})();
// Wait for the player creation to complete
player = await accountCreationLock[id];
// Update player name
if (player.name !== playerToken.name) {
player.name = playerToken.name;
await player.save();
}
}
return player;
// Ensure that the player is now of type PlayerDocument
return player as PlayerDocument;
}
/**
@ -90,7 +93,7 @@ export class PlayerService {
});
playerRankHistory.push(playerToken.rank);
let daysAgo = 1; // Start from yesterday
let daysAgo = 0; // Start from today
for (let i = playerRankHistory.length - daysAgo - 1; i >= 0; i--) {
const rank = playerRankHistory[i];
// Skip inactive days
@ -131,7 +134,7 @@ export class PlayerService {
// Seed the history with ScoreSaber data if no history exists
if (foundPlayer.getDaysTracked() === 0) {
await this.seedPlayerHistory(foundPlayer, player);
await this.seedPlayerHistory(foundPlayer.id, player);
}
// Update current day's statistics
@ -243,4 +246,126 @@ export class PlayerService {
return players.slice(start, end);
}
/**
* Refreshes all the players scores.
*
* @param player the player to refresh
*/
public static async refreshAllPlayerScores(player: PlayerDocument) {
await this.refreshPlayerScoreSaberScores(player);
}
/**
* Ensures that all the players scores from the
* ScoreSaber API are up-to-date.
*
* @param player the player to refresh
* @private
*/
private static async refreshPlayerScoreSaberScores(player: PlayerDocument) {
console.log(`Refreshing scores for ${player.id}...`);
let page = 1;
let hasMorePages = true;
while (hasMorePages) {
const scoresPage = await scoresaberService.lookupPlayerScores({
playerId: player.id,
page: page,
limit: 100,
sort: ScoreSort.recent,
});
if (!scoresPage) {
console.warn(`Failed to fetch scores for ${player.id} on page ${page}.`);
break;
}
let missingScores = 0;
for (const score of scoresPage.playerScores) {
const leaderboard = getScoreSaberLeaderboardFromToken(score.leaderboard);
const scoreSaberScore = await ScoreService.getScoreSaberScore(
player.id,
leaderboard.id + "",
leaderboard.difficulty.difficulty,
leaderboard.difficulty.characteristic,
score.score.baseScore
);
if (scoreSaberScore == null) {
missingScores++;
}
await ScoreService.trackScoreSaberScore(score.score, score.leaderboard, player.id);
}
// Stop paginating if no scores are missing OR if player has seededScores marked true
if ((missingScores === 0 && player.seededScores) || page >= Math.ceil(scoresPage.metadata.total / 100)) {
hasMorePages = false;
}
page++;
await delay(SCORESABER_REQUEST_COOLDOWN); // Cooldown between page requests
}
// Mark player as seeded
player.seededScores = true;
await player.save();
console.log(`Finished refreshing scores for ${player.id}, total pages refreshed: ${page - 1}.`);
}
/**
* Ensures all player scores are up-to-date.
*/
public static async refreshPlayerScores() {
console.log(`Refreshing player score data...`);
const players = await PlayerModel.find({});
console.log(`Found ${players.length} players to refresh.`);
for (const player of players) {
await this.refreshAllPlayerScores(player.id);
await delay(SCORESABER_REQUEST_COOLDOWN); // Cooldown between players
}
}
/**
* Updates the player statistics for all players.
*/
public static async updatePlayerStatistics() {
const pages = 20; // top 1000 players
let toTrack: PlayerDocument[] = await PlayerModel.find({});
const toRemoveIds: string[] = [];
// loop through pages to fetch the top players
console.log(`Fetching ${pages} pages of players from ScoreSaber...`);
for (let i = 0; i < pages; i++) {
const pageNumber = i + 1;
console.log(`Fetching page ${pageNumber}...`);
const page = await scoresaberService.lookupPlayers(pageNumber);
if (page === undefined) {
console.log(`Failed to fetch players on page ${pageNumber}, skipping page...`);
await delay(SCORESABER_REQUEST_COOLDOWN);
continue;
}
for (const player of page.players) {
const foundPlayer = await PlayerService.getPlayer(player.id, true, player);
await PlayerService.trackScoreSaberPlayer(foundPlayer, player);
toRemoveIds.push(foundPlayer.id);
}
await delay(SCORESABER_REQUEST_COOLDOWN);
}
console.log(`Finished tracking player statistics for ${pages} pages, found ${toRemoveIds.length} players.`);
// remove all players that have been tracked
toTrack = toTrack.filter(player => !toRemoveIds.includes(player.id));
console.log(`Tracking ${toTrack.length} player statistics...`);
for (const player of toTrack) {
await PlayerService.trackScoreSaberPlayer(player);
await delay(SCORESABER_REQUEST_COOLDOWN);
}
console.log("Finished tracking player statistics.");
}
}

View File

@ -3,9 +3,10 @@ import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils
import { isProduction } from "@ssr/common/utils/utils";
import { Metadata } from "@ssr/common/types/metadata";
import { NotFoundError } from "elysia";
import BeatSaverService from "./beatsaver.service";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { ScoreSort } from "@ssr/common/score/score-sort";
import { Leaderboards } from "@ssr/common/leaderboard";
import Leaderboard from "@ssr/common/leaderboard/leaderboard";
import LeaderboardService from "./leaderboard.service";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import { PlayerScore } from "@ssr/common/score/player-score";
@ -23,24 +24,20 @@ import {
AdditionalScoreDataModel,
} from "@ssr/common/model/additional-score-data/additional-score-data";
import { BeatLeaderScoreImprovementToken } from "@ssr/common/types/token/beatleader/score/score-improvement";
import Score, { ScoreType } from "@ssr/common/model/score/score";
import { ScoreType } from "@ssr/common/model/score/score";
import { getScoreSaberLeaderboardFromToken, getScoreSaberScoreFromToken } from "@ssr/common/token-creators";
import {
ScoreSaberPreviousScore,
ScoreSaberScore,
ScoreSaberScoreInternal,
ScoreSaberScoreModel,
} from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import { ScoreSorters } from "@ssr/common/sorter/sorters";
import { ScoreSortType } from "@ssr/common/sorter/sort-type";
import { SortDirection } from "@ssr/common/sorter/sort-direction";
import { Pagination } from "../../../common/src/pagination";
import { PlayerService } from "./player.service";
import { ScoreSort } from "@ssr/common/score/score-sort";
import BeatSaverService from "./beatsaver.service";
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import ScoreSaberScoreToken from "@ssr/common/types/token/scoresaber/score-saber-score-token";
import ScoreSaberLeaderboardToken from "@ssr/common/types/token/scoresaber/score-saber-leaderboard-token";
import { MapDifficulty } from "@ssr/common/score/map-difficulty";
import { MapCharacteristic } from "@ssr/common/types/map-characteristic";
import { Page, Pagination } from "@ssr/common/pagination";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import Leaderboard from "@ssr/common/model/leaderboard/leaderboard";
const playerScoresCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
@ -50,8 +47,6 @@ const leaderboardScoresCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
});
const ITEMS_PER_PAGE = 8;
export class ScoreService {
/**
* Notifies the number one score in Discord.
@ -163,6 +158,7 @@ export class ScoreService {
history.scores = scores;
player.setStatisticHistory(today, history);
player.markModified("statisticHistory");
await player.save();
}
@ -178,7 +174,13 @@ export class ScoreService {
leaderboardToken: ScoreSaberLeaderboardToken,
playerId?: string
) {
playerId = playerId || scoreToken.leaderboardPlayerInfo.id;
playerId = (scoreToken.leaderboardPlayerInfo && scoreToken.leaderboardPlayerInfo.id) || playerId;
if (!playerId) {
console.error(`Player ID is undefined, unable to track score: ${scoreToken.id}`);
return;
}
const playerName = (scoreToken.leaderboardPlayerInfo && scoreToken.leaderboardPlayerInfo.name) || "Unknown";
const leaderboard = getScoreSaberLeaderboardFromToken(leaderboardToken);
const score = getScoreSaberScoreFromToken(scoreToken, leaderboard, playerId);
@ -187,12 +189,35 @@ export class ScoreService {
if (player == undefined) {
return;
}
// Update player name
player.name = playerName;
await player.save();
// The score has already been tracked, so ignore it.
if (
(await this.getScoreSaberScore(
playerId,
leaderboard.id + "",
leaderboard.difficulty.difficulty,
leaderboard.difficulty.characteristic,
score.score
)) !== null
) {
await logToChannel(
DiscordChannels.backendLogs,
new EmbedBuilder().setDescription(`Score ${score.scoreId} already tracked`)
);
return;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
delete score.playerInfo;
await ScoreSaberScoreModel.create(score);
console.log(
`Tracked ScoreSaber score for "${playerName}"(${playerId}), difficulty: ${score.difficulty}, score: ${score.score}, pp: ${score.pp.toFixed(2)}pp, leaderboard: ${leaderboard.id}`
);
}
/**
@ -274,6 +299,103 @@ export class ScoreService {
);
}
/**
* Gets the top tracked scores.
*
* @param amount the amount of scores to get
* @returns the top scores
*/
public static async getTopScores(amount: number = 100) {
const foundScores = await ScoreSaberScoreModel.aggregate([
// Start sorting by timestamp descending using the new compound index
{ $sort: { leaderboardId: 1, playerId: 1, timestamp: -1 } },
{
$group: {
_id: { leaderboardId: "$leaderboardId", playerId: "$playerId" },
latestScore: { $first: "$$ROOT" }, // Retrieve the latest score per group
},
},
// Sort by pp of the latest scores in descending order
{ $sort: { "latestScore.pp": -1 } },
{ $limit: amount },
]);
// Collect unique leaderboard IDs
const leaderboardIds = [...new Set(foundScores.map(s => s.latestScore.leaderboardId))];
const leaderboardMap = await this.fetchLeaderboardsInBatch(leaderboardIds);
// Collect player IDs for batch retrieval
const playerIds = foundScores.map(result => result.latestScore.playerId);
const players = await PlayerModel.find({ _id: { $in: playerIds } }).exec();
const playerMap = new Map(players.map(player => [player._id.toString(), player]));
// Prepare to fetch additional data concurrently
const scoreDataPromises = foundScores.map(async result => {
const score: ScoreSaberScore = result.latestScore;
const leaderboardResponse = leaderboardMap[score.leaderboardId];
if (!leaderboardResponse) {
return null; // Skip if leaderboard data is not available
}
const { leaderboard, beatsaver } = leaderboardResponse;
// Fetch additional data concurrently
const [additionalData, previousScore] = await Promise.all([
this.getAdditionalScoreData(
score.playerId,
leaderboard.songHash,
`${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`,
score.score
),
this.getPreviousScore(score.playerId, leaderboard.id + "", score.timestamp),
]);
// Attach additional and previous score data if available
if (additionalData) score.additionalData = additionalData;
if (previousScore) score.previousScore = previousScore;
// Attach player info if available
const player = playerMap.get(score.playerId.toString());
if (player) {
score.playerInfo = {
id: player._id,
name: player.name,
};
}
return {
score: score as ScoreSaberScore,
leaderboard: leaderboard,
beatSaver: beatsaver,
};
});
return (await Promise.all(scoreDataPromises)).filter(score => score !== null);
}
/**
* Fetches leaderboards in a batch.
*
* @param leaderboardIds the ids of the leaderboards
* @returns the fetched leaderboards
* @private
*/
private static async fetchLeaderboardsInBatch(leaderboardIds: string[]) {
// Remove duplicates from leaderboardIds
const uniqueLeaderboardIds = Array.from(new Set(leaderboardIds));
const leaderboardResponses = await Promise.all(
uniqueLeaderboardIds.map(id => LeaderboardService.getLeaderboard<ScoreSaberLeaderboard>("scoresaber", id))
);
return leaderboardResponses.reduce(
(map, response) => {
if (response) map[response.leaderboard.id] = response;
return map;
},
{} as Record<string, { leaderboard: ScoreSaberLeaderboard; beatsaver?: BeatSaverMap }>
);
}
/**
* Gets the additional score data for a player's score.
*
@ -302,146 +424,100 @@ export class ScoreService {
}
/**
* Gets scores for a player.
* Gets a ScoreSaber score.
*
* @param leaderboardName the leaderboard to get the scores from
* @param playerId the players id
* @param page the page to get
* @param sort the sort type to use
* @param direction the direction to sort the scores
* @param search the search to use
* @returns the scores
* @param playerId the player who set the score
* @param leaderboardId the leaderboard id the score was set on
* @param difficulty the difficulty played
* @param characteristic the characteristic played
* @param score the score of the score set
*/
public static async getScoreSaberScore(
playerId: string,
leaderboardId: string,
difficulty: MapDifficulty,
characteristic: MapCharacteristic,
score: number
) {
return ScoreSaberScoreModel.findOne({
playerId: playerId,
leaderboardId: leaderboardId,
difficulty: difficulty,
characteristic: characteristic,
score: score,
});
}
public static async getPlayerScores(
leaderboardName: Leaderboards,
playerId: string,
page: number,
sort: ScoreSortType,
direction: SortDirection,
sort: string,
search?: string
): Promise<PlayerScoresResponse<unknown, unknown> | undefined> {
console.log(
`Fetching scores for ${playerId} on ${leaderboardName}, page: ${page}, sort: ${sort}, direction: ${direction}, search: ${search}`
);
return fetchWithCache(
playerScoresCache,
`player-scores-${leaderboardName}-${playerId}-${page}-${sort}-${search}`,
async () => {
const toReturn: PlayerScore<unknown, unknown>[] | undefined = [];
const scores: PlayerScore<unknown, unknown>[] = [];
let metadata: Metadata = new Metadata(0, 0, 0, 0); // Default values
switch (leaderboardName) {
case "scoresaber": {
let isPlayerTracked = false;
try {
isPlayerTracked = (await PlayerService.getPlayer(playerId, false)) != undefined;
} catch {
/* ignored */
const leaderboardScores = await scoresaberService.lookupPlayerScores({
playerId,
page,
sort: sort as ScoreSort,
search,
});
if (leaderboardScores == undefined) {
break;
}
if (isPlayerTracked) {
const rawScores = ScoreSorters.scoreSaber.sort(
sort,
direction,
(await ScoreSaberScoreModel.find({ playerId: playerId }).exec()) as unknown as ScoreSaberScore[]
);
if (!rawScores || rawScores.length === 0) {
break;
}
const pagination = new Pagination<ScoreSaberScore>().setItemsPerPage(ITEMS_PER_PAGE).setItems(rawScores);
const paginatedPage = pagination.getPage(page);
metadata = paginatedPage.metadata;
metadata = new Metadata(
Math.ceil(leaderboardScores.metadata.total / leaderboardScores.metadata.itemsPerPage),
leaderboardScores.metadata.total,
leaderboardScores.metadata.page,
leaderboardScores.metadata.itemsPerPage
);
for (const score of paginatedPage.items) {
const { leaderboard, beatsaver } = await LeaderboardService.getLeaderboard<ScoreSaberLeaderboard>(
"scoresaber",
String(score.leaderboardId)
);
if (leaderboard == undefined) {
continue;
}
const scorePromises = leaderboardScores.playerScores.map(async token => {
const leaderboard = getScoreSaberLeaderboardFromToken(token.leaderboard);
if (!leaderboard) return undefined;
const additionalData = await this.getAdditionalScoreData(
const score = getScoreSaberScoreFromToken(token.score, leaderboard, playerId);
if (!score) return undefined;
// Fetch additional data, previous score, and BeatSaver map concurrently
const [additionalData, previousScore, beatSaverMap] = await Promise.all([
this.getAdditionalScoreData(
playerId,
leaderboard.songHash,
`${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`,
score.score
);
if (additionalData == undefined) {
continue;
}
),
this.getPreviousScore(playerId, leaderboard.id + "", score.timestamp),
BeatSaverService.getMap(leaderboard.songHash),
]);
toReturn.push({
score: score,
leaderboard: leaderboard,
beatSaver: beatsaver,
});
if (additionalData) {
score.additionalData = additionalData;
}
} else {
// Convert the sort type
let scoreSaberSort: ScoreSort;
switch (sort) {
case ScoreSortType.date: {
scoreSaberSort = ScoreSort.recent;
break;
}
case ScoreSortType.pp: {
scoreSaberSort = ScoreSort.top;
break;
}
default: {
scoreSaberSort = ScoreSort.recent;
break;
}
if (previousScore) {
score.previousScore = previousScore;
}
const rawScores = await scoresaberService.lookupPlayerScores({
playerId: playerId,
page: page,
sort: scoreSaberSort,
search: search,
});
if (!rawScores || rawScores.playerScores.length === 0) {
break;
}
metadata = new Metadata(
Math.ceil(rawScores.metadata.total / rawScores.metadata.itemsPerPage),
rawScores.metadata.total,
rawScores.metadata.page,
rawScores.metadata.itemsPerPage
);
for (const token of rawScores.playerScores) {
const leaderboard = getScoreSaberLeaderboardFromToken(token.leaderboard);
if (leaderboard == undefined) {
continue;
}
const score = getScoreSaberScoreFromToken(token.score, leaderboard, playerId);
if (score == undefined) {
continue;
}
const additionalData = await this.getAdditionalScoreData(
playerId,
leaderboard.songHash,
`${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`,
score.score
);
if (additionalData !== undefined) {
score.additionalData = additionalData;
}
toReturn.push({
score: score,
leaderboard: leaderboard,
beatSaver: await BeatSaverService.getMap(leaderboard.songHash),
});
}
}
return {
score: score,
leaderboard: leaderboard,
beatSaver: beatSaverMap,
} as PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>;
});
const resolvedScores = (await Promise.all(scorePromises)).filter(
(s): s is PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard> => s !== undefined
);
scores.push(...resolvedScores);
break;
}
default: {
@ -449,9 +525,8 @@ export class ScoreService {
}
}
console.log(metadata);
return {
scores: toReturn,
scores: scores,
metadata: metadata,
};
}
@ -531,4 +606,114 @@ export class ScoreService {
}
);
}
/**
* Gets the player's score history for a map.
*
* @param playerId the player's id to get the previous scores for
* @param leaderboardId the leaderboard to get the previous scores on
* @param page the page to get
*/
public static async getScoreHistory(
playerId: string,
leaderboardId: string,
page: number
): Promise<Page<PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>>> {
const scores = await ScoreSaberScoreModel.find({ playerId: playerId, leaderboardId: leaderboardId })
.sort({ timestamp: -1 })
.skip(1);
if (scores == null || scores.length == 0) {
throw new NotFoundError(`No previous scores found for ${playerId} in ${leaderboardId}`);
}
return new Pagination<PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>>()
.setItemsPerPage(8)
.setTotalItems(scores.length)
.getPage(page, async () => {
const toReturn: PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>[] = [];
for (const score of scores) {
const leaderboardResponse = await LeaderboardService.getLeaderboard<ScoreSaberLeaderboard>(
"scoresaber",
leaderboardId
);
if (leaderboardResponse == undefined) {
throw new NotFoundError(`Leaderboard "${leaderboardId}" not found`);
}
const { leaderboard, beatsaver } = leaderboardResponse;
const additionalData = await this.getAdditionalScoreData(
playerId,
leaderboard.songHash,
`${leaderboard.difficulty.difficulty}-${leaderboard.difficulty.characteristic}`,
score.score
);
if (additionalData !== undefined) {
score.additionalData = additionalData;
}
const previousScore = await this.getPreviousScore(playerId, leaderboardId, score.timestamp);
if (previousScore !== undefined) {
score.previousScore = previousScore;
}
toReturn.push({
score: score as unknown as ScoreSaberScore,
leaderboard: leaderboard,
beatSaver: beatsaver,
});
}
return toReturn;
});
}
/**
* Gets the player's previous score for a map.
*
* @param playerId the player's id to get the previous score for
* @param leaderboardId the leaderboard to get the previous score on
* @param timestamp the score's timestamp to get the previous score for
* @returns the score, or undefined if none
*/
public static async getPreviousScore(
playerId: string,
leaderboardId: string,
timestamp: Date
): Promise<ScoreSaberPreviousScore | undefined> {
const scores = await ScoreSaberScoreModel.find({ playerId: playerId, leaderboardId: leaderboardId });
if (scores == null || scores.length == 0) {
return undefined;
}
const scoreIndex = scores.findIndex(score => score.timestamp.getTime() == timestamp.getTime());
const score = scores.find(score => score.timestamp.getTime() == timestamp.getTime());
if (scoreIndex == -1 || score == undefined) {
return undefined;
}
const previousScore = scores[scoreIndex - 1];
if (previousScore == undefined) {
return undefined;
}
return {
score: previousScore.score,
accuracy: previousScore.accuracy,
modifiers: previousScore.modifiers,
misses: previousScore.misses,
missedNotes: previousScore.missedNotes,
badCuts: previousScore.badCuts,
fullCombo: previousScore.fullCombo,
pp: previousScore.pp,
weight: previousScore.weight,
maxCombo: previousScore.maxCombo,
change: {
score: score.score - previousScore.score,
accuracy: score.accuracy - previousScore.accuracy,
misses: score.misses - previousScore.misses,
missedNotes: score.missedNotes - previousScore.missedNotes,
badCuts: score.badCuts - previousScore.badCuts,
pp: score.pp - previousScore.pp,
weight: score.weight && previousScore.weight && score.weight - previousScore.weight,
maxCombo: score.maxCombo - previousScore.maxCombo,
},
} as ScoreSaberPreviousScore;
}
}

View File

@ -1,6 +1,6 @@
type CacheOptions = {
/**
* The time the cached object will be valid for
* The time (in ms) the cached object will be valid for
*/
ttl?: number;

View File

@ -1,4 +1,4 @@
import { HttpCode } from "../common/http-codes";
import { HttpCode } from "../http-codes";
export class InternalServerError extends Error {
constructor(

View File

@ -1,4 +1,4 @@
import { HttpCode } from "../common/http-codes";
import { HttpCode } from "../http-codes";
export class NotFoundError extends Error {
constructor(

View File

@ -1,4 +1,4 @@
import { HttpCode } from "../common/http-codes";
import { HttpCode } from "../http-codes";
export class RateLimitError extends Error {
constructor(

View File

@ -1,29 +0,0 @@
import Leaderboard from "../leaderboard";
import { LeaderboardStatus } from "../leaderboard-status";
export default interface ScoreSaberLeaderboard extends Leaderboard {
/**
* The star count for the leaderboard.
*/
readonly stars: number;
/**
* The total amount of plays.
*/
readonly plays: number;
/**
* The amount of plays today.
*/
readonly dailyPlays: number;
/**
* Whether this leaderboard is qualified to be ranked.
*/
readonly qualified: boolean;
/**
* The status of the map.
*/
readonly status: LeaderboardStatus;
}

View File

@ -1,24 +0,0 @@
import { MapDifficulty } from "../score/map-difficulty";
import { MapCharacteristic } from "../types/map-characteristic";
export default interface LeaderboardDifficulty {
/**
* The id of the leaderboard.
*/
leaderboardId: number;
/**
* The difficulty of the leaderboard.
*/
difficulty: MapDifficulty;
/**
* The characteristic of the leaderboard.
*/
characteristic: MapCharacteristic;
/**
* The raw difficulty of the leaderboard.
*/
difficultyRaw: string;
}

View File

@ -1,75 +0,0 @@
import LeaderboardDifficulty from "./leaderboard-difficulty";
export default interface Leaderboard {
/**
* The id of the leaderboard.
* @private
*/
readonly id: number;
/**
* The hash of the song this leaderboard is for.
* @private
*/
readonly songHash: string;
/**
* The name of the song this leaderboard is for.
* @private
*/
readonly songName: string;
/**
* The sub name of the leaderboard.
* @private
*/
readonly songSubName: string;
/**
* The author of the song this leaderboard is for.
* @private
*/
readonly songAuthorName: string;
/**
* The author of the level this leaderboard is for.
* @private
*/
readonly levelAuthorName: string;
/**
* The difficulty of the leaderboard.
* @private
*/
readonly difficulty: LeaderboardDifficulty;
/**
* The difficulties of the leaderboard.
* @private
*/
readonly difficulties: LeaderboardDifficulty[];
/**
* The maximum score of the leaderboard.
* @private
*/
readonly maxScore: number;
/**
* Whether the leaderboard is ranked.
* @private
*/
readonly ranked: boolean;
/**
* The link to the song art.
* @private
*/
readonly songArt: string;
/**
* The date the leaderboard was created.
* @private
*/
readonly timestamp: Date;
}

View File

@ -0,0 +1,56 @@
import Leaderboard from "../leaderboard";
import { type LeaderboardStatus } from "../leaderboard-status";
import { getModelForClass, modelOptions, Prop, ReturnModelType, Severity } from "@typegoose/typegoose";
import { Document } from "mongoose";
@modelOptions({
options: { allowMixed: Severity.ALLOW },
schemaOptions: {
collection: "scoresaber-leaderboards",
toObject: {
virtuals: true,
transform: function (_, ret) {
ret.id = ret._id;
delete ret._id;
delete ret.__v;
return ret;
},
},
},
})
export default class ScoreSaberLeaderboardInternal extends Leaderboard {
/**
* The star count for the leaderboard.
*/
@Prop({ required: true })
readonly stars!: number;
/**
* The total amount of plays.
*/
@Prop({ required: true })
readonly plays!: number;
/**
* The amount of plays today.
*/
@Prop({ required: true })
readonly dailyPlays!: number;
/**
* Whether this leaderboard is qualified to be ranked.
*/
@Prop({ required: true })
readonly qualified!: boolean;
/**
* The status of the map.
*/
@Prop({ required: true })
readonly status!: LeaderboardStatus;
}
export type ScoreSaberLeaderboard = InstanceType<typeof ScoreSaberLeaderboardInternal>;
export type ScoreSaberLeaderboardDocument = ScoreSaberLeaderboard & Document;
export const ScoreSaberLeaderboardModel: ReturnModelType<typeof ScoreSaberLeaderboardInternal> =
getModelForClass(ScoreSaberLeaderboardInternal);

View File

@ -0,0 +1,29 @@
import { type MapDifficulty } from "../../score/map-difficulty";
import { type MapCharacteristic } from "../../types/map-characteristic";
import { Prop } from "@typegoose/typegoose";
export default class LeaderboardDifficulty {
/**
* The id of the leaderboard.
*/
@Prop({ required: true })
leaderboardId!: number;
/**
* The difficulty of the leaderboard.
*/
@Prop({ required: true })
difficulty!: MapDifficulty;
/**
* The characteristic of the leaderboard.
*/
@Prop({ required: true })
characteristic!: MapCharacteristic;
/**
* The raw difficulty of the leaderboard.
*/
@Prop({ required: true })
difficultyRaw!: string;
}

View File

@ -0,0 +1,99 @@
import LeaderboardDifficulty from "./leaderboard-difficulty";
import { Prop } from "@typegoose/typegoose";
export default class Leaderboard {
/**
* The id of the leaderboard.
* @private
*/
@Prop({ required: true })
private readonly _id?: number;
/**
* The hash of the song this leaderboard is for.
* @private
*/
@Prop({ required: true })
readonly songHash!: string;
/**
* The name of the song this leaderboard is for.
* @private
*/
@Prop({ required: true })
readonly songName!: string;
/**
* The sub name of the leaderboard.
* @private
*/
@Prop({ required: true })
readonly songSubName!: string;
/**
* The author of the song this leaderboard is for.
* @private
*/
@Prop({ required: true })
readonly songAuthorName!: string;
/**
* The author of the level this leaderboard is for.
* @private
*/
@Prop({ required: true })
readonly levelAuthorName!: string;
/**
* The difficulty of the leaderboard.
* @private
*/
@Prop({ required: true, _id: false, type: () => LeaderboardDifficulty })
readonly difficulty!: LeaderboardDifficulty;
/**
* The difficulties of the leaderboard.
* @private
*/
@Prop({ required: true, _id: false, type: () => [LeaderboardDifficulty] })
readonly difficulties!: LeaderboardDifficulty[];
/**
* The maximum score of the leaderboard.
* @private
*/
@Prop({ required: true })
readonly maxScore!: number;
/**
* Whether the leaderboard is ranked.
* @private
*/
@Prop({ required: true })
readonly ranked!: boolean;
/**
* The link to the song art.
* @private
*/
@Prop({ required: true })
readonly songArt!: string;
/**
* The date the leaderboard was created.
* @private
*/
@Prop({ required: true })
readonly timestamp!: Date;
/**
* The date the leaderboard was last refreshed.
* @private
*/
@Prop({ required: true })
lastRefreshed?: Date;
get id(): number {
return this._id ?? 0;
}
}

View File

@ -14,12 +14,24 @@ export class Player {
@prop()
public _id!: string;
/**
* The player's name.
*/
@prop()
public name?: string;
/**
* The player's statistic history.
*/
@prop()
private statisticHistory?: Record<string, PlayerHistory>;
/**
* Whether the player has their scores seeded.
*/
@prop()
public seededScores?: boolean;
/**
* The date the player was last tracked.
*/

View File

@ -1,8 +1,9 @@
import { getModelForClass, modelOptions, plugin, Prop, ReturnModelType, Severity } from "@typegoose/typegoose";
import { getModelForClass, index, modelOptions, plugin, Prop, ReturnModelType, Severity } from "@typegoose/typegoose";
import Score from "../score";
import { type ScoreSaberLeaderboardPlayerInfoToken } from "../../../types/token/scoresaber/score-saber-leaderboard-player-info-token";
import { Document } from "mongoose";
import { AutoIncrementID } from "@typegoose/auto-increment";
import { PreviousScore } from "../previous-score";
@modelOptions({
options: { allowMixed: Severity.ALLOW },
@ -19,6 +20,7 @@ import { AutoIncrementID } from "@typegoose/auto-increment";
},
},
})
@index({ leaderboardId: 1, playerId: 1, timestamp: -1 }) // Compound index for optimized queries
@plugin(AutoIncrementID, {
field: "_id",
startAt: 1,
@ -43,8 +45,8 @@ export class ScoreSaberScoreInternal extends Score {
* The amount of pp for the score.
* @private
*/
@Prop({ required: true })
public readonly pp!: number;
@Prop({ required: true, index: true })
public pp!: number;
/**
* The weight of the score, or undefined if not ranked.
@ -58,6 +60,11 @@ export class ScoreSaberScoreInternal extends Score {
*/
@Prop({ required: true })
public readonly maxCombo!: number;
/**
* The previous score, if any.
*/
public previousScore?: ScoreSaberPreviousScore;
}
class ScoreSaberScorePublic extends ScoreSaberScoreInternal {
@ -67,6 +74,28 @@ class ScoreSaberScorePublic extends ScoreSaberScoreInternal {
public playerInfo!: ScoreSaberLeaderboardPlayerInfoToken;
}
export type ScoreSaberPreviousScore = PreviousScore & {
/**
* The pp of the previous score.
*/
pp: number;
/**
* The weight of the previous score.
*/
weight: number;
/**
* The max combo of the previous score.
*/
maxCombo: number;
/**
* The change between the previous score and the current score.
*/
change?: ScoreSaberPreviousScore;
};
export type ScoreSaberScore = InstanceType<typeof ScoreSaberScorePublic>;
export type ScoreSaberScoreDocument = ScoreSaberScore & Document;
export const ScoreSaberScoreModel: ReturnModelType<typeof ScoreSaberScoreInternal> =

View File

@ -0,0 +1,38 @@
import { Modifier } from "../../score/modifier";
export type PreviousScore = {
/**
* The score of the previous score.
*/
score: number;
/**
* The accuracy of the previous score.
*/
accuracy: number;
/**
* The modifiers of the previous score.
*/
modifiers?: Modifier[];
/**
* The misses of the previous score.
*/
misses: number;
/**
* The missed notes of the previous score.
*/
missedNotes: number;
/**
* The bad cuts of the previous score.
*/
badCuts: number;
/**
* The full combo of the previous score.
*/
fullCombo?: boolean;
};

View File

@ -12,7 +12,7 @@ export default class Score {
* The internal score id.
*/
@prop()
private _id?: number;
public _id?: number;
/**
* The id of the player who set the score.
@ -95,7 +95,7 @@ export default class Score {
* The time the score was set.
* @private
*/
@prop({ required: true })
@prop({ required: true, index: true })
public readonly timestamp!: Date;
}

View File

@ -1,28 +1,15 @@
import { NotFoundError } from "backend/src/error/not-found-error";
import { Metadata } from "./types/metadata";
import { NotFoundError } from "./error/not-found-error";
type FetchItemsFunction<T> = (fetchItems: FetchItems) => Promise<T[]>;
export class Pagination<T> {
/**
* The amount of items per page.
* @private
*/
private itemsPerPage: number = 0;
/**
* The amount of items in total.
* @private
*/
private totalItems: number = 0;
/**
* The items to paginate.
* @private
*/
private items: T[] = [];
private items: T[] | null = null; // Optional array to hold set items
/**
* Sets the number of items per page.
*
* @param itemsPerPage - The number of items per page.
* @returns the pagination
*/
@ -33,8 +20,7 @@ export class Pagination<T> {
/**
* Sets the items to paginate.
*
* @param items the items to paginate
* @param items - The items to paginate.
* @returns the pagination
*/
setItems(items: T[]): Pagination<T> {
@ -44,25 +30,59 @@ export class Pagination<T> {
}
/**
* Gets a page of items.
*
* @param page the page number to retrieve.
* @returns the page of items.
* Sets the total number of items.
* @param totalItems - Total number of items.
* @returns the pagination
*/
setTotalItems(totalItems: number): Pagination<T> {
this.totalItems = totalItems;
return this;
}
/**
* Gets a page of items, using either static items or a dynamic fetchItems callback.
* @param page - The page number to retrieve.
* @param fetchItems - The async function to fetch items if setItems was not used.
* @returns A promise resolving to the page of items.
* @throws throws an error if the page number is invalid.
*/
getPage(page: number): Page<T> {
async getPage(page: number, fetchItems?: FetchItemsFunction<T>): Promise<Page<T>> {
const totalPages = Math.ceil(this.totalItems / this.itemsPerPage);
if (page < 1 || page > totalPages) {
throw new NotFoundError("Invalid page number");
}
const items = this.items.slice((page - 1) * this.itemsPerPage, page * this.itemsPerPage);
return new Page<T>(items, new Metadata(totalPages, this.totalItems, page, this.itemsPerPage));
// Calculate the range of items to fetch for the current page
const start = (page - 1) * this.itemsPerPage;
const end = start + this.itemsPerPage;
let pageItems: T[];
// Use set items if they are present, otherwise use fetchItems callback
if (this.items) {
pageItems = this.items.slice(start, end);
} else if (fetchItems) {
pageItems = await fetchItems(new FetchItems(start, end));
} else {
throw new Error("Items function is not set and no fetchItems callback provided");
}
return new Page<T>(pageItems, new Metadata(totalPages, this.totalItems, page, this.itemsPerPage));
}
}
class Page<T> {
class FetchItems {
readonly start: number;
readonly end: number;
constructor(start: number, end: number) {
this.start = start;
this.end = end;
}
}
export class Page<T> {
readonly items: T[];
readonly metadata: Metadata;
@ -70,4 +90,14 @@ class Page<T> {
this.items = items;
this.metadata = metadata;
}
/**
* Converts the page to a JSON object.
*/
toJSON() {
return {
items: this.items,
metadata: this.metadata,
};
}
}

View File

@ -0,0 +1,10 @@
import { ScoreSaberLeaderboard } from "src/model/leaderboard/impl/scoresaber-leaderboard";
import { ScoreSaberScore } from "../model/score/impl/scoresaber-score";
import { PlayerScore } from "../score/player-score";
export type TopScoresResponse = {
/**
* The top scores.
*/
scores: PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>[];
};

View File

@ -7,7 +7,7 @@ import ScoreSaberPlayerScoresPageToken from "../../types/token/scoresaber/score-
import ScoreSaberLeaderboardToken from "../../types/token/scoresaber/score-saber-leaderboard-token";
import ScoreSaberLeaderboardScoresPageToken from "../../types/token/scoresaber/score-saber-leaderboard-scores-page-token";
import { clamp, lerp } from "../../utils/math-utils";
import { CurvePoint } from "../../utils/curve-point";
import { CurvePoint } from "../../curve-point";
import { SSRCache } from "../../cache";
const API_BASE = "https://scoresaber.com/api";
@ -30,7 +30,7 @@ const LOOKUP_LEADERBOARD_SCORES_ENDPOINT = `${API_BASE}/leaderboard/by-id/:id/sc
const STAR_MULTIPLIER = 42.117208413;
const playerCache = new SSRCache({
ttl: 60 * 30, // 30 minutes
ttl: 60, // 1 minute
});
class ScoreSaberService extends Service {
@ -167,22 +167,22 @@ class ScoreSaberService extends Service {
*
* @param playerId the ID of the player to look up
* @param sort the sort to use
* @param limit the amount of sores to fetch
* @param page the page to get scores for
* @param limit the amount of scores to fetch
* @param search
* @param search the query to search for
* @returns the scores of the player, or undefined
*/
public async lookupPlayerScores({
playerId,
sort,
page,
limit = 8,
page,
search,
}: {
playerId: string;
sort: ScoreSort;
page: number;
limit?: number;
page: number;
search?: string;
useProxy?: boolean;
}): Promise<ScoreSaberPlayerScoresPageToken | undefined> {

View File

@ -1,69 +0,0 @@
import { ScoreSorter } from "../score-sorter";
import { ScoreSaberScore } from "../../model/score/impl/scoresaber-score";
import { ScoreSortType } from "../sort-type";
import { SortDirection } from "../sort-direction";
export class ScoreSaberScoreSorter extends ScoreSorter<ScoreSaberScore> {
sort(type: ScoreSortType, direction: SortDirection, items: ScoreSaberScore[]): ScoreSaberScore[] {
switch (type) {
case ScoreSortType.date:
return this.sortRecent(direction, items);
case ScoreSortType.pp:
return this.sortPp(direction, items);
case ScoreSortType.accuracy:
return this.sortAccuracy(direction, items);
case ScoreSortType.misses:
return this.sortMisses(direction, items);
default:
return items;
}
}
/**
* Sorts the scores by the time they were set.
*
* @param direction the direction to sort the scores
* @param items the scores to sort
* @returns the sorted scores
*/
sortRecent(direction: SortDirection, items: ScoreSaberScore[]): ScoreSaberScore[] {
return items.sort((a, b) =>
direction === SortDirection.ASC
? a.timestamp.getTime() - b.timestamp.getTime()
: b.timestamp.getTime() - a.timestamp.getTime()
);
}
/**
* Sorts the scores by their pp value
*
* @param direction the direction to sort the scores
* @param items the scores to sort
* @returns the sorted scores
*/
sortPp(direction: SortDirection, items: ScoreSaberScore[]): ScoreSaberScore[] {
return items.sort((a, b) => (direction === SortDirection.ASC ? a.pp - b.pp : b.pp - a.pp));
}
/**
* Sorts the scores by their accuracy value
*
* @param direction the direction to sort the scores
* @param items the scores to sort
* @returns the sorted scores
*/
sortAccuracy(direction: SortDirection, items: ScoreSaberScore[]): ScoreSaberScore[] {
return items.sort((a, b) => (direction === SortDirection.ASC ? a.accuracy - b.accuracy : b.accuracy - a.accuracy));
}
/**
* Sorts the scores by their misses
*
* @param direction the direction to sort the scores
* @param items the scores to sort
* @returns the sorted scores
*/
sortMisses(direction: SortDirection, items: ScoreSaberScore[]): ScoreSaberScore[] {
return items.sort((a, b) => (direction === SortDirection.ASC ? a.misses - b.misses : b.misses - a.misses));
}
}

View File

@ -1,14 +0,0 @@
import { ScoreSortType } from "./sort-type";
import { SortDirection } from "./sort-direction";
export abstract class ScoreSorter<T> {
/**
* Sorts the items
*
* @param type the type of sort
* @param direction the direction of the sort
* @param items the items to sort
* @returns the sorted items
*/
public abstract sort(type: ScoreSortType, direction: SortDirection, items: T[]): T[];
}

View File

@ -1,4 +0,0 @@
export enum SortDirection {
ASC = "asc",
DESC = "desc",
}

View File

@ -1,6 +0,0 @@
export enum ScoreSortType {
date = "date",
pp = "pp",
accuracy = "accuracy",
misses = "misses",
}

View File

@ -1,5 +0,0 @@
import { ScoreSaberScoreSorter } from "./impl/scoresaber-sorter";
export const ScoreSorters = {
scoreSaber: new ScoreSaberScoreSorter(),
};

View File

@ -1,9 +1,9 @@
import ScoreSaberLeaderboard from "./leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "./model/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboardToken from "./types/token/scoresaber/score-saber-leaderboard-token";
import LeaderboardDifficulty from "./leaderboard/leaderboard-difficulty";
import LeaderboardDifficulty from "./model/leaderboard/leaderboard-difficulty";
import { getDifficultyFromScoreSaberDifficulty } from "./utils/scoresaber-utils";
import { MapCharacteristic } from "./types/map-characteristic";
import { LeaderboardStatus } from "./leaderboard/leaderboard-status";
import { LeaderboardStatus } from "./model/leaderboard/leaderboard-status";
import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate, parseDate } from "./utils/time-utils";
import ScoreSaberPlayerToken from "./types/token/scoresaber/score-saber-player-token";
import ScoreSaberPlayer, { ScoreSaberBadge, ScoreSaberBio } from "./player/impl/scoresaber-player";
@ -97,7 +97,7 @@ export function getScoreSaberScoreFromToken(
difficulty: leaderboard.difficulty.difficulty,
characteristic: leaderboard.difficulty.characteristic,
score: token.baseScore,
accuracy: (token.baseScore / leaderboard.maxScore) * 100,
accuracy: leaderboard.maxScore ? (token.baseScore / leaderboard.maxScore) * 100 : Infinity,
rank: token.rank,
modifiers: modifiers,
misses: token.missedNotes + token.badCuts,

View File

@ -18,4 +18,9 @@ export type AppStatistics = {
* The amount of cached BeatSaver maps.
*/
cachedBeatSaverMaps: number;
/**
* The amount of cached ScoreSaber leaderboards.
*/
cachedScoreSaberLeaderboards: number;
};

View File

@ -1,8 +1,8 @@
export type ScoreSaberLeaderboardPlayerInfoToken = {
id: string;
name: string;
profilePicture: string;
country: string;
permissions: number;
role: string;
name?: string;
profilePicture?: string;
country?: string;
permissions?: number;
role?: string;
};

View File

@ -1,6 +1,6 @@
import { isServer } from "./utils";
export type CookieName = "playerId" | "lastScoreSort" | "lastScoreSortDirection";
export type CookieName = "playerId" | "lastScoreSort";
/**
* Gets the value of a cookie

View File

@ -4,8 +4,23 @@ import PlayerScoresResponse from "../response/player-scores-response";
import { Config } from "../config";
import { ScoreSort } from "../score/score-sort";
import LeaderboardScoresResponse from "../response/leaderboard-scores-response";
import { ScoreSortType } from "../sorter/sort-type";
import { SortDirection } from "../sorter/sort-direction";
import { Page } from "../pagination";
import { ScoreSaberScore } from "src/model/score/impl/scoresaber-score";
import { PlayerScore } from "../score/player-score";
import ScoreSaberLeaderboard from "../model/leaderboard/impl/scoresaber-leaderboard";
/**
* Fetches the player's scores
*
* @param playerId the id of the player
* @param leaderboardId the id of the leaderboard
* @param page the page
*/
export async function fetchPlayerScoresHistory(playerId: string, leaderboardId: string, page: number) {
return kyFetch<Page<PlayerScore<ScoreSaberScore, ScoreSaberLeaderboard>>>(
`${Config.apiUrl}/scores/history/${playerId}/${leaderboardId}/${page}`
);
}
/**
* Fetches the player's scores
@ -14,19 +29,17 @@ import { SortDirection } from "../sorter/sort-direction";
* @param id the player id
* @param page the page
* @param sort the sort
* @param direction the direction to sort
* @param search the search
*/
export async function fetchPlayerScores<S, L>(
leaderboard: Leaderboards,
id: string,
page: number,
sort: ScoreSortType,
direction: SortDirection,
sort: ScoreSort,
search?: string
) {
return kyFetch<PlayerScoresResponse<S, L>>(
`${Config.apiUrl}/scores/player/${leaderboard}/${id}/${page}/${sort}/${direction}${search ? `?search=${search}` : ""}`
`${Config.apiUrl}/scores/player/${leaderboard}/${id}/${page}/${sort}${search ? `?search=${search}` : ""}`
);
}

View File

@ -0,0 +1,14 @@
/**
* Generates a random string
*
* @param length the length of the string
* @returns the random string
*/
export function randomString(length: number) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}

View File

@ -43,7 +43,6 @@ export async function kyFetch<T>(url: string): Promise<T | undefined> {
try {
return await ky.get<T>(url).json();
} catch (error) {
console.error(`Error fetching data from ${url}:`, error);
return undefined;
}
}

View File

@ -1,12 +1,14 @@
import { withSentryConfig } from "@sentry/nextjs";
import { format } from "@formkit/tempo";
import type { NextConfig } from "next";
/** @type {import('next').NextConfig} */
const nextConfig = {
const nextConfig: NextConfig = {
experimental: {
webpackMemoryOptimizations: true,
optimizePackageImports: ["@ssr/common", "@radix-ui/react-icons", "chart.js", "react-chartjs-2"],
},
images: {
unoptimized: true,
remotePatterns: [
{
protocol: "https",

View File

@ -35,11 +35,11 @@
"framer-motion": "^11.5.4",
"js-cookie": "^3.0.5",
"ky": "^1.7.2",
"lucide-react": "^0.453.0",
"next": "^15.0.0",
"lucide-react": "^0.454.0",
"next": "^15.0.1",
"next-build-id": "^3.0.0",
"next-themes": "^0.3.0",
"react": "18.3.1",
"react": "^18.3.1",
"react-chartjs-2": "^5.2.0",
"react-countup": "^6.5.3",
"react-dom": "18.3.1",

View File

@ -6,7 +6,7 @@ import { LeaderboardData } from "@/components/leaderboard/leaderboard-data";
import { Config } from "@ssr/common/config";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import { LeaderboardResponse } from "@ssr/common/response/leaderboard-response";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { fetchLeaderboard } from "@ssr/common/utils/leaderboard.util";
import { fetchLeaderboardScores } from "@ssr/common/utils/score-utils";
import LeaderboardScoresResponse from "@ssr/common/response/leaderboard-scores-response";

View File

@ -1,9 +1,9 @@
import { Button } from "@/components/ui/button";
import Link from "next/link";
import { AppStatistics } from "@ssr/common/types/backend/app-statistics";
import Statistic from "@/components/home/statistic";
import { kyFetch } from "@ssr/common/utils/utils";
import { Config } from "@ssr/common/config";
import { AppStats } from "@/components/app-statistics";
export const dynamic = "force-dynamic"; // Always generate the page on load
@ -21,15 +21,7 @@ export default async function HomePage() {
<p>ScoreSaber Reloaded is a website that allows you to track your ScoreSaber data over time.</p>
</div>
{statistics && (
<div className="flex items-center flex-col">
<p className="font-semibold">Site Statistics</p>
<Statistic title="Tracked Players" value={statistics.trackedPlayers} />
<Statistic title="Tracked Scores" value={statistics.trackedScores} />
<Statistic title="Additional Scores Data" value={statistics.additionalScoresData} />
<Statistic title="Cached BeatSaver Maps" value={statistics.cachedBeatSaverMaps} />
</div>
)}
{statistics && <AppStats initialStatistics={statistics} />}
<div className="flex gap-2 flex-wrap">
<Link href="/search">

View File

@ -7,14 +7,14 @@ import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { getCookieValue } from "@ssr/common/utils/cookie-utils";
import { Config } from "@ssr/common/config";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
import { ScoreSort } from "@ssr/common/score/score-sort";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { fetchPlayerScores } from "@ssr/common/utils/score-utils";
import PlayerScoresResponse from "@ssr/common/response/player-scores-response";
import { SSRCache } from "@ssr/common/cache";
import { getScoreSaberPlayerFromToken } from "@ssr/common/token-creators";
import { ScoreSortType } from "@ssr/common/sorter/sort-type";
import { SortDirection } from "@ssr/common/sorter/sort-direction";
import { cache } from "react";
import { randomString } from "@ssr/common/utils/string.util";
const UNKNOWN_PLAYER = {
title: "ScoreSaber Reloaded - Unknown Player",
@ -33,14 +33,14 @@ type Props = {
type PlayerData = {
player: ScoreSaberPlayer | undefined;
scores: PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard> | undefined;
sort: ScoreSortType;
direction: SortDirection;
sort: ScoreSort;
page: number;
search: string;
};
const playerCache = new SSRCache({
ttl: 1000 * 60, // 1 minute
const getPlayer = cache(async (id: string): Promise<ScoreSaberPlayer | undefined> => {
const playerToken = await scoresaberService.lookupPlayer(id, true);
return playerToken && (await getScoreSaberPlayerFromToken(playerToken, await getCookieValue("playerId")));
});
/**
@ -50,45 +50,27 @@ const playerCache = new SSRCache({
* @param fetchScores whether to fetch the scores
* @returns the player data and scores
*/
const getPlayerData = async ({ params }: Props, fetchScores: boolean = true): Promise<PlayerData> => {
const getPlayerData = cache(async ({ params }: Props, fetchScores: boolean = true): Promise<PlayerData> => {
const { slug } = await params;
const id = slug[0]; // The players id
const sort: ScoreSortType = (slug[1] as ScoreSortType) || (await getCookieValue("lastScoreSort", ScoreSortType.date)); // The sorting method
const direction: SortDirection =
(slug[2] as SortDirection) || (await getCookieValue("lastScoreSortDirection", SortDirection.DESC)); // The sorting direction
const page = parseInt(slug[3]) || 1; // The page number
const search = (slug[4] as string) || ""; // The search query
const sort: ScoreSort = (slug[1] as ScoreSort) || (await getCookieValue("lastScoreSort", "recent")); // The sorting method
const page = parseInt(slug[2]) || 1; // The page number
const search = (slug[3] as string) || ""; // The search query
const cacheId = `${id}-${sort}-${page}-${search}-${fetchScores}`;
if (playerCache.has(cacheId)) {
return playerCache.get(cacheId) as PlayerData;
}
const playerToken = await scoresaberService.lookupPlayer(id);
const player = playerToken && (await getScoreSaberPlayerFromToken(playerToken, await getCookieValue("playerId")));
const player = await getPlayer(id);
let scores: PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard> | undefined;
if (fetchScores) {
scores = await fetchPlayerScores<ScoreSaberScore, ScoreSaberLeaderboard>(
"scoresaber",
id,
page,
sort,
direction,
search
);
if (fetchScores && player !== undefined) {
scores = await fetchPlayerScores<ScoreSaberScore, ScoreSaberLeaderboard>("scoresaber", id, page, sort, search);
}
const playerData = {
return {
sort: sort,
direction: direction,
page: page,
search: search,
player: player,
scores: scores,
};
playerCache.set(cacheId, playerData);
return playerData;
};
});
export async function generateMetadata(props: Props): Promise<Metadata> {
const { player } = await getPlayerData(props, false);
@ -110,7 +92,7 @@ export async function generateMetadata(props: Props): Promise<Metadata> {
description: `Click here to view the scores for ${player.name}!`,
images: [
{
url: `${Config.apiUrl}/image/player/${player.id}`,
url: `${Config.apiUrl}/image/player/${player.id}?id=${randomString(8)}`,
},
],
},
@ -135,21 +117,14 @@ export async function generateViewport(props: Props): Promise<Viewport> {
}
export default async function PlayerPage(props: Props) {
const { player, scores, sort, direction, page, search } = await getPlayerData(props);
const { player, scores, sort, page, search } = await getPlayerData(props);
if (player == undefined) {
return redirect("/");
}
return (
<div className="flex flex-col h-full w-full">
<PlayerData
initialPlayerData={player}
initialScoreData={scores}
initialSearch={search}
sort={sort}
direction={direction}
page={page}
/>
<PlayerData initialPlayerData={player} initialScoreData={scores} initialSearch={search} sort={sort} page={page} />
</div>
);
}

View File

@ -1,11 +1,11 @@
import { Metadata } from "next";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import NodeCache from "node-cache";
import { ScoreSaberPlayersPageToken } from "@ssr/common/types/token/scoresaber/score-saber-players-page-token";
import Card from "@/components/card";
import RankingData from "@/components/ranking/ranking-data";
import CountryFlag from "@/components/country-flag";
import { normalizedRegionName } from "@ssr/common/utils/region-utils";
import { cache } from "react";
const UNKNOWN_PAGE = {
title: "ScoreSaber Reloaded - Unknown Page",
@ -24,36 +24,27 @@ type RankingPageData = {
country: string | undefined;
};
const rankingCache = new NodeCache({ stdTTL: 60, checkperiod: 120 });
/**
* Gets the ranking data.
*
* @param params the params
* @returns the ranking data
*/
const getRankingData = async ({ params }: Props): Promise<RankingPageData> => {
const getRankingData = cache(async ({ params }: Props): Promise<RankingPageData> => {
const { slug } = await params;
const country = (slug && slug.length > 1 && (slug[0] as string).toUpperCase()) || undefined; // The country query
const page = (slug && parseInt(slug[country != undefined ? 1 : 0])) || 1; // The page number
const cacheId = `${country === undefined ? "global" : country}-${page}`;
if (rankingCache.has(cacheId)) {
return rankingCache.get(cacheId) as RankingPageData;
}
const players =
country == undefined
? await scoresaberService.lookupPlayers(page)
: await scoresaberService.lookupPlayersByCountry(page, country);
const rankingData = {
return {
players: players && players.players.length > 0 ? players : undefined,
page,
country,
};
rankingCache.set(cacheId, rankingData);
return rankingData;
};
});
export async function generateMetadata(props: Props): Promise<Metadata> {
const { players, page, country } = await getRankingData(props);

View File

@ -4,6 +4,10 @@ import Card from "@/components/card";
export const metadata: Metadata = {
title: "Score Feed",
openGraph: {
title: "ScoreSaber Reloaded - Live Scores",
description: "View the live scores set by players on ScoreSaber.",
},
};
export default function ScoresPage() {

View File

@ -0,0 +1,59 @@
import { Metadata } from "next";
import Card from "@/components/card";
import { kyFetch } from "@ssr/common/utils/utils";
import { Config } from "@ssr/common/config";
import { TopScoresResponse } from "@ssr/common/response/top-scores-response";
import Score from "@/components/score/score";
import Link from "next/link";
export const metadata: Metadata = {
title: "Top Scores",
openGraph: {
title: "ScoreSaber Reloaded - Top Scores",
description: "View the top 100 scores set by players on ScoreSaber.",
},
};
export default async function TopScoresPage() {
const scores = await kyFetch<TopScoresResponse>(`${Config.apiUrl}/scores/top`);
return (
<Card className="flex flex-col gap-2 w-full xl:w-[75%]">
<div>
<p className="font-semibold'">Top 100 ScoreSaber Scores</p>
<p className="text-gray-400">This will only show scores that have been tracked.</p>
</div>
{!scores ? (
<p>No scores found</p>
) : (
<div className="flex flex-col gap-2 divide-y divide-border">
{scores.scores.map(({ score, leaderboard, beatSaver }, index) => {
const player = score.playerInfo;
const name = score.playerInfo ? player.name || player.id : score.playerId;
return (
<div key={index} className="flex flex-col pt-2">
<p className="text-sm">
Set by{" "}
<Link href={`/player/${player.id}`}>
<span className="text-ssr hover:brightness-[66%] transition-all transform-gpu">{name}</span>
</Link>
</p>
<Score
score={score}
leaderboard={leaderboard}
beatSaverMap={beatSaver}
settings={{
hideLeaderboardDropdown: true,
hideAccuracyChanger: true,
}}
/>
</div>
);
})}
</div>
)}
</Card>
);
}

View File

@ -5,9 +5,14 @@ import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-sabe
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { setCookieValue } from "@ssr/common/utils/cookie-utils";
import { trackPlayer } from "@ssr/common/utils/player-utils";
import { SSRCache } from "@ssr/common/cache";
const SETTINGS_ID = "SSR"; // DO NOT CHANGE
const playerCache = new SSRCache({
ttl: 60 * 30, // 30 minutes
});
export default class Database extends Dexie {
/**
* The settings for the website.
@ -79,7 +84,15 @@ export default class Database extends Dexie {
if (settings == undefined || settings.playerId == undefined) {
return;
}
return scoresaberService.lookupPlayer(settings.playerId, true);
if (playerCache.has(settings.playerId)) {
return playerCache.get(settings.playerId);
}
const player = scoresaberService.lookupPlayer(settings.playerId);
if (player == undefined) {
return undefined;
}
playerCache.set(settings.playerId, player);
return player;
}
/**
@ -119,10 +132,15 @@ export default class Database extends Dexie {
const friends = await this.friends.toArray();
const players = await Promise.all(
friends.map(async ({ id }) => {
const token = await scoresaberService.lookupPlayer(id, true);
if (playerCache.has(id)) {
return playerCache.get(id);
}
const token = await scoresaberService.lookupPlayer(id);
if (token == undefined) {
return undefined;
}
playerCache.set(id, token);
await trackPlayer(id); // Track the player
return token;
})

View File

@ -0,0 +1,41 @@
"use client";
import Statistic from "@/components/home/statistic";
import { AppStatistics } from "@ssr/common/types/backend/app-statistics";
import { useQuery } from "@tanstack/react-query";
import { kyFetch } from "@ssr/common/utils/utils";
import { Config } from "@ssr/common/config";
import { useEffect, useState } from "react";
type AppStatisticsProps = {
/**
* The app statistics.
*/
initialStatistics: AppStatistics;
};
export function AppStats({ initialStatistics }: AppStatisticsProps) {
const [statistics, setStatistics] = useState(initialStatistics);
const { data } = useQuery({
queryKey: ["app-statistics"],
queryFn: () => kyFetch<AppStatistics>(Config.apiUrl + "/statistics"),
});
useEffect(() => {
if (data) {
setStatistics(data);
}
}, [data]);
return (
<div className="flex items-center flex-col">
<p className="font-semibold">Site Statistics</p>
<Statistic title="Tracked Players" value={statistics.trackedPlayers} />
<Statistic title="Tracked Scores" value={statistics.trackedScores} />
<Statistic title="Additional Scores Data" value={statistics.additionalScoresData} />
<Statistic title="Cached BeatSaver Maps" value={statistics.cachedBeatSaverMaps} />
<Statistic title="Cached ScoreSaber Leaderboards" value={statistics.cachedScoreSaberLeaderboards} />
</div>
);
}

View File

@ -37,6 +37,16 @@ const items: NavbarItem[] = [
link: "/swagger",
openInNewTab: true,
},
{
name: "Score Feed",
link: "/scores/live",
openInNewTab: false,
},
{
name: "Top Scores",
link: "/scores/top",
openInNewTab: false,
},
];
export default function Footer() {

View File

@ -6,7 +6,7 @@ import Card from "@/components/card";
import { DualRangeSlider } from "@/components/ui/dual-range-slider";
import { useDebounce } from "@uidotdev/usehooks";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
type Props = {
/**

View File

@ -3,28 +3,35 @@
import { ScoreStatsToken } from "@ssr/common/types/token/beatleader/score-stats/score-stats";
import { formatTime } from "@ssr/common/utils/time-utils";
import GenericChart, { DatasetConfig } from "@/components/chart/generic-chart";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { ScoreSaberLeaderboard } from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
type Props = {
/**
* The score stats to use in the chart
* The score stats to use in the chart.
*/
scoreStats: ScoreStatsToken;
/**
* The leaderboard to the score was set on.
*/
leaderboard: ScoreSaberLeaderboard;
};
export default function PlayerScoreAccuracyChart({ scoreStats }: Props) {
export default function PlayerScoreAccuracyChart({ scoreStats, leaderboard }: Props) {
const graph = scoreStats.scoreGraphTracker.graph;
const histories: Record<string, (number | null)[]> = {};
const histories: Record<string, (number | null)[]> = {
accuracy: [],
pp: [],
};
const labels: string[] = [];
for (let seconds = 0; seconds < graph.length; seconds++) {
labels.push(formatTime(seconds));
const history = histories["accuracy"];
if (!history) {
histories["accuracy"] = [];
}
histories["accuracy"].push(graph[seconds] * 100);
const acc = graph[seconds] * 100;
histories["accuracy"].push(acc);
histories["pp"].push(scoresaberService.getPp(leaderboard.stars, acc));
}
const datasetConfig: DatasetConfig[] = [
@ -39,7 +46,20 @@ export default function PlayerScoreAccuracyChart({ scoreStats }: Props) {
displayName: "Accuracy",
position: "left",
},
labelFormatter: (value: number) => `${value.toFixed(2)}%`,
labelFormatter: (value: number) => `Accuracy: ${value.toFixed(2)}%`,
},
{
title: "PP",
field: "pp",
color: "#4858ff",
axisId: "y1",
axisConfig: {
reverse: false,
display: true,
displayName: "PP",
position: "right",
},
labelFormatter: (value: number) => `PP: ${value.toFixed(2)}pp`,
},
];

View File

@ -3,7 +3,7 @@
import LeaderboardScores from "@/components/leaderboard/leaderboard-scores";
import { LeaderboardInfo } from "@/components/leaderboard/leaderboard-info";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { LeaderboardResponse } from "@ssr/common/response/leaderboard-response";
import { useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";

View File

@ -2,7 +2,7 @@ import Card from "@/components/card";
import Image from "next/image";
import { LeaderboardSongStarCount } from "@/components/leaderboard/leaderboard-song-star-count";
import ScoreButtons from "@/components/score/score-buttons";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import { getBeatSaverMapperProfileUrl } from "@ssr/common/utils/beatsaver.util";
import FallbackLink from "@/components/fallback-link";

View File

@ -1,13 +1,13 @@
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import { formatNumberWithCommas, formatPp } from "@ssr/common/utils/number-utils";
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import { PlayerInfo } from "@/components/player/player-info";
import { clsx } from "clsx";
import Tooltip from "@/components/tooltip";
import { ScoreTimeSet } from "@/components/score/score-time-set";
import { ScoreModifiers } from "@/components/score/score-modifiers";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreMissesBadge from "@/components/score/badges/score-misses";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import ScoreMissesAndPausesBadge from "@/components/score/badges/score-misses-and-pauses";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
type Props = {
/**
@ -23,10 +23,10 @@ type Props = {
/**
* The claimed player.
*/
claimedPlayer?: ScoreSaberPlayerToken;
highlightedPlayer?: ScoreSaberPlayer;
};
export default function LeaderboardScore({ score, leaderboard, claimedPlayer }: Props) {
export default function LeaderboardScore({ score, leaderboard, highlightedPlayer }: Props) {
const scorePlayer = score.playerInfo;
return (
@ -36,7 +36,7 @@ export default function LeaderboardScore({ score, leaderboard, claimedPlayer }:
{/* Player */}
<td className="px-4 py-2 flex gap-2 whitespace-nowrap">
<PlayerInfo player={scorePlayer} highlightedPlayer={claimedPlayer} />
<PlayerInfo player={scorePlayer} highlightedPlayer={highlightedPlayer} useLink />
</td>
{/* Time Set */}
@ -57,7 +57,7 @@ export default function LeaderboardScore({ score, leaderboard, claimedPlayer }:
score.misses > 0 ? "text-red-500" : "text-green-500"
)}
>
<ScoreMissesBadge score={score} hideXMark />
<ScoreMissesAndPausesBadge score={score} hideXMark />
</td>
{/* Score PP */}

View File

@ -11,11 +11,10 @@ import { Button } from "@/components/ui/button";
import { getDifficulty } from "@/common/song-utils";
import { fetchLeaderboardScores } from "@ssr/common/utils/score-utils";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import LeaderboardScoresResponse from "@ssr/common/response/leaderboard-scores-response";
import useDatabase from "@/hooks/use-database";
import { useLiveQuery } from "dexie-react-hooks";
import LeaderboardScoresSkeleton from "@/components/leaderboard/skeleton/leaderboard-scores-skeleton";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
type LeaderboardScoresProps = {
initialPage?: number;
@ -25,6 +24,7 @@ type LeaderboardScoresProps = {
isLeaderboardPage?: boolean;
leaderboardChanged?: (id: number) => void;
disableUrlChanging?: boolean;
highlightedPlayer?: ScoreSaberPlayer;
};
export default function LeaderboardScores({
@ -35,12 +35,11 @@ export default function LeaderboardScores({
isLeaderboardPage,
leaderboardChanged,
disableUrlChanging,
highlightedPlayer,
}: LeaderboardScoresProps) {
if (!initialPage) {
initialPage = 1;
}
const database = useDatabase();
const claimedPlayer = useLiveQuery(() => database.getClaimedPlayer());
const { width } = useWindowDimensions();
const controls = useAnimation();
@ -65,6 +64,8 @@ export default function LeaderboardScores({
enabled: shouldFetch,
});
console.log(leaderboard);
/**
* Starts the animation for the scores, but only after the initial load.
*/
@ -119,6 +120,8 @@ export default function LeaderboardScores({
return;
}
console.log(selectedLeaderboardId);
// Update the URL
window.history.replaceState(null, "", `/leaderboard/${selectedLeaderboardId}/${currentPage}`);
}, [selectedLeaderboardId, currentPage, disableUrlChanging]);
@ -158,7 +161,7 @@ export default function LeaderboardScores({
borderColor: getDifficulty(difficulty).color,
}}
>
{difficulty}
{difficulty.replace("Plus", "+")}
</Button>
);
})}
@ -182,7 +185,7 @@ export default function LeaderboardScores({
<motion.tbody initial="hidden" animate={controls} className="border-none" variants={scoreAnimation}>
{currentScores.scores.map((playerScore, index) => (
<motion.tr key={index} className="border-b border-border" variants={scoreAnimation}>
<LeaderboardScore score={playerScore} leaderboard={leaderboard} claimedPlayer={claimedPlayer} />
<LeaderboardScore score={playerScore} leaderboard={leaderboard} highlightedPlayer={highlightedPlayer} />
</motion.tr>
))}
</motion.tbody>

View File

@ -1,6 +1,6 @@
import { getDifficulty } from "@/common/song-utils";
import { StarIcon } from "@heroicons/react/24/solid";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
type LeaderboardSongStarCountProps = {
/**

View File

@ -22,12 +22,6 @@ const items: NavbarItem[] = [
align: "left",
icon: <TrendingUpIcon className="h-5 w-5" />,
},
{
name: "Score Feed",
link: "/scores",
align: "left",
icon: <PiSwordFill className="h-5 w-5" />,
},
{
name: "Search",
link: "/search",

View File

@ -16,29 +16,19 @@ import { useLiveQuery } from "dexie-react-hooks";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
import { ScoreSort } from "@ssr/common/score/score-sort";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import PlayerScoresResponse from "@ssr/common/response/player-scores-response";
import { getScoreSaberPlayerFromToken } from "@ssr/common/token-creators";
import { ScoreSortType } from "@ssr/common/sorter/sort-type";
import { SortDirection } from "@ssr/common/sorter/sort-direction";
type Props = {
initialPlayerData: ScoreSaberPlayer;
initialScoreData?: PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
initialSearch?: string;
sort: ScoreSortType;
direction: SortDirection;
sort: ScoreSort;
page: number;
};
export default function PlayerData({
initialPlayerData,
initialScoreData,
initialSearch,
sort,
direction,
page,
}: Props) {
export default function PlayerData({ initialPlayerData, initialScoreData, initialSearch, sort, page }: Props) {
const isMobile = useIsMobile();
const miniRankingsRef = useRef<HTMLDivElement>(null);
const isMiniRankingsVisible = useIsVisible(miniRankingsRef);
@ -75,7 +65,6 @@ export default function PlayerData({
initialSearch={initialSearch}
player={player}
sort={sort}
direction={direction}
page={page}
/>
</article>

View File

@ -71,7 +71,7 @@ export function PlayerInfo({
src={`https://img.fascinated.cc/upload/w_128,h_128/${player.profilePicture}`}
/>
</Avatar>
{!hideCountryFlag && <CountryFlag code={player.country} size={12} />}
{!hideCountryFlag && <CountryFlag code={player.country!} size={12} />}
{useLink ? <Link href={`/player/${player.id}`}>{name}</Link> : name}
</div>
);

View File

@ -1,6 +1,6 @@
import { capitalizeFirstLetter } from "@/common/string-utils";
import useWindowDimensions from "@/hooks/use-window-dimensions";
import { ArrowDownIcon, ClockIcon, TrophyIcon, XMarkIcon } from "@heroicons/react/24/solid";
import { ClockIcon, TrophyIcon, XMarkIcon } from "@heroicons/react/24/solid";
import { useQuery } from "@tanstack/react-query";
import { motion, useAnimation } from "framer-motion";
import { useCallback, useEffect, useRef, useState } from "react";
@ -13,55 +13,44 @@ import { clsx } from "clsx";
import { useDebounce } from "@uidotdev/usehooks";
import { scoreAnimation } from "@/components/score/score-animation";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
import { ScoreSort } from "@ssr/common/score/score-sort";
import { setCookieValue } from "@ssr/common/utils/cookie-utils";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { fetchPlayerScores } from "@ssr/common/utils/score-utils";
import PlayerScoresResponse from "@ssr/common/response/player-scores-response";
import { SortDirection } from "@ssr/common/sorter/sort-direction";
import { ScoreSortType } from "@ssr/common/sorter/sort-type";
type Props = {
initialScoreData?: PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
initialSearch?: string;
player: ScoreSaberPlayer;
sort: ScoreSortType;
direction: SortDirection;
sort: ScoreSort;
page: number;
};
type PageState = {
page: number;
sort: ScoreSortType;
direction: SortDirection;
sort: ScoreSort;
};
const scoreSort = [
{
name: "PP",
value: ScoreSortType.pp,
name: "Top",
value: ScoreSort.top,
icon: <TrophyIcon className="w-5 h-5" />,
},
{
name: "Date",
value: ScoreSortType.date,
},
{
name: "Acc",
value: ScoreSortType.accuracy,
requiresTrackedPlayer: true,
},
{
name: "Misses",
value: ScoreSortType.misses,
requiresTrackedPlayer: true,
name: "Recent",
value: ScoreSort.recent,
icon: <ClockIcon className="w-5 h-5" />,
},
];
export default function PlayerScores({ initialScoreData, initialSearch, player, sort, direction, page }: Props) {
export default function PlayerScores({ initialScoreData, initialSearch, player, sort, page }: Props) {
const { width } = useWindowDimensions();
const controls = useAnimation();
const [pageState, setPageState] = useState<PageState>({ page, sort, direction });
const [pageState, setPageState] = useState<PageState>({ page, sort });
const [previousPage, setPreviousPage] = useState(page);
const [scores, setScores] = useState<PlayerScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard> | undefined>(
initialScoreData
@ -80,7 +69,6 @@ export default function PlayerScores({ initialScoreData, initialSearch, player,
player.id,
pageState.page,
pageState.sort,
pageState.direction,
debouncedSearchTerm
),
enabled: shouldFetch && (debouncedSearchTerm.length >= 3 || debouncedSearchTerm.length === 0),
@ -100,30 +88,14 @@ export default function PlayerScores({ initialScoreData, initialSearch, player,
*
* @param newSort the new sort
*/
const handleSortChange = async (newSort: ScoreSortType) => {
const handleSortChange = async (newSort: ScoreSort) => {
if (newSort !== pageState.sort) {
setPageState({ ...pageState, page: 1, sort: newSort, direction: SortDirection.DESC });
setPageState({ page: 1, sort: newSort });
setShouldFetch(true); // Set to true to trigger fetch
await setCookieValue("lastScoreSort", newSort); // Set the default score sort
}
};
/**
* Change the score sort direction.
*
* @param newDirection the new sort direction
*/
const handleSortDirectionChange = async (newDirection: SortDirection) => {
// Player doesn't have scores tracked anyway, so no need to change this.
if (!player.isBeingTracked) {
return;
}
setPageState({ ...pageState, page: 1, direction: newDirection });
setShouldFetch(true); // Set to true to trigger fetch
await setCookieValue("lastScoreSortDirection", newDirection); // Set the default score sort
};
/**
* Change the score search term.
*
@ -159,9 +131,9 @@ export default function PlayerScores({ initialScoreData, initialSearch, player,
*/
const getUrl = useCallback(
(page: number) => {
return `/player/${player.id}/${pageState.sort}/${pageState.direction}/${page}${isSearchActive ? `?search=${debouncedSearchTerm}` : ""}`;
return `/player/${player.id}/${pageState.sort}/${page}${isSearchActive ? `?search=${debouncedSearchTerm}` : ""}`;
},
[player.id, pageState.sort, pageState.direction, isSearchActive, debouncedSearchTerm]
[debouncedSearchTerm, player.id, pageState.sort, isSearchActive]
);
/**
@ -195,32 +167,18 @@ export default function PlayerScores({ initialScoreData, initialSearch, player,
<div ref={topOfScoresRef} className="absolute" />
<div className="flex gap-2">
{scoreSort
.filter(sort => !(!player.isBeingTracked && sort.requiresTrackedPlayer))
.map(sortOption => (
<Button
key={sortOption.value}
variant={sortOption.value === pageState.sort ? "default" : "outline"}
onClick={async () => {
if (sortOption.value !== pageState.sort) {
await handleSortChange(sortOption.value);
} else {
await handleSortDirectionChange(
pageState.direction === SortDirection.ASC ? SortDirection.DESC : SortDirection.ASC
);
}
}}
size="sm"
className="flex items-center justify-center gap-1"
>
{`${capitalizeFirstLetter(sortOption.name)}`}
{player.isBeingTracked && (
<ArrowDownIcon
className={`w-4 h-4 ${pageState.direction === SortDirection.ASC && pageState.sort === sortOption.value ? "rotate-180" : ""}`}
/>
)}
</Button>
))}
{scoreSort.map(sortOption => (
<Button
key={sortOption.value}
variant={sortOption.value === pageState.sort ? "default" : "outline"}
onClick={() => handleSortChange(sortOption.value)}
size="sm"
className="flex items-center gap-1"
>
{sortOption.icon}
{`${capitalizeFirstLetter(sortOption.name)} Scores`}
</Button>
))}
</div>
<div className="relative w-72 lg:absolute right-0 top-0">
@ -260,12 +218,17 @@ export default function PlayerScores({ initialScoreData, initialSearch, player,
>
{scores.scores.map((score, index) => (
<motion.div key={index} variants={scoreAnimation}>
<Score score={score.score} leaderboard={score.leaderboard} beatSaverMap={score.beatSaver} />
<Score
score={score.score}
leaderboard={score.leaderboard}
beatSaverMap={score.beatSaver}
highlightedPlayer={player}
/>
</motion.div>
))}
</motion.div>
{scores.metadata.totalPages >= 1 && (
{scores.metadata.totalPages > 1 && (
<Pagination
mobilePagination={width < 768}
page={pageState.page}

View File

@ -16,16 +16,33 @@ export function HandAccuracyBadge({ score, hand }: HandAccuracyProps) {
}
const { handAccuracy } = score.additionalData;
const scoreImprovement = score.additionalData.scoreImprovement;
const currentHandAccuracy = handAccuracy[hand];
const previousHandAccuracy = scoreImprovement ? handAccuracy[hand] - scoreImprovement.handAccuracy[hand] : undefined;
const formattedHand = capitalizeFirstLetter(hand);
return (
<div className="flex flex-col items-center justify-center">
<Tooltip display={`${formattedHand} Hand Accuracy`}>
<p>{handAccuracy[hand].toFixed(2)}</p>
<Tooltip
display={
<>
<p className="font-semibold">{formattedHand} Hand Accuracy</p>
<p>Hand Accuracy: {currentHandAccuracy.toFixed(2)}</p>
<p>Accuracy: {((currentHandAccuracy / 115) * 100).toFixed(2)}%</p>
</>
}
>
<p>{currentHandAccuracy.toFixed(2)}</p>
</Tooltip>
{scoreImprovement && previousHandAccuracy && (
<Tooltip display={`Previous ${formattedHand} Hand Accuracy: ${previousHandAccuracy.toFixed(2)}`}>
<Tooltip
display={
<>
<p className="font-semibold">{formattedHand} Hand Accuracy</p>
<p>Hand Accuracy: {previousHandAccuracy.toFixed(2)}</p>
<p>Accuracy: {((previousHandAccuracy / 115) * 100).toFixed(2)}%</p>
</>
}
>
<Change
className="text-xs"
change={scoreImprovement.handAccuracy[hand]}

View File

@ -4,7 +4,7 @@ import Tooltip from "@/components/tooltip";
import { ScoreModifiers } from "@/components/score/score-modifiers";
import { Change } from "@/common/change";
import { ScoreBadgeProps } from "@/components/score/badges/badge-props";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
type ScoreAccuracyProps = ScoreBadgeProps & {
/**
@ -14,8 +14,7 @@ type ScoreAccuracyProps = ScoreBadgeProps & {
};
export function ScoreAccuracyBadge({ score, leaderboard }: ScoreAccuracyProps) {
const scoreImprovement = score.additionalData?.scoreImprovement;
const previousAccuracy = scoreImprovement ? score.accuracy - scoreImprovement.accuracy : undefined;
const previousScore = score.previousScore;
const fcAccuracy = score.additionalData?.fcAccuracy;
const scoreBadge = getScoreBadgeFromAccuracy(score.accuracy);
@ -57,9 +56,13 @@ export function ScoreAccuracyBadge({ score, leaderboard }: ScoreAccuracyProps) {
{modCount > 0 && <ScoreModifiers type="simple" limit={1} score={score} />}
</p>
</Tooltip>
{scoreImprovement && previousAccuracy && (
<Tooltip display={`Previous Accuracy: ${previousAccuracy.toFixed(2)}%`}>
<Change className="text-xs" change={scoreImprovement.accuracy} formatValue={num => `${num.toFixed(2)}%`} />
{previousScore && previousScore.change && (
<Tooltip display={`Previous Accuracy: ${previousScore.accuracy.toFixed(2)}%`}>
<Change
className="text-xs"
change={previousScore.change.accuracy}
formatValue={num => `${num.toFixed(2)}%`}
/>
</Tooltip>
)}
</div>

View File

@ -1,9 +1,9 @@
import { formatNumberWithCommas } from "@ssr/common/utils/number-utils";
import { XMarkIcon } from "@heroicons/react/24/solid";
import clsx from "clsx";
import { PauseIcon } from "@heroicons/react/24/solid";
import { ScoreBadgeProps } from "@/components/score/badges/badge-props";
import { ScoreMissesTooltip } from "@/components/score/score-misses-tooltip";
import { Misses } from "@ssr/common/model/additional-score-data/misses";
import Tooltip from "@/components/tooltip";
type ScoreMissesBadgeProps = ScoreBadgeProps & {
/**
@ -12,7 +12,7 @@ type ScoreMissesBadgeProps = ScoreBadgeProps & {
hideXMark?: boolean;
};
export default function ScoreMissesBadge({ score, hideXMark }: ScoreMissesBadgeProps) {
export default function ScoreMissesAndPausesBadge({ score, hideXMark }: ScoreMissesBadgeProps) {
const additionalData = score.additionalData;
const scoreImprovement = additionalData?.scoreImprovement;
@ -30,20 +30,42 @@ export default function ScoreMissesBadge({ score, hideXMark }: ScoreMissesBadgeP
const isMissImprovement =
previousScoreMisses && scoreImprovement && previousScoreMisses.misses > scoreImprovement.misses.misses;
const pauses = additionalData?.pauses;
return (
<div className="flex flex-col justify-center items-center">
<ScoreMissesTooltip
missedNotes={score.missedNotes}
badCuts={score.badCuts}
bombCuts={misses?.bombCuts}
wallsHit={misses?.wallsHit}
fullCombo={score.fullCombo}
>
<div className="flex gap-1 items-center">
<p>{score.fullCombo ? <span className="text-green-400">FC</span> : formatNumberWithCommas(score.misses)}</p>
{!hideXMark && <XMarkIcon className={clsx("w-5 h-5", score.fullCombo ? "hidden" : "text-red-400")} />}
</div>
</ScoreMissesTooltip>
<div className="flex flex-col justify-center items-center w-full">
<div className="flex items-center gap-1">
<ScoreMissesTooltip
missedNotes={score.missedNotes}
badCuts={score.badCuts}
bombCuts={misses?.bombCuts}
wallsHit={misses?.wallsHit}
fullCombo={score.fullCombo}
>
<div className="flex items-center">
<p>
{score.fullCombo ? <span className="text-green-400">FC</span> : formatNumberWithCommas(score.misses)}
{!hideXMark && !score.fullCombo && <span>x</span>}
</p>
</div>
</ScoreMissesTooltip>
{additionalData && !!pauses && pauses > 0 && (
<>
<p>|</p>
<Tooltip
display={
<p>
{pauses}x Pause{pauses > 1 ? "s" : ""}
</p>
}
>
<div className="flex gap-1 items-center">
<p>{pauses && pauses}</p>
<PauseIcon className="w-4 h-4" />
</div>
</Tooltip>
</>
)}
</div>
{additionalData && previousScoreMisses && scoreImprovement && misses && isMissImprovement && (
<div className="flex gap-2 items-center justify-center">
<ScoreMissesTooltip

View File

@ -1,10 +1,9 @@
import { ScoreBadgeProps } from "@/components/score/badges/badge-props";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import Tooltip from "@/components/tooltip";
import { ensurePositiveNumber, formatPp } from "@ssr/common/utils/number-utils";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { Change } from "@/common/change";
import { Warning } from "@/components/warning";
type ScorePpProps = ScoreBadgeProps & {
/**
@ -14,8 +13,7 @@ type ScorePpProps = ScoreBadgeProps & {
};
export function ScorePpBadge({ score, leaderboard }: ScorePpProps) {
const scoreImprovement = score.additionalData?.scoreImprovement;
const previousAccuracy = scoreImprovement ? score.accuracy - scoreImprovement?.accuracy : undefined;
const previousScore = score.previousScore;
const fcAccuracy = score.additionalData?.fcAccuracy;
const pp = score.pp;
const weight = score.weight;
@ -28,39 +26,29 @@ export function ScorePpBadge({ score, leaderboard }: ScorePpProps) {
return (
<>
<Tooltip
display={
<div className="flex flex-col gap-2">
<div>
<p className="font-semibold">Performance Points</p>
<p>Raw: {formatPp(pp)}pp</p>
<p>
Weighted: {formatPp(weightedPp)}pp ({(100 * weight).toFixed(2)}%)
</p>
{fcPp && <p>Full Combo: {fcPp}pp</p>}
</div>
{previousAccuracy && (
<Warning>
<p className="text-red-700">
The previous pp may not be 100% accurate due to ScoreSaber API limitations.
<div className="flex flex-col items-center justify-center cursor-default">
<Tooltip
display={
<div className="flex flex-col gap-2">
<div>
<p className="font-semibold">Performance Points</p>
<p>Raw: {formatPp(pp)}pp</p>
<p>
Weighted: {formatPp(weightedPp)}pp ({(100 * weight).toFixed(2)}%)
</p>
</Warning>
)}
</div>
}
>
<div className="flex flex-col items-center justify-center cursor-default">
{fcPp && <p>Full Combo: {fcPp}pp</p>}
</div>
</div>
}
>
<p>{formatPp(pp)}pp</p>
{previousAccuracy && (
<Change
className="text-xs"
change={ensurePositiveNumber(pp - scoresaberService.getPp(leaderboard.stars, previousAccuracy))}
isPp
/>
)}
</div>
</Tooltip>
</Tooltip>
{previousScore && previousScore.change && (
<Tooltip display={<p>Previous PP: {formatPp(previousScore.pp)}pp</p>}>
<Change className="text-xs" change={ensurePositiveNumber(previousScore.change.pp)} isPp />
</Tooltip>
)}
</div>
</>
);
}

View File

@ -1,14 +1,19 @@
import { ScoreBadgeProps } from "@/components/score/badges/badge-props";
import { formatNumberWithCommas } from "@ssr/common/utils/number-utils";
import { Change } from "@/common/change";
import Tooltip from "@/components/tooltip";
export function ScoreScoreBadge({ score }: ScoreBadgeProps) {
const scoreImprovement = score.additionalData?.scoreImprovement;
const previousScore = score.previousScore;
return (
<div className="flex flex-col items-center justify-center">
<p>{formatNumberWithCommas(Number(score.score.toFixed(0)))}</p>
{scoreImprovement && <Change className="text-xs" change={scoreImprovement.score} />}
{previousScore && previousScore.change && (
<Tooltip display={<p>Previous Score: {formatNumberWithCommas(previousScore.score)}</p>}>
<Change className="text-xs" change={previousScore.change.score} />
</Tooltip>
)}
</div>
);
}

View File

@ -1,7 +1,7 @@
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import StatValue from "@/components/stat-value";
import { getBeatSaverDifficulty } from "@ssr/common/utils/beatsaver.util";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { formatTime } from "@ssr/common/utils/time-utils";
import { formatNumberWithCommas } from "@ssr/common/utils/number-utils";
import { BombIcon, BrickWallIcon, DrumIcon, MusicIcon, TimerIcon } from "lucide-react";

View File

@ -1,6 +1,6 @@
import StatValue from "@/components/stat-value";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
/**
* A badge to display in the score stats.

View File

@ -12,7 +12,7 @@ import { ArrowDownIcon, ArrowPathIcon } from "@heroicons/react/24/solid";
import clsx from "clsx";
import ScoreEditorButton from "@/components/score/score-editor-button";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import BeatSaberPepeLogo from "@/components/logos/beatsaber-pepe-logo";
@ -21,6 +21,8 @@ type Props = {
leaderboard: ScoreSaberLeaderboard;
beatSaverMap?: BeatSaverMap;
alwaysSingleLine?: boolean;
hideLeaderboardDropdown?: boolean;
hideAccuracyChanger?: boolean;
isLeaderboardLoading?: boolean;
setIsLeaderboardExpanded?: (isExpanded: boolean) => void;
updateScore?: (score: ScoreSaberScore) => void;
@ -31,6 +33,8 @@ export default function ScoreButtons({
leaderboard,
beatSaverMap,
alwaysSingleLine,
hideLeaderboardDropdown,
hideAccuracyChanger,
isLeaderboardLoading,
setIsLeaderboardExpanded,
updateScore,
@ -103,12 +107,12 @@ export default function ScoreButtons({
className={`flex gap-2 ${alwaysSingleLine ? "flex-row" : "flex-row lg:flex-col"} items-center justify-center`}
>
{/* Edit score button */}
{score && leaderboard && updateScore && (
{score && leaderboard && updateScore && !hideAccuracyChanger && (
<ScoreEditorButton score={score} leaderboard={leaderboard} updateScore={updateScore} />
)}
{/* View Leaderboard button */}
{leaderboardExpanded != undefined && setIsLeaderboardExpanded != undefined && (
{leaderboardExpanded != undefined && setIsLeaderboardExpanded != undefined && !hideLeaderboardDropdown && (
<div className="flex items-center justify-center cursor-default">
{isLeaderboardLoading ? (
<ArrowPathIcon className="w-5 h-5 animate-spin" />

View File

@ -7,7 +7,7 @@ import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover
import { ResetIcon } from "@radix-ui/react-icons";
import Tooltip from "@/components/tooltip";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
type ScoreEditorButtonProps = {
score: ScoreSaberScore;

View File

@ -25,7 +25,7 @@ export function ScoreMissesTooltip({
return (
<Tooltip
display={
<div className="flex flex-col">
<div className="flex flex-col w-full">
{!fullCombo ? (
<>
<p className="font-semibold">Misses</p>

View File

@ -3,7 +3,7 @@ import { GlobeAmericasIcon } from "@heroicons/react/24/solid";
import Link from "next/link";
import { getPageFromRank } from "@ssr/common/utils/utils";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { ScoreTimeSet } from "@/components/score/score-time-set";
type Props = {

View File

@ -3,7 +3,7 @@ import Tooltip from "@/components/tooltip";
import { StarIcon } from "@heroicons/react/24/solid";
import Image from "next/image";
import Link from "next/link";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import { getDifficulty } from "@/common/song-utils";

View File

@ -1,8 +1,8 @@
import { getScoreBadgeFromAccuracy } from "@/common/song-utils";
import { ScoreBadge, ScoreBadges } from "@/components/score/score-badge";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import ScoreMissesBadge from "@/components/score/badges/score-misses";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import ScoreMissesAndPausesBadge from "@/components/score/badges/score-misses-and-pauses";
import { HandAccuracyBadge } from "@/components/score/badges/hand-accuracy";
import { ScoreAccuracyBadge } from "@/components/score/badges/score-accuracy";
import { ScorePpBadge } from "@/components/score/badges/score-pp";
@ -60,7 +60,7 @@ const badges: ScoreBadge[] = [
{
name: "Full Combo",
create: (score: ScoreSaberScore) => {
return <ScoreMissesBadge score={score} />;
return <ScoreMissesAndPausesBadge score={score} />;
},
},
];

View File

@ -0,0 +1,63 @@
"use client";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import Score from "@/components/score/score";
import { fetchPlayerScoresHistory } from "@ssr/common/utils/score-utils";
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import Pagination from "@/components/input/pagination";
import { useIsMobile } from "@/hooks/use-is-mobile";
type ScoreHistoryProps = {
/**
* The player who set this score.
*/
playerId: string;
/**
* The leaderboard the score was set on.
*/
leaderboard: ScoreSaberLeaderboard;
};
export function ScoreHistory({ playerId, leaderboard }: ScoreHistoryProps) {
const isMobile = useIsMobile();
const [page, setPage] = useState(1);
const { data, isError, isLoading } = useQuery({
queryKey: [`scoresHistory:${leaderboard.id}`, leaderboard.id, page],
queryFn: async () => fetchPlayerScoresHistory(playerId, leaderboard.id + "", page),
staleTime: 30 * 1000,
});
if (!data || isError) {
return <p className="text-center">No score history found.</p>;
}
return (
<>
{data.items.map(({ score, leaderboard, beatSaver }) => (
<Score
key={score.scoreId}
score={score}
leaderboard={leaderboard}
beatSaverMap={beatSaver}
settings={{
hideLeaderboardDropdown: true,
hideAccuracyChanger: true,
}}
/>
))}
<Pagination
mobilePagination={isMobile}
page={page}
totalPages={data.metadata.totalPages}
loadingPage={isLoading ? page : undefined}
onPageChange={newPage => {
setPage(newPage);
}}
/>
</>
);
}

View File

@ -0,0 +1,54 @@
import PlayerScoreAccuracyChart from "@/components/leaderboard/chart/player-score-accuracy-chart";
import LeaderboardScores from "@/components/leaderboard/leaderboard-scores";
import { ScoreStatsToken } from "@ssr/common/types/token/beatleader/score-stats/score-stats";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import LeaderboardScoresResponse from "@ssr/common/response/leaderboard-scores-response";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
type ScoreOverviewProps = {
/**
* The player to highlight
*/
highlightedPlayer?: ScoreSaberPlayer;
/**
* The initial page to show
*/
initialPage: number;
/**
* The score stats for this score.
*/
scoreStats?: ScoreStatsToken;
/**
* The leaderboard the score was set on.
*/
leaderboard: ScoreSaberLeaderboard;
/**
* The scores so show.
*/
scores?: LeaderboardScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
};
export function ScoreOverview({ highlightedPlayer, scoreStats, initialPage, leaderboard, scores }: ScoreOverviewProps) {
return (
<>
{scoreStats && (
<div className="flex gap-2">
<PlayerScoreAccuracyChart scoreStats={scoreStats} leaderboard={leaderboard} />
</div>
)}
<LeaderboardScores
initialPage={initialPage}
initialScores={scores}
leaderboard={leaderboard}
highlightedPlayer={highlightedPlayer}
disableUrlChanging
/>
</>
);
}

View File

@ -1,171 +1,150 @@
"use client";
import LeaderboardScores from "@/components/leaderboard/leaderboard-scores";
import { useEffect, useState } from "react";
import { motion } from "framer-motion";
import { useQuery } from "@tanstack/react-query";
import { CubeIcon } from "@heroicons/react/24/solid";
import { TrendingUpIcon } from "lucide-react";
import ScoreButtons from "./score-buttons";
import ScoreSongInfo from "./score-song-info";
import ScoreRankInfo from "./score-rank-info";
import ScoreStats from "./score-stats";
import { motion } from "framer-motion";
import { getPageFromRank } from "@ssr/common/utils/utils";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/leaderboard/impl/scoresaber-leaderboard";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import { useIsMobile } from "@/hooks/use-is-mobile";
import Card from "@/components/card";
import { MapStats } from "@/components/score/map-stats";
import PlayerScoreAccuracyChart from "@/components/leaderboard/chart/player-score-accuracy-chart";
import { useQuery } from "@tanstack/react-query";
import { Button } from "@/components/ui/button";
import { ScoreOverview } from "@/components/score/score-views/score-overview";
import { ScoreHistory } from "@/components/score/score-views/score-history";
import { getPageFromRank } from "@ssr/common/utils/utils";
import { fetchLeaderboardScores } from "@ssr/common/utils/score-utils";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { beatLeaderService } from "@ssr/common/service/impl/beatleader";
import { useIsMobile } from "@/hooks/use-is-mobile";
import { ScoreSaberScore } from "@ssr/common/model/score/impl/scoresaber-score";
import ScoreSaberLeaderboard from "@ssr/common/model/leaderboard/impl/scoresaber-leaderboard";
import { BeatSaverMap } from "@ssr/common/model/beatsaver/map";
import LeaderboardScoresResponse from "@ssr/common/response/leaderboard-scores-response";
import { ScoreStatsToken } from "@ssr/common/types/token/beatleader/score-stats/score-stats";
import { beatLeaderService } from "@ssr/common/service/impl/beatleader";
import ScoreSaberPlayer from "@ssr/common/player/impl/scoresaber-player";
type Props = {
/**
* The score to display.
*/
highlightedPlayer?: ScoreSaberPlayer;
score: ScoreSaberScore;
/**
* The leaderboard.
*/
leaderboard: ScoreSaberLeaderboard;
/**
* The beat saver map for this song.
*/
beatSaverMap?: BeatSaverMap;
/**
* Score settings
*/
settings?: {
noScoreButtons: boolean;
noScoreButtons?: boolean;
hideLeaderboardDropdown?: boolean;
hideAccuracyChanger?: boolean;
};
};
type LeaderboardDropdownData = {
/**
* The initial scores.
*/
type DropdownData = {
scores?: LeaderboardScoresResponse<ScoreSaberScore, ScoreSaberLeaderboard>;
/**
* The score stats for this score,
*/
scoreStats?: ScoreStatsToken;
};
export default function Score({ leaderboard, beatSaverMap, score, settings }: Props) {
const scoresPage = getPageFromRank(score.rank, 12);
type Mode = {
name: string;
icon: React.ReactElement;
};
const isMobile = useIsMobile();
const [baseScore, setBaseScore] = useState<number>(score.score);
const modes: Mode[] = [
{ name: "Overview", icon: <CubeIcon className="w-4 h-4" /> },
{ name: "Score History", icon: <TrendingUpIcon className="w-4 h-4" /> },
];
export default function Score({ leaderboard, beatSaverMap, score, settings, highlightedPlayer }: Props) {
const [baseScore, setBaseScore] = useState(score.score);
const [isLeaderboardExpanded, setIsLeaderboardExpanded] = useState(false);
const [loading, setLoading] = useState(false);
const [leaderboardDropdownData, setLeaderboardDropdownData] = useState<LeaderboardDropdownData | undefined>();
const [dropdownData, setDropdownData] = useState<DropdownData | undefined>();
const [selectedMode, setSelectedMode] = useState<Mode>(modes[0]);
const { data, isError, isLoading } = useQuery<LeaderboardDropdownData>({
queryKey: ["leaderboardDropdownData", leaderboard.id, score.scoreId, isLeaderboardExpanded],
const scoresPage = getPageFromRank(score.rank, 12);
const isMobile = useIsMobile();
const { data, isLoading } = useQuery<DropdownData>({
queryKey: [`leaderboardDropdownData:${leaderboard.id}`, leaderboard.id, score.scoreId, isLeaderboardExpanded],
queryFn: async () => {
const scores = await fetchLeaderboardScores<ScoreSaberScore, ScoreSaberLeaderboard>(
"scoresaber",
leaderboard.id + "",
leaderboard.id.toString(),
scoresPage
);
const scoreStats = score.additionalData
? await beatLeaderService.lookupScoreStats(score.additionalData.scoreId)
: undefined;
return {
scores: scores,
scoreStats: scoreStats,
};
return { scores, scoreStats };
},
staleTime: 30 * 1000,
staleTime: 30000,
enabled: loading,
});
useEffect(() => {
if (data) {
setLeaderboardDropdownData({
...data,
scores: data.scores,
scoreStats: data.scoreStats,
});
setDropdownData(data);
setLoading(false);
}
}, [data]);
useEffect(() => {
/**
* Reset the leaderboard dropdown when the score changes
*/
setIsLeaderboardExpanded(false);
setDropdownData(undefined);
setSelectedMode(modes[0]);
}, [score.scoreId]);
useEffect(() => {
setBaseScore(score.score);
}, [score]);
const accuracy = (baseScore / leaderboard.maxScore) * 100;
const pp = baseScore === score.score ? score.pp : scoresaberService.getPp(leaderboard.stars, accuracy);
const handleLeaderboardOpen = (isExpanded: boolean) => {
if (!isExpanded) {
setLeaderboardDropdownData(undefined);
setSelectedMode(modes[0]);
setDropdownData(undefined);
} else {
setLoading(true);
}
setIsLeaderboardExpanded(isExpanded);
};
/**
* Set the base score
*/
useEffect(() => {
if (score?.score) {
setBaseScore(score.score);
}
}, [score]);
const handleModeChange = (mode: Mode) => {
setSelectedMode(mode);
};
/**
* Close the leaderboard when the score changes
*/
useEffect(() => {
setIsLeaderboardExpanded(false);
setLeaderboardDropdownData(undefined);
}, [score.scoreId]);
const accuracy = (baseScore / leaderboard.maxScore) * 100;
const pp = baseScore === score.score ? score.pp : scoresaberService.getPp(leaderboard.stars, accuracy);
// Dynamic grid column classes
const gridColsClass = settings?.noScoreButtons
? "grid-cols-[20px 1fr_1fr] lg:grid-cols-[0.5fr_4fr_300px]" // Fewer columns if no buttons
: "grid-cols-[20px 1fr_1fr] lg:grid-cols-[0.5fr_4fr_1fr_300px]"; // Original with buttons
return (
<div className="pb-2 pt-2">
{/* Score Info */}
<div className={`grid w-full gap-2 lg:gap-0 ${gridColsClass}`}>
<ScoreRankInfo score={score} leaderboard={leaderboard} />
<ScoreSongInfo leaderboard={leaderboard} beatSaverMap={beatSaverMap} />
{settings?.noScoreButtons !== true && (
{!settings?.noScoreButtons && (
<ScoreButtons
leaderboard={leaderboard}
beatSaverMap={beatSaverMap}
score={score}
alwaysSingleLine={isMobile}
setIsLeaderboardExpanded={(isExpanded: boolean) => {
handleLeaderboardOpen(isExpanded);
}}
hideLeaderboardDropdown={settings?.hideLeaderboardDropdown}
hideAccuracyChanger={settings?.hideAccuracyChanger}
setIsLeaderboardExpanded={handleLeaderboardOpen}
isLeaderboardLoading={isLoading}
updateScore={score => {
setBaseScore(score.score);
}}
updateScore={updatedScore => setBaseScore(updatedScore.score)}
/>
)}
<ScoreStats
score={{
...score,
accuracy: accuracy ? accuracy : score.accuracy,
pp: pp ? pp : score.pp,
}}
leaderboard={leaderboard}
/>
<ScoreStats score={{ ...score, accuracy, pp }} leaderboard={leaderboard} />
</div>
{/* Leaderboard */}
{isLeaderboardExpanded && leaderboardDropdownData && !loading && (
{isLeaderboardExpanded && dropdownData && !loading && (
<motion.div
initial={{ opacity: 0, y: -50 }}
exit={{ opacity: 0, y: -50 }}
@ -173,20 +152,39 @@ export default function Score({ leaderboard, beatSaverMap, score, settings }: Pr
className="w-full mt-2"
>
<Card className="flex gap-4 w-full relative border border-input">
<MapStats leaderboard={leaderboard} beatSaver={beatSaverMap} />
{leaderboardDropdownData.scoreStats && (
<div className="flex gap-2">
<PlayerScoreAccuracyChart scoreStats={leaderboardDropdownData.scoreStats} />
<div className="flex flex-col lg:flex-row w-full gap-2 justify-center">
<div className="flex clex-col justify-center lg:justify-start gap-2">
{modes.map((mode, i) => (
<Button
key={i}
variant={mode.name === selectedMode.name ? "default" : "outline"}
onClick={() => handleModeChange(mode)}
className="flex gap-2"
>
{mode.icon}
<p>{mode.name}</p>
</Button>
))}
</div>
<div>
<MapStats leaderboard={leaderboard} beatSaver={beatSaverMap} />
</div>
</div>
{selectedMode.name === "Overview" && (
<ScoreOverview
scores={dropdownData.scores}
leaderboard={leaderboard}
initialPage={scoresPage}
scoreStats={dropdownData.scoreStats}
highlightedPlayer={highlightedPlayer}
/>
)}
<LeaderboardScores
initialPage={scoresPage}
initialScores={leaderboardDropdownData.scores}
leaderboard={leaderboard}
disableUrlChanging
/>
{selectedMode.name === "Score History" && (
<ScoreHistory playerId={score.playerId} leaderboard={leaderboard} />
)}
</Card>
</motion.div>
)}