diff --git a/src/controllers/api/shipDecorationsController.ts b/src/controllers/api/shipDecorationsController.ts index c56c0a8d..69c959a5 100644 --- a/src/controllers/api/shipDecorationsController.ts +++ b/src/controllers/api/shipDecorationsController.ts @@ -1,20 +1,17 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; -import { IShipDecorationsRequest } from "@/src/types/personalRoomsTypes"; -import { logger } from "@/src/utils/logger"; +import { IShipDecorationsRequest, IResetShipDecorationsRequest } from "@/src/types/personalRoomsTypes"; import { RequestHandler } from "express"; -import { handleSetShipDecorations } from "@/src/services/shipCustomizationsService"; +import { handleResetShipDecorations, handleSetShipDecorations } from "@/src/services/shipCustomizationsService"; export const shipDecorationsController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const shipDecorationsRequest = JSON.parse(req.body as string) as IShipDecorationsRequest; - - try { + if (req.query.reset == "1") { + const request = JSON.parse(req.body as string) as IResetShipDecorationsRequest; + const response = await handleResetShipDecorations(accountId, request); + res.send(response); + } else { + const shipDecorationsRequest = JSON.parse(req.body as string) as IShipDecorationsRequest; const placedDecoration = await handleSetShipDecorations(accountId, shipDecorationsRequest); res.send(placedDecoration); - } catch (error: unknown) { - if (error instanceof Error) { - logger.error(`error in shipDecorationsController: ${error.message}`); - res.status(400).json({ error: error.message }); - } } }; diff --git a/src/services/shipCustomizationsService.ts b/src/services/shipCustomizationsService.ts index 7edbdfde..ef5d7e8c 100644 --- a/src/services/shipCustomizationsService.ts +++ b/src/services/shipCustomizationsService.ts @@ -1,6 +1,8 @@ import { getPersonalRooms } from "@/src/services/personalRoomsService"; import { getShip } from "@/src/services/shipService"; import { + IResetShipDecorationsRequest, + IResetShipDecorationsResponse, ISetPlacedDecoInfoRequest, ISetShipCustomizationsRequest, IShipDecorationsRequest, @@ -154,7 +156,6 @@ export const handleSetShipDecorations = async ( if (!config.unlockAllShipDecorations) { const inventory = await getInventory(accountId); - const itemType = Object.entries(ExportResources).find(arr => arr[1].deco == placedDecoration.Type)![0]; if (placedDecoration.Sockets !== undefined) { addFusionTreasures(inventory, [{ ItemType: itemType, Sockets: placedDecoration.Sockets, ItemCount: -1 }]); } else { @@ -198,6 +199,51 @@ const getRoomsForBootLocation = ( return personalRooms.Ship.Rooms; }; +export const handleResetShipDecorations = async ( + accountId: string, + request: IResetShipDecorationsRequest +): Promise => { + const [personalRooms, inventory] = await Promise.all([getPersonalRooms(accountId), getInventory(accountId)]); + const room = getRoomsForBootLocation(personalRooms, request).find(room => room.Name === request.Room); + if (!room) { + throw new Error(`unknown room: ${request.Room}`); + } + + for (const deco of room.PlacedDecos) { + const entry = Object.entries(ExportResources).find(arr => arr[1].deco == deco.Type); + if (!entry) { + throw new Error(`unknown deco type: ${deco.Type}`); + } + const [itemType, meta] = entry; + if (meta.capacityCost === undefined) { + throw new Error(`unknown deco type: ${deco.Type}`); + } + + // refund item + if (!config.unlockAllShipDecorations) { + if (deco.Sockets !== undefined) { + addFusionTreasures(inventory, [{ ItemType: itemType, Sockets: deco.Sockets, ItemCount: 1 }]); + } else { + addShipDecorations(inventory, [{ ItemType: itemType, ItemCount: 1 }]); + } + } + + // refund capacity + room.MaxCapacity += meta.capacityCost; + } + + // empty room + room.PlacedDecos.splice(0, room.PlacedDecos.length); + + await Promise.all([personalRooms.save(), inventory.save()]); + + return { + ResetRoom: request.Room, + ClaimedDecos: [], // Not sure what this is for; the client already implies that the decos were returned to inventory. + NewCapacity: room.MaxCapacity + }; +}; + export const handleSetPlacedDecoInfo = async (accountId: string, req: ISetPlacedDecoInfoRequest): Promise => { if (req.GuildId && req.ComponentId) { const guild = (await Guild.findById(req.GuildId))!; diff --git a/src/types/personalRoomsTypes.ts b/src/types/personalRoomsTypes.ts index d418fafc..8a73fe29 100644 --- a/src/types/personalRoomsTypes.ts +++ b/src/types/personalRoomsTypes.ts @@ -150,6 +150,17 @@ export interface IShipDecorationsResponse { NewRoom?: string; } +export interface IResetShipDecorationsRequest { + Room: string; + BootLocation?: TBootLocation; +} + +export interface IResetShipDecorationsResponse { + ResetRoom: string; + ClaimedDecos: []; + NewCapacity: number; +} + export interface ISetPlacedDecoInfoRequest { DecoType: string; DecoId: string;