|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const MAGIC_PACKS_DIR = path.join(__dirname, '..', 'src', 'packs', 'magic'); |
| 5 | + |
| 6 | +function parseDamage(description) { |
| 7 | + const match = description.match(/Daño *(base)* *(\d+)/i); |
| 8 | + return match ? parseInt(match[2], 10) : 0; |
| 9 | +} |
| 10 | + |
| 11 | +function parseArea(description) { |
| 12 | + const match = description.match(/[Áá]rea de (\d+) metros/i); |
| 13 | + return match ? parseInt(match[1], 10) : 0; |
| 14 | +} |
| 15 | + |
| 16 | +function parseResistanceEffect(description) { |
| 17 | + const resistances = { |
| 18 | + physical: 'RF', |
| 19 | + disease: 'RE', |
| 20 | + poison: 'RV', |
| 21 | + magic: 'RM', |
| 22 | + psychic: 'RP' |
| 23 | + }; |
| 24 | + |
| 25 | + for (const [type, code] of Object.entries(resistances)) { |
| 26 | + const beforePattern = new RegExp(`(\\d+) *[RFEVMP]{0,2} *o* *${code}`, 'i'); |
| 27 | + const afterPattern = new RegExp(`${code} *o* *[RFEVMP]{0,2} *(\\d+)`, 'i'); |
| 28 | + |
| 29 | + let match = description.match(beforePattern); |
| 30 | + if (match) return { value: parseInt(match[1], 10), type }; |
| 31 | + |
| 32 | + match = description.match(afterPattern); |
| 33 | + if (match) return { value: parseInt(match[1], 10), type }; |
| 34 | + } |
| 35 | + |
| 36 | + return { value: 0, type: null }; |
| 37 | +} |
| 38 | + |
| 39 | +function migrateSpellFile(filePath) { |
| 40 | + const fileName = path.basename(filePath); |
| 41 | + |
| 42 | + try { |
| 43 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 44 | + const spellData = JSON.parse(fileContent); |
| 45 | + |
| 46 | + if (spellData.type !== 'spell') { |
| 47 | + console.log(` ⊘ Skipping ${fileName} (not a spell)`); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + let modified = false; |
| 52 | + const grades = ['base', 'intermediate', 'advanced', 'arcane']; |
| 53 | + |
| 54 | + for (const gradeName of grades) { |
| 55 | + const grade = spellData.system?.grades?.[gradeName]; |
| 56 | + if (!grade) continue; |
| 57 | + |
| 58 | + const description = grade.description?.value || ''; |
| 59 | + const damage = parseDamage(description); |
| 60 | + const area = parseArea(description); |
| 61 | + const resistanceEffect = parseResistanceEffect(description); |
| 62 | + |
| 63 | + if (damage > 0 && !grade.damage) { |
| 64 | + grade.damage = { value: damage }; |
| 65 | + modified = true; |
| 66 | + } else if (grade.damage && grade.damage.value !== damage && damage > 0) { |
| 67 | + console.log(` ⚠ ${fileName} [${gradeName}]: damage mismatch`); |
| 68 | + } |
| 69 | + |
| 70 | + if (area > 0 && !grade.area) { |
| 71 | + grade.area = { value: area }; |
| 72 | + modified = true; |
| 73 | + } else if (grade.area && grade.area.value !== area && area > 0) { |
| 74 | + console.log(` ⚠ ${fileName} [${gradeName}]: area mismatch`); |
| 75 | + } |
| 76 | + |
| 77 | + if (resistanceEffect.value > 0 && !grade.resistanceEffect) { |
| 78 | + grade.resistanceEffect = resistanceEffect; |
| 79 | + modified = true; |
| 80 | + } else if ( |
| 81 | + grade.resistanceEffect && |
| 82 | + (grade.resistanceEffect.value !== resistanceEffect.value || |
| 83 | + grade.resistanceEffect.type !== resistanceEffect.type) && |
| 84 | + resistanceEffect.value > 0 |
| 85 | + ) { |
| 86 | + console.log(` ⚠ ${fileName} [${gradeName}]: resistance mismatch`); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (modified) { |
| 91 | + fs.writeFileSync(filePath, JSON.stringify(spellData, null, 2), 'utf8'); |
| 92 | + console.log(` ✓ Migrated ${fileName}`); |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + console.log(` ○ ${fileName} already has structured data`); |
| 97 | + return false; |
| 98 | + } catch (error) { |
| 99 | + console.error(` ✗ Error processing ${fileName}:`, error.message); |
| 100 | + return false; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function migrateAllSpells() { |
| 105 | + console.log('Starting spell migration to structured data format...\n'); |
| 106 | + |
| 107 | + if (!fs.existsSync(MAGIC_PACKS_DIR)) { |
| 108 | + console.error(`Error: Magic packs directory not found at ${MAGIC_PACKS_DIR}`); |
| 109 | + process.exit(1); |
| 110 | + } |
| 111 | + |
| 112 | + const files = fs.readdirSync(MAGIC_PACKS_DIR) |
| 113 | + .filter(file => file.endsWith('.json') && file.startsWith('spell_')); |
| 114 | + |
| 115 | + console.log(`Found ${files.length} spell files\n`); |
| 116 | + |
| 117 | + let migratedCount = 0; |
| 118 | + let skippedCount = 0; |
| 119 | + let errorCount = 0; |
| 120 | + |
| 121 | + for (const file of files) { |
| 122 | + const result = migrateSpellFile(path.join(MAGIC_PACKS_DIR, file)); |
| 123 | + if (result === true) migratedCount++; |
| 124 | + else if (result === false) skippedCount++; |
| 125 | + else errorCount++; |
| 126 | + } |
| 127 | + |
| 128 | + console.log('\n' + '='.repeat(60)); |
| 129 | + console.log(`Migration complete!`); |
| 130 | + console.log(` ✓ Migrated: ${migratedCount}`); |
| 131 | + console.log(` ○ Already migrated: ${skippedCount}`); |
| 132 | + if (errorCount > 0) console.log(` ✗ Errors: ${errorCount}`); |
| 133 | + console.log('='.repeat(60)); |
| 134 | +} |
| 135 | + |
| 136 | +migrateAllSpells(); |
0 commit comments