|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const PSYCHIC_PACKS_DIR = path.join(__dirname, '..', 'src', 'packs', 'psychic_powers'); |
| 5 | + |
| 6 | +function parseDamage(text) { |
| 7 | + const match = (text ?? '').match(/Daño\s*(base)?\s*(\d+)/i); |
| 8 | + return match ? parseInt(match[2], 10) : 0; |
| 9 | +} |
| 10 | + |
| 11 | +function parseFatigue(text) { |
| 12 | + const match = (text ?? '').match(/Fatiga\s*(\d+)/i); |
| 13 | + return match ? parseInt(match[1], 10) : 0; |
| 14 | +} |
| 15 | + |
| 16 | +function parseShieldPoints(text) { |
| 17 | + const effect = (text ?? '').replace('.', ''); |
| 18 | + |
| 19 | + const m1 = effect.match(/(\d+)\s+puntos de resistencia/i); |
| 20 | + if (m1) return parseInt(m1[1], 10) || 0; |
| 21 | + |
| 22 | + const m2 = effect.match(/(\d+)\s*PV/i); |
| 23 | + if (m2) return parseInt(m2[1], 10) || 0; |
| 24 | + |
| 25 | + return 0; |
| 26 | +} |
| 27 | + |
| 28 | +function parseAffectsInmaterial(text) { |
| 29 | + return /afecta a seres inmateriales/i.test(text ?? ''); |
| 30 | +} |
| 31 | + |
| 32 | +function ensureEffectStructured(effect) { |
| 33 | + effect.value ??= ''; |
| 34 | + |
| 35 | + effect.damage ??= { value: 0 }; |
| 36 | + effect.fatigue ??= { value: 0 }; |
| 37 | + effect.shieldPoints ??= { value: 0 }; |
| 38 | + effect.affectsInmaterial ??= { value: false }; |
| 39 | +} |
| 40 | + |
| 41 | +function migratePsychicPowerFile(filePath) { |
| 42 | + const fileName = path.basename(filePath); |
| 43 | + |
| 44 | + try { |
| 45 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 46 | + const powerData = JSON.parse(fileContent); |
| 47 | + |
| 48 | + if (powerData.type !== 'psychicPower') { |
| 49 | + console.log(` ⊘ Skipping ${fileName} (not a psychicPower)`); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + let modified = false; |
| 54 | + |
| 55 | + const effects = powerData.system?.effects ?? {}; |
| 56 | + for (const [potency, effect] of Object.entries(effects)) { |
| 57 | + if (!effect) continue; |
| 58 | + |
| 59 | + // if the template changed, some entries might still be strings |
| 60 | + if (typeof effect === 'string') { |
| 61 | + effects[potency] = { value: effect }; |
| 62 | + modified = true; |
| 63 | + } |
| 64 | + |
| 65 | + ensureEffectStructured(effects[potency]); |
| 66 | + |
| 67 | + const text = effects[potency].value ?? ''; |
| 68 | + |
| 69 | + const damage = parseDamage(text); |
| 70 | + const fatigue = parseFatigue(text); |
| 71 | + const shieldPoints = parseShieldPoints(text); |
| 72 | + const affectsInmaterial = parseAffectsInmaterial(text); |
| 73 | + |
| 74 | + if (damage > 0 && effects[potency].damage.value !== damage) { |
| 75 | + effects[potency].damage.value = damage; |
| 76 | + modified = true; |
| 77 | + } |
| 78 | + |
| 79 | + if (fatigue > 0 && effects[potency].fatigue.value !== fatigue) { |
| 80 | + effects[potency].fatigue.value = fatigue; |
| 81 | + modified = true; |
| 82 | + } |
| 83 | + |
| 84 | + if (shieldPoints > 0 && effects[potency].shieldPoints.value !== shieldPoints) { |
| 85 | + effects[potency].shieldPoints.value = shieldPoints; |
| 86 | + modified = true; |
| 87 | + } |
| 88 | + |
| 89 | + if (affectsInmaterial && effects[potency].affectsInmaterial.value !== true) { |
| 90 | + effects[potency].affectsInmaterial.value = true; |
| 91 | + modified = true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // write back |
| 96 | + powerData.system.effects = effects; |
| 97 | + |
| 98 | + if (modified) { |
| 99 | + fs.writeFileSync(filePath, JSON.stringify(powerData, null, 2), 'utf8'); |
| 100 | + console.log(` ✓ Migrated ${fileName}`); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + console.log(` ○ ${fileName} already has structured data`); |
| 105 | + return false; |
| 106 | + } catch (error) { |
| 107 | + console.error(` ✗ Error processing ${fileName}:`, error.message); |
| 108 | + return false; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function migrateAllPsychicPowers() { |
| 113 | + console.log('Starting psychic power migration to structured data format...\n'); |
| 114 | + |
| 115 | + if (!fs.existsSync(PSYCHIC_PACKS_DIR)) { |
| 116 | + console.error(`Error: Psychic packs directory not found at ${PSYCHIC_PACKS_DIR}`); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | + |
| 120 | + const files = fs |
| 121 | + .readdirSync(PSYCHIC_PACKS_DIR) |
| 122 | + .filter(file => file.endsWith('.json') && file.startsWith('psychicPower_')); |
| 123 | + |
| 124 | + console.log(`Found ${files.length} psychic power files\n`); |
| 125 | + |
| 126 | + let migratedCount = 0; |
| 127 | + let skippedCount = 0; |
| 128 | + let errorCount = 0; |
| 129 | + |
| 130 | + for (const file of files) { |
| 131 | + const result = migratePsychicPowerFile(path.join(PSYCHIC_PACKS_DIR, file)); |
| 132 | + if (result === true) migratedCount++; |
| 133 | + else if (result === false) skippedCount++; |
| 134 | + else errorCount++; |
| 135 | + } |
| 136 | + |
| 137 | + console.log('\n' + '='.repeat(60)); |
| 138 | + console.log(`Migration complete!`); |
| 139 | + console.log(` ✓ Migrated: ${migratedCount}`); |
| 140 | + console.log(` ○ Already migrated: ${skippedCount}`); |
| 141 | + if (errorCount > 0) console.log(` ✗ Errors: ${errorCount}`); |
| 142 | + console.log('='.repeat(60)); |
| 143 | +} |
| 144 | + |
| 145 | +migrateAllPsychicPowers(); |
0 commit comments