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 1/3] 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 -- 2.47.2 From d0e6d4f0d5c139ab4605f6ee5d490e8e8e5fae4a Mon Sep 17 00:00:00 2001 From: janisslsm Date: Sun, 2 Jun 2024 15:11:02 +0000 Subject: [PATCH 2/3] Apply prettier changes --- .../api/setSupporedSyndicateController.ts | 2 +- .../api/syndicateSacrificeController.ts | 3 +- src/services/inventoryService.ts | 28 +++++++++++++------ src/types/requestTypes.ts | 9 +++--- src/types/syndicateTypes.ts | 2 +- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/controllers/api/setSupporedSyndicateController.ts b/src/controllers/api/setSupporedSyndicateController.ts index c01d7e9d..e6f1d548 100644 --- a/src/controllers/api/setSupporedSyndicateController.ts +++ b/src/controllers/api/setSupporedSyndicateController.ts @@ -11,4 +11,4 @@ const setSupportedSyndicateController: RequestHandler = async (request, response response.json({}); }; -export { setSupportedSyndicateController }; \ No newline at end of file +export { setSupportedSyndicateController }; diff --git a/src/controllers/api/syndicateSacrificeController.ts b/src/controllers/api/syndicateSacrificeController.ts index f78679e8..3688551a 100644 --- a/src/controllers/api/syndicateSacrificeController.ts +++ b/src/controllers/api/syndicateSacrificeController.ts @@ -5,7 +5,6 @@ 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 = {}; @@ -23,4 +22,4 @@ const syndicateSacrificeController: RequestHandler = async (request, response) = response.json(reply); }; -export { syndicateSacrificeController }; \ No newline at end of file +export { syndicateSacrificeController }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 28ddcb5e..75b7c524 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -17,7 +17,12 @@ import { ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes"; import { IGenericUpdate } from "../types/genericUpdate"; -import { IArtifactsRequest, IMissionInventoryUpdateRequest, IThemeUpdateRequest, IUpdateChallengeProgressRequest } 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"; @@ -166,7 +171,10 @@ export const updateTheme = async (data: IThemeUpdateRequest, accountId: string) await inventory.save(); }; -export const syndicateSacrifice = async (data: ISyndicateSacrifice, accountId: string): Promise => { +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); @@ -176,8 +184,8 @@ export const syndicateSacrifice = async (data: ISyndicateSacrifice, accountId: s Level: data.SacrificeLevel, LevelIncrease: level <= 0 ? 1 : level, NewEpisodeReward: syndicate?.Tag == "RadioLegionIntermission9Syndicate" - } - + }; + if (syndicate?.Title !== undefined) syndicate.Title += 1; await inventory.save(); @@ -327,7 +335,10 @@ export const updateChallengeProgress = async (challenges: IUpdateChallengeProgre await inventory.save(); }; -export const addSeasonalChallengeHistory = (inventory: IInventoryDatabaseDocument, itemsArray: ISeasonChallengeHistory[] | undefined) => { +export const addSeasonalChallengeHistory = ( + inventory: IInventoryDatabaseDocument, + itemsArray: ISeasonChallengeHistory[] | undefined +) => { const category = inventory.SeasonChallengeHistory; itemsArray?.forEach(({ challenge, id }) => { @@ -383,9 +394,10 @@ export const missionInventoryUpdate = async (data: IMissionInventoryUpdateReques // syndicate data.AffiliationChanges?.forEach(affiliation => { - const syndicate = inventory.Affiliations.find(x => x.Tag == affiliation.Tag); + 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.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({ @@ -396,7 +408,7 @@ export const missionInventoryUpdate = async (data: IMissionInventoryUpdateReques FreeFavorsUsed: [] }); } - }) + }); // Gear XP gearKeys.forEach(key => addGearExpByCategory(inventory, data[key], key)); diff --git a/src/types/requestTypes.ts b/src/types/requestTypes.ts index 5063d11f..bdb68be4 100644 --- a/src/types/requestTypes.ts +++ b/src/types/requestTypes.ts @@ -27,11 +27,10 @@ export interface IThemeUpdateRequest { Sounds?: string; } -export interface IAffiliationChange -{ - Tag: string, - Standing: number, - Title: number +export interface IAffiliationChange { + Tag: string; + Standing: number; + Title: number; } export interface IUpdateChallengeProgressRequest { diff --git a/src/types/syndicateTypes.ts b/src/types/syndicateTypes.ts index 255548ca..fae4e446 100644 --- a/src/types/syndicateTypes.ts +++ b/src/types/syndicateTypes.ts @@ -10,4 +10,4 @@ export interface ISyndicateSacrificeResponse { LevelIncrease: number; InventoryChanges: any[]; NewEpisodeReward: boolean; -} \ No newline at end of file +} -- 2.47.2 From 00740e31dddaf93ee7d771b4a9e49abf2b5a94b0 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:27:03 +0300 Subject: [PATCH 3/3] remove setSupporedSyndicateController.ts and use getAccountIdForRequest also added syndicateSacrifice.php to api.ts, forgot to transfer it over from my old codebase. --- .../api/setSupporedSyndicateController.ts | 14 -------------- .../api/syndicateSacrificeController.ts | 4 ++-- src/routes/api.ts | 2 ++ src/services/inventoryService.ts | 7 ------- 4 files changed, 4 insertions(+), 23 deletions(-) delete mode 100644 src/controllers/api/setSupporedSyndicateController.ts diff --git a/src/controllers/api/setSupporedSyndicateController.ts b/src/controllers/api/setSupporedSyndicateController.ts deleted file mode 100644 index c01d7e9d..00000000 --- a/src/controllers/api/setSupporedSyndicateController.ts +++ /dev/null @@ -1,14 +0,0 @@ -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 index f78679e8..b874a653 100644 --- a/src/controllers/api/syndicateSacrificeController.ts +++ b/src/controllers/api/syndicateSacrificeController.ts @@ -2,11 +2,11 @@ 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 = request.query.accountId as string; + const accountId = await getAccountIdForRequest(request); const body = getJSONfromString(request.body); let reply = {}; try { diff --git a/src/routes/api.ts b/src/routes/api.ts index 8ca15002..fc1a1ee2 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 28ddcb5e..84439ed9 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -185,13 +185,6 @@ export const syndicateSacrifice = async (data: ISyndicateSacrifice, accountId: s 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, -- 2.47.2