📄 src/models/level-state.ts
D-OPEN SOVEREIGN
Documentation & Insights
Integer level, 1-based (Math.floor(raw) + 1).Progress toward the next level, 0–100.Returns the current level state for a username. Never throws — defaults to level 1.Subscribe to level updates. The listener receives the username whose level
changed. Call the returned function to unsubscribe.1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 *
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 *
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15export interface LevelState {
16 /** Integer level, 1-based (Math.floor(raw) + 1). */
17 level: number;
18 /** Progress toward the next level, 0–100. */
19 progress: number;
20}
21
22const _levels = new Map<string, LevelState>();
23const _listeners = new Set<(username: string) => void>();
24
25if (typeof window !== 'undefined') {
26 window.addEventListener('LevelUpdate', (evt: Event) => {
27 const { username, level } = (evt as CustomEvent<{ username: string; level: number }>).detail;
28 if (!username) {
29 console.error('[models_v2 LevelState] LevelUpdate event missing username', evt);
30 return;
31 }
32 _levels.set(username, {
33 level: Math.floor(level) + 1,
34 progress: 100 * (level - Math.floor(level)),
35 });
36 _listeners.forEach(fn => fn(username));
37 });
38}
39
40/** Returns the current level state for a username. Never throws — defaults to level 1. */
41export const getLevel = (username: string): LevelState =>
42 _levels.get(username) ?? { level: 1, progress: 0 };
43
44/**
45 * Subscribe to level updates. The listener receives the username whose level
46 * changed. Call the returned function to unsubscribe.
47 */
48export const subscribeLevel = (listener: (username: string) => void): (() => void) => {
49 _listeners.add(listener);
50 return () => _listeners.delete(listener);
51};
52