返回提交历史
Modified
src/controllers/api/shipDecorationsController.ts
+8
-11
Modified
src/services/shipCustomizationsService.ts
+47
-1
Modified
src/types/personalRoomsTypes.ts
+11
-0
XFEstudio/XFESpaceNinjaServer
feat: reset decorations (#2516)
Closes #2514 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2516 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
2f642df2
代码差异
3 个文件
+66
-12
@@ -1,20 +1,17 @@
1
1
import { getAccountIdForRequest } from "@/src/services/loginService";
2
import { IShipDecorationsRequest } from "@/src/types/personalRoomsTypes";
3
import { logger } from "@/src/utils/logger";
2
import { IShipDecorationsRequest, IResetShipDecorationsRequest } from "@/src/types/personalRoomsTypes";
4
3
import { RequestHandler } from "express";
5
import { handleSetShipDecorations } from "@/src/services/shipCustomizationsService";
4
import { handleResetShipDecorations, handleSetShipDecorations } from "@/src/services/shipCustomizationsService";
6
5
7
6
export const shipDecorationsController: RequestHandler = async (req, res) => {
8
7
const accountId = await getAccountIdForRequest(req);
9
const shipDecorationsRequest = JSON.parse(req.body as string) as IShipDecorationsRequest;
10
11
try {
8
if (req.query.reset == "1") {
9
const request = JSON.parse(req.body as string) as IResetShipDecorationsRequest;
10
const response = await handleResetShipDecorations(accountId, request);
11
res.send(response);
12
} else {
13
const shipDecorationsRequest = JSON.parse(req.body as string) as IShipDecorationsRequest;
12
14
const placedDecoration = await handleSetShipDecorations(accountId, shipDecorationsRequest);
13
15
res.send(placedDecoration);
14
} catch (error: unknown) {
15
if (error instanceof Error) {
16
logger.error(`error in shipDecorationsController: ${error.message}`);
17
res.status(400).json({ error: error.message });
18
}
19
16
}
20
17
};
@@ -1,6 +1,8 @@
1
1
import { getPersonalRooms } from "@/src/services/personalRoomsService";
2
2
import { getShip } from "@/src/services/shipService";
3
3
import {
4
IResetShipDecorationsRequest,
5
IResetShipDecorationsResponse,
4
6
ISetPlacedDecoInfoRequest,
5
7
ISetShipCustomizationsRequest,
6
8
IShipDecorationsRequest,
@@ -154,7 +156,6 @@ export const handleSetShipDecorations = async (
154
156
155
157
if (!config.unlockAllShipDecorations) {
156
158
const inventory = await getInventory(accountId);
157
const itemType = Object.entries(ExportResources).find(arr => arr[1].deco == placedDecoration.Type)![0];
158
159
if (placedDecoration.Sockets !== undefined) {
159
160
addFusionTreasures(inventory, [{ ItemType: itemType, Sockets: placedDecoration.Sockets, ItemCount: -1 }]);
160
161
} else {
@@ -198,6 +199,51 @@ const getRoomsForBootLocation = (
198
199
return personalRooms.Ship.Rooms;
199
200
};
200
201
202
export const handleResetShipDecorations = async (
203
accountId: string,
204
request: IResetShipDecorationsRequest
205
): Promise<IResetShipDecorationsResponse> => {
206
const [personalRooms, inventory] = await Promise.all([getPersonalRooms(accountId), getInventory(accountId)]);
207
const room = getRoomsForBootLocation(personalRooms, request).find(room => room.Name === request.Room);
208
if (!room) {
209
throw new Error(`unknown room: ${request.Room}`);
210
}
211
212
for (const deco of room.PlacedDecos) {
213
const entry = Object.entries(ExportResources).find(arr => arr[1].deco == deco.Type);
214
if (!entry) {
215
throw new Error(`unknown deco type: ${deco.Type}`);
216
}
217
const [itemType, meta] = entry;
218
if (meta.capacityCost === undefined) {
219
throw new Error(`unknown deco type: ${deco.Type}`);
220
}
221
222
// refund item
223
if (!config.unlockAllShipDecorations) {
224
if (deco.Sockets !== undefined) {
225
addFusionTreasures(inventory, [{ ItemType: itemType, Sockets: deco.Sockets, ItemCount: 1 }]);
226
} else {
227
addShipDecorations(inventory, [{ ItemType: itemType, ItemCount: 1 }]);
228
}
229
}
230
231
// refund capacity
232
room.MaxCapacity += meta.capacityCost;
233
}
234
235
// empty room
236
room.PlacedDecos.splice(0, room.PlacedDecos.length);
237
238
await Promise.all([personalRooms.save(), inventory.save()]);
239
240
return {
241
ResetRoom: request.Room,
242
ClaimedDecos: [], // Not sure what this is for; the client already implies that the decos were returned to inventory.
243
NewCapacity: room.MaxCapacity
244
};
245
};
246
201
247
export const handleSetPlacedDecoInfo = async (accountId: string, req: ISetPlacedDecoInfoRequest): Promise<void> => {
202
248
if (req.GuildId && req.ComponentId) {
203
249
const guild = (await Guild.findById(req.GuildId))!;
@@ -150,6 +150,17 @@ export interface IShipDecorationsResponse {
150
150
NewRoom?: string;
151
151
}
152
152
153
export interface IResetShipDecorationsRequest {
154
Room: string;
155
BootLocation?: TBootLocation;
156
}
157
158
export interface IResetShipDecorationsResponse {
159
ResetRoom: string;
160
ClaimedDecos: [];
161
NewCapacity: number;
162
}
163
153
164
export interface ISetPlacedDecoInfoRequest {
154
165
DecoType: string;
155
166
DecoId: string;