|
| 1 | +/** |
| 2 | + * Chronicle Sync - Generic System Adapter |
| 3 | + * |
| 4 | + * A data-driven adapter that reads field definitions from the Chronicle |
| 5 | + * /systems/:id/character-fields API. This allows any game system — including |
| 6 | + * custom-uploaded ones — to sync character fields between Chronicle and Foundry |
| 7 | + * without a hand-written adapter, as long as the system manifest includes |
| 8 | + * foundry_path annotations on its character fields. |
| 9 | + * |
| 10 | + * Field definitions specify: |
| 11 | + * - key: Chronicle field key (e.g. "hp_current") |
| 12 | + * - foundry_path: dot-notation path on actor.system (e.g. "system.attributes.hp.value") |
| 13 | + * - foundry_writable: whether Chronicle may write back to this Foundry path (default true) |
| 14 | + * - type: field type ("number", "string", etc.) for casting |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Create a generic adapter instance by fetching field definitions from the API. |
| 19 | + * |
| 20 | + * @param {import('../api-client.mjs').ChronicleAPI} api - Chronicle API client. |
| 21 | + * @param {string} chronicleSystemId - The Chronicle system ID (e.g. "dnd5e"). |
| 22 | + * @returns {Promise<{systemId: string, characterTypeSlug: string, toChronicleFields: function, fromChronicleFields: function}|null>} |
| 23 | + */ |
| 24 | +export async function createGenericAdapter(api, chronicleSystemId) { |
| 25 | + let fieldDefs; |
| 26 | + try { |
| 27 | + const resp = await api.get(`/systems/${chronicleSystemId}/character-fields`); |
| 28 | + if (!resp || !resp.fields || resp.fields.length === 0) { |
| 29 | + console.warn(`Chronicle: Generic adapter — no character fields for system "${chronicleSystemId}"`); |
| 30 | + return null; |
| 31 | + } |
| 32 | + fieldDefs = resp; |
| 33 | + } catch (err) { |
| 34 | + console.error(`Chronicle: Generic adapter — failed to load field defs for "${chronicleSystemId}"`, err); |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + // Only include fields that have a foundry_path annotation. |
| 39 | + const mappedFields = fieldDefs.fields.filter((f) => f.foundry_path); |
| 40 | + if (mappedFields.length === 0) { |
| 41 | + console.warn(`Chronicle: Generic adapter — no fields with foundry_path for "${chronicleSystemId}"`); |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + const writableFields = mappedFields.filter((f) => f.foundry_writable !== false); |
| 46 | + |
| 47 | + console.log( |
| 48 | + `Chronicle: Generic adapter loaded for "${chronicleSystemId}" — ` + |
| 49 | + `${mappedFields.length} fields mapped, ${writableFields.length} writable` |
| 50 | + ); |
| 51 | + |
| 52 | + return { |
| 53 | + /** Chronicle system ID. */ |
| 54 | + systemId: chronicleSystemId, |
| 55 | + |
| 56 | + /** Character entity type slug from the manifest. */ |
| 57 | + characterTypeSlug: fieldDefs.preset_slug || `${chronicleSystemId}-character`, |
| 58 | + |
| 59 | + /** |
| 60 | + * Extract Chronicle-compatible fields_data from a Foundry Actor. |
| 61 | + * Reads each mapped field from the actor using its foundry_path. |
| 62 | + * |
| 63 | + * @param {Actor} actor - Foundry Actor document. |
| 64 | + * @returns {object} Chronicle fields_data object. |
| 65 | + */ |
| 66 | + toChronicleFields(actor) { |
| 67 | + const result = {}; |
| 68 | + for (const field of mappedFields) { |
| 69 | + const value = _getNestedValue(actor, field.foundry_path); |
| 70 | + result[field.key] = value ?? null; |
| 71 | + } |
| 72 | + return result; |
| 73 | + }, |
| 74 | + |
| 75 | + /** |
| 76 | + * Convert Chronicle entity fields_data into a Foundry Actor update. |
| 77 | + * Only writes to fields marked as foundry_writable (or defaulting to true). |
| 78 | + * Returns dot-notation keys for actor.update(). |
| 79 | + * |
| 80 | + * @param {object} entity - Chronicle entity with fields_data. |
| 81 | + * @returns {object} Foundry Actor update data. |
| 82 | + */ |
| 83 | + fromChronicleFields(entity) { |
| 84 | + const f = entity.fields_data || {}; |
| 85 | + const update = {}; |
| 86 | + |
| 87 | + for (const field of writableFields) { |
| 88 | + const value = f[field.key]; |
| 89 | + if (value == null) continue; |
| 90 | + |
| 91 | + // Cast to appropriate type. |
| 92 | + if (field.type === 'number') { |
| 93 | + update[field.foundry_path] = Number(value); |
| 94 | + } else { |
| 95 | + update[field.foundry_path] = value; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Name is synced at document level. |
| 100 | + if (entity.name) update.name = entity.name; |
| 101 | + |
| 102 | + return update; |
| 103 | + }, |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Read a nested value from an object using dot-notation path. |
| 109 | + * Supports both nested objects and Foundry's system data. |
| 110 | + * e.g., _getNestedValue(actor, "system.abilities.str.value") |
| 111 | + * |
| 112 | + * @param {object} obj |
| 113 | + * @param {string} path |
| 114 | + * @returns {*} |
| 115 | + */ |
| 116 | +function _getNestedValue(obj, path) { |
| 117 | + const keys = path.split('.'); |
| 118 | + let current = obj; |
| 119 | + for (const key of keys) { |
| 120 | + if (current == null || typeof current !== 'object') return undefined; |
| 121 | + current = current[key]; |
| 122 | + } |
| 123 | + return current; |
| 124 | +} |
0 commit comments