From 304f5996fb4502804b79b74b72c6cd96553be057 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 10 Mar 2023 21:50:43 +0000 Subject: [PATCH] fix(arabot): add xp to next level to fix levels being broken --- .../migration.sql | 2 ++ prisma/schema.prisma | 13 ++++---- src/utils/database/xp.ts | 33 ++++++++----------- 3 files changed, 23 insertions(+), 25 deletions(-) create mode 100644 prisma/migrations/20230310212159_xp_to_next_level/migration.sql diff --git a/prisma/migrations/20230310212159_xp_to_next_level/migration.sql b/prisma/migrations/20230310212159_xp_to_next_level/migration.sql new file mode 100644 index 0000000..61ded02 --- /dev/null +++ b/prisma/migrations/20230310212159_xp_to_next_level/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Xp" ADD COLUMN "xpToNextLevel" INTEGER NOT NULL DEFAULT 0; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 56e07db..2174ae5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -98,12 +98,13 @@ model Verify { } model Xp { - user User @relation(fields: [userId], references: [id]) - userId String @id - level Int @default(0) - xp Int @default(0) - messageCount Int @default(0) - lastMessage DateTime @default(now()) + user User @relation(fields: [userId], references: [id]) + userId String @id + level Int @default(0) + xp Int @default(0) + xpToNextLevel Int @default(0) + messageCount Int @default(0) + lastMessage DateTime @default(now()) } // Economy diff --git a/src/utils/database/xp.ts b/src/utils/database/xp.ts index d992081..b70bc8c 100644 --- a/src/utils/database/xp.ts +++ b/src/utils/database/xp.ts @@ -13,6 +13,7 @@ export async function getUser(userId: Snowflake) { }, select: { xp: true, + xpToNextLevel: true, level: true, }, }); @@ -21,11 +22,17 @@ export async function getUser(userId: Snowflake) { export async function addXp(userId: Snowflake, xp: number) { const user = await getUser(userId); + let xpNextLevel = xp; let level = 0; - if (user !== null - && xpToNextLevel(user.level, user.xp + xp) < 0) { - level = 1; + if (user !== null) { + xpNextLevel = xpToNextLevel(user.level, user.xpToNextLevel + xp); + if (xpNextLevel < 0) { + xpNextLevel = -xpNextLevel; + level = 1; + } else { + xpNextLevel = user.xpToNextLevel + xp; + } } await container.database.xp.upsert({ @@ -34,6 +41,7 @@ export async function addXp(userId: Snowflake, xp: number) { }, update: { xp: { increment: xp }, + xpToNextLevel: xpNextLevel, level: { increment: level }, messageCount: { increment: 1 }, lastMessage: new Date(), @@ -51,6 +59,7 @@ export async function addXp(userId: Snowflake, xp: number) { }, messageCount: 1, xp, + xpToNextLevel: xp, }, }); } @@ -98,22 +107,8 @@ export async function getRank(userId: Snowflake) { }, }); - const xp = await container.database.xp.findUnique({ - where: { - userId, - }, - select: { - level: true, - xp: true, - }, - }); - - if (xp === null) { - return info; - } - - info.level = xp.level; - info.xp = xp.xp; + info.level = user.level; + info.xp = user.xp; return info; }