Files
ffxi-trusts/convert_to_json.js
2025-07-12 23:10:19 +01:00

115 lines
3.4 KiB
JavaScript

/**
* Script to convert trusts.txt to JSON format
*/
const fs = require('fs');
// Read the trusts.txt file
const trustsText = fs.readFileSync('trusts.txt', 'utf8');
// Define the sections we want to extract
const sections = [
'Job',
'Spells',
'Abilities',
'Weapon Skills',
'Acquisition',
'Special Features',
'Trust Synergy'
];
// Function to parse the trusts data
function parseTrustsData(text) {
// First, let's clean up the text and split it into character entries
const cleanedText = text.replace(/\r\n/g, '\n');
// Split by character name pattern (name followed by "Job")
const characterEntries = cleanedText.split(/\n\n(?=[A-Za-z][A-Za-z\s\-']+\n(?:Job|Spells|Abilities|Weapon Skills|Acquisition|Special Features|Trust Synergy))/);
const characters = [];
// Process each character entry
for (const entry of characterEntries) {
if (!entry.trim()) continue;
// Extract the character name (first line)
const lines = entry.split('\n');
const name = lines[0].trim();
// Skip if this doesn't look like a character name
if (!name || name.length > 50 || name.includes(':') || name.includes('\t')) continue;
// Create a new character entry
const character = {
name: name,
role: ''
};
// Extract role from char_list.txt if available
try {
const charList = fs.readFileSync('char_list.txt', 'utf8');
const roleRegex = new RegExp(`(Tank|Melee Fighter|Ranged Fighter|Offensive Caster|Healer|Support|Special|Unity Concord)\\s*\\n[^\\n]*${name}\\b`, 'i');
const roleMatch = charList.match(roleRegex);
if (roleMatch) {
character.role = roleMatch[1];
}
} catch (err) {
// If char_list.txt can't be read, continue without role information
}
// Process the character's data
let currentSection = '';
let sectionContent = '';
// Skip the name line
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
// Check for section headers
let sectionFound = false;
for (const section of sections) {
if (line.startsWith(section)) {
// If we were collecting content for a previous section, save it
if (currentSection) {
character[currentSection] = sectionContent.trim();
}
// Start a new section
currentSection = section.toLowerCase().replace(/\s+/g, '_');
sectionContent = line.substring(section.length).trim();
sectionFound = true;
break;
}
}
// If no section header was found, add content to the current section
if (!sectionFound && currentSection) {
sectionContent += '\n' + line;
} else if (!sectionFound && !currentSection) {
// This is part of the character's basic info (like Job)
if (line.startsWith('Job')) {
character.job = line;
}
}
}
// Save the last section if there is one
if (currentSection) {
character[currentSection] = sectionContent.trim();
}
characters.push(character);
}
return characters;
}
// Parse the data
const characters = parseTrustsData(trustsText);
// Write the JSON file
fs.writeFileSync('trusts.json', JSON.stringify(characters, null, 2));
console.log(`Converted ${characters.length} characters to JSON format.`);