adding loadoutSlots, purchasing slots,

This commit is contained in:
Ordis 2023-12-28 16:22:13 +01:00
parent f197facd72
commit e7db0147ec
9 changed files with 209 additions and 75 deletions

View File

@ -1,24 +1,36 @@
import { parseString } from "@/src/helpers/general";
import { getInventory } from "@/src/services/inventoryService";
import { getInventory, updateCurrency } from "@/src/services/inventoryService";
import { RequestHandler } from "express";
import { updateSlots } from "@/src/services/inventoryService";
import { SlotNameToInventoryName } from "@/src/types/purchaseTypes";
export interface IInventoryChanges {
InventoryChanges: { [key: string]: unknown };
}
/*
loadout slots are additionally purchased slots only
1 slot per mastery rank is automatically given above mr10, without database needing to save the mastery slots
extra = everything above the base + 2 slots (e.g. for warframes)
new slot = extra + 1 and slots +1
using slot = slots -1, except for when purchased with platinum, then slots are included in price
export interface IInventorySlotsRequest {
Bin: "PveBonusLoadoutBin";
}
e.g. number of frames:
19 slots, 71 extra
= 71 - 19 + 2 = 54
19 actually available slots in ingame inventory = 17 extra + 2 Base (base amount depends on slot) (+ 1 for every mastery rank above 10)
number of frames = extra - slots + 2
*/
// eslint-disable-next-line @typescript-eslint/no-misused-promises
export const inventorySlotsController: RequestHandler = async (req, res) => {
const accountId = parseString(req.query.accountId);
//const body = req.body as IInventorySlotsRequest;
//const body = JSON.parse(req.body as string) as IInventorySlotsRequest;
//check which slot was purchased
//console.log(body);
const inventory = await getInventory(accountId);
//TODO: check which slot was purchased because pvpBonus is also possible
//add slots
res.json({ InventoryChanges: { PremiumCreditsFree: -20, PremiumCredits: -20 } } satisfies IInventoryChanges);
const currencyChanges = await updateCurrency(-20, true, accountId);
await updateSlots(accountId, SlotNameToInventoryName.LOADOUT, 1, 1);
//console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");
res.json({ InventoryChanges: currencyChanges });
};

View File

@ -3,11 +3,9 @@ import { toPurchaseRequest } from "@/src/helpers/purchaseHelpers";
import { handlePurchase } from "@/src/services/purchaseService";
import { Request, Response } from "express";
const purchaseController = async (req: Request, res: Response) => {
export const purchaseController = async (req: Request, res: Response) => {
const purchaseRequest = toPurchaseRequest(JSON.parse(String(req.body)));
const accountId = parseString(req.query.accountId);
const response = await handlePurchase(purchaseRequest, accountId);
res.json(response);
};
export { purchaseController };

View File

@ -1,9 +1,10 @@
import { parseBoolean, parseNumber, parseString } from "@/src/helpers/general";
import { WeaponTypeInternal } from "@/src/services/inventoryService";
import { IPurchaseRequest } from "@/src/types/purchaseTypes";
import { slotPurchaseNameToSlotName } from "@/src/services/purchaseService";
import { IPurchaseRequest, SlotPurchaseName } from "@/src/types/purchaseTypes";
import { weapons } from "@/static/data/items";
const toPurchaseRequest = (purchaseRequest: unknown): IPurchaseRequest => {
export const toPurchaseRequest = (purchaseRequest: unknown): IPurchaseRequest => {
if (!purchaseRequest || typeof purchaseRequest !== "object") {
throw new Error("incorrect or missing purchase request data");
}
@ -40,7 +41,7 @@ const toPurchaseRequest = (purchaseRequest: unknown): IPurchaseRequest => {
throw new Error("invalid purchaseRequest");
};
const getWeaponType = (weaponName: string) => {
export const getWeaponType = (weaponName: string) => {
const weaponInfo = weapons.find(i => i.uniqueName === weaponName);
if (!weaponInfo) {
@ -56,4 +57,11 @@ const getWeaponType = (weaponName: string) => {
return weaponType;
};
export { toPurchaseRequest, getWeaponType };
export const isSlotPurchaseName = (slotPurchaseName: string): slotPurchaseName is SlotPurchaseName => {
return slotPurchaseName in slotPurchaseNameToSlotName;
};
export const parseSlotPurchaseName = (slotPurchaseName: string) => {
if (!isSlotPurchaseName(slotPurchaseName)) throw new Error(`invalid slot name ${slotPurchaseName}`);
return slotPurchaseName;
};

View File

@ -2,9 +2,9 @@ import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
import new_inventory from "@/static/fixed_responses/postTutorialInventory.json";
import config from "@/config.json";
import { Types } from "mongoose";
import { ISuitDatabase, ISuitClient } from "@/src/types/inventoryTypes/SuitTypes";
import { SlotType } from "@/src/types/purchaseTypes";
import { IWeaponDatabase, IWeaponClient } from "@/src/types/inventoryTypes/weaponTypes";
import { ISuitClient } from "@/src/types/inventoryTypes/SuitTypes";
import { SlotNames } from "@/src/types/purchaseTypes";
import { IWeaponClient } from "@/src/types/inventoryTypes/weaponTypes";
import {
IChallengeProgress,
IConsumable,
@ -41,8 +41,6 @@ export const createInventory = async (accountOwnerId: Types.ObjectId, loadOutPre
}
};
//const updateInventory = async (accountOwnerId: Types.ObjectId, inventoryChanges: any) => {};
export const getInventory = async (accountOwnerId: string) => {
const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId });
@ -53,7 +51,7 @@ export const getInventory = async (accountOwnerId: string) => {
return inventory;
};
//TODO: genericMethod for all the add methods, they share a lot of logic
//TODO: maybe genericMethod for all the add methods, they share a lot of logic
export const addSentinel = async (sentinelName: string, accountId: string) => {
const inventory = await getInventory(accountId);
const sentinelIndex = inventory.Sentinels.push({ ItemType: sentinelName, Configs: [], XP: 0 });
@ -75,32 +73,54 @@ export const addMechSuit = async (mechsuitName: string, accountId: string) => {
return changedInventory.MechSuits[suitIndex - 1].toJSON();
};
export const updateSlots = async (slotType: SlotType, accountId: string, slots: number) => {
export const updateSlots = async (accountId: string, slotName: SlotNames, slotAmount: number, extraAmount: number) => {
const inventory = await getInventory(accountId);
switch (slotType) {
case SlotType.SUIT:
inventory.SuitBin.Slots += slots;
break;
case SlotType.WEAPON:
inventory.WeaponBin.Slots += slots;
break;
case SlotType.MECHSUIT:
inventory.MechBin.Slots += slots;
break;
default:
throw new Error("invalid slot type");
inventory[slotName].Slots += slotAmount;
if (inventory[slotName].Extra === undefined) {
inventory[slotName].Extra = extraAmount;
} else {
inventory[slotName].Extra += extraAmount;
}
await inventory.save();
};
export const updateCurrency = async (price: number, usePremium: boolean, accountId: string) => {
const currencyName = usePremium ? "PremiumCredits" : "RegularCredits";
const inventory = await getInventory(accountId);
inventory[currencyName] = inventory[currencyName] - price;
if (usePremium) {
if (inventory.PremiumCreditsFree > 0) {
inventory.PremiumCreditsFree += price;
}
inventory.PremiumCredits += price;
} else {
inventory.RegularCredits += price;
}
const modifiedPaths = inventory.modifiedPaths();
type currencyKeys = "RegularCredits" | "PremiumCredits" | "PremiumCreditsFree";
const currencyChanges = {} as Record<currencyKeys, number>;
modifiedPaths.forEach(path => {
currencyChanges[path as currencyKeys] = -price;
});
console.log(currencyChanges, "changes");
//let changes = {} as keyof currencyKeys;
// const obj2 = modifiedPaths.reduce(
// (obj, key) => {
// obj[key as keyof currencyKeys] = price;
// return obj;
// },
// {} as Record<keyof currencyKeys, number>
// );
await inventory.save();
return { [currencyName]: -price };
return currencyChanges;
};
// TODO: AffiliationMods support (Nightwave).
@ -157,7 +177,7 @@ export const addCustomization = async (customizatonName: string, accountId: stri
const flavourItemIndex = inventory.FlavourItems.push({ ItemType: customizatonName }) - 1;
const changedInventory = await inventory.save();
return changedInventory.FlavourItems[flavourItemIndex].toJSON(); //mongoose bug forces as FlavourItem
return changedInventory.FlavourItems[flavourItemIndex].toJSON();
};
const addGearExpByCategory = (

View File

@ -1,4 +1,4 @@
import { getWeaponType } from "@/src/helpers/purchaseHelpers";
import { getWeaponType, parseSlotPurchaseName } from "@/src/helpers/purchaseHelpers";
import { getSubstringFromKeyword } from "@/src/helpers/stringHelpers";
import {
addBooster,
@ -7,9 +7,10 @@ import {
addPowerSuit,
addSentinel,
addWeapon,
updateCurrency,
updateSlots
} from "@/src/services/inventoryService";
import { IPurchaseRequest, SlotType } from "@/src/types/purchaseTypes";
import { IPurchaseRequest, IPurchaseResponse, SlotNameToInventoryName, SlotPurchase } from "@/src/types/purchaseTypes";
export const getStoreItemCategory = (storeItem: string) => {
const storeItemString = getSubstringFromKeyword(storeItem, "StoreItems/");
@ -32,57 +33,109 @@ export const handlePurchase = async (purchaseRequest: IPurchaseRequest, accountI
const internalName = purchaseRequest.PurchaseParams.StoreItem.replace("/StoreItems", "");
console.log("Store category", storeCategory);
let purchaseResponse;
let inventoryChanges;
switch (storeCategory) {
case "Powersuits":
purchaseResponse = await handlePowersuitPurchase(internalName, accountId);
inventoryChanges = await handlePowersuitPurchase(internalName, accountId);
break;
case "Weapons":
purchaseResponse = await handleWeaponsPurchase(internalName, accountId);
inventoryChanges = await handleWeaponsPurchase(internalName, accountId);
break;
case "Types":
purchaseResponse = await handleTypesPurchase(internalName, accountId);
inventoryChanges = await handleTypesPurchase(internalName, accountId);
break;
case "Boosters":
purchaseResponse = await handleBoostersPurchase(internalName, accountId);
inventoryChanges = await handleBoostersPurchase(internalName, accountId);
break;
default:
throw new Error(`unknown store category: ${storeCategory} not implemented or new`);
}
// const currencyResponse = await updateCurrency(
// purchaseRequest.PurchaseParams.ExpectedPrice,
// purchaseRequest.PurchaseParams.UsePremium,
// accountId
// );
if (!inventoryChanges) throw new Error("purchase response was undefined");
// (purchaseResponse as IPurchaseResponse).InventoryChanges = {
// ...purchaseResponse.InventoryChanges,
// ...currencyResponse
// };
const currencyChanges = await updateCurrency(
purchaseRequest.PurchaseParams.ExpectedPrice,
purchaseRequest.PurchaseParams.UsePremium,
accountId
);
return purchaseResponse;
inventoryChanges.InventoryChanges = {
...currencyChanges,
...inventoryChanges.InventoryChanges
};
return inventoryChanges;
};
export const slotPurchaseNameToSlotName: SlotPurchase = {
SuitSlotItem: { name: "SuitBin", slotsPerPurchase: 1 },
TwoSentinelSlotItem: { name: "SentinelBin", slotsPerPurchase: 2 },
TwoWeaponSlotItem: { name: "WeaponBin", slotsPerPurchase: 2 },
SpaceSuitSlotItem: { name: "SpaceSuitBin", slotsPerPurchase: 1 },
TwoSpaceWeaponSlotItem: { name: "SpaceWeaponBin", slotsPerPurchase: 2 },
MechSlotItem: { name: "MechBin", slotsPerPurchase: 1 },
TwoOperatorWeaponSlotItem: { name: "OperatorAmpBin", slotsPerPurchase: 2 },
RandomModSlotItem: { name: "RandomModBin", slotsPerPurchase: 3 },
TwoCrewShipSalvageSlotItem: { name: "CrewShipSalvageBin", slotsPerPurchase: 2 },
CrewMemberSlotItem: { name: "CrewMemberBin", slotsPerPurchase: 1 }
};
// // extra = everything above the base +2 slots (depending on slot type)
// // new slot above base = extra + 1 and slots +1
// // new frame = slots -1
// // number of frames = extra - slots + 2
const handleSlotPurchase = async (slotPurchaseNameFull: string, accountId: string) => {
console.log("slot name", slotPurchaseNameFull);
const slotPurchaseName = parseSlotPurchaseName(
slotPurchaseNameFull.substring(slotPurchaseNameFull.lastIndexOf("/") + 1)
);
console.log(slotPurchaseName, "slot purchase name");
await updateSlots(
accountId,
slotPurchaseNameToSlotName[slotPurchaseName].name,
slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase,
slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase
);
console.log(
slotPurchaseNameToSlotName[slotPurchaseName].name,
slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase,
"slots added"
);
return {
InventoryChanges: {
[slotPurchaseNameToSlotName[slotPurchaseName].name]: {
count: 0,
platinum: 1,
Slots: slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase,
Extra: slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase
}
}
};
};
const handleWeaponsPurchase = async (weaponName: string, accountId: string) => {
const weaponType = getWeaponType(weaponName);
const addedWeapon = await addWeapon(weaponType, weaponName, accountId);
await updateSlots(SlotType.WEAPON, accountId, -1);
await updateSlots(accountId, SlotNameToInventoryName.WEAPON, 0, 1);
return {
InventoryChanges: {
WeaponBin: { count: 1, platinum: 0, Slots: -1 },
[weaponType]: [addedWeapon]
}
};
} as IPurchaseResponse;
};
const handlePowersuitPurchase = async (powersuitName: string, accountId: string) => {
if (powersuitName.includes("EntratiMech")) {
const mechSuit = await addMechSuit(powersuitName, accountId);
await updateSlots(SlotType.MECHSUIT, accountId, -1);
await updateSlots(accountId, SlotNameToInventoryName.MECHSUIT, 0, 1);
console.log("mech suit", mechSuit);
return {
@ -94,11 +147,11 @@ const handlePowersuitPurchase = async (powersuitName: string, accountId: string)
},
MechSuits: [mechSuit]
}
};
} as IPurchaseResponse;
}
const suit = await addPowerSuit(powersuitName, accountId);
await updateSlots(SlotType.SUIT, accountId, -1);
await updateSlots(accountId, SlotNameToInventoryName.SUIT, 0, 1);
return {
InventoryChanges: {
@ -112,6 +165,7 @@ const handlePowersuitPurchase = async (powersuitName: string, accountId: string)
};
};
//TODO: change to getInventory, apply changes then save at the end
const handleTypesPurchase = async (typesName: string, accountId: string) => {
const typeCategory = getStoreItemTypesCategory(typesName);
console.log("type category", typeCategory);
@ -122,6 +176,8 @@ const handleTypesPurchase = async (typesName: string, accountId: string) => {
// break;
case "Sentinels":
return await handleSentinelPurchase(typesName, accountId);
case "SlotItems":
return await handleSlotPurchase(typesName, accountId);
default:
throw new Error(`unknown Types category: ${typeCategory} not implemented or new`);
}
@ -130,6 +186,8 @@ const handleTypesPurchase = async (typesName: string, accountId: string) => {
const handleSentinelPurchase = async (sentinelName: string, accountId: string) => {
const sentinel = await addSentinel(sentinelName, accountId);
await updateSlots(accountId, SlotNameToInventoryName.SENTINEL, 0, 1);
return {
InventoryChanges: {
SentinelBin: { count: 1, platinum: 0, Slots: -1 },

View File

@ -3,7 +3,7 @@ import { IPolarity } from "@/src/types/inventoryTypes/commonInventoryTypes";
import { Types } from "mongoose";
import { IItemConfig } from "./commonInventoryTypes";
export interface ISuitClient extends ISuitDatabase {
export interface ISuitClient extends Omit<ISuitDatabase, "_id"> {
ItemId: IOid;
}

View File

@ -341,7 +341,7 @@ export interface ICrewShipHarnessConfig {
}
export interface ISlots {
Extra?: number;
Extra: number; // can be undefined, but not if used via mongoose
Slots: number;
}

View File

@ -1,4 +1,5 @@
import { ISuitDatabase } from "@/src/types/inventoryTypes/SuitTypes";
import { slotPurchaseNameToSlotName } from "@/src/services/purchaseService";
import { ISuitClient } from "@/src/types/inventoryTypes/SuitTypes";
import { IFlavourItem } from "@/src/types/inventoryTypes/inventoryTypes";
import { IWeaponClient } from "@/src/types/inventoryTypes/weaponTypes";
@ -22,11 +23,14 @@ export interface IPurchaseResponse {
InventoryChanges: {
SuitBin?: IBinChanges;
WeaponBin?: IBinChanges;
Suits?: ISuitDatabase[];
MechBin?: IBinChanges;
MechSuits?: ISuitClient[];
Suits?: ISuitClient[];
LongGuns?: IWeaponClient[];
Pistols?: IWeaponClient[];
Melee?: IWeaponClient[];
PremiumCredits?: number;
PremiumCreditsFree?: number;
RegularCredits?: number;
FlavourItems?: IFlavourItem[];
};
@ -36,10 +40,42 @@ export type IBinChanges = {
count: number;
platinum: number;
Slots: number;
Extra?: number;
};
export enum SlotType {
export enum SlotNameToInventoryName {
SUIT = "SuitBin",
WEAPON = "WeaponBin",
MECHSUIT = "MechBin"
MECHSUIT = "MechBin",
LOADOUT = "PveBonusLoadoutBin",
SENTINEL = "SentinelBin"
}
export type SlotPurchaseName =
| "SuitSlotItem"
| "TwoSentinelSlotItem"
| "TwoWeaponSlotItem"
| "SpaceSuitSlotItem"
| "TwoSpaceWeaponSlotItem"
| "MechSlotItem"
| "TwoOperatorWeaponSlotItem"
| "RandomModSlotItem"
| "TwoCrewShipSalvageSlotItem"
| "CrewMemberSlotItem";
export type SlotNames =
| "SuitBin"
| "WeaponBin"
| "MechBin"
| "PveBonusLoadoutBin"
| "SentinelBin"
| "SpaceSuitBin"
| "SpaceWeaponBin"
| "OperatorAmpBin"
| "RandomModBin"
| "CrewShipSalvageBin"
| "CrewMemberBin";
export type SlotPurchase = {
[P in SlotPurchaseName]: { name: SlotNames; slotsPerPurchase: number };
};

View File

@ -10,14 +10,14 @@ import {
import { IWeaponClient } from "./inventoryTypes/weaponTypes";
import { ISuitClient } from "./inventoryTypes/SuitTypes";
interface IArtifactsRequest {
export interface IArtifactsRequest {
Upgrade: ICrewShipSalvagedWeaponSkin;
LevelDiff: number;
Cost: number;
FusionPointCost: number;
}
interface IMissionInventoryUpdateRequest {
export interface IMissionInventoryUpdateRequest {
rewardsMultiplier?: number;
ActiveBoosters?: IBooster[];
LongGuns?: IWeaponClient[];
@ -35,7 +35,7 @@ interface IMissionInventoryUpdateRequest {
Missions?: IMission;
}
interface IMissionInventoryUpdateRequestRewardInfo {
export interface IMissionInventoryUpdateRequestRewardInfo {
node: string;
rewardTier?: number;
nightmareMode?: boolean;
@ -50,4 +50,6 @@ interface IMissionInventoryUpdateRequestRewardInfo {
rewardSeed?: number;
}
export { IArtifactsRequest, IMissionInventoryUpdateRequest };
export interface IInventorySlotsRequest {
Bin: "PveBonusLoadoutBin";
}