From b7c72f99d87a757d0d9a92ea91a80298d7060af6 Mon Sep 17 00:00:00 2001 From: Black Ram <67595890+BlackRam-oss@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:31:49 +0200 Subject: [PATCH] getTimesLabelOpened --- src/interface/PixiVNJson.ts | 2 +- src/interface/PixiVNJsonConditions.ts | 8 +++--- src/interface/PixiVNJsonOperations.ts | 2 +- ...ixiVNJsonStorage.ts => PixiVNJsonValue.ts} | 24 +++++++++++++---- src/interface/index.ts | 2 +- src/managers/StepManager.ts | 27 ++++++++++++++----- src/managers/StorageManager.ts | 2 +- 7 files changed, 47 insertions(+), 20 deletions(-) rename src/interface/{PixiVNJsonStorage.ts => PixiVNJsonValue.ts} (68%) diff --git a/src/interface/PixiVNJson.ts b/src/interface/PixiVNJson.ts index 64f9a5ea..a64bea82 100644 --- a/src/interface/PixiVNJson.ts +++ b/src/interface/PixiVNJson.ts @@ -1,6 +1,6 @@ import PixiVNJsonIfElse from "./PixiVNJsonIfElse" import PixiVNJsonLabels from "./PixiVNJsonLabels" -import { PixiVNJsonStorageSet } from "./PixiVNJsonStorage" +import { PixiVNJsonStorageSet } from "./PixiVNJsonValue" /** * PixiVNJson It can be defined as a programming language to write a narrative written in json. diff --git a/src/interface/PixiVNJsonConditions.ts b/src/interface/PixiVNJsonConditions.ts index 815065f6..a3bc1fb5 100644 --- a/src/interface/PixiVNJsonConditions.ts +++ b/src/interface/PixiVNJsonConditions.ts @@ -1,7 +1,7 @@ import { StorageElementType } from "../types" import PixiVNJsonLabelCondition from "./PixiVNJsonLabelCondition" -import { PixiVNJsonStorageGet } from "./PixiVNJsonStorage" import PixiVNJsonUnionCondition from "./PixiVNJsonUnionCondition" +import { PixiVNJsonValueGet } from "./PixiVNJsonValue" /** * Comparation for PixiVNJson. @@ -12,11 +12,11 @@ type PixiVNJsonComparation = { /** * Left value of the comparation */ - leftValue: StorageElementType | PixiVNJsonStorageGet, + leftValue: StorageElementType | PixiVNJsonValueGet, /** * Right value of the comparation */ - rightValue: StorageElementType | PixiVNJsonStorageGet, + rightValue: StorageElementType | PixiVNJsonValueGet, /** * Operator of the comparation */ @@ -28,7 +28,7 @@ type PixiVNJsonValueCondition = { /** * Value to check, if it is true, or if it not null or undefined */ - value: StorageElementType | PixiVNJsonStorageGet + value: StorageElementType | PixiVNJsonValueGet } /** diff --git a/src/interface/PixiVNJsonOperations.ts b/src/interface/PixiVNJsonOperations.ts index a18a13d2..2033ce31 100644 --- a/src/interface/PixiVNJsonOperations.ts +++ b/src/interface/PixiVNJsonOperations.ts @@ -1,6 +1,6 @@ import PixiVNJsonIfElse from "./PixiVNJsonIfElse" import { PixiVNJsonCanvas, PixiVNJsonSound } from "./PixiVNJsonMedia" -import { PixiVNJsonStorageSet } from "./PixiVNJsonStorage" +import { PixiVNJsonStorageSet } from "./PixiVNJsonValue" export type PixiVNJsonOperation = PixiVNJsonStorageSet | PixiVNJsonCanvas | PixiVNJsonSound diff --git a/src/interface/PixiVNJsonStorage.ts b/src/interface/PixiVNJsonValue.ts similarity index 68% rename from src/interface/PixiVNJsonStorage.ts rename to src/interface/PixiVNJsonValue.ts index 34c82ce9..03f95b70 100644 --- a/src/interface/PixiVNJsonStorage.ts +++ b/src/interface/PixiVNJsonValue.ts @@ -1,8 +1,8 @@ import { getFlag } from "../functions" import { StorageElementType } from "../types" -export type PixiVNJsonStorageGet = { - type: "storage" +type PixiVNJsonStorageGet = { + type: "value" storageOperationType: "get", /** * Key of the storage @@ -15,8 +15,22 @@ export type PixiVNJsonStorageGet = { storageType: "storage" | "flagStorage", } +type PixiVNJsonLabelGet = { + type: "value" + storageOperationType: "get", + /** + * Id of the label + */ + label: string, + /** + * If it is a label, the value will be get with the function {@link narration.getTimesLabelOpened} + */ + storageType: "label", +} +export type PixiVNJsonValueGet = PixiVNJsonStorageGet | PixiVNJsonLabelGet + type PixiVNJsonOnlyStorageSet = { - type: "storage" + type: "value" storageOperationType: "set", /** * Key of the storage @@ -25,7 +39,7 @@ type PixiVNJsonOnlyStorageSet = { /** * Value to be set in the storage */ - value: StorageElementType | PixiVNJsonStorageGet, + value: StorageElementType | PixiVNJsonValueGet, /** * Type of the storage, if it is a flagStorage or a storage. */ @@ -33,7 +47,7 @@ type PixiVNJsonOnlyStorageSet = { } type PixiVNJsonFlagSet = { - type: "storage" + type: "value" storageOperationType: "set", /** * Key of the storage diff --git a/src/interface/index.ts b/src/interface/index.ts index 9725d4cb..43f5cef8 100644 --- a/src/interface/index.ts +++ b/src/interface/index.ts @@ -19,8 +19,8 @@ export type { PixiVNJsonDialogText, default as PixiVNJsonLabelStep } from './Pix export type { PixiVNJsonCanvas, PixiVNJsonSound } from './PixiVNJsonMedia'; export type { PixiVNJsonOperation, default as PixiVNJsonOperations } from './PixiVNJsonOperations'; export type { default as PixiVNJsonStepSwitch } from './PixiVNJsonStepSwitch'; -export type { PixiVNJsonStorageGet, PixiVNJsonStorageSet } from './PixiVNJsonStorage'; export type { default as PixiVNJsonUnionCondition } from './PixiVNJsonUnionCondition'; +export type { PixiVNJsonValueGet as PixiVNJsonStorageGet, PixiVNJsonStorageSet } from './PixiVNJsonValue'; export type { default as SoundOptions, SoundPlayOptions } from './SoundOptions'; export type { default as TickerHistory, TickerHistoryForExport } from './TickerHistory'; export type { TickerProgrationType } from './TickerProgrationType'; diff --git a/src/managers/StepManager.ts b/src/managers/StepManager.ts index d3c54f59..35044244 100644 --- a/src/managers/StepManager.ts +++ b/src/managers/StepManager.ts @@ -16,7 +16,7 @@ import { ChoiceMenuOptionsType, Close, DialogueType, HistoryChoiceMenuOption } f import { LabelIdType } from "../types/LabelIdType" import { StepLabelPropsType, StepLabelResultType, StepLabelType } from "../types/StepLabelType" -type AllOpenedLabelsType = { [key: LabelIdType]: number } +type AllOpenedLabelsType = { [key: LabelIdType]: { biggestStep: number, openCount: number } } type AllChoicesMadeType = { label: LabelIdType, step: number, choice: number, stepSha1: string } type CurrentStepTimesCounterMemoty = { [key: LabelIdType]: { [key: number]: { lastStepIndexs: number[], stepSha1: string } } } @@ -37,10 +37,10 @@ export default class GameStepManager { * the key is the label id and the biggest step opened. */ private static get allOpenedLabels() { - return storage.getVariable(storage.keysSystem.ALL_OPENED_LABELS_KEY) || {} + return storage.getVariable(storage.keysSystem.OPENED_LABELS_COUNTER_KEY) || {} } private static set allOpenedLabels(value: AllOpenedLabelsType) { - storage.setVariable(storage.keysSystem.ALL_OPENED_LABELS_KEY, value) + storage.setVariable(storage.keysSystem.OPENED_LABELS_COUNTER_KEY, value) } /** * Counter of execution times of the current step. Current execution is also included. @@ -246,9 +246,10 @@ export default class GameStepManager { */ private static addLabelHistory(label: LabelIdType, stepIndex: number, stepSha: string, choiseMade?: number) { let allOpenedLabels = GameStepManager.allOpenedLabels - let oldStepIndex = GameStepManager.allOpenedLabels[label] + let oldStepIndex = GameStepManager.allOpenedLabels[label]?.biggestStep || 0 + let openCount = GameStepManager.allOpenedLabels[label]?.openCount || 0 if (!oldStepIndex || oldStepIndex < stepIndex) { - allOpenedLabels[label] = stepIndex + allOpenedLabels[label] = { biggestStep: stepIndex, openCount: openCount } GameStepManager.allOpenedLabels = allOpenedLabels } @@ -274,6 +275,11 @@ export default class GameStepManager { label: label, currentStepIndex: 0, }) + let allOpenedLabels = GameStepManager.allOpenedLabels + let biggestStep = GameStepManager.allOpenedLabels[label]?.biggestStep || 0 + let openCount = GameStepManager.allOpenedLabels[label]?.openCount || 0 + allOpenedLabels[label] = { biggestStep: biggestStep, openCount: openCount + 1 } + GameStepManager.allOpenedLabels = allOpenedLabels } /** * Close the current label and add it to the history. @@ -382,7 +388,7 @@ export default class GameStepManager { labelId = label.id } let allOpenedLabels = GameStepManager.allOpenedLabels - let lastStep = allOpenedLabels[labelId] + let lastStep = allOpenedLabels[labelId]?.biggestStep || 0 if (lastStep) { let currentLabel = getLabelById(labelId) if (currentLabel) { @@ -448,13 +454,20 @@ export default class GameStepManager { public static get isCurrentStepAlreadyOpened(): boolean { let currentLabel = GameStepManager.currentLabelId if (currentLabel) { - let lastStep = GameStepManager.allOpenedLabels[currentLabel] + let lastStep = GameStepManager.allOpenedLabels[currentLabel]?.openCount || 0 if (GameStepManager.currentLabelStepIndex && lastStep >= GameStepManager.currentLabelStepIndex) { return true } } return false } + /** + * Get times a label has been opened + * @returns times a label has been opened + */ + public static getTimesLabelOpened(label: LabelIdType): number { + return GameStepManager.allOpenedLabels[label]?.openCount || 0 + } /* Run Methods */ diff --git a/src/managers/StorageManager.ts b/src/managers/StorageManager.ts index 760d7aba..b135b223 100644 --- a/src/managers/StorageManager.ts +++ b/src/managers/StorageManager.ts @@ -39,7 +39,7 @@ export default class GameStorageManager { /** * The key of a list of all labels that have been opened during the progression of the steps. */ - ALL_OPENED_LABELS_KEY: "___all_opened_labels___", + OPENED_LABELS_COUNTER_KEY: "___opened_labels_counter___", /** * The key of a list of all choices that have been made during the progression of the steps. */