SpaceNinjaServer/src/controllers/api/missionInventoryUpdateController.ts

278 lines
9.6 KiB
TypeScript
Raw Normal View History

2023-08-30 22:56:50 +04:00
import { RequestHandler } from "express";
2023-08-30 07:51:56 +04:00
import { missionInventoryUpdate } from "@/src/services/inventoryService";
2023-09-03 21:25:54 +04:00
import {
2023-09-04 01:11:57 +04:00
IMissionInventoryUpdate,
IMissionInventoryUpdateRewardInfo,
IMissionRewardResponse,
IReward
2023-09-03 21:25:54 +04:00
} from "@/src/types/missionInventoryUpdateType";
import { RawUpgrade } from "@/src/types/inventoryTypes/inventoryTypes";
2023-09-02 01:56:08 +04:00
import missionsDropTable from "@/static/json/missions-drop-table.json";
2023-09-03 21:25:54 +04:00
import {
modNames,
relicNames,
skinNames,
miscNames,
resourceNames,
gearNames,
arcaneNames,
craftNames
} from "@/static/data/items";
2023-09-01 15:38:41 +04:00
/*
**** INPUT ****
- [ ] crossPlaySetting
- [ ] rewardsMultiplier
- [ ] ActiveBoosters
2023-08-30 07:51:56 +04:00
- [x] LongGuns
- [x] Pistols
- [x] Suits
- [x] Melee
- [x] RawUpgrades
- [x] MiscItems
2023-08-30 08:14:59 +04:00
- [x] RegularCredits
- [ ] RandomUpgradesIdentified
- [ ] MissionFailed
- [ ] MissionStatus
- [ ] CurrentLoadOutIds
- [ ] AliveTime
- [ ] MissionTime
- [ ] Missions
- [ ] CompletedAlerts
- [ ] LastRegionPlayed
- [ ] GameModeId
- [ ] hosts
- [x] ChallengeProgress
- [ ] SeasonChallengeHistory
- [ ] PS
- [ ] ActiveDojoColorResearch
- [ ] RewardInfo
- [ ] ReceivedCeremonyMsg
- [ ] LastCeremonyResetDate
- [ ] MissionPTS
- [ ] RepHash
- [ ] EndOfMatchUpload
- [ ] ObjectiveReached
- [ ] FpsAvg
- [ ] FpsMin
- [ ] FpsMax
- [ ] FpsSamples
*/
2023-08-30 17:32:08 +04:00
2023-08-30 22:56:50 +04:00
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const missionInventoryUpdateController: RequestHandler = async (req, res) => {
2023-08-30 20:14:06 +04:00
const [data] = String(req.body).split("\n");
const id = req.query.accountId as string;
2023-08-30 17:32:08 +04:00
try {
2023-09-04 01:11:57 +04:00
const parsedData = JSON.parse(data) as IMissionInventoryUpdate;
2023-08-30 20:14:06 +04:00
if (typeof parsedData !== "object" || parsedData === null) throw new Error("Invalid data format");
2023-09-03 12:22:27 +04:00
const { InventoryChanges, MissionRewards } = getRewards(parsedData.RewardInfo);
const missionCredits = parsedData.RegularCredits || 0;
2023-09-03 12:22:27 +04:00
const creditsBonus = InventoryChanges.RegularCredits || 1000;
const totalCredits = missionCredits + creditsBonus;
2023-09-03 12:22:27 +04:00
const MissionCredits = [missionCredits, missionCredits];
const CreditsBonus = [creditsBonus, creditsBonus];
const TotalCredits = [totalCredits, totalCredits];
2023-09-03 12:22:27 +04:00
const FusionPoints =
parsedData.FusionPoints || InventoryChanges.FusionPoints
? (parsedData.FusionPoints || 0) + (InventoryChanges.FusionPoints || 0)
: undefined;
// combine reward and loot
parsedData.RegularCredits = totalCredits;
if (FusionPoints) parsedData.FusionPoints = FusionPoints;
if (InventoryChanges.RawUpgrades && !parsedData.RawUpgrades) parsedData.RawUpgrades = [];
InventoryChanges.RawUpgrades?.forEach(i => parsedData.RawUpgrades!.push(i));
if (InventoryChanges.MiscItems && !parsedData.MiscItems) parsedData.RawUpgrades = [];
InventoryChanges.MiscItems?.forEach(i => parsedData.MiscItems!.push(i));
const Inventory = await missionInventoryUpdate(parsedData, id);
InventoryChanges.RawUpgrades?.forEach(
(i: RawUpgrade) => (i.LastAdded = Inventory.RawUpgrades.find(j => j.ItemType === i.ItemType)?.LastAdded)
);
2023-09-02 22:47:57 +04:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2023-09-03 12:22:27 +04:00
const InventoryJson = JSON.stringify(Inventory);
res.json({
// InventoryJson, // this part will reset game data and missions will be locked
TotalCredits,
CreditsBonus,
2023-09-03 12:22:27 +04:00
MissionCredits,
MissionRewards,
InventoryChanges,
...(FusionPoints !== undefined && { FusionPoints })
});
2023-08-30 17:32:08 +04:00
} catch (err) {
2023-08-30 20:14:06 +04:00
console.error("Error parsing JSON data:", err);
2023-08-30 17:32:08 +04:00
}
2023-06-01 17:08:05 -07:00
};
/*
**** OUTPUT ****
- [x] InventoryJson
2023-09-03 12:22:27 +04:00
- [x] MissionRewards
- [x] TotalCredits
- [x] CreditsBonus
- [x] MissionCredits
2023-09-03 12:22:27 +04:00
- [x] InventoryChanges
- [x] FusionPoints
*/
2023-09-04 15:00:40 +04:00
// need reverse engineer rewardSeed, otherwise ingame displayed rotation loot will be different than added to db
2023-09-03 12:22:27 +04:00
const getRewards = (
2023-09-04 01:11:57 +04:00
rewardInfo: IMissionInventoryUpdateRewardInfo | undefined
): { InventoryChanges: IMissionInventoryUpdate; MissionRewards: IMissionRewardResponse[] } => {
2023-09-03 12:22:27 +04:00
if (!rewardInfo) return { InventoryChanges: {}, MissionRewards: [] };
2023-09-04 01:11:57 +04:00
const rewards = (missionsDropTable as { [key: string]: IReward[] })[rewardInfo.node];
2023-09-03 12:22:27 +04:00
if (!rewards) return { InventoryChanges: {}, MissionRewards: [] };
2023-09-01 15:38:41 +04:00
2023-09-04 15:00:40 +04:00
const rotationCount = rewardInfo.rewardQualifications?.length || 0;
const rotations = getRotations(rotationCount);
const drops: IReward[] = [];
for (const rotation of rotations) {
const rotationRewards = rewards.filter(i => i.rotation === rotation);
// Separate guaranteed and chance drops
const guaranteedDrops: IReward[] = [];
const chanceDrops: IReward[] = [];
for (const reward of rotationRewards) {
if (reward.chance === 100) guaranteedDrops.push(reward);
else chanceDrops.push(reward);
}
const randomDrop = getRandomRewardByChance(chanceDrops);
if (randomDrop) guaranteedDrops.push(randomDrop);
drops.push(...guaranteedDrops);
2023-09-01 15:38:41 +04:00
}
2023-09-04 15:00:40 +04:00
console.log("Mission rewards:", drops);
2023-09-01 15:38:41 +04:00
2023-09-04 15:00:40 +04:00
return formatRewardsToInventoryType(drops);
};
const getRotations = (rotationCount: number): (string | undefined)[] => {
if (rotationCount === 0) return [undefined];
const rotations = ["A", "B", "C"];
let rotationIndex = 0;
const rotatedValues = [];
for (let i = 1; i <= rotationCount; i++) {
rotatedValues.push(rotations[rotationIndex]);
rotationIndex = (rotationIndex + 1) % 3;
}
2023-09-03 12:22:27 +04:00
2023-09-04 15:00:40 +04:00
return rotatedValues;
2023-09-01 15:38:41 +04:00
};
2023-09-04 01:11:57 +04:00
const getRandomRewardByChance = (data: IReward[] | undefined): IReward | undefined => {
2023-09-01 15:38:41 +04:00
if (!data || data.length == 0) return;
const totalChance = data.reduce((sum, item) => sum + item.chance, 0);
const randomValue = Math.random() * totalChance;
let cumulativeChance = 0;
for (const item of data) {
cumulativeChance += item.chance;
if (randomValue <= cumulativeChance) {
return item;
}
}
return;
};
2023-09-03 12:22:27 +04:00
const formatRewardsToInventoryType = (
2023-09-04 01:11:57 +04:00
rewards: IReward[]
): { InventoryChanges: IMissionInventoryUpdate; MissionRewards: IMissionRewardResponse[] } => {
const InventoryChanges: IMissionInventoryUpdate = {};
const MissionRewards: IMissionRewardResponse[] = [];
2023-09-03 12:22:27 +04:00
rewards.forEach(i => {
2023-09-03 21:25:54 +04:00
const mod = modNames[i.name];
const skin = skinNames[i.name];
const gear = gearNames[i.name];
const arcane = arcaneNames[i.name];
const craft = craftNames[i.name];
const misc = miscNames[i.name] || miscNames[i.name.replace(/\d+X\s*/, "")];
const resource = resourceNames[i.name] || resourceNames[i.name.replace(/\d+X\s*/, "")];
2023-09-02 01:56:08 +04:00
const relic =
2023-09-03 21:25:54 +04:00
relicNames[i.name.replace("Relic", "Exceptional")] ||
relicNames[i.name.replace("Relic (Radiant)", "Radiant")];
2023-09-02 01:56:08 +04:00
if (mod) {
2023-09-03 12:22:27 +04:00
if (!InventoryChanges.RawUpgrades) InventoryChanges.RawUpgrades = [];
InventoryChanges.RawUpgrades.push({ ItemType: mod, ItemCount: 1 });
MissionRewards.push({
StoreItem: mod.replace("/Lotus/", "/Lotus/StoreItems/"),
TypeName: mod,
UpgradeLevel: 0,
ItemCount: 1,
TweetText: `${i.name} (Mod)`,
ProductCategory: "Upgrades"
});
2023-09-02 01:56:08 +04:00
} else if (skin) {
2023-09-03 12:22:27 +04:00
/* skin */
2023-09-02 01:56:08 +04:00
} else if (gear) {
2023-09-03 12:22:27 +04:00
/* gear */
2023-09-02 01:56:08 +04:00
} else if (arcane) {
2023-09-03 12:22:27 +04:00
/* arcane */
2023-09-02 01:56:08 +04:00
} else if (craft) {
2023-09-03 12:22:27 +04:00
/* craft */
} else if (misc || resource) {
if (!InventoryChanges.MiscItems) InventoryChanges.MiscItems = [];
const ItemType = misc || resource;
const ItemCount = parseInt(i.name) || 1;
InventoryChanges.MiscItems.push({ ItemType, ItemCount });
2023-09-02 01:56:08 +04:00
} else if (relic) {
2023-09-03 12:22:27 +04:00
/* relic */
2023-09-02 01:56:08 +04:00
} else if (i.name.includes(" Endo")) {
2023-09-03 12:22:27 +04:00
InventoryChanges.FusionPoints = parseInt(i.name);
2023-09-02 01:56:08 +04:00
} else if (i.name.includes(" Credits Cache") || i.name.includes("Return: ")) {
2023-09-03 12:22:27 +04:00
InventoryChanges.RegularCredits = parseInt(i.name.replace(/ Credits Cache|Return: |,/g, ""));
2023-09-02 01:56:08 +04:00
}
});
2023-09-03 12:22:27 +04:00
return { InventoryChanges, MissionRewards };
2023-09-02 01:56:08 +04:00
};
2023-09-03 21:25:54 +04:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _missionRewardsCheckAllNamings = () => {
2023-09-04 01:11:57 +04:00
let tempRewards: IReward[] = [];
Object.values(missionsDropTable as { [key: string]: IReward[] }).forEach(i => {
2023-09-03 21:25:54 +04:00
i.forEach(j => {
tempRewards.push(j);
});
});
tempRewards = tempRewards
.filter(i => !modNames[i.name])
.filter(i => !skinNames[i.name])
.filter(i => !miscNames[i.name])
.filter(i => !miscNames[i.name.replace(/\d+X\s*/, "")])
.filter(i => !resourceNames[i.name])
.filter(i => !resourceNames[i.name.replace(/\d+X\s*/, "")])
.filter(i => !gearNames[i.name])
.filter(i => !arcaneNames[i.name])
.filter(i => !craftNames[i.name])
.filter(i => {
// return true;
// return !relicNames[i.name.replace("Relic", "Exceptional")];
// console.log(i.name.replace("Relic", "Exceptional"));
return (
!relicNames[i.name.replace("Relic", "Exceptional")] &&
!relicNames[i.name.replace("Relic (Radiant)", "Radiant")]
);
})
.filter(i => !i.name.includes(" Endo"))
.filter(i => !i.name.includes(" Credits Cache") && !i.name.includes("Return: "));
console.log(tempRewards);
};
// _missionRewardsCheckAllNamings();
2023-06-01 17:08:05 -07:00
export { missionInventoryUpdateController };