This commit is contained in:
cay 2026-03-29 13:22:35 +01:00
parent daa3a852c9
commit 07da6edcc1
2 changed files with 145 additions and 4 deletions

79
app.js
View File

@ -148,7 +148,84 @@ app.get("/api/building/:id", requireLogin, async (req, res) => {
nextLevelPoints: nextLevel[0]?.required_points || null,
description: info[0].description,
history: info[0].history,
upgradeCost: `${nextLevel[0]?.wood} Holz, ${nextLevel[0]?.stone} Stein, ${nextLevel[0]?.gold} Gold`,
upgradeCost: nextLevel[0]
? `${nextLevel[0].wood} Holz, ${nextLevel[0].stone} Stein, ${nextLevel[0].gold} Gold`
: "Max Level erreicht",
upgradeWood: nextLevel[0]?.wood ?? null,
upgradeStone: nextLevel[0]?.stone ?? null,
upgradeGold: nextLevel[0]?.gold ?? null,
});
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });
}
});
/* ========================
Route für Gebäude Upgrade
======================== */
app.post("/api/building/:id/upgrade", requireLogin, async (req, res) => {
const buildingId = req.params.id;
const userId = req.session.user.id;
try {
// Aktuelles Level holen
const [[userBuilding]] = await db.query(
"SELECT id, level FROM user_buildings WHERE user_id = ? AND building_id = ?",
[userId, buildingId],
);
if (!userBuilding) {
return res.status(404).json({ error: "Gebäude nicht gefunden" });
}
const nextLevel = userBuilding.level + 1;
// Upgrade-Kosten für nächstes Level holen
const [[levelData]] = await db.query(
"SELECT required_points, wood, stone, gold FROM building_levels WHERE building_id = ? AND level = ?",
[buildingId, nextLevel],
);
if (!levelData) {
return res.status(400).json({ error: "Maximales Level bereits erreicht" });
}
// Ressourcen des Spielers prüfen
const [[currency]] = await db.query(
"SELECT wood, stone, gold FROM account_currency WHERE account_id = ?",
[userId],
);
if (!currency) {
return res.status(400).json({ error: "Keine Währungsdaten gefunden" });
}
if (currency.wood < levelData.wood || currency.stone < levelData.stone || currency.gold < levelData.gold) {
return res.status(400).json({
error: "Nicht genügend Ressourcen",
required: { wood: levelData.wood, stone: levelData.stone, gold: levelData.gold },
current: { wood: currency.wood, stone: currency.stone, gold: currency.gold },
});
}
// Ressourcen abziehen
await db.query(
"UPDATE account_currency SET wood = wood - ?, stone = stone - ?, gold = gold - ? WHERE account_id = ?",
[levelData.wood, levelData.stone, levelData.gold, userId],
);
// Level erhöhen, Punkte zurücksetzen
await db.query(
"UPDATE user_buildings SET level = ?, points = 0 WHERE id = ?",
[nextLevel, userBuilding.id],
);
res.json({
success: true,
newLevel: nextLevel,
cost: { wood: levelData.wood, stone: levelData.stone, gold: levelData.gold },
});
} catch (err) {
console.error(err);

View File

@ -89,8 +89,19 @@ document.querySelectorAll(".building").forEach((building) => {
const buildingType = Number(data.type);
if (buildingModules[buildingType]) {
if (keepTabsVisible.has(buildingType)) {
// Tabs sichtbar lassen (z.B. Mine) — Info-Tab bleibt aktiv
// Tabs sichtbar lassen (z.B. Mine)
tabs.style.display = "flex";
// Aktionen-Tab aktivieren
document
.querySelectorAll(".tab")
.forEach((t) => t.classList.remove("active"));
document
.querySelectorAll(".tab-content")
.forEach((c) => c.classList.remove("active"));
document
.querySelector(".tab[data-tab='actions']")
.classList.add("active");
document.getElementById("tab-actions").classList.add("active");
} else {
// Tabs ausblenden, eigenes Voll-UI (Wohnhaus, Schwarzmarkt)
tabs.style.display = "none";
@ -102,8 +113,13 @@ document.querySelectorAll(".building").forEach((building) => {
document.getElementById("tab-upgrade").innerHTML = `
<div class="popup-info-title">Upgrade</div>
<div class="popup-stat-row"><span class="popup-stat-key">Kosten</span><span class="popup-stat-val">${data.upgradeCost}</span></div>
<button class="popup-upgrade-btn"> UPGRADE STARTEN </button>
<div class="popup-stat-row"><span class="popup-stat-key">Holz</span><span class="popup-stat-val">${data.upgradeWood ?? ""}</span></div>
<div class="popup-stat-row"><span class="popup-stat-key">Stein</span><span class="popup-stat-val">${data.upgradeStone ?? ""}</span></div>
<div class="popup-stat-row"><span class="popup-stat-key">Gold</span><span class="popup-stat-val">${data.upgradeGold ?? ""}</span></div>
<button class="popup-upgrade-btn" id="upgrade-btn" data-building="${url.split('/').pop()}"
${data.upgradeWood === null ? 'disabled style="opacity:0.4;cursor:not-allowed;"' : ''}>
UPGRADE STARTEN
</button>
`;
} catch (error) {
@ -139,6 +155,54 @@ document.querySelector(".popup-close").onclick = () => {
popup.classList.remove("active");
};
/* ================================
Upgrade Button
================================ */
document.addEventListener("click", async (e) => {
const btn = e.target.closest("#upgrade-btn");
if (!btn || btn.disabled) return;
const buildingId = btn.dataset.building;
btn.disabled = true;
btn.textContent = "Wird durchgeführt...";
try {
const res = await fetch("/api/building/" + buildingId + "/upgrade", {
method: "POST",
});
const data = await res.json();
if (!res.ok || data.error) {
window.showNotification(data.error || "Upgrade fehlgeschlagen.", "Upgrade", "⚒️");
btn.disabled = false;
btn.textContent = "⚒ UPGRADE STARTEN ⚒";
return;
}
window.showNotification(
`Upgrade erfolgreich!\nNeues Level: ${data.newLevel}\n\nKosten: ${data.cost.wood} Holz, ${data.cost.stone} Stein, ${data.cost.gold} Gold`,
"Upgrade",
"⚒️"
);
// Popup neu laden mit aktualisierten Daten
const building = document.querySelector(`.building[href="/building/${buildingId}"]`);
if (building) building.dispatchEvent(new MouseEvent("click", { bubbles: true }));
// HUD aktualisieren
import("/js/hud.js").then(({ loadHud }) => loadHud());
} catch (err) {
console.error("Upgrade Fehler:", err);
window.showNotification("Fehler beim Upgrade. Bitte erneut versuchen.", "Fehler", "⚠️");
btn.disabled = false;
btn.textContent = "⚒ UPGRADE STARTEN ⚒";
}
});
/* ================================
Tooltip
================================ */