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

46 lines
1.4 KiB
TypeScript

// server/api/village/action.post.ts
import { getUserIdFromSession } from '../../utils/auth';
import { buildOnTile, clearTile, moveObject, removeObject } from '../../services/villageService';
import { getVillageState } from '../../services/villageService';
export default defineEventHandler(async (event) => {
const userId = await getUserIdFromSession(event);
const body = await readBody(event);
const { tileId, actionType, payload } = body;
if (!tileId || !actionType) {
throw createError({ statusCode: 400, statusMessage: 'Missing tileId or actionType' });
}
switch (actionType) {
case 'BUILD':
if (!payload?.buildingType) {
throw createError({ statusCode: 400, statusMessage: 'Missing buildingType for BUILD action' });
}
await buildOnTile(userId, tileId, payload.buildingType);
break;
case 'CLEAR':
await clearTile(userId, tileId);
break;
case 'MOVE':
if (!payload?.toTileId) {
throw createError({ statusCode: 400, statusMessage: 'Missing toTileId for MOVE action' });
}
await moveObject(userId, tileId, payload.toTileId);
break;
case 'REMOVE':
await removeObject(userId, tileId);
break;
default:
throw createError({ statusCode: 400, statusMessage: 'Invalid actionType' });
}
// Return the full updated village state
return getVillageState(userId);
});