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

refactor: don't pass undefined to getRandomMissionRewards (#913)

Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/913 Co-authored-by: Ordis <134585663+OrdisPrime@users.noreply.github.com> Co-committed-by: Ordis <134585663+OrdisPrime@users.noreply.github.com>

0fbf300d
Ordis <134585663+OrdisPrime@users.noreply.github.com>
提交于

代码差异

2 个文件 +38 -37
Modified src/controllers/api/missionInventoryUpdateController.ts +4 -18
@@ -2,11 +2,7 @@ import { RequestHandler } from "express";
2 2 import { getJSONfromString } from "@/src/helpers/stringHelpers";
3 3 import { getAccountIdForRequest } from "@/src/services/loginService";
4 4 import { IMissionInventoryUpdateRequest } from "@/src/types/requestTypes";
5 import {
6 addMissionInventoryUpdates,
7 addMissionRewards,
8 calculateFinalCredits
9 } from "@/src/services/missionInventoryUpdateService";
5 import { addMissionInventoryUpdates, addMissionRewards } from "@/src/services/missionInventoryUpdateService";
10 6 import { getInventory } from "@/src/services/inventoryService";
11 7 import { getInventoryResponse } from "./inventoryController";
12 8
@@ -49,7 +45,7 @@ import { getInventoryResponse } from "./inventoryController";
49 45 - [ ] FpsMax
50 46 - [ ] FpsSamples
51 47 */
52
48 //move credit calc in here, return MissionRewards: [] if no reward info
53 49 // eslint-disable-next-line @typescript-eslint/no-misused-promises
54 50 export const missionInventoryUpdateController: RequestHandler = async (req, res): Promise<void> => {
55 51 const accountId = await getAccountIdForRequest(req);
@@ -67,18 +63,8 @@ export const missionInventoryUpdateController: RequestHandler = async (req, res)
67 63 });
68 64 return;
69 65 }
70 const missionRewardsResults = await addMissionRewards(inventory, missionReport);
71 if (!missionRewardsResults) {
72 throw new Error("Failed to add mission rewards, should not happen");
73 }
74 const { MissionRewards, inventoryChanges, missionCompletionCredits } = missionRewardsResults;
75 66
76 //creditBonus is not correct for mirage mission 3
77 const credits = calculateFinalCredits(inventory, {
78 missionCompletionCredits,
79 missionDropCredits: missionReport.RegularCredits ?? 0,
80 rngRewardCredits: inventoryChanges.RegularCredits as number
81 });
67 const { MissionRewards, inventoryChanges, credits } = await addMissionRewards(inventory, missionReport);
82 68
83 69 await inventory.save();
84 70 const inventoryResponse = await getInventoryResponse(inventory, true);
@@ -90,7 +76,7 @@ export const missionInventoryUpdateController: RequestHandler = async (req, res)
90 76 MissionRewards,
91 77 ...credits,
92 78 ...inventoryUpdates,
93 FusionPoints: inventoryChanges.FusionPoints
79 FusionPoints: inventoryChanges?.FusionPoints
94 80 });
95 81 };
96 82
Modified src/services/missionInventoryUpdateService.ts +34 -19
@@ -200,10 +200,17 @@ export const addMissionInventoryUpdates = (
200 200 //TODO: return type of partial missioninventoryupdate response
201 201 export const addMissionRewards = async (
202 202 inventory: TInventoryDatabaseDocument,
203 { RewardInfo: rewardInfo, LevelKeyName: levelKeyName, Missions: missions }: IMissionInventoryUpdateRequest
203 {
204 RewardInfo: rewardInfo,
205 LevelKeyName: levelKeyName,
206 Missions: missions,
207 RegularCredits: creditDrops
208 }: IMissionInventoryUpdateRequest
204 209 ) => {
205 210 if (!rewardInfo) {
206 logger.warn("no reward info provided");
211 //TODO: if there is a case where you can have credits collected during a mission but no rewardInfo, add credits needs to be handled earlier
212 logger.debug(`Mission ${missions!.Tag} did not have Reward Info `);
213 return { MissionRewards: [] };
207 214 }
208 215
209 216 //TODO: check double reward merging
@@ -214,11 +221,12 @@ export const addMissionRewards = async (
214 221 const inventoryChanges: IInventoryChanges = {};
215 222
216 223 let missionCompletionCredits = 0;
217 //inventory change is what the client has not rewarded itself, credit updates seem to be taken from totalCredits
224 //inventory change is what the client has not rewarded itself, also the client needs to know the credit changes for display
218 225 if (levelKeyName) {
219 226 const fixedLevelRewards = getLevelKeyRewards(levelKeyName);
220 227 //logger.debug(`fixedLevelRewards ${fixedLevelRewards}`);
221 228 for (const reward of fixedLevelRewards) {
229 //quest stage completion credit rewards
222 230 if (reward.rewardType == "RT_CREDITS") {
223 231 inventory.RegularCredits += reward.amount;
224 232 missionCompletionCredits += reward.amount;
@@ -231,6 +239,16 @@ export const addMissionRewards = async (
231 239 }
232 240 }
233 241
242 for (const reward of MissionRewards) {
243 //TODO: additem should take in storeItems
244 const inventoryChange = await addItem(inventory, reward.StoreItem.replace("StoreItems/", ""), reward.ItemCount);
245 //TODO: combineInventoryChanges improve type safety, merging 2 of the same item?
246 //TODO: check for the case when two of the same item are added, combineInventoryChanges should merge them, but the client also merges them
247 //TODO: some conditional types to rule out binchanges?
248 combineInventoryChanges(inventoryChanges, inventoryChange.InventoryChanges);
249 }
250
251 //node based credit rewards for mission completion
234 252 if (missions) {
235 253 const node = getNode(missions.Tag);
236 254
@@ -242,27 +260,24 @@ export const addMissionRewards = async (
242 260 }
243 261 }
244 262
245 //TODO: resolve issue with creditbundles
246 for (const reward of MissionRewards) {
247 //TODO: additem should take in storeItems
248 const inventoryChange = await addItem(inventory, reward.StoreItem.replace("StoreItems/", ""), reward.ItemCount);
249 //TODO: combineInventoryChanges improve type safety, merging 2 of the same item?
250 //TODO: check for the case when two of the same item are added, combineInventoryChanges should merge them
251 //TODO: some conditional types to rule out binchanges?
252 combineInventoryChanges(inventoryChanges, inventoryChange.InventoryChanges);
253 }
263 //creditBonus is not correct for mirage mission 3
264 const credits = addCredits(inventory, {
265 missionCompletionCredits,
266 missionDropCredits: creditDrops ?? 0,
267 rngRewardCredits: inventoryChanges.RegularCredits ?? 0
268 });
254 269
255 return { inventoryChanges, MissionRewards, missionCompletionCredits };
270 return { inventoryChanges, MissionRewards, credits };
256 271 };
257 272
258 //might not be faithful to original
273 //slightly inaccurate compared to official
259 274 //TODO: consider ActiveBoosters
260 export const calculateFinalCredits = (
275 export const addCredits = (
261 276 inventory: HydratedDocument<IInventoryDatabase>,
262 277 {
263 278 missionDropCredits,
264 279 missionCompletionCredits,
265 rngRewardCredits = 0
280 rngRewardCredits
266 281 }: { missionDropCredits: number; missionCompletionCredits: number; rngRewardCredits: number }
267 282 ) => {
268 283 const hasDailyCreditBonus = true;
@@ -275,7 +290,7 @@ export const calculateFinalCredits = (
275 290 };
276 291
277 292 if (hasDailyCreditBonus) {
278 inventory.RegularCredits += totalCredits;
293 inventory.RegularCredits += missionCompletionCredits;
279 294 finalCredits.CreditBonus[1] *= 2;
280 295 finalCredits.MissionCredits[1] *= 2;
281 296 finalCredits.TotalCredits[1] *= 2;
@@ -295,9 +310,9 @@ function getLevelCreditRewards(nodeName: string): number {
295 310 //TODO: get dark sektor fixed credit rewards and railjack bonus
296 311 }
297 312
298 function getRandomMissionDrops(RewardInfo: IRewardInfo | undefined): IRngResult[] {
313 function getRandomMissionDrops(RewardInfo: IRewardInfo): IRngResult[] {
299 314 const drops: IRngResult[] = [];
300 if (RewardInfo && RewardInfo.node in ExportRegions) {
315 if (RewardInfo.node in ExportRegions) {
301 316 const region = ExportRegions[RewardInfo.node];
302 317 const rewardManifests = region.rewardManifests ?? [];
303 318