Skip to content

Commit

Permalink
Don't wait for MB on score submit + Skill points
Browse files Browse the repository at this point in the history
  • Loading branch information
RubberDuckShobe committed Aug 30, 2023
1 parent 5866007 commit 1a15abb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- Added the required column `skillPoints` to the `Score` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Score" ADD COLUMN "skillPoints" INTEGER NOT NULL DEFAULT 0;
UPDATE "Score" SET "skillPoints" = ROUND(("score"::float / "goldThreshold") * (("leagueId" + 1) * 100));
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ model Score {
xstats String
density Int
vehicleId Int
score Int
score Int
skillPoints Int
rideTime DateTime @default(now()) //NOTE: When sending to the game, get it as UNIX time, divide by 1000 and Math.floor it
feats String
songLength Int
Expand Down
25 changes: 13 additions & 12 deletions routes/as1/gameplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import crypto from "crypto";
import { addMusicBrainzInfo } from "../../util/musicbrainz";
import { removeTagsFromTitle, tagsFromTitle } from "../../util/gamemodeTags";
import { Static, Type } from "@sinclair/typebox";
import { calcSkillPoints } from "../../util/rankings";

const xmlBuilder = new xml2js.Builder();

Expand Down Expand Up @@ -285,19 +286,17 @@ export default async function routes(fastify: FastifyInstance) {
request.body.artist
);

try {
if (!song.mbid) {
fastify.log.info(
`Looking up MusicBrainz info for song ${song.id} with length ${
request.body.songlength * 10
}`
);
await addMusicBrainzInfo(song, request.body.songlength * 10);
}
} catch (e) {
fastify.log.error(
`Failed to look up MusicBrainz info: ${e}\n${e.stack}`
if (!song.mbid) {
fastify.log.info(
`Looking up MusicBrainz info for song ${song.id} with length ${
request.body.songlength * 10
}`
);
addMusicBrainzInfo(song, request.body.songlength * 10).catch((e) => {
fastify.log.error(
`Failed to look up MusicBrainz info: ${e}\n${e.stack}`
);
});
}

const prevScore = await prisma.score.findUnique({
Expand Down Expand Up @@ -329,6 +328,7 @@ export default async function routes(fastify: FastifyInstance) {
feats: request.body.feats,
songLength: request.body.songlength,
goldThreshold: request.body.goldthreshold,
skillPoints: calcSkillPoints(request.body.score, request.body.goldthreshold, request.body.league),
iss: request.body.iss,
isj: request.body.isj,
songId: request.body.songid,
Expand All @@ -347,6 +347,7 @@ export default async function routes(fastify: FastifyInstance) {
feats: request.body.feats,
songLength: request.body.songlength,
goldThreshold: request.body.goldthreshold,
skillPoints: calcSkillPoints(request.body.score, request.body.goldthreshold, request.body.league),
iss: request.body.iss,
isj: request.body.isj,
rideTime: new Date(),
Expand Down
16 changes: 16 additions & 0 deletions util/rankings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
TODO: Implement rankings properly, using skill points.
Formula for skill points is Math.round((score / goldThreshold) * multiplier)
The multiplier is 300 for Elite, 200 for Pro and 100 for Casual leagues respectively
Maybe use Redis or something for the ranking stuff as well?
*/

import { Song, User } from "@prisma/client";
import { prisma } from "./db";

Expand All @@ -11,6 +18,15 @@ interface DBUserRank {
total_score: bigint;
}

export function calcSkillPoints(
score: number,
goldThreshold: number,
leagueId: number
): number {
const multiplier = (leagueId + 1) * 100;
return Math.round((score / goldThreshold) * multiplier);
}

export async function getPopularSongs(
page: number,
pageSize: number,
Expand Down

0 comments on commit 1a15abb

Please sign in to comment.