dok/routes/himmelstor.route.js
2026-04-14 07:50:14 +01:00

113 lines
3.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ============================================================
routes/himmelstor.route.js
GET /himmelstor/daily Tagesherausforderung Spielfeld
GET /himmelstor/weekly Wochenherausforderung Spielfeld
============================================================ */
const express = require("express");
const router = express.Router();
const db = require("../database/database");
function requireLogin(req, res, next) {
if (!req.session?.user) return res.status(401).json({ error: "Nicht eingeloggt" });
next();
}
/* HP-Formel: 20 + (level-1)*2 */
function calcAvatarHp(level) {
return 20 + (Math.max(1, Math.min(50, level || 1)) - 1) * 2;
}
async function getPlayerHp(userId) {
try {
const [[acc]] = await db.query("SELECT level FROM accounts WHERE id = ?", [userId]);
return calcAvatarHp(acc?.level ?? 1);
} catch {
return 20;
}
}
/* ── GET /himmelstor/daily ─────────────────────────────── */
router.get("/daily", requireLogin, async (req, res) => {
const userId = req.session.user.id;
const { match: matchId, slot } = req.query;
if (!matchId) {
return res.render("1v1-battlefield", {
title: "☀️ Daily Herausforderung",
matchId: null,
mySlot: "player1",
player1: req.session?.user?.ingame_name || "Spieler 1",
player2: "Gegner",
player1hp: 20,
player1mana: 3,
player2hp: 20,
player2mana: 3,
});
}
try {
const [[me]] = await db.query("SELECT ingame_name FROM accounts WHERE id = ?", [userId]);
const hp = await getPlayerHp(userId);
const isP1 = slot === "player1";
res.render("1v1-battlefield", {
title: "☀️ Daily Herausforderung",
matchId,
mySlot: slot || "player1",
player1: isP1 ? (me?.ingame_name || "Du") : "Gegner",
player2: isP1 ? "Gegner" : (me?.ingame_name || "Du"),
player1hp: isP1 ? hp : 20,
player1mana: 3,
player2hp: isP1 ? 20 : hp,
player2mana: 3,
});
} catch (err) {
console.error("[Himmelstor /daily]", err);
res.status(500).send("Fehler beim Laden des Spielfelds.");
}
});
/* ── GET /himmelstor/weekly ────────────────────────────── */
router.get("/weekly", requireLogin, async (req, res) => {
const userId = req.session.user.id;
const { match: matchId, slot } = req.query;
if (!matchId) {
return res.render("1v1-battlefield", {
title: "🌙 Weekly Herausforderung",
matchId: null,
mySlot: "player1",
player1: req.session?.user?.ingame_name || "Spieler 1",
player2: "Gegner",
player1hp: 20,
player1mana: 3,
player2hp: 20,
player2mana: 3,
});
}
try {
const [[me]] = await db.query("SELECT ingame_name FROM accounts WHERE id = ?", [userId]);
const hp = await getPlayerHp(userId);
const isP1 = slot === "player1";
res.render("1v1-battlefield", {
title: "🌙 Weekly Herausforderung",
matchId,
mySlot: slot || "player1",
player1: isP1 ? (me?.ingame_name || "Du") : "Gegner",
player2: isP1 ? "Gegner" : (me?.ingame_name || "Du"),
player1hp: isP1 ? hp : 20,
player1mana: 3,
player2hp: isP1 ? 20 : hp,
player2mana: 3,
});
} catch (err) {
console.error("[Himmelstor /weekly]", err);
res.status(500).send("Fehler beim Laden des Spielfelds.");
}
});
module.exports = router;