XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

SpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 1 Star 0
返回提交历史

XFEstudio/SpaceNinjaServer

chore: improve setAccountCheat (#3693)

add key validation & opportunistically use Inventory.updateOne to save a roundtrip Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3693 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

f0214a50
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

2 个文件 +33 -8
Modified src/controllers/custom/setAccountCheatController.ts +31 -6
@@ -1,13 +1,26 @@
1 1 import { getInventory } from "../../services/inventoryService.ts";
2 2 import { getAccountIdForRequest } from "../../services/loginService.ts";
3 3 import { sendWsBroadcastEx, sendWsBroadcastTo } from "../../services/wsService.ts";
4 import type { IAccountCheats } from "../../types/inventoryTypes/inventoryTypes.ts";
4 import {
5 accountCheatBooleans,
6 accountCheatNumbers,
7 type IAccountCheats,
8 type TAccountCheatBooleanKey,
9 type TAccountCheatNumberKey
10 } from "../../types/inventoryTypes/inventoryTypes.ts";
5 11 import type { RequestHandler } from "express";
6 12 import { logger } from "../../utils/logger.ts";
7 13 import { lockCheats } from "../../services/cheatsService.ts";
14 import { Inventory } from "../../models/inventoryModels/inventoryModel.ts";
8 15
9 16 export const setAccountCheatController: RequestHandler = async (req, res) => {
10 17 const payload = req.body as ISetAccountCheatRequest;
18 if (
19 accountCheatBooleans.indexOf(payload.key as TAccountCheatBooleanKey) == -1 &&
20 accountCheatNumbers.indexOf(payload.key as TAccountCheatNumberKey) == -1
21 ) {
22 throw new Error(`unexpected setAccountCheat key: ${payload.key}`);
23 }
11 24 if (payload.value == undefined) {
12 25 logger.warn(`Aborting setting ${payload.key} as undefined!`);
13 26 return;
@@ -15,13 +28,25 @@ export const setAccountCheatController: RequestHandler = async (req, res) => {
15 28
16 29 const accountId = await getAccountIdForRequest(req);
17 30 const meta = payload.value ? lockCheats[payload.key] : undefined;
18 const inventory = await getInventory(accountId, meta ? `${payload.key} ${meta.projection}` : payload.key);
19 31
20 inventory[payload.key] = payload.value as never;
21 if (meta && !meta.isInventoryInIdealState(inventory)) {
22 res.send("retroactivable");
32 if (meta) {
33 const inventory = await getInventory(accountId, `${payload.key} ${meta.projection}`);
34 inventory[payload.key] = payload.value as never;
35 if (!meta.isInventoryInIdealState(inventory)) {
36 res.send("retroactivable");
37 }
38 await inventory.save();
39 } else {
40 await Inventory.updateOne(
41 {
42 accountOwnerId: accountId
43 },
44 {
45 [payload.key]: payload.value
46 }
47 );
23 48 }
24 await inventory.save();
49
25 50 res.end();
26 51 if (
27 52 [
Modified src/types/inventoryTypes/inventoryTypes.ts +2 -2
@@ -91,8 +91,8 @@ export const accountCheatNumbers = [
91 91 "nightwaveStandingMultiplier"
92 92 ] as const;
93 93
94 type TAccountCheatBooleanKey = (typeof accountCheatBooleans)[number];
95 type TAccountCheatNumberKey = (typeof accountCheatNumbers)[number];
94 export type TAccountCheatBooleanKey = (typeof accountCheatBooleans)[number];
95 export type TAccountCheatNumberKey = (typeof accountCheatNumbers)[number];
96 96
97 97 type IAccountCheatBooleans = {
98 98 [_ in TAccountCheatBooleanKey]: boolean | undefined;