generated from nhcarrigan/template
29c817230d
## 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>
157 lines
5.2 KiB
TypeScript
157 lines
5.2 KiB
TypeScript
/**
|
|
* @file Crafting route handling recipe crafting mechanics.
|
|
* @copyright nhcarrigan
|
|
* @license Naomi's Public License
|
|
* @author Naomi Carrigan
|
|
*/
|
|
/* eslint-disable max-lines-per-function -- Route handler requires many steps */
|
|
/* eslint-disable max-statements -- Route handler requires many statements */
|
|
/* eslint-disable complexity -- Route handler has inherent complexity */
|
|
import { Hono } from "hono";
|
|
import { defaultRecipes } from "../data/recipes.js";
|
|
import { prisma } from "../db/client.js";
|
|
import { authMiddleware } from "../middleware/auth.js";
|
|
import type { HonoEnvironment } from "../types/hono.js";
|
|
import type {
|
|
CraftRecipeRequest,
|
|
CraftRecipeResponse,
|
|
GameState,
|
|
} from "@elysium/types";
|
|
|
|
const craftRouter = new Hono<HonoEnvironment>();
|
|
|
|
craftRouter.use("*", authMiddleware);
|
|
|
|
const recomputeCraftedMultipliers = (
|
|
craftedRecipeIds: Array<string>,
|
|
): {
|
|
craftedGoldMultiplier: number;
|
|
craftedEssenceMultiplier: number;
|
|
craftedClickMultiplier: number;
|
|
craftedCombatMultiplier: number;
|
|
} => {
|
|
return {
|
|
craftedClickMultiplier: defaultRecipes.filter((r) => {
|
|
return craftedRecipeIds.includes(r.id) && r.bonus.type === "click_power";
|
|
}).reduce((mult, r) => {
|
|
// eslint-disable-next-line capitalized-comments -- v8 ignore
|
|
/* v8 ignore next -- @preserve */
|
|
return mult * r.bonus.value;
|
|
}, 1),
|
|
craftedCombatMultiplier: defaultRecipes.filter((r) => {
|
|
return craftedRecipeIds.includes(r.id) && r.bonus.type === "combat_power";
|
|
}).reduce((mult, r) => {
|
|
// eslint-disable-next-line capitalized-comments -- v8 ignore
|
|
/* v8 ignore next -- @preserve */
|
|
return mult * r.bonus.value;
|
|
}, 1),
|
|
craftedEssenceMultiplier: defaultRecipes.filter((r) => {
|
|
return (
|
|
craftedRecipeIds.includes(r.id) && r.bonus.type === "essence_income"
|
|
);
|
|
}).reduce((mult, r) => {
|
|
// eslint-disable-next-line capitalized-comments -- v8 ignore
|
|
/* v8 ignore next -- @preserve */
|
|
return mult * r.bonus.value;
|
|
}, 1),
|
|
craftedGoldMultiplier: defaultRecipes.filter((r) => {
|
|
return craftedRecipeIds.includes(r.id) && r.bonus.type === "gold_income";
|
|
}).reduce((mult, r) => {
|
|
return mult * r.bonus.value;
|
|
}, 1),
|
|
};
|
|
};
|
|
|
|
craftRouter.post("/", async(context) => {
|
|
const discordId = context.get("discordId");
|
|
const body = await context.req.json<CraftRecipeRequest>();
|
|
|
|
const { recipeId } = body;
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- Runtime body validation
|
|
if (!recipeId) {
|
|
return context.json({ error: "recipeId is required" }, 400);
|
|
}
|
|
|
|
const recipe = defaultRecipes.find((r) => {
|
|
return r.id === recipeId;
|
|
});
|
|
if (!recipe) {
|
|
return context.json({ error: "Unknown recipe" }, 404);
|
|
}
|
|
|
|
const record = await prisma.gameState.findUnique({ where: { discordId } });
|
|
if (!record) {
|
|
return context.json({ error: "No save found" }, 404);
|
|
}
|
|
|
|
const rawState: unknown = record.state;
|
|
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Prisma returns JsonValue; cast to GameState */
|
|
const state = rawState as GameState;
|
|
|
|
if (!state.exploration) {
|
|
return context.json({ error: "No exploration state found" }, 400);
|
|
}
|
|
|
|
if (state.exploration.craftedRecipeIds.includes(recipeId)) {
|
|
return context.json({ error: "Recipe already crafted" }, 400);
|
|
}
|
|
|
|
// Verify the player has all required materials
|
|
for (const requirement of recipe.requiredMaterials) {
|
|
const material = state.exploration.materials.find((m) => {
|
|
return m.materialId === requirement.materialId;
|
|
});
|
|
const quantity = material?.quantity ?? 0;
|
|
if (quantity < requirement.quantity) {
|
|
return context.json(
|
|
{
|
|
error: `Not enough ${requirement.materialId} (need ${String(requirement.quantity)}, have ${String(quantity)})`,
|
|
},
|
|
400,
|
|
);
|
|
}
|
|
}
|
|
|
|
// Deduct materials
|
|
for (const requirement of recipe.requiredMaterials) {
|
|
const material = state.exploration.materials.find((m) => {
|
|
return m.materialId === requirement.materialId;
|
|
});
|
|
if (material) {
|
|
material.quantity = material.quantity - requirement.quantity;
|
|
}
|
|
}
|
|
|
|
// Add recipe and recompute all multipliers from scratch
|
|
state.exploration.craftedRecipeIds.push(recipeId);
|
|
const updatedMultipliers = recomputeCraftedMultipliers(
|
|
state.exploration.craftedRecipeIds,
|
|
);
|
|
state.exploration.craftedGoldMultiplier
|
|
= updatedMultipliers.craftedGoldMultiplier;
|
|
state.exploration.craftedEssenceMultiplier
|
|
= updatedMultipliers.craftedEssenceMultiplier;
|
|
state.exploration.craftedClickMultiplier
|
|
= updatedMultipliers.craftedClickMultiplier;
|
|
state.exploration.craftedCombatMultiplier
|
|
= updatedMultipliers.craftedCombatMultiplier;
|
|
|
|
await prisma.gameState.update({
|
|
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Prisma requires object */
|
|
data: { state: state as object, updatedAt: Date.now() },
|
|
where: { discordId },
|
|
});
|
|
|
|
const bonusType = recipe.bonus.type;
|
|
const bonusValue = recipe.bonus.value;
|
|
const response: CraftRecipeResponse = {
|
|
bonusType,
|
|
bonusValue,
|
|
recipeId,
|
|
...updatedMultipliers,
|
|
};
|
|
return context.json(response);
|
|
});
|
|
|
|
export { craftRouter };
|