SpaceNinjaServer/src/services/questService.ts
Sainan 0b29d33652
All checks were successful
Build / build (20) (pull_request) Successful in 59s
Build / build (22) (pull_request) Successful in 1m4s
Build / build (20) (push) Successful in 35s
Build / build (18) (push) Successful in 1m4s
Build / build (22) (push) Successful in 1m7s
Build / build (18) (pull_request) Successful in 35s
fix: add missing quest keys at updateQuestKey
it's possible the quest key was not in already in the inventory but the quest was still available due to unlockAllQuests
2025-02-18 00:43:48 +01:00

65 lines
2.3 KiB
TypeScript

import { IKeyChainRequest } from "@/src/controllers/api/giveKeyChainTriggeredItemsController";
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
import { IInventoryDatabase, IQuestKeyDatabase, IQuestStage } from "@/src/types/inventoryTypes/inventoryTypes";
import { logger } from "@/src/utils/logger";
import { HydratedDocument } from "mongoose";
export interface IUpdateQuestRequest {
QuestKeys: Omit<IQuestKeyDatabase, "CompletionDate">[];
PS: string;
questCompletion: boolean;
PlayerShipEvents: unknown[];
crossPlaySetting: string;
DoQuestReward: boolean;
}
export const updateQuestKey = (
inventory: HydratedDocument<IInventoryDatabase>,
questKeyUpdate: IUpdateQuestRequest["QuestKeys"]
): void => {
if (questKeyUpdate.length > 1) {
logger.error(`more than 1 quest key not supported`);
throw new Error("more than 1 quest key not supported");
}
let questKeyIndex = inventory.QuestKeys.findIndex(questKey => questKey.ItemType === questKeyUpdate[0].ItemType);
if (questKeyIndex === -1) {
// it's possible the quest key was not in already in the inventory but the quest was still available due to unlockAllQuests
questKeyIndex = inventory.QuestKeys.push({ ItemType: questKeyUpdate[0].ItemType }) - 1;
}
inventory.QuestKeys[questKeyIndex] = questKeyUpdate[0];
if (questKeyUpdate[0].Completed) {
inventory.QuestKeys[questKeyIndex].CompletionDate = new Date();
}
};
export const updateQuestStage = (
inventory: TInventoryDatabaseDocument,
{ KeyChain, ChainStage }: IKeyChainRequest,
questStageUpdate: IQuestStage
): void => {
const quest = inventory.QuestKeys.find(quest => quest.ItemType === KeyChain);
if (!quest) {
throw new Error(`Quest ${KeyChain} not found in QuestKeys`);
}
if (!quest.Progress) {
throw new Error(`Progress should always exist when giving keychain triggered items or messages`);
}
const questStage = quest.Progress[ChainStage];
if (!questStage) {
const questStageIndex = quest.Progress.push(questStageUpdate) - 1;
if (questStageIndex !== ChainStage) {
throw new Error(`Quest stage index mismatch: ${questStageIndex} !== ${ChainStage}`);
}
return;
}
Object.assign(questStage, questStageUpdate);
};