SpaceNinjaServer/src/controllers/api/missionInventoryUpdateController.ts

142 lines
4.0 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-01 15:38:41 +04:00
import { MissionInventoryUpdate, MissionInventoryUpdateRewardInfo } from "@/src/types/missionInventoryUpdateType";
import missionNames from "@/static/data/mission-names.json";
import missionReward from "@/static/data/mission-rewards.json";
/*
**** 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
// TODO - salt check
try {
2023-08-30 20:14:06 +04:00
const parsedData = JSON.parse(data) as MissionInventoryUpdate;
if (typeof parsedData !== "object" || parsedData === null) throw new Error("Invalid data format");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const InventoryJson = JSON.stringify(await missionInventoryUpdate(parsedData, id));
const missionCredits = parsedData.RegularCredits || 0;
const creditsBonus = 0;
const totalCredits = missionCredits + creditsBonus;
const MissionCredits = [missionCredits, missionCredits]; // collected credits
const CreditsBonus = [creditsBonus, creditsBonus]; // mission reward
const TotalCredits = [totalCredits, totalCredits];
2023-09-01 15:38:41 +04:00
console.log(getRewards(parsedData.RewardInfo));
res.json({
// InventoryJson, // this part will reset game data and missions will be locked
TotalCredits,
CreditsBonus,
MissionCredits
});
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
- [ ] MissionRewards
- [x] TotalCredits
- [x] CreditsBonus
- [x] MissionCredits
- [ ] InventoryChanges
2023-08-31 22:20:55 +04:00
- [ ] FusionPoints int
*/
2023-09-01 15:38:41 +04:00
interface MissionNames {
[key: string]: string;
}
const getRewards = (rewardInfo: MissionInventoryUpdateRewardInfo | undefined): Reward[] | undefined => {
if (!rewardInfo) return;
const missionName = (missionNames as MissionNames)[rewardInfo.node];
const rewards = missionReward.find(i => i.mission === missionName)?.rewards;
if (!rewards) return [];
// TODO - add Rotation logic
// Separate guaranteed and chance drops
const guaranteedDrops: Reward[] = [];
const chanceDrops: Reward[] = [];
for (const reward of rewards) {
if (reward.chance === 100) guaranteedDrops.push(reward);
else chanceDrops.push(reward);
}
const randomDrop = getRandomRewardByChance(chanceDrops);
if (randomDrop) guaranteedDrops.push(randomDrop);
return guaranteedDrops;
};
interface Reward {
name: string;
chance: number;
rotation?: string;
}
const getRandomRewardByChance = (data: Reward[] | undefined): Reward | undefined => {
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-06-01 17:08:05 -07:00
export { missionInventoryUpdateController };