forked from OpenWF/SpaceNinjaServer
		
	feat: syndicates (#269)
Co-authored-by: janisslsm <janisslsm@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									3e4eeb601d
								
							
						
					
					
						commit
						2a1a4e77b5
					
				
							
								
								
									
										25
									
								
								src/controllers/api/syndicateSacrificeController.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/controllers/api/syndicateSacrificeController.ts
									
									
									
									
									
										Normal file
									
								
							@ -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 };
 | 
			
		||||
@ -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();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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 };
 | 
			
		||||
 | 
			
		||||
@ -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<ISyndicateSacrificeResponse> => {
 | 
			
		||||
    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));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1028,6 +1028,11 @@ export interface ISeasonChallengeHistory {
 | 
			
		||||
    id: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface ISeasonChallengeCompletions {
 | 
			
		||||
    challenge: string;
 | 
			
		||||
    id: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface ISentientSpawnChanceBoosters {
 | 
			
		||||
    numOceanMissionsCompleted: number;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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[];
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										13
									
								
								src/types/syndicateTypes.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/types/syndicateTypes.ts
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user