From 304529c56da78fb83f298654c583823e00294c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=81nis?= <97699850+janisslsm@users.noreply.github.com> Date: Sun, 2 Jun 2024 18:10:25 +0300 Subject: [PATCH] initial work on syndicates --- .../api/setSupporedSyndicateController.ts | 14 ++++ .../api/syndicateSacrificeController.ts | 26 +++++++ .../api/updateChallengeProgressController.ts | 16 ++--- src/services/inventoryService.ts | 70 ++++++++++++++++++- src/types/inventoryTypes/inventoryTypes.ts | 5 ++ src/types/requestTypes.ts | 18 ++++- src/types/syndicateTypes.ts | 13 ++++ 7 files changed, 150 insertions(+), 12 deletions(-) create mode 100644 src/controllers/api/setSupporedSyndicateController.ts create mode 100644 src/controllers/api/syndicateSacrificeController.ts create mode 100644 src/types/syndicateTypes.ts diff --git a/src/controllers/api/setSupporedSyndicateController.ts b/src/controllers/api/setSupporedSyndicateController.ts new file mode 100644 index 00000000..c01d7e9d --- /dev/null +++ b/src/controllers/api/setSupporedSyndicateController.ts @@ -0,0 +1,14 @@ +import { updateSyndicate } from "@/src/services/inventoryService"; +import { RequestHandler } from "express"; + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +const setSupportedSyndicateController: RequestHandler = async (request, response) => { + const accountId = request.query.accountId as string; + const syndicate = request.query.syndicate as string; + + await updateSyndicate(syndicate, accountId); + + response.json({}); +}; + +export { setSupportedSyndicateController }; \ No newline at end of file diff --git a/src/controllers/api/syndicateSacrificeController.ts b/src/controllers/api/syndicateSacrificeController.ts new file mode 100644 index 00000000..f78679e8 --- /dev/null +++ b/src/controllers/api/syndicateSacrificeController.ts @@ -0,0 +1,26 @@ +import { getJSONfromString } from "@/src/helpers/stringHelpers"; +import { syndicateSacrifice } from "@/src/services/inventoryService"; +import { ISyndicateSacrifice } from "@/src/types/syndicateTypes"; +import { RequestHandler } from "express"; + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +const syndicateSacrificeController: RequestHandler = async (request, response) => { + + const accountId = request.query.accountId as string; + const body = getJSONfromString(request.body); + let reply = {}; + try { + const update = JSON.parse(body) as ISyndicateSacrifice; + if (typeof update !== "object") { + throw new Error("Invalid data format"); + } + + reply = await syndicateSacrifice(update, accountId); + } catch (err) { + console.error("Error parsing JSON data:", err); + } + + response.json(reply); +}; + +export { syndicateSacrificeController }; \ No newline at end of file diff --git a/src/controllers/api/updateChallengeProgressController.ts b/src/controllers/api/updateChallengeProgressController.ts index 45490ce3..e41880e6 100644 --- a/src/controllers/api/updateChallengeProgressController.ts +++ b/src/controllers/api/updateChallengeProgressController.ts @@ -1,19 +1,15 @@ import { RequestHandler } from "express"; -import { IChallengeProgress } from "@/src/types/inventoryTypes/inventoryTypes"; import { getJSONfromString } from "@/src/helpers/stringHelpers"; import { getAccountIdForRequest } from "@/src/services/loginService"; -import { getInventory, addChallenges } from "@/src/services/inventoryService"; - -interface IUpdateChallengeProgessRequest { - ChallengeProgress: IChallengeProgress[]; -} +import { updateChallengeProgress } from "@/src/services/inventoryService"; +import { IUpdateChallengeProgressRequest } from "@/src/types/requestTypes"; const updateChallengeProgressController: RequestHandler = async (req, res) => { - const payload: IUpdateChallengeProgessRequest = getJSONfromString(req.body.toString()); + const payload: IUpdateChallengeProgressRequest = getJSONfromString(req.body.toString()); const accountId = await getAccountIdForRequest(req); - const inventory = await getInventory(accountId); - addChallenges(inventory, payload.ChallengeProgress); - await inventory.save(); + + await updateChallengeProgress(payload, accountId); + res.status(200).end(); }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index bc10e555..28ddcb5e 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -13,12 +13,14 @@ import { IMiscItem, IMission, IRawUpgrade, + ISeasonChallengeHistory, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes"; import { IGenericUpdate } from "../types/genericUpdate"; -import { IArtifactsRequest, IMissionInventoryUpdateRequest, IThemeUpdateRequest } from "../types/requestTypes"; +import { IArtifactsRequest, IMissionInventoryUpdateRequest, IThemeUpdateRequest, IUpdateChallengeProgressRequest } from "../types/requestTypes"; import { logger } from "@/src/utils/logger"; import { WeaponTypeInternal } from "@/src/services/itemDataService"; +import { ISyndicateSacrifice, ISyndicateSacrificeResponse } from "../types/syndicateTypes"; export const createInventory = async ( accountOwnerId: Types.ObjectId, @@ -164,6 +166,32 @@ export const updateTheme = async (data: IThemeUpdateRequest, accountId: string) await inventory.save(); }; +export const syndicateSacrifice = async (data: ISyndicateSacrifice, accountId: string): Promise => { + const inventory = await getInventory(accountId); + const syndicate = inventory.Affiliations.find(x => x.Tag == data.AffiliationTag); + const level = data.SacrificeLevel - (syndicate?.Title ?? 0); + const res: ISyndicateSacrificeResponse = { + AffiliationTag: data.AffiliationTag, + InventoryChanges: [], + Level: data.SacrificeLevel, + LevelIncrease: level <= 0 ? 1 : level, + NewEpisodeReward: syndicate?.Tag == "RadioLegionIntermission9Syndicate" + } + + if (syndicate?.Title !== undefined) syndicate.Title += 1; + + await inventory.save(); + + return res; +}; + +export const updateSyndicate = async (syndicate: string, accountId: string) => { + const inventory = await getInventory(accountId); + + inventory.SupportedSyndicate = syndicate; + await inventory.save(); +}; + export const addWeapon = async ( weaponType: WeaponTypeInternal, weaponName: string, @@ -290,6 +318,29 @@ export const addMods = (inventory: IInventoryDatabaseDocument, itemsArray: IRawU }); }; +export const updateChallengeProgress = async (challenges: IUpdateChallengeProgressRequest, accountId: string) => { + const inventory = await getInventory(accountId); + + addChallenges(inventory, challenges.ChallengeProgress); + addSeasonalChallengeHistory(inventory, challenges.SeasonChallengeHistory); + + await inventory.save(); +}; + +export const addSeasonalChallengeHistory = (inventory: IInventoryDatabaseDocument, itemsArray: ISeasonChallengeHistory[] | undefined) => { + const category = inventory.SeasonChallengeHistory; + + itemsArray?.forEach(({ challenge, id }) => { + const itemIndex = category.findIndex(i => i.challenge === challenge); + + if (itemIndex !== -1) { + category[itemIndex].id = id; + } else { + category.push({ challenge, id }); + } + }); +}; + export const addChallenges = (inventory: IInventoryDatabaseDocument, itemsArray: IChallengeProgress[] | undefined) => { const category = inventory.ChallengeProgress; @@ -330,6 +381,23 @@ export const missionInventoryUpdate = async (data: IMissionInventoryUpdateReques // endo inventory.FusionPoints += FusionPoints || 0; + // syndicate + data.AffiliationChanges?.forEach(affiliation => { + const syndicate = inventory.Affiliations.find(x => x.Tag == affiliation.Tag); + if (syndicate !== undefined) { + syndicate.Standing = syndicate.Standing === undefined ? affiliation.Standing : syndicate.Standing + affiliation.Standing; + syndicate.Title = syndicate.Title === undefined ? affiliation.Title : syndicate.Title + affiliation.Title; + } else { + inventory.Affiliations.push({ + Standing: affiliation.Standing, + Title: affiliation.Title, + Tag: affiliation.Tag, + FreeFavorsEarned: [], + FreeFavorsUsed: [] + }); + } + }) + // Gear XP gearKeys.forEach(key => addGearExpByCategory(inventory, data[key], key)); diff --git a/src/types/inventoryTypes/inventoryTypes.ts b/src/types/inventoryTypes/inventoryTypes.ts index 1173be27..decec7c1 100644 --- a/src/types/inventoryTypes/inventoryTypes.ts +++ b/src/types/inventoryTypes/inventoryTypes.ts @@ -1028,6 +1028,11 @@ export interface ISeasonChallengeHistory { id: string; } +export interface ISeasonChallengeCompletions { + challenge: string; + id: string; +} + export interface ISentientSpawnChanceBoosters { numOceanMissionsCompleted: number; } diff --git a/src/types/requestTypes.ts b/src/types/requestTypes.ts index 6693c598..5063d11f 100644 --- a/src/types/requestTypes.ts +++ b/src/types/requestTypes.ts @@ -7,7 +7,9 @@ import { ICrewShipSalvagedWeaponSkin, IMiscItem, IMission, - IRawUpgrade + IRawUpgrade, + ISeasonChallengeCompletions, + ISeasonChallengeHistory } from "./inventoryTypes/inventoryTypes"; import { IWeaponClient } from "./inventoryTypes/weaponTypes"; import { ISuitClient } from "./inventoryTypes/SuitTypes"; @@ -25,9 +27,23 @@ export interface IThemeUpdateRequest { Sounds?: string; } +export interface IAffiliationChange +{ + Tag: string, + Standing: number, + Title: number +} + +export interface IUpdateChallengeProgressRequest { + ChallengeProgress: IChallengeProgress[]; + SeasonChallengeHistory: ISeasonChallengeHistory[]; + SeasonChallengeCompletions: ISeasonChallengeCompletions[]; +} + export interface IMissionInventoryUpdateRequest { rewardsMultiplier?: number; ActiveBoosters?: IBooster[]; + AffiliationChanges?: IAffiliationChange[]; LongGuns?: IWeaponClient[]; Pistols?: IWeaponClient[]; Suits?: ISuitClient[]; diff --git a/src/types/syndicateTypes.ts b/src/types/syndicateTypes.ts new file mode 100644 index 00000000..255548ca --- /dev/null +++ b/src/types/syndicateTypes.ts @@ -0,0 +1,13 @@ +export interface ISyndicateSacrifice { + AffiliationTag: string; + SacrificeLevel: number; + AllowMultiple: boolean; +} + +export interface ISyndicateSacrificeResponse { + AffiliationTag: string; + Level: number; + LevelIncrease: number; + InventoryChanges: any[]; + NewEpisodeReward: boolean; +} \ No newline at end of file