-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee5dccc
commit 9b0088a
Showing
2 changed files
with
63 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Redis from "ioredis"; | ||
import { PrismaClient } from "@prisma/client"; | ||
const redis = new Redis(process.env.REDIS_URL); | ||
const prisma = new PrismaClient(); | ||
|
||
async function main() { | ||
//Get all users and combine all skillPoints across all their scores | ||
const users = await prisma.user.findMany({ | ||
include: { | ||
scores: true, | ||
}, | ||
}); | ||
const usersTotalSkillPoints = users.map((user) => { | ||
const totalPoints = user.scores.reduce((acc, score) => { | ||
return acc + score.skillPoints; | ||
}, 0); | ||
return { ...user, totalSkillPoints: totalPoints }; | ||
}); | ||
//Add users with total skill points to sorted list in Redis | ||
for (const user of usersTotalSkillPoints) { | ||
await redis.zadd("leaderboard", user.totalSkillPoints, user.id); | ||
} | ||
} | ||
|
||
main() | ||
.then(async () => { | ||
await prisma.$disconnect(); | ||
}) | ||
|
||
.catch(async (e) => { | ||
console.error(e); | ||
|
||
await prisma.$disconnect(); | ||
|
||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
import { PrismaClient, Song, User } from "@prisma/client"; | ||
import Redis from "ioredis"; | ||
|
||
export const prisma = new PrismaClient({ | ||
log: [ | ||
{ | ||
emit: "event", | ||
level: "query", | ||
export const redis = new Redis(process.env.REDIS_URL); | ||
|
||
const prismaOrig = new PrismaClient(); | ||
//potentially hacky solution for leaderboard | ||
export const prisma = prismaOrig.$extends({ | ||
name: "leaderboardExt", | ||
query: { | ||
score: { | ||
async upsert({ args, query }) { | ||
//If score already exists, add difference of skillPoints to leaderboard ranking | ||
const score = await prisma.score.findUnique({ where: args.where }); | ||
if (score) { | ||
//create.score and update.score are the same, the latter has a weird secondary type so no comparisons allowed | ||
if (score.score < args.create.score) { | ||
const diff = args.create.score - score.score; | ||
await redis.zincrby("leaderboard", diff, score.userId); | ||
} | ||
} else { | ||
await redis.zadd( | ||
"leaderboard", | ||
args.create.score, | ||
args.create.userId | ||
); | ||
} | ||
return query(args); | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
export const redis = new Redis(process.env.REDIS_URL); | ||
|
||
export interface ExtendedUser extends User { | ||
totalScore: number; | ||
totalPlays: number; | ||
favoriteCharacter?: number; | ||
favoriteSong?: Song; | ||
} | ||
|
||
/* | ||
prisma.$on("query", (e) => { | ||
console.log("DB query took " + e.duration + "ms"); | ||
}); | ||
*/ |