feat: reset decorations (#2516)
Closes #2514 Reviewed-on: #2516 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
parent
62314e89c7
commit
2f642df20a
@ -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 });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -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<IResetShipDecorationsResponse> => {
|
||||
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<void> => {
|
||||
if (req.GuildId && req.ComponentId) {
|
||||
const guild = (await Guild.findById(req.GuildId))!;
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user