minen update 5

This commit is contained in:
Cay 2026-03-16 14:09:05 +00:00
parent ca71c379a6
commit 02f2369cd6

View File

@ -12,12 +12,12 @@ const auth = require("../middleware/auth");
async function ensureTimer(userBuildingId) {
const [[existing]] = await db.query(
"SELECT last_collected FROM building_collect_timer WHERE user_building_id = ?",
[userBuildingId]
[userBuildingId],
);
if (!existing) {
await db.query(
"INSERT INTO building_collect_timer (user_building_id, last_collected) VALUES (?, NOW())",
[userBuildingId]
[userBuildingId],
);
}
}
@ -45,7 +45,7 @@ async function loadMineData(userId, buildingId) {
WHERE ub.user_id = ?
AND ub.building_id = ?
`,
[userId, buildingId]
[userId, buildingId],
);
return rows;
}
@ -61,7 +61,7 @@ router.get("/:buildingId/status", auth, async (req, res) => {
// user_building holen
const [[userBuilding]] = await db.query(
"SELECT id, level FROM user_buildings WHERE user_id = ? AND building_id = ?",
[userId, buildingId]
[userId, buildingId],
);
if (!userBuilding) {
@ -78,7 +78,9 @@ router.get("/:buildingId/status", auth, async (req, res) => {
const { cycle_seconds, last_collected, level } = rows[0];
const elapsed = Math.floor((Date.now() - new Date(last_collected).getTime()) / 1000);
const elapsed = Math.floor(
(Date.now() - new Date(last_collected).getTime()) / 1000,
);
const cycles = Math.floor(elapsed / cycle_seconds);
const nextIn = cycle_seconds - (elapsed % cycle_seconds);
@ -102,7 +104,6 @@ router.get("/:buildingId/status", auth, async (req, res) => {
next_cycle_in_seconds: nextIn,
cycle_seconds,
});
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });
@ -121,7 +122,7 @@ router.post("/:buildingId/collect", auth, async (req, res) => {
// user_building holen
const [[userBuilding]] = await db.query(
"SELECT id FROM user_buildings WHERE user_id = ? AND building_id = ?",
[userId, buildingId]
[userId, buildingId],
);
if (!userBuilding) {
@ -138,7 +139,9 @@ router.post("/:buildingId/collect", auth, async (req, res) => {
const { user_building_id, cycle_seconds, last_collected } = rows[0];
const elapsed = Math.floor((Date.now() - new Date(last_collected).getTime()) / 1000);
const elapsed = Math.floor(
(Date.now() - new Date(last_collected).getTime()) / 1000,
);
const cycles = Math.floor(elapsed / cycle_seconds);
if (cycles < 1) {
@ -152,28 +155,37 @@ router.post("/:buildingId/collect", auth, async (req, res) => {
});
}
// ── Alle Ressourcen atomar in EINEM Query schreiben ──────────────
// Verhindert inkonsistente Zustände bei Verbindungsabbrüchen
const setClauses = rows
.map((r) => `\`${r.resource}\` = \`${r.resource}\` + ${r.amount * cycles}`)
.join(", ");
// Jede Ressource einzeln gutschreiben
const allowedResources = [
"gold",
"silver",
"copper",
"iron",
"wood",
"stone",
"gems",
];
for (const row of rows) {
if (!allowedResources.includes(row.resource)) continue;
const toAdd = row.amount * cycles;
await db.query(
`INSERT INTO account_currency (account_id)
VALUES (?)
ON DUPLICATE KEY UPDATE ${setClauses}`,
[userId]
`UPDATE account_currency SET \`${row.resource}\` = \`${row.resource}\` + ? WHERE account_id = ?`,
[toAdd, userId],
);
}
// Timer zuruecksetzen: last_collected um genau die abgeschlossenen
// Zyklen vorruecken Restsekunden bleiben erhalten, kein Verlust
const newLastCollected = new Date(
new Date(last_collected).getTime() + cycles * cycle_seconds * 1000
new Date(last_collected).getTime() + cycles * cycle_seconds * 1000,
);
await db.query(
"UPDATE building_collect_timer SET last_collected = ? WHERE user_building_id = ?",
[newLastCollected, user_building_id]
[newLastCollected, user_building_id],
);
const collected = rows.map((r) => ({
@ -182,7 +194,6 @@ router.post("/:buildingId/collect", auth, async (req, res) => {
}));
res.json({ success: true, cycles, collected });
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });