Skip to main content

Function Signature

function createAchievements<TId extends string>(
  config: Config<TId>
): AchievementEngine<TId>
Creates and initializes an achievement engine with the provided configuration. This is the main entry point for setting up achievement tracking in your application.

Parameters

config
Config<TId>
required
Configuration object for the achievement engine

Returns

engine
AchievementEngine<TId>
An achievement engine instance with methods for unlocking achievements, tracking progress, and subscribing to state changes.

Example

import { createAchievements, defineAchievements } from '@achievements-manager/core';

const definitions = defineAchievements([
  {
    id: 'first-login',
    label: 'Welcome!',
    description: 'Log in for the first time',
  },
  {
    id: 'complete-profile',
    label: 'Profile Complete',
    description: 'Fill out all profile fields',
    maxProgress: 5,
  },
]);

const engine = createAchievements({
  definitions,
  onUnlock: (id) => {
    console.log(`Achievement unlocked: ${id}`);
  },
  onTamperDetected: (key) => {
    console.warn(`Tampered data detected: ${key}`);
  },
});

Behavior

Initialization

When createAchievements is called, it:
  1. Loads persisted state from storage (unlocked achievements, progress, collected items)
  2. Verifies integrity of stored data using hash validation
  3. If tampering is detected, fires onTamperDetected and resets to clean state
  4. Returns a ready-to-use engine instance

Integrity Protection

The engine automatically protects against data tampering:
  • Each storage write includes a hash of the data
  • On read, the hash is verified against the stored data
  • If verification fails, onTamperDetected is called and the data is discarded
  • All storage keys are wiped together to maintain consistency

Storage Keys

The engine uses three storage keys (with optional prefix from localStorageAdapter):
  • unlocked - Set of unlocked achievement IDs
  • progress - Progress values for achievements with maxProgress
  • items - Collected items for achievements using collectItem()
Each key has a corresponding :hash key for integrity verification.

See Also

Build docs developers (and LLMs) love