2025-01-31 17:24:42 +01:00
|
|
|
import { IKeyChainRequest } from "@/src/controllers/api/giveKeyChainTriggeredItemsController";
|
|
|
|
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
|
|
|
import { IInventoryDatabase, IQuestKeyDatabase, IQuestStage } from "@/src/types/inventoryTypes/inventoryTypes";
|
2025-01-24 14:13:21 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
|
|
|
import { HydratedDocument } from "mongoose";
|
|
|
|
|
2025-01-31 17:24:42 +01:00
|
|
|
export interface IUpdateQuestRequest {
|
|
|
|
QuestKeys: Omit<IQuestKeyDatabase, "CompletionDate">[];
|
|
|
|
PS: string;
|
|
|
|
questCompletion: boolean;
|
|
|
|
PlayerShipEvents: unknown[];
|
|
|
|
crossPlaySetting: string;
|
|
|
|
DoQuestReward: boolean;
|
|
|
|
}
|
|
|
|
|
2025-01-24 14:13:21 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2025-02-18 17:14:42 -08:00
|
|
|
const questKeyIndex = inventory.QuestKeys.findIndex(questKey => questKey.ItemType === questKeyUpdate[0].ItemType);
|
|
|
|
|
2025-01-24 14:13:21 +01:00
|
|
|
if (questKeyIndex === -1) {
|
2025-02-18 17:14:42 -08:00
|
|
|
throw new Error(`quest key ${questKeyUpdate[0].ItemType} not found`);
|
2025-01-24 14:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inventory.QuestKeys[questKeyIndex] = questKeyUpdate[0];
|
|
|
|
|
|
|
|
if (questKeyUpdate[0].Completed) {
|
|
|
|
inventory.QuestKeys[questKeyIndex].CompletionDate = new Date();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-01-31 17:24:42 +01:00
|
|
|
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);
|
|
|
|
};
|
2025-02-18 17:14:42 -08:00
|
|
|
|
|
|
|
export const addQuestKey = (inventory: TInventoryDatabaseDocument, questKey: IQuestKeyDatabase): void => {
|
|
|
|
if (inventory.QuestKeys.some(q => q.ItemType === questKey.ItemType)) {
|
|
|
|
logger.error(`quest key ${questKey.ItemType} already exists`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inventory.QuestKeys.push(questKey);
|
|
|
|
};
|