habits.andr33v.ru/server/api/admin/village/trigger-tick.post.ts

50 lines
1.5 KiB
TypeScript

// server/api/admin/village/trigger-tick.post.ts
import { getUserIdFromSession } from '../../../utils/auth';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
// This is a simplified constant. In a real scenario, this might be shared from a single source.
const CLEANING_TIME_MS = 24 * 60 * 60 * 1000; // 24 hours
export default defineEventHandler(async (event) => {
const userId = await getUserIdFromSession(event);
// Simple admin check
if (userId !== 1) {
throw createError({ statusCode: 403, statusMessage: 'Forbidden' });
}
const village = await prisma.village.findUniqueOrThrow({ where: { userId } });
const now = Date.now();
const yesterday = new Date(now - 24 * 60 * 60 * 1000);
const clearingFastForwardDate = new Date(now - CLEANING_TIME_MS + 5000); // 5 seconds past completion
await prisma.$transaction([
// 1. Fast-forward any tiles that are currently being cleared
prisma.villageTile.updateMany({
where: {
villageId: village.id,
terrainState: 'CLEARING',
},
data: {
clearingStartedAt: clearingFastForwardDate,
},
}),
// 2. Fast-forward any fields to be ready for EXP gain
prisma.villageObject.updateMany({
where: {
villageId: village.id,
type: 'FIELD',
},
data: {
lastExpAt: yesterday,
},
}),
]);
return { success: true, message: 'Clearing and Field timers have been fast-forwarded.' };
});