|
| 1 | +# Input String Encoder |
| 2 | + |
| 3 | +A schema-driven library for encoding and decoding game state into compact, human-readable strings. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Compact encoding**: 40-60% size reduction vs JSON |
| 8 | +- **Human-readable**: Colon-delimited format for easy debugging |
| 9 | +- **Type-safe**: Full TypeScript inference |
| 10 | +- **Migration support**: Built-in version migration system |
| 11 | +- **Smart optimization**: Automatic sparse encoding for arrays, JSON compression with lz-string |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { defineSchema, createEncoder } from '@puzzmo/sdk/inputs' |
| 17 | + |
| 18 | +// Define your data type |
| 19 | +type GameData = { |
| 20 | + revealed: boolean[] |
| 21 | + score: number |
| 22 | + annotations: Record<string, { level: number; color: number }> |
| 23 | +} |
| 24 | + |
| 25 | +// Create a schema |
| 26 | +const schema = defineSchema<GameData>({ |
| 27 | + version: 1, |
| 28 | + fields: { |
| 29 | + revealed: { type: 'bitArray' }, |
| 30 | + score: { type: 'int' }, |
| 31 | + annotations: { |
| 32 | + type: 'sparseMap', |
| 33 | + valueEncoder: (ann) => `${ann.level}.${ann.color}`, |
| 34 | + valueDecoder: (str) => { |
| 35 | + const [level, color] = str.split('.').map(Number) |
| 36 | + return { level, color } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +}) |
| 41 | + |
| 42 | +// Create encoder/decoder with context for length-dependent fields |
| 43 | +const { encode, decode } = createEncoder(schema, { |
| 44 | + revealed: { length: 100 } // Required for bitArray decoding |
| 45 | +}) |
| 46 | + |
| 47 | +// Use it |
| 48 | +const data = { revealed: [...], score: 42, annotations: { ... } } |
| 49 | +const inputString = encode(data) // "v1:a3f5...:42:0,0=2.1;1,1=3.0" |
| 50 | +const decoded = decode(inputString) |
| 51 | +``` |
| 52 | + |
| 53 | +## Field Types |
| 54 | + |
| 55 | +| Type | Use Case | Encoding | Example | |
| 56 | +|------|----------|----------|---------| |
| 57 | +| `bitArray` | Boolean arrays (revealed tiles, flags) | Hex (4 bits/char) | `[true, false, false, false, false, true]` → `"12"` | |
| 58 | +| `intArray` | Number arrays (scores, experience) | Smart sparse/CSV | `[0, 0, 5, 0, 3]` → `"2=5,4=3"` | |
| 59 | +| `int` | Single integers | Decimal | `42` → `"42"` | |
| 60 | +| `string` | Text values | As-is | `"Alice"` → `"Alice"` | |
| 61 | +| `stringArray` | String lists | Comma-separated | `["a", "b", "c"]` → `"a,b,c"` | |
| 62 | +| `sparseMap` | Key-value maps with custom encoding | `idx=value,...` or `key=value;...` | `{a: {x: 1}}` → `"0=1.2"` (with keys) or `"a=1.2"` | |
| 63 | +| `json` | Complex objects (fallback) | Compressed JSON | Auto lz-string compression | |
| 64 | + |
| 65 | +## Migrations |
| 66 | + |
| 67 | +Add version migrations to your schema: |
| 68 | + |
| 69 | +```typescript |
| 70 | +const schema = defineSchema({ |
| 71 | + version: 2, |
| 72 | + fields: { |
| 73 | + name: { type: 'string' }, |
| 74 | + value: { type: 'int' } |
| 75 | + }, |
| 76 | + migrations: { |
| 77 | + 0: (old) => ({ ...old, value: 0 }), |
| 78 | + 1: (old) => ({ ...old, name: old.name.toUpperCase() }) |
| 79 | + } |
| 80 | +}) |
| 81 | + |
| 82 | +const { migrate } = createEncoder(schema) |
| 83 | +const updated = migrate("v0:alice") // "v2:ALICE:1" |
| 84 | +``` |
| 85 | + |
| 86 | +## Context for Optimization |
| 87 | + |
| 88 | +Several field types benefit from context information: |
| 89 | + |
| 90 | +```typescript |
| 91 | +const { encode, decode } = createEncoder(schema, { |
| 92 | + revealed: { length: 100 }, // Required for bitArray |
| 93 | + scores: { length: 10 }, // Required for sparse intArray |
| 94 | + annotations: { keys: tileIds } // Optional: optimize sparseMap with numeric indices |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +**sparseMap optimization**: Providing ordered keys converts string keys to numeric indices: |
| 99 | +- Without keys: `"0,0=2.1;1,1=3.0"` (17 chars, `;` separator) |
| 100 | +- With keys: `"0=2.1,11=3.0"` (13 chars, `,` separator) |
| 101 | + |
| 102 | +The keys array must match the key order used when creating the data. |
| 103 | + |
| 104 | +## Documentation |
| 105 | + |
| 106 | +See detailed documentation in [types.ts](./types.ts) for: |
| 107 | +- Complete field type specifications |
| 108 | +- When to use each type |
| 109 | +- Encoding/decoding details |
| 110 | +- Migration patterns |
| 111 | +- Best practices |
0 commit comments