generated from nhcarrigan/template
feat: initial prototype — core game systems (#30)
## Summary This PR represents the full v1 prototype, implementing the core game systems for Elysium. - Full idle/clicker RPG loop: resource collection, crafting, boss fights, exploration, and quests - Adventurer hiring with batch size selector and progressive tier cost scaling - Prestige, transcendence, and apotheosis systems with auto-prestige support - Character sheet, titles, leaderboards, companion system, and daily login bonuses - Auto-quest and auto-boss toggles - Discord webhook notifications on prestige/transcendence/apotheosis - Discord role awarded on apotheosis - Responsive design and overarching story/lore system - In-game sound effects and browser notifications for key events - Support link button in the resource bar - Full test coverage (100% on `apps/api` and `packages/types`) - CI pipeline: lint → build → test ## Closes Closes #1 Closes #2 Closes #3 Closes #4 Closes #5 Closes #6 Closes #7 Closes #8 Closes #9 Closes #10 Closes #11 Closes #12 Closes #13 Closes #14 Closes #16 Closes #19 Closes #20 Closes #21 Closes #22 Closes #23 Closes #24 Closes #25 Closes #26 Closes #27 Closes #29 ✨ This issue was created with help from Hikari~ 🌸 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Reviewed-on: #30 Co-authored-by: Hikari <hikari@nhcarrigan.com> Co-committed-by: Hikari <hikari@nhcarrigan.com>
This commit was merged in pull request #30.
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @file Authentication routes for Discord OAuth.
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
/* eslint-disable max-lines-per-function -- Auth callback requires many steps */
|
||||
/* eslint-disable max-statements -- Auth callback requires many statements */
|
||||
import { Hono } from "hono";
|
||||
import { initialGameState } from "../data/initialState.js";
|
||||
import { prisma } from "../db/client.js";
|
||||
import {
|
||||
buildOAuthUrl,
|
||||
exchangeCode,
|
||||
fetchDiscordUser,
|
||||
} from "../services/discord.js";
|
||||
import { signToken } from "../services/jwt.js";
|
||||
import type { Player } from "@elysium/types";
|
||||
|
||||
const authRouter = new Hono();
|
||||
|
||||
authRouter.get("/url", (context) => {
|
||||
try {
|
||||
const url = buildOAuthUrl();
|
||||
return context.json({ url });
|
||||
} catch {
|
||||
return context.json({ error: "Failed to build OAuth URL" }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
authRouter.get("/callback", async(context) => {
|
||||
const code = context.req.query("code");
|
||||
|
||||
if (code === undefined || code === "") {
|
||||
return context.json({ error: "Missing code parameter" }, 400);
|
||||
}
|
||||
|
||||
try {
|
||||
const tokenData = await exchangeCode(code);
|
||||
const discordUser = await fetchDiscordUser(tokenData.access_token);
|
||||
|
||||
const existing = await prisma.player.findUnique({
|
||||
where: { discordId: discordUser.id },
|
||||
});
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
if (!existing) {
|
||||
const player = await prisma.player.create({
|
||||
data: {
|
||||
avatar: discordUser.avatar,
|
||||
characterName: discordUser.username,
|
||||
createdAt: now,
|
||||
discordId: discordUser.id,
|
||||
discriminator: discordUser.discriminator,
|
||||
lastSavedAt: now,
|
||||
totalClicks: 0,
|
||||
totalGoldEarned: 0,
|
||||
username: discordUser.username,
|
||||
},
|
||||
});
|
||||
|
||||
const playerShape: Player = {
|
||||
avatar: player.avatar ?? null,
|
||||
characterName: player.characterName,
|
||||
createdAt: player.createdAt,
|
||||
discordId: player.discordId,
|
||||
discriminator: player.discriminator,
|
||||
lastSavedAt: player.lastSavedAt,
|
||||
lifetimeAchievementsUnlocked: player.lifetimeAchievementsUnlocked,
|
||||
lifetimeAdventurersRecruited: player.lifetimeAdventurersRecruited,
|
||||
lifetimeBossesDefeated: player.lifetimeBossesDefeated,
|
||||
lifetimeClicks: player.lifetimeClicks,
|
||||
lifetimeGoldEarned: player.lifetimeGoldEarned,
|
||||
lifetimeQuestsCompleted: player.lifetimeQuestsCompleted,
|
||||
totalClicks: player.totalClicks,
|
||||
totalGoldEarned: player.totalGoldEarned,
|
||||
username: player.username,
|
||||
};
|
||||
|
||||
const freshState = initialGameState(
|
||||
playerShape,
|
||||
playerShape.characterName,
|
||||
);
|
||||
await prisma.gameState.create({
|
||||
data: {
|
||||
discordId: player.discordId,
|
||||
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Prisma requires never type */
|
||||
state: freshState as unknown as never,
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
|
||||
const jwtToken = signToken(player.discordId);
|
||||
|
||||
// eslint-disable-next-line capitalized-comments -- v8 ignore
|
||||
/* v8 ignore next -- @preserve */
|
||||
const clientUrl = process.env.CORS_ORIGIN ?? "http://localhost:5173";
|
||||
return context.redirect(
|
||||
`${clientUrl}/auth/callback?token=${jwtToken}&isNew=true`,
|
||||
);
|
||||
}
|
||||
|
||||
const updated = await prisma.player.update({
|
||||
data: {
|
||||
avatar: discordUser.avatar,
|
||||
discriminator: discordUser.discriminator,
|
||||
username: discordUser.username,
|
||||
},
|
||||
where: { discordId: discordUser.id },
|
||||
});
|
||||
|
||||
const jwtToken = signToken(updated.discordId);
|
||||
|
||||
// eslint-disable-next-line capitalized-comments -- v8 ignore
|
||||
/* v8 ignore next -- @preserve */
|
||||
const clientUrl = process.env.CORS_ORIGIN ?? "http://localhost:5173";
|
||||
return context.redirect(
|
||||
`${clientUrl}/auth/callback?token=${jwtToken}&isNew=false`,
|
||||
);
|
||||
} catch {
|
||||
// eslint-disable-next-line capitalized-comments -- v8 ignore
|
||||
/* v8 ignore next -- @preserve */
|
||||
const clientUrl = process.env.CORS_ORIGIN ?? "http://localhost:5173";
|
||||
return context.redirect(`${clientUrl}/auth/callback?error=auth_failed`);
|
||||
}
|
||||
});
|
||||
|
||||
export { authRouter };
|
||||
Reference in New Issue
Block a user