added chunithm version selector in admin settings
This commit is contained in:
54
lib/api.ts
54
lib/api.ts
@ -2,6 +2,7 @@
|
||||
|
||||
import { getAuth } from "@/auth/queries/getauth";
|
||||
import { artemis, daphnis } from "@/lib/prisma";
|
||||
import { GameVersion } from "@/prisma/schemas/daphnis/generated/daphnis";
|
||||
|
||||
export const getUsername = async () => {
|
||||
const { user } = await getAuth();
|
||||
@ -45,3 +46,56 @@ export async function verifyAimeCodeAgainstArtemis() {
|
||||
});
|
||||
return aimeUser;
|
||||
}
|
||||
|
||||
const GameVersionToNumber: Record<GameVersion, number> = {
|
||||
[GameVersion.LuminousPlus]: 16,
|
||||
[GameVersion.Luminous]: 15,
|
||||
[GameVersion.SunPlus]: 14,
|
||||
[GameVersion.Sun]: 13,
|
||||
[GameVersion.NewPlus]: 12,
|
||||
[GameVersion.New]: 10,
|
||||
};
|
||||
|
||||
export async function getGameVersion(): Promise<GameVersion> {
|
||||
const { user } = await getAuth();
|
||||
|
||||
if (!user || !user.accessCode) {
|
||||
throw new Error("User is not authenticated or accessCode is missing");
|
||||
}
|
||||
|
||||
const aimeUser = await daphnis.user.findFirst({
|
||||
where: {
|
||||
accessCode: user.accessCode,
|
||||
},
|
||||
select: {
|
||||
gameVersion: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!aimeUser || !aimeUser.gameVersion) {
|
||||
throw new Error("Game version not found for the user");
|
||||
}
|
||||
|
||||
// console.log("User Game Version:", aimeUser.gameVersion);
|
||||
|
||||
const gameVersionEnum = aimeUser.gameVersion as GameVersion;
|
||||
|
||||
if (!(gameVersionEnum in GameVersionToNumber)) {
|
||||
throw new Error("Unknown game version");
|
||||
}
|
||||
|
||||
return gameVersionEnum;
|
||||
}
|
||||
|
||||
export async function getSupportedVersionNumber(): Promise<number> {
|
||||
const gameVersion = await getGameVersion();
|
||||
|
||||
const versionNumber = GameVersionToNumber[gameVersion];
|
||||
|
||||
if (versionNumber === undefined) {
|
||||
throw new Error("Unknown version number");
|
||||
}
|
||||
|
||||
// console.log(typeof versionNumber);
|
||||
return versionNumber;
|
||||
}
|
||||
|
@ -1,3 +1,9 @@
|
||||
import { getAuth } from "@/auth/queries/getauth";
|
||||
import { daphnis } from "./prisma";
|
||||
import { GameVersion } from "@/prisma/schemas/daphnis/generated/daphnis";
|
||||
import { getGameVersion } from "./api";
|
||||
import { LucideSuperscript } from "lucide-react";
|
||||
|
||||
export const getDifficultyClass = (level: number) => {
|
||||
switch (level) {
|
||||
case 0:
|
||||
@ -53,7 +59,3 @@ export const getGrade = (score: number) => {
|
||||
if (score < 500000) return "D";
|
||||
return "";
|
||||
};
|
||||
|
||||
export const supportedVersionNumber = Number(
|
||||
process.env.SUPPORTED_CHUNITHM_VERSION_NUMBER,
|
||||
);
|
||||
|
@ -1,20 +1,36 @@
|
||||
import { PrismaClient as daphnisClient } from "@/prisma/schemas/daphnis/generated/daphnis";
|
||||
import { PrismaClient as artemisClient } from "@/prisma/schemas/artemis/generated/artemis";
|
||||
import { PrismaClient as DaphnisClient } from "@/prisma/schemas/daphnis/generated/daphnis";
|
||||
import { PrismaClient as ArtemisClient } from "@/prisma/schemas/artemis/generated/artemis";
|
||||
|
||||
const DaphnisSingleton = () => {
|
||||
return new daphnisClient();
|
||||
// Singleton pattern for Daphnis client
|
||||
const DaphnisClientSingleton = () => {
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
return new DaphnisClient();
|
||||
}
|
||||
|
||||
// In development mode, reuse existing global instance if available
|
||||
if (globalThis.daphnisClient) {
|
||||
return globalThis.daphnisClient as DaphnisClient;
|
||||
}
|
||||
const client = new DaphnisClient();
|
||||
globalThis.daphnisClient = client;
|
||||
return client;
|
||||
};
|
||||
|
||||
const aremisSingleton = () => {
|
||||
return new artemisClient();
|
||||
// Singleton pattern for Artemis client
|
||||
const ArtemisClientSingleton = () => {
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
return new ArtemisClient();
|
||||
}
|
||||
|
||||
// In development mode, reuse existing global instance if available
|
||||
if (globalThis.artemisClient) {
|
||||
return globalThis.artemisClient as ArtemisClient;
|
||||
}
|
||||
const client = new ArtemisClient();
|
||||
globalThis.artemisClient = client;
|
||||
return client;
|
||||
};
|
||||
|
||||
declare global {
|
||||
var daphnis: undefined | ReturnType<typeof DaphnisSingleton>;
|
||||
var artemis: undefined | ReturnType<typeof aremisSingleton>;
|
||||
}
|
||||
|
||||
export const daphnis = globalThis.daphnis ?? DaphnisSingleton();
|
||||
export const artemis = globalThis.artemis ?? aremisSingleton();
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalThis.daphnis = daphnis;
|
||||
// Exporting the singletons
|
||||
export const daphnis = DaphnisClientSingleton();
|
||||
export const artemis = ArtemisClientSingleton();
|
||||
|
Reference in New Issue
Block a user