|
| 1 | +import { openSimpleInputDialog } from '../../../utils/dialogs/openSimpleInputDialog.js'; |
| 2 | + |
| 3 | +const TYPE_MARKER = '{"type":"SecondaryAbility","attribute":"intelligence"}'; |
| 4 | + |
| 5 | +function toTypedNode(raw, fallbackKey) { |
| 6 | + const src = raw && typeof raw === 'object' ? raw : {}; |
| 7 | + |
| 8 | + return { |
| 9 | + __type: TYPE_MARKER, |
| 10 | + key: String(src.key ?? fallbackKey), |
| 11 | + attribute: String(src.attribute ?? 'intelligence'), |
| 12 | + base: { value: Number(src.base?.value ?? src.base ?? 0) || 0 }, |
| 13 | + special: { value: Number(src.special?.value ?? src.special ?? 0) || 0 } |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +function normalizeSecondarySpecialSkills(raw) { |
| 18 | + if (Array.isArray(raw)) return {}; |
| 19 | + |
| 20 | + if (!raw || typeof raw !== 'object') return {}; |
| 21 | + |
| 22 | + const out = {}; |
| 23 | + for (const [key, value] of Object.entries(raw)) { |
| 24 | + if (value == null) continue; |
| 25 | + out[key] = toTypedNode(value, key); |
| 26 | + } |
| 27 | + |
| 28 | + return out; |
| 29 | +} |
| 30 | + |
| 31 | +function sanitizeKey(name) { |
| 32 | + const normalized = String(name ?? '') |
| 33 | + .trim() |
| 34 | + .replaceAll('.', '_'); |
| 35 | + |
| 36 | + return normalized || 'secondarySpecialSkill'; |
| 37 | +} |
| 38 | + |
| 39 | +function uniqueKey(map, baseKey) { |
| 40 | + let candidate = baseKey; |
| 41 | + let i = 2; |
| 42 | + |
| 43 | + while (Object.prototype.hasOwnProperty.call(map, candidate)) { |
| 44 | + candidate = `${baseKey}_${i}`; |
| 45 | + i += 1; |
| 46 | + } |
| 47 | + |
| 48 | + return candidate; |
| 49 | +} |
| 50 | + |
| 51 | +export async function addSecondarySpecialSkill(sheet) { |
| 52 | + const name = await openSimpleInputDialog({ |
| 53 | + content: game.i18n.localize('dialogs.items.secondarySkill.content') |
| 54 | + }); |
| 55 | + |
| 56 | + const cleanName = String(name ?? '').trim(); |
| 57 | + if (!cleanName) return; |
| 58 | + |
| 59 | + const existing = normalizeSecondarySpecialSkills( |
| 60 | + sheet.actor.system.secondaries?.secondarySpecialSkills |
| 61 | + ); |
| 62 | + const key = uniqueKey(existing, sanitizeKey(cleanName)); |
| 63 | + |
| 64 | + existing[key] = { |
| 65 | + __type: TYPE_MARKER, |
| 66 | + key, |
| 67 | + attribute: 'intelligence', |
| 68 | + base: { value: 0 }, |
| 69 | + special: { value: 0 } |
| 70 | + }; |
| 71 | + |
| 72 | + await sheet.actor.update({ 'system.secondaries.secondarySpecialSkills': existing }); |
| 73 | +} |
| 74 | + |
| 75 | +addSecondarySpecialSkill.action = 'add-secondary-special-skill'; |
| 76 | + |
| 77 | +const SECONDARY_SPECIAL_SKILL_PATH_PREFIX = 'system.secondaries.secondarySpecialSkills.'; |
| 78 | + |
| 79 | +function secondarySpecialSkillKeyFromPath(path) { |
| 80 | + const normalized = String(path ?? ''); |
| 81 | + if (!normalized.startsWith(SECONDARY_SPECIAL_SKILL_PATH_PREFIX)) return ''; |
| 82 | + return normalized.slice(SECONDARY_SPECIAL_SKILL_PATH_PREFIX.length); |
| 83 | +} |
| 84 | + |
| 85 | +export async function deleteSecondarySpecialSkill(sheet, eventOrTarget) { |
| 86 | + eventOrTarget?.preventDefault?.(); |
| 87 | + eventOrTarget?.stopPropagation?.(); |
| 88 | + |
| 89 | + const actor = sheet?.actor; |
| 90 | + if (!actor) return; |
| 91 | + |
| 92 | + const el = eventOrTarget?.currentTarget ?? eventOrTarget; |
| 93 | + const key = |
| 94 | + String(el?.dataset?.secondarySpecialSkillKey ?? '').trim() || |
| 95 | + secondarySpecialSkillKeyFromPath(el?.dataset?.path); |
| 96 | + if (!key) return; |
| 97 | + |
| 98 | + await sheet?._flushPendingSheetUpdatesImmediately?.(); |
| 99 | + |
| 100 | + await actor.update( |
| 101 | + { |
| 102 | + [`system.secondaries.secondarySpecialSkills.${key}`]: null |
| 103 | + }, |
| 104 | + { |
| 105 | + unset: true |
| 106 | + } |
| 107 | + ); |
| 108 | + |
| 109 | + sheet.render?.(false); |
| 110 | +} |
0 commit comments