@flare-engine/achievements 0.4.18 → 0.4.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context.d.mts +9 -0
- package/dist/context.d.mts.map +1 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +16 -0
- package/dist/context.js.map +1 -0
- package/dist/context.mjs +15 -0
- package/dist/context.mjs.map +1 -0
- package/dist/index.d.mts +4 -74
- package/dist/index.d.ts +4 -74
- package/dist/index.js +8 -158
- package/dist/index.mjs +3 -153
- package/dist/memory.d.mts +42 -0
- package/dist/memory.d.mts.map +1 -0
- package/dist/memory.d.ts +42 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +126 -0
- package/dist/memory.js.map +1 -0
- package/dist/memory.mjs +125 -0
- package/dist/memory.mjs.map +1 -0
- package/dist/provider.d.mts +26 -0
- package/dist/provider.d.mts.map +1 -0
- package/dist/provider.d.ts +26 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +21 -0
- package/dist/provider.js.map +1 -0
- package/dist/provider.mjs +21 -0
- package/dist/provider.mjs.map +1 -0
- package/package.json +1 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AchievementsProvider } from "./provider.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
4
|
+
* `NoopAchievementsProvider`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const AchievementsContext: import("react").Context<AchievementsProvider>;
|
|
7
|
+
/** Hook returning the current `AchievementsProvider`. */
|
|
8
|
+
export declare function useAchievements(): AchievementsProvider;
|
|
9
|
+
//# sourceMappingURL=context.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.mts","names":[],"sources":["../src/context.ts"],"mappings":";;;;;qBAOa,qCAAmB,QAAA;;wBAIhB,mBAAmB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AchievementsProvider } from "./provider.js";
|
|
2
|
+
/**
|
|
3
|
+
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
4
|
+
* `NoopAchievementsProvider`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const AchievementsContext: import("react").Context<AchievementsProvider>;
|
|
7
|
+
/** Hook returning the current `AchievementsProvider`. */
|
|
8
|
+
export declare function useAchievements(): AchievementsProvider;
|
|
9
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","names":[],"sources":["../src/context.ts"],"mappings":";;;;;qBAOa,qCAAmB,QAAA;;wBAIhB,mBAAmB"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const require_provider = require("./provider.js");
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/context.ts
|
|
4
|
+
/**
|
|
5
|
+
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
6
|
+
* `NoopAchievementsProvider`.
|
|
7
|
+
*/
|
|
8
|
+
const AchievementsContext = /* @__PURE__ */ (0, react.createContext)(require_provider.NoopAchievementsProvider);
|
|
9
|
+
/** Hook returning the current `AchievementsProvider`. */
|
|
10
|
+
function useAchievements() {
|
|
11
|
+
return (0, react.useContext)(AchievementsContext);
|
|
12
|
+
}
|
|
13
|
+
exports.AchievementsContext = AchievementsContext;
|
|
14
|
+
exports.useAchievements = useAchievements;
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","names":["createContext","NoopAchievementsProvider","useContext"],"sources":["../src/context.ts"],"sourcesContent":["import { createContext, useContext } from 'react';\nimport { type AchievementsProvider, NoopAchievementsProvider } from './provider.js';\n\n/**\n * React context carrying the active `AchievementsProvider`. Defaults to\n * `NoopAchievementsProvider`.\n */\nexport const AchievementsContext =\n /* @__PURE__ */ createContext<AchievementsProvider>(NoopAchievementsProvider);\n\n/** Hook returning the current `AchievementsProvider`. */\nexport function useAchievements(): AchievementsProvider {\n return useContext(AchievementsContext);\n}\n"],"mappings":";;;;;;;AAOA,MAAa,sBACKA,iBAAAA,GAAAA,MAAAA,cAAAA,CAAoCC,iBAAAA,wBAAwB;;AAG9E,SAAgB,kBAAwC;CACtD,QAAA,GAAOC,MAAAA,WAAAA,CAAW,mBAAmB;AACvC"}
|
package/dist/context.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NoopAchievementsProvider } from "./provider.mjs";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
// src/context.ts
|
|
4
|
+
/**
|
|
5
|
+
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
6
|
+
* `NoopAchievementsProvider`.
|
|
7
|
+
*/
|
|
8
|
+
const AchievementsContext = /* @__PURE__ */ createContext(NoopAchievementsProvider);
|
|
9
|
+
/** Hook returning the current `AchievementsProvider`. */
|
|
10
|
+
function useAchievements() {
|
|
11
|
+
return useContext(AchievementsContext);
|
|
12
|
+
}
|
|
13
|
+
export { AchievementsContext, useAchievements };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.mjs","names":[],"sources":["../src/context.ts"],"sourcesContent":["import { createContext, useContext } from 'react';\nimport { type AchievementsProvider, NoopAchievementsProvider } from './provider.js';\n\n/**\n * React context carrying the active `AchievementsProvider`. Defaults to\n * `NoopAchievementsProvider`.\n */\nexport const AchievementsContext =\n /* @__PURE__ */ createContext<AchievementsProvider>(NoopAchievementsProvider);\n\n/** Hook returning the current `AchievementsProvider`. */\nexport function useAchievements(): AchievementsProvider {\n return useContext(AchievementsContext);\n}\n"],"mappings":";;;;;;;AAOA,MAAa,sBACK,8BAAoC,wBAAwB;;AAG9E,SAAgB,kBAAwC;CACtD,OAAO,WAAW,mBAAmB;AACvC"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,74 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
id: AchievementId;
|
|
6
|
-
unlocked: boolean;
|
|
7
|
-
/** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */
|
|
8
|
-
progress: number;
|
|
9
|
-
unlockedAt?: number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Achievement provider interface. Consumers supply a concrete adapter (Game
|
|
13
|
-
* Center, Google Play Games, Steam, custom backend) at the app boundary.
|
|
14
|
-
* `unlock()` is idempotent by contract — adapters must dedupe.
|
|
15
|
-
*/
|
|
16
|
-
interface AchievementsProvider {
|
|
17
|
-
unlock(id: AchievementId, progress?: number): Promise<void>;
|
|
18
|
-
getState(id: AchievementId): Promise<AchievementState>;
|
|
19
|
-
getAll(): Promise<readonly AchievementState[]>;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
23
|
-
* unlocks resolve silently.
|
|
24
|
-
*/
|
|
25
|
-
export declare const NoopAchievementsProvider: AchievementsProvider;
|
|
26
|
-
/**
|
|
27
|
-
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
28
|
-
* `NoopAchievementsProvider`.
|
|
29
|
-
*/
|
|
30
|
-
export declare const AchievementsContext: import("react").Context<AchievementsProvider>;
|
|
31
|
-
/** Hook returning the current `AchievementsProvider`. */
|
|
32
|
-
export declare function useAchievements(): AchievementsProvider;
|
|
33
|
-
/**
|
|
34
|
-
* Structural async key/value store the storage-backed provider persists into.
|
|
35
|
-
* Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`
|
|
36
|
-
* so a real adapter can be injected without depending on the storage package.
|
|
37
|
-
*/
|
|
38
|
-
interface AchievementsStore {
|
|
39
|
-
/** Read a value. Resolves to `null` if the key is not present. */
|
|
40
|
-
get(key: string): Promise<string | null>;
|
|
41
|
-
/** Write a value. */
|
|
42
|
-
set(key: string, value: string): Promise<void>;
|
|
43
|
-
}
|
|
44
|
-
/** Options common to the in-memory and storage-backed providers. */
|
|
45
|
-
interface MemoryAchievementsOptions {
|
|
46
|
-
/** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */
|
|
47
|
-
now?: () => number;
|
|
48
|
-
}
|
|
49
|
-
/** Options for the storage-backed provider. */
|
|
50
|
-
interface StorageAchievementsOptions extends MemoryAchievementsOptions {
|
|
51
|
-
/** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */
|
|
52
|
-
key?: string;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
56
|
-
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
57
|
-
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
58
|
-
*/
|
|
59
|
-
export declare function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider;
|
|
60
|
-
/**
|
|
61
|
-
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
62
|
-
* {@link createMemoryAchievements} and persists into the injected
|
|
63
|
-
* {@link AchievementsStore}.
|
|
64
|
-
*
|
|
65
|
-
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
66
|
-
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
67
|
-
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
68
|
-
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
69
|
-
* are serialized through a single chained tail promise so a read-modify-write can
|
|
70
|
-
* never interleave and lose an update under concurrent unlocks.
|
|
71
|
-
*/
|
|
72
|
-
export declare function createStorageAchievements(store: AchievementsStore, opts?: StorageAchievementsOptions): AchievementsProvider;
|
|
73
|
-
export type { AchievementId, AchievementState, AchievementsProvider, AchievementsStore, MemoryAchievementsOptions, StorageAchievementsOptions };
|
|
74
|
-
//# sourceMappingURL=index.d.mts.map
|
|
1
|
+
import { AchievementsContext, useAchievements } from "./context.mjs";
|
|
2
|
+
import { AchievementsStore, MemoryAchievementsOptions, StorageAchievementsOptions, createMemoryAchievements, createStorageAchievements } from "./memory.mjs";
|
|
3
|
+
import { AchievementId, AchievementState, AchievementsProvider, NoopAchievementsProvider } from "./provider.mjs";
|
|
4
|
+
export { type AchievementId, type AchievementState, AchievementsContext, type AchievementsProvider, type AchievementsStore, type MemoryAchievementsOptions, NoopAchievementsProvider, type StorageAchievementsOptions, createMemoryAchievements, createStorageAchievements, useAchievements };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,74 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
id: AchievementId;
|
|
6
|
-
unlocked: boolean;
|
|
7
|
-
/** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */
|
|
8
|
-
progress: number;
|
|
9
|
-
unlockedAt?: number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Achievement provider interface. Consumers supply a concrete adapter (Game
|
|
13
|
-
* Center, Google Play Games, Steam, custom backend) at the app boundary.
|
|
14
|
-
* `unlock()` is idempotent by contract — adapters must dedupe.
|
|
15
|
-
*/
|
|
16
|
-
interface AchievementsProvider {
|
|
17
|
-
unlock(id: AchievementId, progress?: number): Promise<void>;
|
|
18
|
-
getState(id: AchievementId): Promise<AchievementState>;
|
|
19
|
-
getAll(): Promise<readonly AchievementState[]>;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
23
|
-
* unlocks resolve silently.
|
|
24
|
-
*/
|
|
25
|
-
export declare const NoopAchievementsProvider: AchievementsProvider;
|
|
26
|
-
/**
|
|
27
|
-
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
28
|
-
* `NoopAchievementsProvider`.
|
|
29
|
-
*/
|
|
30
|
-
export declare const AchievementsContext: import("react").Context<AchievementsProvider>;
|
|
31
|
-
/** Hook returning the current `AchievementsProvider`. */
|
|
32
|
-
export declare function useAchievements(): AchievementsProvider;
|
|
33
|
-
/**
|
|
34
|
-
* Structural async key/value store the storage-backed provider persists into.
|
|
35
|
-
* Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`
|
|
36
|
-
* so a real adapter can be injected without depending on the storage package.
|
|
37
|
-
*/
|
|
38
|
-
interface AchievementsStore {
|
|
39
|
-
/** Read a value. Resolves to `null` if the key is not present. */
|
|
40
|
-
get(key: string): Promise<string | null>;
|
|
41
|
-
/** Write a value. */
|
|
42
|
-
set(key: string, value: string): Promise<void>;
|
|
43
|
-
}
|
|
44
|
-
/** Options common to the in-memory and storage-backed providers. */
|
|
45
|
-
interface MemoryAchievementsOptions {
|
|
46
|
-
/** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */
|
|
47
|
-
now?: () => number;
|
|
48
|
-
}
|
|
49
|
-
/** Options for the storage-backed provider. */
|
|
50
|
-
interface StorageAchievementsOptions extends MemoryAchievementsOptions {
|
|
51
|
-
/** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */
|
|
52
|
-
key?: string;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
56
|
-
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
57
|
-
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
58
|
-
*/
|
|
59
|
-
export declare function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider;
|
|
60
|
-
/**
|
|
61
|
-
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
62
|
-
* {@link createMemoryAchievements} and persists into the injected
|
|
63
|
-
* {@link AchievementsStore}.
|
|
64
|
-
*
|
|
65
|
-
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
66
|
-
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
67
|
-
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
68
|
-
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
69
|
-
* are serialized through a single chained tail promise so a read-modify-write can
|
|
70
|
-
* never interleave and lose an update under concurrent unlocks.
|
|
71
|
-
*/
|
|
72
|
-
export declare function createStorageAchievements(store: AchievementsStore, opts?: StorageAchievementsOptions): AchievementsProvider;
|
|
73
|
-
export type { AchievementId, AchievementState, AchievementsProvider, AchievementsStore, MemoryAchievementsOptions, StorageAchievementsOptions };
|
|
74
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
import { AchievementsContext, useAchievements } from "./context.js";
|
|
2
|
+
import { AchievementsStore, MemoryAchievementsOptions, StorageAchievementsOptions, createMemoryAchievements, createStorageAchievements } from "./memory.js";
|
|
3
|
+
import { AchievementId, AchievementState, AchievementsProvider, NoopAchievementsProvider } from "./provider.js";
|
|
4
|
+
export { type AchievementId, type AchievementState, AchievementsContext, type AchievementsProvider, type AchievementsStore, type MemoryAchievementsOptions, NoopAchievementsProvider, type StorageAchievementsOptions, createMemoryAchievements, createStorageAchievements, useAchievements };
|
package/dist/index.js
CHANGED
|
@@ -1,159 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
async getState(id) {
|
|
11
|
-
return {
|
|
12
|
-
id,
|
|
13
|
-
unlocked: false,
|
|
14
|
-
progress: 0
|
|
15
|
-
};
|
|
16
|
-
},
|
|
17
|
-
async getAll() {
|
|
18
|
-
return [];
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
// src/context.ts
|
|
22
|
-
/**
|
|
23
|
-
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
24
|
-
* `NoopAchievementsProvider`.
|
|
25
|
-
*/
|
|
26
|
-
const AchievementsContext = (0, react.createContext)(NoopAchievementsProvider);
|
|
27
|
-
/** Hook returning the current `AchievementsProvider`. */
|
|
28
|
-
function useAchievements() {
|
|
29
|
-
return (0, react.useContext)(AchievementsContext);
|
|
30
|
-
}
|
|
31
|
-
// src/memory.ts
|
|
32
|
-
const DEFAULT_KEY = "flare.achievements";
|
|
33
|
-
/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */
|
|
34
|
-
function normalizeProgress(value) {
|
|
35
|
-
if (!Number.isFinite(value)) return 0;
|
|
36
|
-
if (value < 0) return 0;
|
|
37
|
-
if (value > 1) return 1;
|
|
38
|
-
return value;
|
|
39
|
-
}
|
|
40
|
-
/** Default state for an untracked id — no `unlockedAt`. */
|
|
41
|
-
function defaultState(id) {
|
|
42
|
-
return {
|
|
43
|
-
id,
|
|
44
|
-
unlocked: false,
|
|
45
|
-
progress: 0
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Pure state transition shared by both providers. Given the current state, an
|
|
50
|
-
* optional progress arg and a clock, returns the next state:
|
|
51
|
-
*
|
|
52
|
-
* - `progress` omitted means a FULL unlock (target progress `1`).
|
|
53
|
-
* - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.
|
|
54
|
-
* - Progress is MONOTONIC — it never decreases below the stored value; a lower
|
|
55
|
-
* requested progress is ignored.
|
|
56
|
-
* - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps
|
|
57
|
-
* `unlockedAt` once (via `now`). An already-unlocked state keeps its original
|
|
58
|
-
* `unlockedAt`, so re-unlocking is a no-op.
|
|
59
|
-
*/
|
|
60
|
-
function mergeAchievement(current, progress, now) {
|
|
61
|
-
const requested = progress === void 0 ? 1 : normalizeProgress(progress);
|
|
62
|
-
const nextProgress = requested > current.progress ? requested : current.progress;
|
|
63
|
-
if (!current.unlocked && nextProgress < 1) return {
|
|
64
|
-
id: current.id,
|
|
65
|
-
unlocked: false,
|
|
66
|
-
progress: nextProgress
|
|
67
|
-
};
|
|
68
|
-
const unlockedAt = current.unlockedAt ?? now();
|
|
69
|
-
return {
|
|
70
|
-
id: current.id,
|
|
71
|
-
unlocked: true,
|
|
72
|
-
progress: 1,
|
|
73
|
-
unlockedAt
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
78
|
-
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
79
|
-
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
80
|
-
*/
|
|
81
|
-
function createMemoryAchievements(opts) {
|
|
82
|
-
const now = opts?.now ?? (() => Date.now());
|
|
83
|
-
const states = /* @__PURE__ */ new Map();
|
|
84
|
-
return {
|
|
85
|
-
async unlock(id, progress) {
|
|
86
|
-
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
87
|
-
},
|
|
88
|
-
async getState(id) {
|
|
89
|
-
return states.get(id) ?? defaultState(id);
|
|
90
|
-
},
|
|
91
|
-
async getAll() {
|
|
92
|
-
return [...states.values()];
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
98
|
-
* {@link createMemoryAchievements} and persists into the injected
|
|
99
|
-
* {@link AchievementsStore}.
|
|
100
|
-
*
|
|
101
|
-
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
102
|
-
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
103
|
-
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
104
|
-
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
105
|
-
* are serialized through a single chained tail promise so a read-modify-write can
|
|
106
|
-
* never interleave and lose an update under concurrent unlocks.
|
|
107
|
-
*/
|
|
108
|
-
function createStorageAchievements(store, opts) {
|
|
109
|
-
const now = opts?.now ?? (() => Date.now());
|
|
110
|
-
const key = opts?.key ?? DEFAULT_KEY;
|
|
111
|
-
const states = /* @__PURE__ */ new Map();
|
|
112
|
-
let hydration;
|
|
113
|
-
let tail = Promise.resolve();
|
|
114
|
-
function hydrate() {
|
|
115
|
-
if (hydration === void 0) {
|
|
116
|
-
const attempt = store.get(key).then((raw) => {
|
|
117
|
-
if (raw === null) return;
|
|
118
|
-
try {
|
|
119
|
-
const parsed = JSON.parse(raw);
|
|
120
|
-
for (const state of parsed) states.set(state.id, state);
|
|
121
|
-
} catch {}
|
|
122
|
-
}).catch((err) => {
|
|
123
|
-
if (hydration === attempt) hydration = void 0;
|
|
124
|
-
throw err;
|
|
125
|
-
});
|
|
126
|
-
hydration = attempt;
|
|
127
|
-
}
|
|
128
|
-
return hydration;
|
|
129
|
-
}
|
|
130
|
-
function persist() {
|
|
131
|
-
return store.set(key, JSON.stringify([...states.values()]));
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
unlock(id, progress) {
|
|
135
|
-
const run = tail.then(async () => {
|
|
136
|
-
await hydrate();
|
|
137
|
-
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
138
|
-
await persist();
|
|
139
|
-
});
|
|
140
|
-
tail = run.catch(() => {});
|
|
141
|
-
return run;
|
|
142
|
-
},
|
|
143
|
-
async getState(id) {
|
|
144
|
-
await hydrate();
|
|
145
|
-
return states.get(id) ?? defaultState(id);
|
|
146
|
-
},
|
|
147
|
-
async getAll() {
|
|
148
|
-
await hydrate();
|
|
149
|
-
return [...states.values()];
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
exports.AchievementsContext = AchievementsContext;
|
|
154
|
-
exports.NoopAchievementsProvider = NoopAchievementsProvider;
|
|
155
|
-
exports.createMemoryAchievements = createMemoryAchievements;
|
|
156
|
-
exports.createStorageAchievements = createStorageAchievements;
|
|
157
|
-
exports.useAchievements = useAchievements;
|
|
158
|
-
|
|
159
|
-
//# sourceMappingURL=index.js.map
|
|
2
|
+
const require_context = require("./context.js");
|
|
3
|
+
const require_memory = require("./memory.js");
|
|
4
|
+
const require_provider = require("./provider.js");
|
|
5
|
+
exports.AchievementsContext = require_context.AchievementsContext;
|
|
6
|
+
exports.NoopAchievementsProvider = require_provider.NoopAchievementsProvider;
|
|
7
|
+
exports.createMemoryAchievements = require_memory.createMemoryAchievements;
|
|
8
|
+
exports.createStorageAchievements = require_memory.createStorageAchievements;
|
|
9
|
+
exports.useAchievements = require_context.useAchievements;
|
package/dist/index.mjs
CHANGED
|
@@ -1,154 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
5
|
-
* unlocks resolve silently.
|
|
6
|
-
*/
|
|
7
|
-
const NoopAchievementsProvider = {
|
|
8
|
-
async unlock() {},
|
|
9
|
-
async getState(id) {
|
|
10
|
-
return {
|
|
11
|
-
id,
|
|
12
|
-
unlocked: false,
|
|
13
|
-
progress: 0
|
|
14
|
-
};
|
|
15
|
-
},
|
|
16
|
-
async getAll() {
|
|
17
|
-
return [];
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
// src/context.ts
|
|
21
|
-
/**
|
|
22
|
-
* React context carrying the active `AchievementsProvider`. Defaults to
|
|
23
|
-
* `NoopAchievementsProvider`.
|
|
24
|
-
*/
|
|
25
|
-
const AchievementsContext = createContext(NoopAchievementsProvider);
|
|
26
|
-
/** Hook returning the current `AchievementsProvider`. */
|
|
27
|
-
function useAchievements() {
|
|
28
|
-
return useContext(AchievementsContext);
|
|
29
|
-
}
|
|
30
|
-
// src/memory.ts
|
|
31
|
-
const DEFAULT_KEY = "flare.achievements";
|
|
32
|
-
/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */
|
|
33
|
-
function normalizeProgress(value) {
|
|
34
|
-
if (!Number.isFinite(value)) return 0;
|
|
35
|
-
if (value < 0) return 0;
|
|
36
|
-
if (value > 1) return 1;
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
/** Default state for an untracked id — no `unlockedAt`. */
|
|
40
|
-
function defaultState(id) {
|
|
41
|
-
return {
|
|
42
|
-
id,
|
|
43
|
-
unlocked: false,
|
|
44
|
-
progress: 0
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Pure state transition shared by both providers. Given the current state, an
|
|
49
|
-
* optional progress arg and a clock, returns the next state:
|
|
50
|
-
*
|
|
51
|
-
* - `progress` omitted means a FULL unlock (target progress `1`).
|
|
52
|
-
* - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.
|
|
53
|
-
* - Progress is MONOTONIC — it never decreases below the stored value; a lower
|
|
54
|
-
* requested progress is ignored.
|
|
55
|
-
* - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps
|
|
56
|
-
* `unlockedAt` once (via `now`). An already-unlocked state keeps its original
|
|
57
|
-
* `unlockedAt`, so re-unlocking is a no-op.
|
|
58
|
-
*/
|
|
59
|
-
function mergeAchievement(current, progress, now) {
|
|
60
|
-
const requested = progress === void 0 ? 1 : normalizeProgress(progress);
|
|
61
|
-
const nextProgress = requested > current.progress ? requested : current.progress;
|
|
62
|
-
if (!current.unlocked && nextProgress < 1) return {
|
|
63
|
-
id: current.id,
|
|
64
|
-
unlocked: false,
|
|
65
|
-
progress: nextProgress
|
|
66
|
-
};
|
|
67
|
-
const unlockedAt = current.unlockedAt ?? now();
|
|
68
|
-
return {
|
|
69
|
-
id: current.id,
|
|
70
|
-
unlocked: true,
|
|
71
|
-
progress: 1,
|
|
72
|
-
unlockedAt
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
77
|
-
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
78
|
-
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
79
|
-
*/
|
|
80
|
-
function createMemoryAchievements(opts) {
|
|
81
|
-
const now = opts?.now ?? (() => Date.now());
|
|
82
|
-
const states = /* @__PURE__ */ new Map();
|
|
83
|
-
return {
|
|
84
|
-
async unlock(id, progress) {
|
|
85
|
-
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
86
|
-
},
|
|
87
|
-
async getState(id) {
|
|
88
|
-
return states.get(id) ?? defaultState(id);
|
|
89
|
-
},
|
|
90
|
-
async getAll() {
|
|
91
|
-
return [...states.values()];
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
97
|
-
* {@link createMemoryAchievements} and persists into the injected
|
|
98
|
-
* {@link AchievementsStore}.
|
|
99
|
-
*
|
|
100
|
-
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
101
|
-
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
102
|
-
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
103
|
-
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
104
|
-
* are serialized through a single chained tail promise so a read-modify-write can
|
|
105
|
-
* never interleave and lose an update under concurrent unlocks.
|
|
106
|
-
*/
|
|
107
|
-
function createStorageAchievements(store, opts) {
|
|
108
|
-
const now = opts?.now ?? (() => Date.now());
|
|
109
|
-
const key = opts?.key ?? DEFAULT_KEY;
|
|
110
|
-
const states = /* @__PURE__ */ new Map();
|
|
111
|
-
let hydration;
|
|
112
|
-
let tail = Promise.resolve();
|
|
113
|
-
function hydrate() {
|
|
114
|
-
if (hydration === void 0) {
|
|
115
|
-
const attempt = store.get(key).then((raw) => {
|
|
116
|
-
if (raw === null) return;
|
|
117
|
-
try {
|
|
118
|
-
const parsed = JSON.parse(raw);
|
|
119
|
-
for (const state of parsed) states.set(state.id, state);
|
|
120
|
-
} catch {}
|
|
121
|
-
}).catch((err) => {
|
|
122
|
-
if (hydration === attempt) hydration = void 0;
|
|
123
|
-
throw err;
|
|
124
|
-
});
|
|
125
|
-
hydration = attempt;
|
|
126
|
-
}
|
|
127
|
-
return hydration;
|
|
128
|
-
}
|
|
129
|
-
function persist() {
|
|
130
|
-
return store.set(key, JSON.stringify([...states.values()]));
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
unlock(id, progress) {
|
|
134
|
-
const run = tail.then(async () => {
|
|
135
|
-
await hydrate();
|
|
136
|
-
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
137
|
-
await persist();
|
|
138
|
-
});
|
|
139
|
-
tail = run.catch(() => {});
|
|
140
|
-
return run;
|
|
141
|
-
},
|
|
142
|
-
async getState(id) {
|
|
143
|
-
await hydrate();
|
|
144
|
-
return states.get(id) ?? defaultState(id);
|
|
145
|
-
},
|
|
146
|
-
async getAll() {
|
|
147
|
-
await hydrate();
|
|
148
|
-
return [...states.values()];
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
1
|
+
import { AchievementsContext, useAchievements } from "./context.mjs";
|
|
2
|
+
import { createMemoryAchievements, createStorageAchievements } from "./memory.mjs";
|
|
3
|
+
import { NoopAchievementsProvider } from "./provider.mjs";
|
|
152
4
|
export { AchievementsContext, NoopAchievementsProvider, createMemoryAchievements, createStorageAchievements, useAchievements };
|
|
153
|
-
|
|
154
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AchievementsProvider } from "./provider.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Structural async key/value store the storage-backed provider persists into.
|
|
4
|
+
* Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`
|
|
5
|
+
* so a real adapter can be injected without depending on the storage package.
|
|
6
|
+
*/
|
|
7
|
+
export interface AchievementsStore {
|
|
8
|
+
/** Read a value. Resolves to `null` if the key is not present. */
|
|
9
|
+
get(key: string): Promise<string | null>;
|
|
10
|
+
/** Write a value. */
|
|
11
|
+
set(key: string, value: string): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/** Options common to the in-memory and storage-backed providers. */
|
|
14
|
+
export interface MemoryAchievementsOptions {
|
|
15
|
+
/** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */
|
|
16
|
+
now?: () => number;
|
|
17
|
+
}
|
|
18
|
+
/** Options for the storage-backed provider. */
|
|
19
|
+
export interface StorageAchievementsOptions extends MemoryAchievementsOptions {
|
|
20
|
+
/** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */
|
|
21
|
+
key?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
25
|
+
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
26
|
+
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider;
|
|
29
|
+
/**
|
|
30
|
+
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
31
|
+
* {@link createMemoryAchievements} and persists into the injected
|
|
32
|
+
* {@link AchievementsStore}.
|
|
33
|
+
*
|
|
34
|
+
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
35
|
+
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
36
|
+
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
37
|
+
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
38
|
+
* are serialized through a single chained tail promise so a read-modify-write can
|
|
39
|
+
* never interleave and lose an update under concurrent unlocks.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createStorageAchievements(store: AchievementsStore, opts?: StorageAchievementsOptions): AchievementsProvider;
|
|
42
|
+
//# sourceMappingURL=memory.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.mts","names":[],"sources":["../src/memory.ts"],"mappings":";;;;;;iBAOiB;;EAEf,IAAI,cAAc;;EAElB,IAAI,aAAa,gBAAgB;;;iBAIlB;;EAEf;;;iBAIe,mCAAmC;;EAElD;;;;;;;wBAiDc,yBAAyB,OAAO,4BAA4B;;;;;;;;;;;;;wBA4B5D,0BACd,OAAO,mBACP,OAAO,6BACN"}
|
package/dist/memory.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AchievementsProvider } from "./provider.js";
|
|
2
|
+
/**
|
|
3
|
+
* Structural async key/value store the storage-backed provider persists into.
|
|
4
|
+
* Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`
|
|
5
|
+
* so a real adapter can be injected without depending on the storage package.
|
|
6
|
+
*/
|
|
7
|
+
export interface AchievementsStore {
|
|
8
|
+
/** Read a value. Resolves to `null` if the key is not present. */
|
|
9
|
+
get(key: string): Promise<string | null>;
|
|
10
|
+
/** Write a value. */
|
|
11
|
+
set(key: string, value: string): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/** Options common to the in-memory and storage-backed providers. */
|
|
14
|
+
export interface MemoryAchievementsOptions {
|
|
15
|
+
/** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */
|
|
16
|
+
now?: () => number;
|
|
17
|
+
}
|
|
18
|
+
/** Options for the storage-backed provider. */
|
|
19
|
+
export interface StorageAchievementsOptions extends MemoryAchievementsOptions {
|
|
20
|
+
/** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */
|
|
21
|
+
key?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
25
|
+
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
26
|
+
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider;
|
|
29
|
+
/**
|
|
30
|
+
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
31
|
+
* {@link createMemoryAchievements} and persists into the injected
|
|
32
|
+
* {@link AchievementsStore}.
|
|
33
|
+
*
|
|
34
|
+
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
35
|
+
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
36
|
+
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
37
|
+
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
38
|
+
* are serialized through a single chained tail promise so a read-modify-write can
|
|
39
|
+
* never interleave and lose an update under concurrent unlocks.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createStorageAchievements(store: AchievementsStore, opts?: StorageAchievementsOptions): AchievementsProvider;
|
|
42
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","names":[],"sources":["../src/memory.ts"],"mappings":";;;;;;iBAOiB;;EAEf,IAAI,cAAc;;EAElB,IAAI,aAAa,gBAAgB;;;iBAIlB;;EAEf;;;iBAIe,mCAAmC;;EAElD;;;;;;;wBAiDc,yBAAyB,OAAO,4BAA4B;;;;;;;;;;;;;wBA4B5D,0BACd,OAAO,mBACP,OAAO,6BACN"}
|
package/dist/memory.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// src/memory.ts
|
|
2
|
+
const DEFAULT_KEY = "flare.achievements";
|
|
3
|
+
/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */
|
|
4
|
+
function normalizeProgress(value) {
|
|
5
|
+
if (!Number.isFinite(value)) return 0;
|
|
6
|
+
if (value < 0) return 0;
|
|
7
|
+
if (value > 1) return 1;
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
/** Default state for an untracked id — no `unlockedAt`. */
|
|
11
|
+
function defaultState(id) {
|
|
12
|
+
return {
|
|
13
|
+
id,
|
|
14
|
+
unlocked: false,
|
|
15
|
+
progress: 0
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Pure state transition shared by both providers. Given the current state, an
|
|
20
|
+
* optional progress arg and a clock, returns the next state:
|
|
21
|
+
*
|
|
22
|
+
* - `progress` omitted means a FULL unlock (target progress `1`).
|
|
23
|
+
* - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.
|
|
24
|
+
* - Progress is MONOTONIC — it never decreases below the stored value; a lower
|
|
25
|
+
* requested progress is ignored.
|
|
26
|
+
* - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps
|
|
27
|
+
* `unlockedAt` once (via `now`). An already-unlocked state keeps its original
|
|
28
|
+
* `unlockedAt`, so re-unlocking is a no-op.
|
|
29
|
+
*/
|
|
30
|
+
function mergeAchievement(current, progress, now) {
|
|
31
|
+
const requested = progress === void 0 ? 1 : normalizeProgress(progress);
|
|
32
|
+
const nextProgress = requested > current.progress ? requested : current.progress;
|
|
33
|
+
if (!current.unlocked && nextProgress < 1) return {
|
|
34
|
+
id: current.id,
|
|
35
|
+
unlocked: false,
|
|
36
|
+
progress: nextProgress
|
|
37
|
+
};
|
|
38
|
+
const unlockedAt = current.unlockedAt ?? now();
|
|
39
|
+
return {
|
|
40
|
+
id: current.id,
|
|
41
|
+
unlocked: true,
|
|
42
|
+
progress: 1,
|
|
43
|
+
unlockedAt
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
48
|
+
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
49
|
+
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
50
|
+
*/
|
|
51
|
+
function createMemoryAchievements(opts) {
|
|
52
|
+
const now = opts?.now ?? (() => Date.now());
|
|
53
|
+
const states = /* @__PURE__ */ new Map();
|
|
54
|
+
return {
|
|
55
|
+
async unlock(id, progress) {
|
|
56
|
+
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
57
|
+
},
|
|
58
|
+
async getState(id) {
|
|
59
|
+
return states.get(id) ?? defaultState(id);
|
|
60
|
+
},
|
|
61
|
+
async getAll() {
|
|
62
|
+
return [...states.values()];
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
68
|
+
* {@link createMemoryAchievements} and persists into the injected
|
|
69
|
+
* {@link AchievementsStore}.
|
|
70
|
+
*
|
|
71
|
+
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
72
|
+
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
73
|
+
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
74
|
+
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
75
|
+
* are serialized through a single chained tail promise so a read-modify-write can
|
|
76
|
+
* never interleave and lose an update under concurrent unlocks.
|
|
77
|
+
*/
|
|
78
|
+
function createStorageAchievements(store, opts) {
|
|
79
|
+
const now = opts?.now ?? (() => Date.now());
|
|
80
|
+
const key = opts?.key ?? DEFAULT_KEY;
|
|
81
|
+
const states = /* @__PURE__ */ new Map();
|
|
82
|
+
let hydration;
|
|
83
|
+
let tail = Promise.resolve();
|
|
84
|
+
function hydrate() {
|
|
85
|
+
if (hydration === void 0) {
|
|
86
|
+
const attempt = store.get(key).then((raw) => {
|
|
87
|
+
if (raw === null) return;
|
|
88
|
+
try {
|
|
89
|
+
const parsed = JSON.parse(raw);
|
|
90
|
+
for (const state of parsed) states.set(state.id, state);
|
|
91
|
+
} catch {}
|
|
92
|
+
}).catch((err) => {
|
|
93
|
+
if (hydration === attempt) hydration = void 0;
|
|
94
|
+
throw err;
|
|
95
|
+
});
|
|
96
|
+
hydration = attempt;
|
|
97
|
+
}
|
|
98
|
+
return hydration;
|
|
99
|
+
}
|
|
100
|
+
function persist() {
|
|
101
|
+
return store.set(key, JSON.stringify([...states.values()]));
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
unlock(id, progress) {
|
|
105
|
+
const run = tail.then(async () => {
|
|
106
|
+
await hydrate();
|
|
107
|
+
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
108
|
+
await persist();
|
|
109
|
+
});
|
|
110
|
+
tail = run.catch(() => {});
|
|
111
|
+
return run;
|
|
112
|
+
},
|
|
113
|
+
async getState(id) {
|
|
114
|
+
await hydrate();
|
|
115
|
+
return states.get(id) ?? defaultState(id);
|
|
116
|
+
},
|
|
117
|
+
async getAll() {
|
|
118
|
+
await hydrate();
|
|
119
|
+
return [...states.values()];
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
exports.createMemoryAchievements = createMemoryAchievements;
|
|
124
|
+
exports.createStorageAchievements = createStorageAchievements;
|
|
125
|
+
|
|
126
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","names":[],"sources":["../src/memory.ts"],"sourcesContent":["import type { AchievementId, AchievementState, AchievementsProvider } from './provider.js';\n\n/**\n * Structural async key/value store the storage-backed provider persists into.\n * Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`\n * so a real adapter can be injected without depending on the storage package.\n */\nexport interface AchievementsStore {\n /** Read a value. Resolves to `null` if the key is not present. */\n get(key: string): Promise<string | null>;\n /** Write a value. */\n set(key: string, value: string): Promise<void>;\n}\n\n/** Options common to the in-memory and storage-backed providers. */\nexport interface MemoryAchievementsOptions {\n /** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\n/** Options for the storage-backed provider. */\nexport interface StorageAchievementsOptions extends MemoryAchievementsOptions {\n /** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */\n key?: string;\n}\n\nconst DEFAULT_KEY = 'flare.achievements';\n\n/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */\nfunction normalizeProgress(value: number): number {\n if (!Number.isFinite(value)) return 0;\n if (value < 0) return 0;\n if (value > 1) return 1;\n return value;\n}\n\n/** Default state for an untracked id — no `unlockedAt`. */\nfunction defaultState(id: AchievementId): AchievementState {\n return { id, unlocked: false, progress: 0 };\n}\n\n/**\n * Pure state transition shared by both providers. Given the current state, an\n * optional progress arg and a clock, returns the next state:\n *\n * - `progress` omitted means a FULL unlock (target progress `1`).\n * - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.\n * - Progress is MONOTONIC — it never decreases below the stored value; a lower\n * requested progress is ignored.\n * - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps\n * `unlockedAt` once (via `now`). An already-unlocked state keeps its original\n * `unlockedAt`, so re-unlocking is a no-op.\n */\nfunction mergeAchievement(\n current: AchievementState,\n progress: number | undefined,\n now: () => number,\n): AchievementState {\n const requested = progress === undefined ? 1 : normalizeProgress(progress);\n const nextProgress = requested > current.progress ? requested : current.progress;\n if (!current.unlocked && nextProgress < 1) {\n return { id: current.id, unlocked: false, progress: nextProgress };\n }\n const unlockedAt = current.unlockedAt ?? now();\n return { id: current.id, unlocked: true, progress: 1, unlockedAt };\n}\n\n/**\n * In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with\n * a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.\n * State is not persisted — it lives for the lifetime of the returned provider.\n */\nexport function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const states = new Map<AchievementId, AchievementState>();\n return {\n async unlock(id: AchievementId, progress?: number): Promise<void> {\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [...states.values()];\n },\n };\n}\n\n/**\n * Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of\n * {@link createMemoryAchievements} and persists into the injected\n * {@link AchievementsStore}.\n *\n * State hydrates lazily from `store.get(key)` on the first call (a JSON array of\n * `AchievementState`; a parse error hydrates empty) and is written back via\n * `store.set` after every mutation. A rejected `store.get` rejects that call and\n * the next call retries; nothing is written until a read succeeds. All mutations\n * are serialized through a single chained tail promise so a read-modify-write can\n * never interleave and lose an update under concurrent unlocks.\n */\nexport function createStorageAchievements(\n store: AchievementsStore,\n opts?: StorageAchievementsOptions,\n): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const key = opts?.key ?? DEFAULT_KEY;\n const states = new Map<AchievementId, AchievementState>();\n let hydration: Promise<void> | undefined;\n let tail: Promise<void> = Promise.resolve();\n\n function hydrate(): Promise<void> {\n if (hydration === undefined) {\n const attempt: Promise<void> = store\n .get(key)\n .then((raw) => {\n if (raw === null) return;\n try {\n const parsed = JSON.parse(raw) as AchievementState[];\n for (const state of parsed) states.set(state.id, state);\n } catch {\n // Corrupt payload — hydrate empty.\n }\n })\n .catch((err: unknown) => {\n // Compare-and-clear: drop only OUR failed attempt so the next call retries the read.\n if (hydration === attempt) hydration = undefined;\n throw err;\n });\n hydration = attempt;\n }\n return hydration;\n }\n\n function persist(): Promise<void> {\n return store.set(key, JSON.stringify([...states.values()]));\n }\n\n return {\n unlock(id: AchievementId, progress?: number): Promise<void> {\n const run = tail.then(async () => {\n await hydrate();\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n await persist();\n });\n // Swallow on the tail so one failed write cannot break serialization for\n // later mutations; the caller still observes the error via `run`.\n tail = run.catch(() => {});\n return run;\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n await hydrate();\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n await hydrate();\n return [...states.values()];\n },\n };\n}\n"],"mappings":";AA0BA,MAAM,cAAc;;AAGpB,SAAS,kBAAkB,OAAuB;CAChD,IAAI,CAA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AAyB,MAC5B,IAAI,GAAG,CAAC,CACR,MAAM,QAAQ;IACb,IAAI,QAAQ,MAAM;IAClB,IAAI;KACF,MAAM,SAAS,KAAK,MAAM,GAAG;KAC7B,KAAK,MAAM,SAAS,QAAQ,OAAO,IAAI,MAAM,IAAI,KAAK;IACxD,QAAQ,CAER;GACF,CAAC,CAAC,CACD,OAAO,QAAiB;IAEvB,IAAI,cAAc,SAAS,YAAY,KAAA;IACvC,MAAM;GACR,CAAC;GACH,YAAY;EACd;EACA,OAAO;CACT;CAEA,SAAS,UAAyB;EAChC,OAAO,MAAM,IAAI,KAAK,KAAK,UAAU,CAAC,GAAG,OAAO,OAAO,CAAC,CAAC,CAAC;CAC5D;CAEA,OAAO;EACL,OAAO,IAAmB,UAAkC;GAC1D,MAAM,MAAM,KAAK,KAAK,YAAY;IAChC,MAAM,QAAQ;IACd,OAAO,IAAI,IAAI,iBAAiB,OAAO,IAAI,EAAE,KAAK,aAAa,EAAE,GAAG,UAAU,GAAG,CAAC;IAClF,MAAM,QAAQ;GAChB,CAAC;GAGD,OAAO,IAAI,YAAY,CAAC,CAAC;GACzB,OAAO;EACT;EACA,MAAM,SAAS,IAA8C;GAC3D,MAAM,QAAQ;GACd,OAAO,OAAO,IAAI,EAAE,KAAK,aAAa,EAAE;EAC1C;EACA,MAAM,SAA+C;GACnD,MAAM,QAAQ;GACd,OAAO,CAAC,GAAG,OAAO,OAAO,CAAC;EAC5B;CACF;AACF"}
|
package/dist/memory.mjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// src/memory.ts
|
|
2
|
+
const DEFAULT_KEY = "flare.achievements";
|
|
3
|
+
/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */
|
|
4
|
+
function normalizeProgress(value) {
|
|
5
|
+
if (!Number.isFinite(value)) return 0;
|
|
6
|
+
if (value < 0) return 0;
|
|
7
|
+
if (value > 1) return 1;
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
/** Default state for an untracked id — no `unlockedAt`. */
|
|
11
|
+
function defaultState(id) {
|
|
12
|
+
return {
|
|
13
|
+
id,
|
|
14
|
+
unlocked: false,
|
|
15
|
+
progress: 0
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Pure state transition shared by both providers. Given the current state, an
|
|
20
|
+
* optional progress arg and a clock, returns the next state:
|
|
21
|
+
*
|
|
22
|
+
* - `progress` omitted means a FULL unlock (target progress `1`).
|
|
23
|
+
* - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.
|
|
24
|
+
* - Progress is MONOTONIC — it never decreases below the stored value; a lower
|
|
25
|
+
* requested progress is ignored.
|
|
26
|
+
* - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps
|
|
27
|
+
* `unlockedAt` once (via `now`). An already-unlocked state keeps its original
|
|
28
|
+
* `unlockedAt`, so re-unlocking is a no-op.
|
|
29
|
+
*/
|
|
30
|
+
function mergeAchievement(current, progress, now) {
|
|
31
|
+
const requested = progress === void 0 ? 1 : normalizeProgress(progress);
|
|
32
|
+
const nextProgress = requested > current.progress ? requested : current.progress;
|
|
33
|
+
if (!current.unlocked && nextProgress < 1) return {
|
|
34
|
+
id: current.id,
|
|
35
|
+
unlocked: false,
|
|
36
|
+
progress: nextProgress
|
|
37
|
+
};
|
|
38
|
+
const unlockedAt = current.unlockedAt ?? now();
|
|
39
|
+
return {
|
|
40
|
+
id: current.id,
|
|
41
|
+
unlocked: true,
|
|
42
|
+
progress: 1,
|
|
43
|
+
unlockedAt
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with
|
|
48
|
+
* a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.
|
|
49
|
+
* State is not persisted — it lives for the lifetime of the returned provider.
|
|
50
|
+
*/
|
|
51
|
+
function createMemoryAchievements(opts) {
|
|
52
|
+
const now = opts?.now ?? (() => Date.now());
|
|
53
|
+
const states = /* @__PURE__ */ new Map();
|
|
54
|
+
return {
|
|
55
|
+
async unlock(id, progress) {
|
|
56
|
+
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
57
|
+
},
|
|
58
|
+
async getState(id) {
|
|
59
|
+
return states.get(id) ?? defaultState(id);
|
|
60
|
+
},
|
|
61
|
+
async getAll() {
|
|
62
|
+
return [...states.values()];
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of
|
|
68
|
+
* {@link createMemoryAchievements} and persists into the injected
|
|
69
|
+
* {@link AchievementsStore}.
|
|
70
|
+
*
|
|
71
|
+
* State hydrates lazily from `store.get(key)` on the first call (a JSON array of
|
|
72
|
+
* `AchievementState`; a parse error hydrates empty) and is written back via
|
|
73
|
+
* `store.set` after every mutation. A rejected `store.get` rejects that call and
|
|
74
|
+
* the next call retries; nothing is written until a read succeeds. All mutations
|
|
75
|
+
* are serialized through a single chained tail promise so a read-modify-write can
|
|
76
|
+
* never interleave and lose an update under concurrent unlocks.
|
|
77
|
+
*/
|
|
78
|
+
function createStorageAchievements(store, opts) {
|
|
79
|
+
const now = opts?.now ?? (() => Date.now());
|
|
80
|
+
const key = opts?.key ?? DEFAULT_KEY;
|
|
81
|
+
const states = /* @__PURE__ */ new Map();
|
|
82
|
+
let hydration;
|
|
83
|
+
let tail = Promise.resolve();
|
|
84
|
+
function hydrate() {
|
|
85
|
+
if (hydration === void 0) {
|
|
86
|
+
const attempt = store.get(key).then((raw) => {
|
|
87
|
+
if (raw === null) return;
|
|
88
|
+
try {
|
|
89
|
+
const parsed = JSON.parse(raw);
|
|
90
|
+
for (const state of parsed) states.set(state.id, state);
|
|
91
|
+
} catch {}
|
|
92
|
+
}).catch((err) => {
|
|
93
|
+
if (hydration === attempt) hydration = void 0;
|
|
94
|
+
throw err;
|
|
95
|
+
});
|
|
96
|
+
hydration = attempt;
|
|
97
|
+
}
|
|
98
|
+
return hydration;
|
|
99
|
+
}
|
|
100
|
+
function persist() {
|
|
101
|
+
return store.set(key, JSON.stringify([...states.values()]));
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
unlock(id, progress) {
|
|
105
|
+
const run = tail.then(async () => {
|
|
106
|
+
await hydrate();
|
|
107
|
+
states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));
|
|
108
|
+
await persist();
|
|
109
|
+
});
|
|
110
|
+
tail = run.catch(() => {});
|
|
111
|
+
return run;
|
|
112
|
+
},
|
|
113
|
+
async getState(id) {
|
|
114
|
+
await hydrate();
|
|
115
|
+
return states.get(id) ?? defaultState(id);
|
|
116
|
+
},
|
|
117
|
+
async getAll() {
|
|
118
|
+
await hydrate();
|
|
119
|
+
return [...states.values()];
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
export { createMemoryAchievements, createStorageAchievements };
|
|
124
|
+
|
|
125
|
+
//# sourceMappingURL=memory.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.mjs","names":[],"sources":["../src/memory.ts"],"sourcesContent":["import type { AchievementId, AchievementState, AchievementsProvider } from './provider.js';\n\n/**\n * Structural async key/value store the storage-backed provider persists into.\n * Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`\n * so a real adapter can be injected without depending on the storage package.\n */\nexport interface AchievementsStore {\n /** Read a value. Resolves to `null` if the key is not present. */\n get(key: string): Promise<string | null>;\n /** Write a value. */\n set(key: string, value: string): Promise<void>;\n}\n\n/** Options common to the in-memory and storage-backed providers. */\nexport interface MemoryAchievementsOptions {\n /** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\n/** Options for the storage-backed provider. */\nexport interface StorageAchievementsOptions extends MemoryAchievementsOptions {\n /** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */\n key?: string;\n}\n\nconst DEFAULT_KEY = 'flare.achievements';\n\n/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */\nfunction normalizeProgress(value: number): number {\n if (!Number.isFinite(value)) return 0;\n if (value < 0) return 0;\n if (value > 1) return 1;\n return value;\n}\n\n/** Default state for an untracked id — no `unlockedAt`. */\nfunction defaultState(id: AchievementId): AchievementState {\n return { id, unlocked: false, progress: 0 };\n}\n\n/**\n * Pure state transition shared by both providers. Given the current state, an\n * optional progress arg and a clock, returns the next state:\n *\n * - `progress` omitted means a FULL unlock (target progress `1`).\n * - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.\n * - Progress is MONOTONIC — it never decreases below the stored value; a lower\n * requested progress is ignored.\n * - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps\n * `unlockedAt` once (via `now`). An already-unlocked state keeps its original\n * `unlockedAt`, so re-unlocking is a no-op.\n */\nfunction mergeAchievement(\n current: AchievementState,\n progress: number | undefined,\n now: () => number,\n): AchievementState {\n const requested = progress === undefined ? 1 : normalizeProgress(progress);\n const nextProgress = requested > current.progress ? requested : current.progress;\n if (!current.unlocked && nextProgress < 1) {\n return { id: current.id, unlocked: false, progress: nextProgress };\n }\n const unlockedAt = current.unlockedAt ?? now();\n return { id: current.id, unlocked: true, progress: 1, unlockedAt };\n}\n\n/**\n * In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with\n * a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.\n * State is not persisted — it lives for the lifetime of the returned provider.\n */\nexport function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const states = new Map<AchievementId, AchievementState>();\n return {\n async unlock(id: AchievementId, progress?: number): Promise<void> {\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [...states.values()];\n },\n };\n}\n\n/**\n * Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of\n * {@link createMemoryAchievements} and persists into the injected\n * {@link AchievementsStore}.\n *\n * State hydrates lazily from `store.get(key)` on the first call (a JSON array of\n * `AchievementState`; a parse error hydrates empty) and is written back via\n * `store.set` after every mutation. A rejected `store.get` rejects that call and\n * the next call retries; nothing is written until a read succeeds. All mutations\n * are serialized through a single chained tail promise so a read-modify-write can\n * never interleave and lose an update under concurrent unlocks.\n */\nexport function createStorageAchievements(\n store: AchievementsStore,\n opts?: StorageAchievementsOptions,\n): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const key = opts?.key ?? DEFAULT_KEY;\n const states = new Map<AchievementId, AchievementState>();\n let hydration: Promise<void> | undefined;\n let tail: Promise<void> = Promise.resolve();\n\n function hydrate(): Promise<void> {\n if (hydration === undefined) {\n const attempt: Promise<void> = store\n .get(key)\n .then((raw) => {\n if (raw === null) return;\n try {\n const parsed = JSON.parse(raw) as AchievementState[];\n for (const state of parsed) states.set(state.id, state);\n } catch {\n // Corrupt payload — hydrate empty.\n }\n })\n .catch((err: unknown) => {\n // Compare-and-clear: drop only OUR failed attempt so the next call retries the read.\n if (hydration === attempt) hydration = undefined;\n throw err;\n });\n hydration = attempt;\n }\n return hydration;\n }\n\n function persist(): Promise<void> {\n return store.set(key, JSON.stringify([...states.values()]));\n }\n\n return {\n unlock(id: AchievementId, progress?: number): Promise<void> {\n const run = tail.then(async () => {\n await hydrate();\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n await persist();\n });\n // Swallow on the tail so one failed write cannot break serialization for\n // later mutations; the caller still observes the error via `run`.\n tail = run.catch(() => {});\n return run;\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n await hydrate();\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n await hydrate();\n return [...states.values()];\n },\n };\n}\n"],"mappings":";AA0BA,MAAM,cAAc;;AAGpB,SAA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,cAAc,KAAA,GAAW;GAC3B,MAAM,UAAyB,MAC5B,IAAI,GAAG,CAAC,CACR,MAAM,QAAQ;IACb,IAAI,QAAQ,MAAM;IAClB,IAAI;KACF,MAAM,SAAS,KAAK,MAAM,GAAG;KAC7B,KAAK,MAAM,SAAS,QAAQ,OAAO,IAAI,MAAM,IAAI,KAAK;IACxD,QAAQ,CAER;GACF,CAAC,CAAC,CACD,OAAO,QAAiB;IAEvB,IAAI,cAAc,SAAS,YAAY,KAAA;IACvC,MAAM;GACR,CAAC;GACH,YAAY;EACd;EACA,OAAO;CACT;CAEA,SAAS,UAAyB;EAChC,OAAO,MAAM,IAAI,KAAK,KAAK,UAAU,CAAC,GAAG,OAAO,OAAO,CAAC,CAAC,CAAC;CAC5D;CAEA,OAAO;EACL,OAAO,IAAmB,UAAkC;GAC1D,MAAM,MAAM,KAAK,KAAK,YAAY;IAChC,MAAM,QAAQ;IACd,OAAO,IAAI,IAAI,iBAAiB,OAAO,IAAI,EAAE,KAAK,aAAa,EAAE,GAAG,UAAU,GAAG,CAAC;IAClF,MAAM,QAAQ;GAChB,CAAC;GAGD,OAAO,IAAI,YAAY,CAAC,CAAC;GACzB,OAAO;EACT;EACA,MAAM,SAAS,IAA8C;GAC3D,MAAM,QAAQ;GACd,OAAO,OAAO,IAAI,EAAE,KAAK,aAAa,EAAE;EAC1C;EACA,MAAM,SAA+C;GACnD,MAAM,QAAQ;GACd,OAAO,CAAC,GAAG,OAAO,OAAO,CAAC;EAC5B;CACF;AACF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Opaque achievement identifier — adapter-defined. */
|
|
2
|
+
export type AchievementId = string;
|
|
3
|
+
/** Current state of an achievement for the local player. */
|
|
4
|
+
export interface AchievementState {
|
|
5
|
+
id: AchievementId;
|
|
6
|
+
unlocked: boolean;
|
|
7
|
+
/** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */
|
|
8
|
+
progress: number;
|
|
9
|
+
unlockedAt?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Achievement provider interface. Consumers supply a concrete adapter (Game
|
|
13
|
+
* Center, Google Play Games, Steam, custom backend) at the app boundary.
|
|
14
|
+
* `unlock()` is idempotent by contract — adapters must dedupe.
|
|
15
|
+
*/
|
|
16
|
+
export interface AchievementsProvider {
|
|
17
|
+
unlock(id: AchievementId, progress?: number): Promise<void>;
|
|
18
|
+
getState(id: AchievementId): Promise<AchievementState>;
|
|
19
|
+
getAll(): Promise<readonly AchievementState[]>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
23
|
+
* unlocks resolve silently.
|
|
24
|
+
*/
|
|
25
|
+
export declare const NoopAchievementsProvider: AchievementsProvider;
|
|
26
|
+
//# sourceMappingURL=provider.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.mts","names":[],"sources":["../src/provider.ts"],"mappings":";YACY;;iBAGK;EACf,IAAI;EACJ;;EAEA;EACA;;;;;;;iBAQe;EACf,OAAO,IAAI,eAAe,oBAAoB;EAC9C,SAAS,IAAI,gBAAgB,QAAQ;EACrC,UAAU,iBAAiB;;;;;;qBAOhB,0BAA0B"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Opaque achievement identifier — adapter-defined. */
|
|
2
|
+
export type AchievementId = string;
|
|
3
|
+
/** Current state of an achievement for the local player. */
|
|
4
|
+
export interface AchievementState {
|
|
5
|
+
id: AchievementId;
|
|
6
|
+
unlocked: boolean;
|
|
7
|
+
/** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */
|
|
8
|
+
progress: number;
|
|
9
|
+
unlockedAt?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Achievement provider interface. Consumers supply a concrete adapter (Game
|
|
13
|
+
* Center, Google Play Games, Steam, custom backend) at the app boundary.
|
|
14
|
+
* `unlock()` is idempotent by contract — adapters must dedupe.
|
|
15
|
+
*/
|
|
16
|
+
export interface AchievementsProvider {
|
|
17
|
+
unlock(id: AchievementId, progress?: number): Promise<void>;
|
|
18
|
+
getState(id: AchievementId): Promise<AchievementState>;
|
|
19
|
+
getAll(): Promise<readonly AchievementState[]>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
23
|
+
* unlocks resolve silently.
|
|
24
|
+
*/
|
|
25
|
+
export declare const NoopAchievementsProvider: AchievementsProvider;
|
|
26
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","names":[],"sources":["../src/provider.ts"],"mappings":";YACY;;iBAGK;EACf,IAAI;EACJ;;EAEA;EACA;;;;;;;iBAQe;EACf,OAAO,IAAI,eAAe,oBAAoB;EAC9C,SAAS,IAAI,gBAAgB,QAAQ;EACrC,UAAU,iBAAiB;;;;;;qBAOhB,0BAA0B"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/provider.ts
|
|
2
|
+
/**
|
|
3
|
+
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
4
|
+
* unlocks resolve silently.
|
|
5
|
+
*/
|
|
6
|
+
const NoopAchievementsProvider = {
|
|
7
|
+
async unlock() {},
|
|
8
|
+
async getState(id) {
|
|
9
|
+
return {
|
|
10
|
+
id,
|
|
11
|
+
unlocked: false,
|
|
12
|
+
progress: 0
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
async getAll() {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
exports.NoopAchievementsProvider = NoopAchievementsProvider;
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","names":[],"sources":["../src/provider.ts"],"sourcesContent":["/** Opaque achievement identifier — adapter-defined. */\nexport type AchievementId = string;\n\n/** Current state of an achievement for the local player. */\nexport interface AchievementState {\n id: AchievementId;\n unlocked: boolean;\n /** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */\n progress: number;\n unlockedAt?: number;\n}\n\n/**\n * Achievement provider interface. Consumers supply a concrete adapter (Game\n * Center, Google Play Games, Steam, custom backend) at the app boundary.\n * `unlock()` is idempotent by contract — adapters must dedupe.\n */\nexport interface AchievementsProvider {\n unlock(id: AchievementId, progress?: number): Promise<void>;\n getState(id: AchievementId): Promise<AchievementState>;\n getAll(): Promise<readonly AchievementState[]>;\n}\n\n/**\n * Default no-op provider. All queries report `unlocked: false, progress: 0`;\n * unlocks resolve silently.\n */\nexport const NoopAchievementsProvider: AchievementsProvider = {\n async unlock(): Promise<void> {\n // no-op\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return { id, unlocked: false, progress: 0 };\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [];\n },\n};\n"],"mappings":";;;;;AA2BA,MAAa,2BAAiD;CAC5D,MAAM,SAAwB,CAE9B;CACA,MAAM,SAAS,IAA8C;EAC3D,OAAO;GAAE;GAAI,UAAU;GAAO,UAAU;EAAE;CAC5C;CACA,MAAM,SAA+C;EACnD,OAAO,CAAC;CACV;AACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/provider.ts
|
|
2
|
+
/**
|
|
3
|
+
* Default no-op provider. All queries report `unlocked: false, progress: 0`;
|
|
4
|
+
* unlocks resolve silently.
|
|
5
|
+
*/
|
|
6
|
+
const NoopAchievementsProvider = {
|
|
7
|
+
async unlock() {},
|
|
8
|
+
async getState(id) {
|
|
9
|
+
return {
|
|
10
|
+
id,
|
|
11
|
+
unlocked: false,
|
|
12
|
+
progress: 0
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
async getAll() {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export { NoopAchievementsProvider };
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=provider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.mjs","names":[],"sources":["../src/provider.ts"],"sourcesContent":["/** Opaque achievement identifier — adapter-defined. */\nexport type AchievementId = string;\n\n/** Current state of an achievement for the local player. */\nexport interface AchievementState {\n id: AchievementId;\n unlocked: boolean;\n /** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */\n progress: number;\n unlockedAt?: number;\n}\n\n/**\n * Achievement provider interface. Consumers supply a concrete adapter (Game\n * Center, Google Play Games, Steam, custom backend) at the app boundary.\n * `unlock()` is idempotent by contract — adapters must dedupe.\n */\nexport interface AchievementsProvider {\n unlock(id: AchievementId, progress?: number): Promise<void>;\n getState(id: AchievementId): Promise<AchievementState>;\n getAll(): Promise<readonly AchievementState[]>;\n}\n\n/**\n * Default no-op provider. All queries report `unlocked: false, progress: 0`;\n * unlocks resolve silently.\n */\nexport const NoopAchievementsProvider: AchievementsProvider = {\n async unlock(): Promise<void> {\n // no-op\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return { id, unlocked: false, progress: 0 };\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [];\n },\n};\n"],"mappings":";;;;;AA2BA,MAAa,2BAAiD;CAC5D,MAAM,SAAwB,CAE9B;CACA,MAAM,SAAS,IAA8C;EAC3D,OAAO;GAAE;GAAI,UAAU;GAAO,UAAU;EAAE;CAC5C;CACA,MAAM,SAA+C;EACnD,OAAO,CAAC;CACV;AACF"}
|
package/package.json
CHANGED
package/dist/index.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"mappings":";KACY;;UAGK;EACf,IAAI;EACJ;;EAEA;EACA;;;;;;;UAQe;EACf,OAAO,IAAI,eAAe,oBAAoB;EAC9C,SAAS,IAAI,gBAAgB,QAAQ;EACrC,UAAU,iBAAiB;;;;;;qBAOhB,0BAA0B;;;;;qBCpB1B,qCAAmB,QAAA;;wBAGhB,mBAAmB;;;;;;UCHlB;;EAEf,IAAI,cAAc;;EAElB,IAAI,aAAa,gBAAgB;;;UAIlB;;EAEf;;;UAIe,mCAAmC;;EAElD;;;;;;;wBAiDc,yBAAyB,OAAO,4BAA4B;;;;;;;;;;;;;wBA4B5D,0BACd,OAAO,mBACP,OAAO,6BACN"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"mappings":";KACY;;UAGK;EACf,IAAI;EACJ;;EAEA;EACA;;;;;;;UAQe;EACf,OAAO,IAAI,eAAe,oBAAoB;EAC9C,SAAS,IAAI,gBAAgB,QAAQ;EACrC,UAAU,iBAAiB;;;;;;qBAOhB,0BAA0B;;;;;qBCpB1B,qCAAmB,QAAA;;wBAGhB,mBAAmB;;;;;;UCHlB;;EAEf,IAAI,cAAc;;EAElB,IAAI,aAAa,gBAAgB;;;UAIlB;;EAEf;;;UAIe,mCAAmC;;EAElD;;;;;;;wBAiDc,yBAAyB,OAAO,4BAA4B;;;;;;;;;;;;;wBA4B5D,0BACd,OAAO,mBACP,OAAO,6BACN"}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["createContext","useContext"],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"sourcesContent":["/** Opaque achievement identifier — adapter-defined. */\nexport type AchievementId = string;\n\n/** Current state of an achievement for the local player. */\nexport interface AchievementState {\n id: AchievementId;\n unlocked: boolean;\n /** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */\n progress: number;\n unlockedAt?: number;\n}\n\n/**\n * Achievement provider interface. Consumers supply a concrete adapter (Game\n * Center, Google Play Games, Steam, custom backend) at the app boundary.\n * `unlock()` is idempotent by contract — adapters must dedupe.\n */\nexport interface AchievementsProvider {\n unlock(id: AchievementId, progress?: number): Promise<void>;\n getState(id: AchievementId): Promise<AchievementState>;\n getAll(): Promise<readonly AchievementState[]>;\n}\n\n/**\n * Default no-op provider. All queries report `unlocked: false, progress: 0`;\n * unlocks resolve silently.\n */\nexport const NoopAchievementsProvider: AchievementsProvider = {\n async unlock(): Promise<void> {\n // no-op\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return { id, unlocked: false, progress: 0 };\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [];\n },\n};\n","import { createContext, useContext } from 'react';\nimport { type AchievementsProvider, NoopAchievementsProvider } from './provider.js';\n\n/**\n * React context carrying the active `AchievementsProvider`. Defaults to\n * `NoopAchievementsProvider`.\n */\nexport const AchievementsContext = createContext<AchievementsProvider>(NoopAchievementsProvider);\n\n/** Hook returning the current `AchievementsProvider`. */\nexport function useAchievements(): AchievementsProvider {\n return useContext(AchievementsContext);\n}\n","import type { AchievementId, AchievementState, AchievementsProvider } from './provider.js';\n\n/**\n * Structural async key/value store the storage-backed provider persists into.\n * Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`\n * so a real adapter can be injected without depending on the storage package.\n */\nexport interface AchievementsStore {\n /** Read a value. Resolves to `null` if the key is not present. */\n get(key: string): Promise<string | null>;\n /** Write a value. */\n set(key: string, value: string): Promise<void>;\n}\n\n/** Options common to the in-memory and storage-backed providers. */\nexport interface MemoryAchievementsOptions {\n /** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\n/** Options for the storage-backed provider. */\nexport interface StorageAchievementsOptions extends MemoryAchievementsOptions {\n /** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */\n key?: string;\n}\n\nconst DEFAULT_KEY = 'flare.achievements';\n\n/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */\nfunction normalizeProgress(value: number): number {\n if (!Number.isFinite(value)) return 0;\n if (value < 0) return 0;\n if (value > 1) return 1;\n return value;\n}\n\n/** Default state for an untracked id — no `unlockedAt`. */\nfunction defaultState(id: AchievementId): AchievementState {\n return { id, unlocked: false, progress: 0 };\n}\n\n/**\n * Pure state transition shared by both providers. Given the current state, an\n * optional progress arg and a clock, returns the next state:\n *\n * - `progress` omitted means a FULL unlock (target progress `1`).\n * - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.\n * - Progress is MONOTONIC — it never decreases below the stored value; a lower\n * requested progress is ignored.\n * - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps\n * `unlockedAt` once (via `now`). An already-unlocked state keeps its original\n * `unlockedAt`, so re-unlocking is a no-op.\n */\nfunction mergeAchievement(\n current: AchievementState,\n progress: number | undefined,\n now: () => number,\n): AchievementState {\n const requested = progress === undefined ? 1 : normalizeProgress(progress);\n const nextProgress = requested > current.progress ? requested : current.progress;\n if (!current.unlocked && nextProgress < 1) {\n return { id: current.id, unlocked: false, progress: nextProgress };\n }\n const unlockedAt = current.unlockedAt ?? now();\n return { id: current.id, unlocked: true, progress: 1, unlockedAt };\n}\n\n/**\n * In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with\n * a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.\n * State is not persisted — it lives for the lifetime of the returned provider.\n */\nexport function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const states = new Map<AchievementId, AchievementState>();\n return {\n async unlock(id: AchievementId, progress?: number): Promise<void> {\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [...states.values()];\n },\n };\n}\n\n/**\n * Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of\n * {@link createMemoryAchievements} and persists into the injected\n * {@link AchievementsStore}.\n *\n * State hydrates lazily from `store.get(key)` on the first call (a JSON array of\n * `AchievementState`; a parse error hydrates empty) and is written back via\n * `store.set` after every mutation. A rejected `store.get` rejects that call and\n * the next call retries; nothing is written until a read succeeds. All mutations\n * are serialized through a single chained tail promise so a read-modify-write can\n * never interleave and lose an update under concurrent unlocks.\n */\nexport function createStorageAchievements(\n store: AchievementsStore,\n opts?: StorageAchievementsOptions,\n): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const key = opts?.key ?? DEFAULT_KEY;\n const states = new Map<AchievementId, AchievementState>();\n let hydration: Promise<void> | undefined;\n let tail: Promise<void> = Promise.resolve();\n\n function hydrate(): Promise<void> {\n if (hydration === undefined) {\n const attempt: Promise<void> = store\n .get(key)\n .then((raw) => {\n if (raw === null) return;\n try {\n const parsed = JSON.parse(raw) as AchievementState[];\n for (const state of parsed) states.set(state.id, state);\n } catch {\n // Corrupt payload — hydrate empty.\n }\n })\n .catch((err: unknown) => {\n // Compare-and-clear: drop only OUR failed attempt so the next call retries the read.\n if (hydration === attempt) hydration = undefined;\n throw err;\n });\n hydration = attempt;\n }\n return hydration;\n }\n\n function persist(): Promise<void> {\n return store.set(key, JSON.stringify([...states.values()]));\n }\n\n return {\n unlock(id: AchievementId, progress?: number): Promise<void> {\n const run = tail.then(async () => {\n await hydrate();\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n await persist();\n });\n // Swallow on the tail so one failed write cannot break serialization for\n // later mutations; the caller still observes the error via `run`.\n tail = run.catch(() => {});\n return run;\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n await hydrate();\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n await hydrate();\n return [...states.values()];\n },\n };\n}\n"],"mappings":";;;;;;;AA2BA,MAAa,2BAAiD;CAC5D,MAAM,SAAwB,CAE9B;CACA,MAAM,SAAS,IAA8C;EAC3D,OAAO;GAAE;GAAI,UAAU;GAAO,UAAU;EAAE;CAC5C;CACA,MAAM,SAA+C;EACnD,OAAO,CAAC;CACV;AACF;;;;;;AC9BA,MAAa,uBAAA,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K,IAAI;CACzC,MAAM,MAAM,MAAM,OAAO;CACzB,MAAM,yBAAS,IAAI,IAAqC;CACxD,IAAI;CACJ,IAAI,OAAsB,QAAQ,QAAQ;CAE1C,SAAS,UAAyB;EAChC,IAAI,cAAc,KAAA,GAAW;GAC3B,MAAM,UAAyB,MAC5B,IAAI,GAAG,CAAC,CACR,MAAM,QAAQ;IACb,IAAI,QAAQ,MAAM;IAClB,IAAI;KACF,MAAM,SAAS,KAAK,MAAM,GAAG;KAC7B,KAAK,MAAM,SAAS,QAAQ,OAAO,IAAI,MAAM,IAAI,KAAK;IACxD,QAAQ,CAER;GACF,CAAC,CAAC,CACD,OAAO,QAAiB;IAEvB,IAAI,cAAc,SAAS,YAAY,KAAA;IACvC,MAAM;GACR,CAAC;GACH,YAAY;EACd;EACA,OAAO;CACT;CAEA,SAAS,UAAyB;EAChC,OAAO,MAAM,IAAI,KAAK,KAAK,UAAU,CAAC,GAAG,OAAO,OAAO,CAAC,CAAC,CAAC;CAC5D;CAEA,OAAO;EACL,OAAO,IAAmB,UAAkC;GAC1D,MAAM,MAAM,KAAK,KAAK,YAAY;IAChC,MAAM,QAAQ;IACd,OAAO,IAAI,IAAI,iBAAiB,OAAO,IAAI,EAAE,KAAK,aAAa,EAAE,GAAG,UAAU,GAAG,CAAC;IAClF,MAAM,QAAQ;GAChB,CAAC;GAGD,OAAO,IAAI,YAAY,CAAC,CAAC;GACzB,OAAO;EACT;EACA,MAAM,SAAS,IAA8C;GAC3D,MAAM,QAAQ;GACd,OAAO,OAAO,IAAI,EAAE,KAAK,aAAa,EAAE;EAC1C;EACA,MAAM,SAA+C;GACnD,MAAM,QAAQ;GACd,OAAO,CAAC,GAAG,OAAO,OAAO,CAAC;EAC5B;CACF;AACF"}
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/memory.ts"],"sourcesContent":["/** Opaque achievement identifier — adapter-defined. */\nexport type AchievementId = string;\n\n/** Current state of an achievement for the local player. */\nexport interface AchievementState {\n id: AchievementId;\n unlocked: boolean;\n /** Partial progress in the range [0, 1]. `1` implies `unlocked: true`. */\n progress: number;\n unlockedAt?: number;\n}\n\n/**\n * Achievement provider interface. Consumers supply a concrete adapter (Game\n * Center, Google Play Games, Steam, custom backend) at the app boundary.\n * `unlock()` is idempotent by contract — adapters must dedupe.\n */\nexport interface AchievementsProvider {\n unlock(id: AchievementId, progress?: number): Promise<void>;\n getState(id: AchievementId): Promise<AchievementState>;\n getAll(): Promise<readonly AchievementState[]>;\n}\n\n/**\n * Default no-op provider. All queries report `unlocked: false, progress: 0`;\n * unlocks resolve silently.\n */\nexport const NoopAchievementsProvider: AchievementsProvider = {\n async unlock(): Promise<void> {\n // no-op\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return { id, unlocked: false, progress: 0 };\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [];\n },\n};\n","import { createContext, useContext } from 'react';\nimport { type AchievementsProvider, NoopAchievementsProvider } from './provider.js';\n\n/**\n * React context carrying the active `AchievementsProvider`. Defaults to\n * `NoopAchievementsProvider`.\n */\nexport const AchievementsContext = createContext<AchievementsProvider>(NoopAchievementsProvider);\n\n/** Hook returning the current `AchievementsProvider`. */\nexport function useAchievements(): AchievementsProvider {\n return useContext(AchievementsContext);\n}\n","import type { AchievementId, AchievementState, AchievementsProvider } from './provider.js';\n\n/**\n * Structural async key/value store the storage-backed provider persists into.\n * Matches the `get`/`set` subset of `@flare-engine/storage`'s `StorageAdapter`\n * so a real adapter can be injected without depending on the storage package.\n */\nexport interface AchievementsStore {\n /** Read a value. Resolves to `null` if the key is not present. */\n get(key: string): Promise<string | null>;\n /** Write a value. */\n set(key: string, value: string): Promise<void>;\n}\n\n/** Options common to the in-memory and storage-backed providers. */\nexport interface MemoryAchievementsOptions {\n /** Clock used to stamp `unlockedAt` on latch. Defaults to `() => Date.now()`. */\n now?: () => number;\n}\n\n/** Options for the storage-backed provider. */\nexport interface StorageAchievementsOptions extends MemoryAchievementsOptions {\n /** Persistence key passed to the injected store. Defaults to `'flare.achievements'`. */\n key?: string;\n}\n\nconst DEFAULT_KEY = 'flare.achievements';\n\n/** Coerce raw progress to `[0, 1]`; a non-finite value (NaN/Infinity) becomes `0`. */\nfunction normalizeProgress(value: number): number {\n if (!Number.isFinite(value)) return 0;\n if (value < 0) return 0;\n if (value > 1) return 1;\n return value;\n}\n\n/** Default state for an untracked id — no `unlockedAt`. */\nfunction defaultState(id: AchievementId): AchievementState {\n return { id, unlocked: false, progress: 0 };\n}\n\n/**\n * Pure state transition shared by both providers. Given the current state, an\n * optional progress arg and a clock, returns the next state:\n *\n * - `progress` omitted means a FULL unlock (target progress `1`).\n * - Progress is clamped to `[0, 1]`; non-finite input is treated as `0`.\n * - Progress is MONOTONIC — it never decreases below the stored value; a lower\n * requested progress is ignored.\n * - Unlock LATCHES: reaching progress `1` sets `unlocked: true` and stamps\n * `unlockedAt` once (via `now`). An already-unlocked state keeps its original\n * `unlockedAt`, so re-unlocking is a no-op.\n */\nfunction mergeAchievement(\n current: AchievementState,\n progress: number | undefined,\n now: () => number,\n): AchievementState {\n const requested = progress === undefined ? 1 : normalizeProgress(progress);\n const nextProgress = requested > current.progress ? requested : current.progress;\n if (!current.unlocked && nextProgress < 1) {\n return { id: current.id, unlocked: false, progress: nextProgress };\n }\n const unlockedAt = current.unlockedAt ?? now();\n return { id: current.id, unlocked: true, progress: 1, unlockedAt };\n}\n\n/**\n * In-memory `AchievementsProvider`. Progress is monotonic and unlocks latch with\n * a single `unlockedAt` stamp; see {@link mergeAchievement} for exact semantics.\n * State is not persisted — it lives for the lifetime of the returned provider.\n */\nexport function createMemoryAchievements(opts?: MemoryAchievementsOptions): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const states = new Map<AchievementId, AchievementState>();\n return {\n async unlock(id: AchievementId, progress?: number): Promise<void> {\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n return states.get(id) ?? defaultState(id);\n },\n async getAll(): Promise<readonly AchievementState[]> {\n return [...states.values()];\n },\n };\n}\n\n/**\n * Storage-backed `AchievementsProvider`. Shares the monotonic/latch semantics of\n * {@link createMemoryAchievements} and persists into the injected\n * {@link AchievementsStore}.\n *\n * State hydrates lazily from `store.get(key)` on the first call (a JSON array of\n * `AchievementState`; a parse error hydrates empty) and is written back via\n * `store.set` after every mutation. A rejected `store.get` rejects that call and\n * the next call retries; nothing is written until a read succeeds. All mutations\n * are serialized through a single chained tail promise so a read-modify-write can\n * never interleave and lose an update under concurrent unlocks.\n */\nexport function createStorageAchievements(\n store: AchievementsStore,\n opts?: StorageAchievementsOptions,\n): AchievementsProvider {\n const now = opts?.now ?? (() => Date.now());\n const key = opts?.key ?? DEFAULT_KEY;\n const states = new Map<AchievementId, AchievementState>();\n let hydration: Promise<void> | undefined;\n let tail: Promise<void> = Promise.resolve();\n\n function hydrate(): Promise<void> {\n if (hydration === undefined) {\n const attempt: Promise<void> = store\n .get(key)\n .then((raw) => {\n if (raw === null) return;\n try {\n const parsed = JSON.parse(raw) as AchievementState[];\n for (const state of parsed) states.set(state.id, state);\n } catch {\n // Corrupt payload — hydrate empty.\n }\n })\n .catch((err: unknown) => {\n // Compare-and-clear: drop only OUR failed attempt so the next call retries the read.\n if (hydration === attempt) hydration = undefined;\n throw err;\n });\n hydration = attempt;\n }\n return hydration;\n }\n\n function persist(): Promise<void> {\n return store.set(key, JSON.stringify([...states.values()]));\n }\n\n return {\n unlock(id: AchievementId, progress?: number): Promise<void> {\n const run = tail.then(async () => {\n await hydrate();\n states.set(id, mergeAchievement(states.get(id) ?? defaultState(id), progress, now));\n await persist();\n });\n // Swallow on the tail so one failed write cannot break serialization for\n // later mutations; the caller still observes the error via `run`.\n tail = run.catch(() => {});\n return run;\n },\n async getState(id: AchievementId): Promise<AchievementState> {\n await hydrate();\n return states.get(id) ?? defaultState(id);\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