habits.andr33v.ru/server/api/village/tick.post.ts

40 lines
1.3 KiB
TypeScript

// server/api/village/tick.post.ts
import { getAuthenticatedUserId } from '~/server/utils/auth';
import { processVillageTick } from '~/server/services/villageService';
/**
* This endpoint is called on every route change for an authenticated user.
* It's responsible for triggering the "tick" of the village simulation,
* which can include things like clearing tiles, generating resources, etc.
*/
export default defineEventHandler(async (event) => {
try {
const userId = getAuthenticatedUserId(event);
// Delegate the core logic to the villageService
const result = await processVillageTick(userId);
// The response can be simple, or return a meaningful state if the client needs to react.
// For now, a success message is sufficient.
return {
success: true,
data: result,
};
} catch (e: any) {
// If getAuthenticatedUserId throws, it will be a 401 error, which should be propagated.
if (e.statusCode === 401) {
throw e;
}
console.error('Error processing village tick:', e);
// For other errors, return a generic 500.
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error',
message: 'An unexpected error occurred while processing the village tick.',
});
}
});