SpaceNinjaServer/src/controllers/api/missionInventoryUpdateController.ts

303 lines
11 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-05 02:03:00 +04:00
import { modNames, relicNames, miscNames, resourceNames, gearNames, blueprintNames } 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-05 02:03:00 +04:00
const FusionPoints = (parsedData.FusionPoints || 0) + (InventoryChanges.FusionPoints || 0) || undefined;
2023-09-03 12:22:27 +04:00
// combine reward and loot
parsedData.RegularCredits = totalCredits;
if (FusionPoints) parsedData.FusionPoints = FusionPoints;
2023-09-05 02:03:00 +04:00
inventoryFields.forEach((field: InventoryFieldType) => {
if (InventoryChanges[field] && !parsedData[field]) parsedData[field] = [];
InventoryChanges[field]?.forEach(i => parsedData[field]!.push(i));
});
2023-09-03 12:22:27 +04:00
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-05 02:03:00 +04:00
const inventoryFields = ["RawUpgrades", "MiscItems", "Consumables", "Recipes"] as const;
type InventoryFieldType = (typeof inventoryFields)[number];
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 21:07:31 +04:00
// const testDrops = [
// { chance: 7.69, name: "Lith W3 Relic", rotation: "B" },
// { chance: 7.69, name: "Lith W3 Relic", rotation: "B" },
// { chance: 10.82, name: "2X Orokin Cell", rotation: "C" },
// { chance: 10.82, name: "Link Armor", rotation: "C" },
// { chance: 10.82, name: "200 Endo", rotation: "C" },
2023-09-04 21:07:31 +04:00
// { chance: 10.82, name: "2,000,000 Credits Cache", rotation: "C" },
2023-09-05 02:03:00 +04:00
// { chance: 7.69, name: "Health Restore (Large)", rotation: "C" },
// { chance: 7.69, name: "Vapor Specter Blueprint", rotation: "C" }
2023-09-04 21:07:31 +04:00
// ];
// console.log("Mission rewards:", testDrops);
// return formatRewardsToInventoryType(testDrops);
2023-09-01 15:38:41 +04:00
2023-09-04 21:07:31 +04:00
console.log("Mission rewards:", drops);
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[] = [];
for (const reward of rewards) {
if (itemCheck(InventoryChanges, MissionRewards, reward.name)) continue;
if (reward.name.includes(" Endo")) {
2023-09-04 21:07:31 +04:00
if (!InventoryChanges.FusionPoints) InventoryChanges.FusionPoints = 0;
InventoryChanges.FusionPoints += getCountFromName(reward.name);
} else if (reward.name.includes(" Credits Cache") || reward.name.includes("Return: ")) {
2023-09-04 21:07:31 +04:00
if (!InventoryChanges.RegularCredits) InventoryChanges.RegularCredits = 0;
InventoryChanges.RegularCredits += getCountFromName(reward.name);
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
};
const itemCheck = (
InventoryChanges: IMissionInventoryUpdate,
MissionRewards: IMissionRewardResponse[],
name: string
) => {
const rewardCheck = {
RawUpgrades: modNames[name],
Consumables: gearNames[name],
MiscItems:
miscNames[name] ||
miscNames[name.replace(/\d+X\s*/, "")] ||
resourceNames[name] ||
resourceNames[name.replace(/\d+X\s*/, "")] ||
relicNames[name.replace("Relic", "Intact")] ||
relicNames[name.replace("Relic (Radiant)", "Radiant")],
Recipes: blueprintNames[name]
};
for (const key of Object.keys(rewardCheck) as InventoryFieldType[]) {
if (rewardCheck[key]) {
addRewardResponse(InventoryChanges, MissionRewards, name, rewardCheck[key]!, key);
return true;
}
}
return false;
};
2023-09-04 21:07:31 +04:00
const getCountFromName = (name: string) => {
const regex = /(^(?:\d{1,3}(?:,\d{3})*(?:\.\d+)?)(\s|X))|(\s(?:\d{1,3}(?:,\d{3})*(?:\.\d+)?)$)/;
const countMatches = name.match(regex);
return countMatches ? parseInt(countMatches[0].replace(/,/g, ""), 10) : 1;
};
const addRewardResponse = (
InventoryChanges: IMissionInventoryUpdate,
MissionRewards: IMissionRewardResponse[],
ItemName: string,
ItemType: string,
2023-09-05 02:03:00 +04:00
InventoryCategory: InventoryFieldType
2023-09-04 21:07:31 +04:00
) => {
if (!ItemType) return;
if (!InventoryChanges[InventoryCategory]) InventoryChanges[InventoryCategory] = [];
const ItemCount = getCountFromName(ItemName);
const TweetText = `${ItemName}`;
const existReward = InventoryChanges[InventoryCategory]!.find(j => j.ItemType === ItemType);
if (existReward) {
existReward.ItemCount += ItemCount;
const missionReward = MissionRewards.find(j => j.TypeName === ItemType);
if (missionReward) missionReward.ItemCount += ItemCount;
} else {
InventoryChanges[InventoryCategory]!.push({ ItemType, ItemCount });
MissionRewards.push({
ItemCount,
TweetText,
ProductCategory: InventoryCategory,
StoreItem: ItemType.replace("/Lotus/", "/Lotus/StoreItems/"),
TypeName: ItemType
});
}
};
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 => !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 => {
return (
2023-09-04 21:07:31 +04:00
!relicNames[i.name.replace("Relic", "Intact")] &&
2023-09-03 21:25:54 +04:00
!relicNames[i.name.replace("Relic (Radiant)", "Radiant")]
);
})
2023-09-05 02:03:00 +04:00
.filter(i => !blueprintNames[i.name])
2023-09-03 21:25:54 +04:00
.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 };