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: simplify rngService (#1073)

getRandomWeightedReward now takes any object with lowercase 'rarity', and the only alternative to it is the 'uc' variant which takes any object with uppercase 'Rarity' usage of IRngResult is now also optional Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1073 Co-authored-by: Sainan <sainan@calamity.inc> Co-committed-by: Sainan <sainan@calamity.inc>

f3f1bfc8
Sainan <sainan@calamity.inc>
提交于

代码差异

5 个文件 +18 -39
Modified src/controllers/api/activateRandomModController.ts +4 -5
@@ -3,7 +3,7 @@ import { IRivenChallenge } from "@/src/helpers/rivenFingerprintHelper";
3 3 import { getJSONfromString } from "@/src/helpers/stringHelpers";
4 4 import { addMods, getInventory } from "@/src/services/inventoryService";
5 5 import { getAccountIdForRequest } from "@/src/services/loginService";
6 import { getRandomElement, getRandomInt, getRandomReward, IRngResult } from "@/src/services/rngService";
6 import { getRandomElement, getRandomInt, getRandomReward } from "@/src/services/rngService";
7 7 import { logger } from "@/src/utils/logger";
8 8 import { RequestHandler } from "express";
9 9 import { ExportUpgrades } from "warframe-public-export-plus";
@@ -26,15 +26,14 @@ export const activateRandomModController: RequestHandler = async (req, res) => {
26 26 Required: getRandomInt(challenge.countRange[0], challenge.countRange[1])
27 27 };
28 28 if (Math.random() < challenge.complicationChance) {
29 const complicationsAsRngResults: IRngResult[] = [];
29 const complications: { type: string; probability: number }[] = [];
30 30 for (const complication of challenge.complications) {
31 complicationsAsRngResults.push({
31 complications.push({
32 32 type: complication.fullName,
33 itemCount: 1,
34 33 probability: complication.weight
35 34 });
36 35 }
37 fingerprintChallenge.Complication = getRandomReward(complicationsAsRngResults)!.type;
36 fingerprintChallenge.Complication = getRandomReward(complications)!.type;
38 37 logger.debug(
39 38 `riven rolled challenge ${fingerprintChallenge.Type} with complication ${fingerprintChallenge.Complication}`
40 39 );
Modified src/controllers/api/dronesController.ts +2 -2
@@ -2,7 +2,7 @@ import { toMongoDate, toOid } from "@/src/helpers/inventoryHelpers";
2 2 import { config } from "@/src/services/configService";
3 3 import { addMiscItems, getInventory } from "@/src/services/inventoryService";
4 4 import { getAccountIdForRequest } from "@/src/services/loginService";
5 import { getRandomInt, getRandomWeightedReward3 } from "@/src/services/rngService";
5 import { getRandomInt, getRandomWeightedRewardUc } from "@/src/services/rngService";
6 6 import { IMongoDate, IOid } from "@/src/types/commonTypes";
7 7 import { IDroneClient } from "@/src/types/inventoryTypes/inventoryTypes";
8 8 import { IInventoryChanges } from "@/src/types/purchaseTypes";
@@ -56,7 +56,7 @@ export const dronesController: RequestHandler = async (req, res) => {
56 56 Math.random() < system.damageChance
57 57 ? getRandomInt(system.droneDamage.minValue, system.droneDamage.maxValue)
58 58 : 0;
59 const resource = getRandomWeightedReward3(system.resources, droneMeta.probabilities)!;
59 const resource = getRandomWeightedRewardUc(system.resources, droneMeta.probabilities)!;
60 60 //logger.debug(`drone rolled`, resource);
61 61 drone.ResourceType = "/Lotus/" + resource.StoreItem.substring(18);
62 62 const resourceMeta = ExportResources[drone.ResourceType];
Modified src/helpers/relicHelper.ts +2 -2
@@ -1,7 +1,7 @@
1 1 import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
2 2 import { IVoidTearParticipantInfo } from "@/src/types/requestTypes";
3 3 import { ExportRelics, ExportRewards, TRarity } from "warframe-public-export-plus";
4 import { getRandomWeightedReward2, IRngResult } from "@/src/services/rngService";
4 import { getRandomWeightedReward, IRngResult } from "@/src/services/rngService";
5 5 import { logger } from "@/src/utils/logger";
6 6 import { addMiscItems } from "@/src/services/inventoryService";
7 7 import { handleStoreItemAcquisition } from "@/src/services/purchaseService";
@@ -13,7 +13,7 @@ export const crackRelic = async (
13 13 const relic = ExportRelics[participant.VoidProjection];
14 14 const weights = refinementToWeights[relic.quality];
15 15 logger.debug(`opening a relic of quality ${relic.quality}; rarity weights are`, weights);
16 const reward = getRandomWeightedReward2(
16 const reward = getRandomWeightedReward(
17 17 ExportRewards[relic.rewardManifest][0] as { type: string; itemCount: number; rarity: TRarity }[], // rarity is nullable in PE+ typings, but always present for relics
18 18 weights
19 19 )!;
Modified src/services/purchaseService.ts +4 -4
@@ -8,7 +8,7 @@ import {
8 8 updateCurrency,
9 9 updateSlots
10 10 } from "@/src/services/inventoryService";
11 import { getRandomWeightedReward } from "@/src/services/rngService";
11 import { getRandomWeightedRewardUc } from "@/src/services/rngService";
12 12 import { getVendorManifestByOid } from "@/src/services/serversideVendorsService";
13 13 import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
14 14 import { IPurchaseRequest, IPurchaseResponse, SlotPurchase, IInventoryChanges } from "@/src/types/purchaseTypes";
@@ -325,14 +325,14 @@ const handleBoosterPackPurchase = async (
325 325 };
326 326 for (let i = 0; i != quantity; ++i) {
327 327 for (const weights of pack.rarityWeightsPerRoll) {
328 const result = getRandomWeightedReward(pack.components, weights);
328 const result = getRandomWeightedRewardUc(pack.components, weights);
329 329 if (result) {
330 330 logger.debug(`booster pack rolled`, result);
331 331 purchaseResponse.BoosterPackItems +=
332 result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};';
332 result.Item.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};';
333 333 combineInventoryChanges(
334 334 purchaseResponse.InventoryChanges,
335 (await addItem(inventory, result.type, result.itemCount)).InventoryChanges
335 (await addItem(inventory, result.Item, 1)).InventoryChanges
336 336 );
337 337 }
338 338 }
Modified src/services/rngService.ts +6 -26
@@ -34,45 +34,25 @@ export const getRandomReward = <T extends { probability: number }>(pool: T[]): T
34 34 throw new Error("What the fuck?");
35 35 };
36 36
37 export const getRandomWeightedReward = (
38 pool: { Item: string; Rarity: TRarity }[],
39 weights: Record<TRarity, number>
40 ): IRngResult | undefined => {
41 const resultPool: IRngResult[] = [];
42 const rarityCounts: Record<TRarity, number> = { COMMON: 0, UNCOMMON: 0, RARE: 0, LEGENDARY: 0 };
43 for (const entry of pool) {
44 ++rarityCounts[entry.Rarity];
45 }
46 for (const entry of pool) {
47 resultPool.push({
48 type: entry.Item,
49 itemCount: 1,
50 probability: weights[entry.Rarity] / rarityCounts[entry.Rarity]
51 });
52 }
53 return getRandomReward(resultPool);
54 };
55
56 export const getRandomWeightedReward2 = (
57 pool: { type: string; itemCount: number; rarity: TRarity }[],
37 export const getRandomWeightedReward = <T extends { rarity: TRarity }>(
38 pool: T[],
58 39 weights: Record<TRarity, number>
59 ): IRngResult | undefined => {
60 const resultPool: IRngResult[] = [];
40 ): (T & { probability: number }) | undefined => {
41 const resultPool: (T & { probability: number })[] = [];
61 42 const rarityCounts: Record<TRarity, number> = { COMMON: 0, UNCOMMON: 0, RARE: 0, LEGENDARY: 0 };
62 43 for (const entry of pool) {
63 44 ++rarityCounts[entry.rarity];
64 45 }
65 46 for (const entry of pool) {
66 47 resultPool.push({
67 type: entry.type,
68 itemCount: entry.itemCount,
48 ...entry,
69 49 probability: weights[entry.rarity] / rarityCounts[entry.rarity]
70 50 });
71 51 }
72 52 return getRandomReward(resultPool);
73 53 };
74 54
75 export const getRandomWeightedReward3 = <T extends { Rarity: TRarity }>(
55 export const getRandomWeightedRewardUc = <T extends { Rarity: TRarity }>(
76 56 pool: T[],
77 57 weights: Record<TRarity, number>
78 58 ): (T & { probability: number }) | undefined => {