diff --git a/src/controllers/api/syndicateSacrificeController.ts b/src/controllers/api/syndicateSacrificeController.ts new file mode 100644 index 000000000..e6ee04b9e --- /dev/null +++ b/src/controllers/api/syndicateSacrificeController.ts @@ -0,0 +1,25 @@ +import { getJSONfromString } from "@/src/helpers/stringHelpers"; +import { syndicateSacrifice } from "@/src/services/inventoryService"; +import { ISyndicateSacrifice } from "@/src/types/syndicateTypes"; +import { RequestHandler } from "express"; +import { getAccountIdForRequest } from "@/src/services/loginService"; +// eslint-disable-next-line @typescript-eslint/no-misused-promises +const syndicateSacrificeController: RequestHandler = async (request, response) => { + const accountId = await getAccountIdForRequest(request); + 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 }; diff --git a/src/controllers/api/updateChallengeProgressController.ts b/src/controllers/api/updateChallengeProgressController.ts index 45490ce3e..e41880e64 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/routes/api.ts b/src/routes/api.ts index 8ca15002b..fc1a1ee24 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -52,6 +52,7 @@ import { getGuildLogController } from "../controllers/api/getGuildLogController" import { guildTechController } from "../controllers/api/guildTechController"; import { dojoController } from "@/src/controllers/api/dojoController"; import { getGuildDojoController } from "@/src/controllers/api/getGuildDojoController"; +import { syndicateSacrificeController } from "../controllers/api/syndicateSacrificeController"; const apiRouter = express.Router(); @@ -114,5 +115,6 @@ apiRouter.post("/createGuild.php", createGuildController); apiRouter.post("/sell.php", sellController); apiRouter.post("/upgrades.php", upgradesController); apiRouter.post("/guildTech.php", guildTechController); +apiRouter.post("/syndicateSacrifice.php", syndicateSacrificeController); export { apiRouter }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index bc10e5557..6727a75ec 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -13,12 +13,19 @@ 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 +171,28 @@ 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 addWeapon = async ( weaponType: WeaponTypeInternal, weaponName: string, @@ -290,6 +319,32 @@ 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 +385,24 @@ 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 1173be277..decec7c17 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 6693c5983..bdb68be44 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,22 @@ 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 000000000..fae4e446a --- /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; +}