forked from profectus/Profectus
Add complete function to challenge
This commit is contained in:
parent
9c45653b8b
commit
14b1420b55
1 changed files with 28 additions and 7 deletions
|
@ -25,7 +25,7 @@ import {
|
||||||
ProcessedComputable
|
ProcessedComputable
|
||||||
} from "util/computed";
|
} from "util/computed";
|
||||||
import { createLazyProxy } from "util/proxies";
|
import { createLazyProxy } from "util/proxies";
|
||||||
import { computed, Ref, unref } from "vue";
|
import { computed, Ref, unref, watch, WatchStopHandle } from "vue";
|
||||||
|
|
||||||
export const ChallengeType = Symbol("ChallengeType");
|
export const ChallengeType = Symbol("ChallengeType");
|
||||||
|
|
||||||
|
@ -62,6 +62,7 @@ export interface BaseChallenge {
|
||||||
maxed: Ref<boolean>;
|
maxed: Ref<boolean>;
|
||||||
active: PersistentRef<boolean>;
|
active: PersistentRef<boolean>;
|
||||||
toggle: VoidFunction;
|
toggle: VoidFunction;
|
||||||
|
complete: (remainInChallenge?: boolean) => void;
|
||||||
type: typeof ChallengeType;
|
type: typeof ChallengeType;
|
||||||
[Component]: typeof ChallengeComponent;
|
[Component]: typeof ChallengeComponent;
|
||||||
[GatherProps]: () => Record<string, unknown>;
|
[GatherProps]: () => Record<string, unknown>;
|
||||||
|
@ -87,7 +88,7 @@ export type GenericChallenge = Replace<
|
||||||
{
|
{
|
||||||
visibility: ProcessedComputable<Visibility>;
|
visibility: ProcessedComputable<Visibility>;
|
||||||
canStart: ProcessedComputable<boolean>;
|
canStart: ProcessedComputable<boolean>;
|
||||||
canComplete: ProcessedComputable<boolean>;
|
canComplete: ProcessedComputable<boolean | DecimalSource>;
|
||||||
completionLimit: ProcessedComputable<DecimalSource>;
|
completionLimit: ProcessedComputable<DecimalSource>;
|
||||||
mark: ProcessedComputable<boolean>;
|
mark: ProcessedComputable<boolean>;
|
||||||
}
|
}
|
||||||
|
@ -134,11 +135,7 @@ export function createChallenge<T extends ChallengeOptions>(
|
||||||
challenge.toggle = function () {
|
challenge.toggle = function () {
|
||||||
const genericChallenge = challenge as GenericChallenge;
|
const genericChallenge = challenge as GenericChallenge;
|
||||||
if (genericChallenge.active.value) {
|
if (genericChallenge.active.value) {
|
||||||
if (
|
if (unref(genericChallenge.canComplete) && !genericChallenge.maxed.value) {
|
||||||
genericChallenge.canComplete &&
|
|
||||||
unref(genericChallenge.canComplete) &&
|
|
||||||
!genericChallenge.maxed.value
|
|
||||||
) {
|
|
||||||
let completions: boolean | DecimalSource = unref(genericChallenge.canComplete);
|
let completions: boolean | DecimalSource = unref(genericChallenge.canComplete);
|
||||||
if (typeof completions === "boolean") {
|
if (typeof completions === "boolean") {
|
||||||
completions = 1;
|
completions = 1;
|
||||||
|
@ -158,6 +155,30 @@ export function createChallenge<T extends ChallengeOptions>(
|
||||||
genericChallenge.onEnter?.();
|
genericChallenge.onEnter?.();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
challenge.complete = function (remainInChallenge?: boolean) {
|
||||||
|
const genericChallenge = challenge as GenericChallenge;
|
||||||
|
let completions: boolean | DecimalSource = unref(genericChallenge.canComplete);
|
||||||
|
if (
|
||||||
|
genericChallenge.active.value &&
|
||||||
|
completions !== false &&
|
||||||
|
(completions === true || Decimal.neq(0, completions)) &&
|
||||||
|
!genericChallenge.maxed.value
|
||||||
|
) {
|
||||||
|
if (typeof completions === "boolean") {
|
||||||
|
completions = 1;
|
||||||
|
}
|
||||||
|
genericChallenge.completions.value = Decimal.min(
|
||||||
|
Decimal.add(genericChallenge.completions.value, completions),
|
||||||
|
unref(genericChallenge.completionLimit)
|
||||||
|
);
|
||||||
|
genericChallenge.onComplete?.();
|
||||||
|
if (remainInChallenge !== true) {
|
||||||
|
genericChallenge.active.value = false;
|
||||||
|
genericChallenge.onExit?.();
|
||||||
|
genericChallenge.reset?.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
processComputable(challenge as T, "visibility");
|
processComputable(challenge as T, "visibility");
|
||||||
setDefault(challenge, "visibility", Visibility.Visible);
|
setDefault(challenge, "visibility", Visibility.Visible);
|
||||||
const visibility = challenge.visibility as ProcessedComputable<Visibility>;
|
const visibility = challenge.visibility as ProcessedComputable<Visibility>;
|
||||||
|
|
Loading…
Reference in a new issue