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

133 lines
4.3 KiB
JavaScript

/**
* Parser for FFXI Trust character data
*/
class TrustParser {
constructor() {
this.roles = [];
this.characters = {};
this.charactersByRole = {};
this.allCharacters = [];
}
/**
* Process the JSON data to organize it for the application
* @param {Array} jsonData - Array of character objects from the JSON file
*/
processJsonData(jsonData) {
// Extract unique roles
const roleSet = new Set();
jsonData.forEach(char => {
if (char.role && char.role.trim()) {
roleSet.add(char.role.trim());
}
});
this.roles = Array.from(roleSet);
// Initialize charactersByRole
this.roles.forEach(role => {
this.charactersByRole[role] = [];
});
// Process each character
jsonData.forEach(char => {
if (!char.name || char.name === 'Tank' ||
char.name.startsWith('Complete') ||
char.name.startsWith('Trade') ||
char.name.startsWith('Notice:') ||
char.name.startsWith('Be in') ||
char.name.startsWith('*')) {
return; // Skip invalid entries
}
// Create a character object with normalized property names
const character = {
name: char.name,
role: char.role || 'Unknown',
job: char.job || '',
spells: char.spells || '',
abilities: char.abilities || '',
weaponSkills: char.weapon_skills || '',
acquisition: char.acquisition || '',
specialFeatures: char.special_features || '',
trustSynergy: char.trust_synergy || ''
};
// Add to characters object
this.characters[char.name] = character;
// Add to charactersByRole
if (character.role && this.charactersByRole[character.role]) {
this.charactersByRole[character.role].push(character.name);
}
// Add to allCharacters
this.allCharacters.push({
name: character.name,
role: character.role
});
});
}
/**
* Load the JSON data file
* @param {Function} callback - Function to call when loading is complete
*/
loadData(callback) {
fetch('trusts.json')
.then(response => response.json())
.then(data => {
this.processJsonData(data);
// Call the callback with the processed data
if (callback) {
callback({
roles: this.roles,
characters: this.characters,
charactersByRole: this.charactersByRole,
allCharacters: this.allCharacters
});
}
})
.catch(error => {
console.error('Error loading trust data:', error);
});
}
/**
* Search for characters by name or other attributes
* @param {string} query - Search query
* @returns {Array} - Array of matching characters
*/
searchCharacters(query) {
if (!query) return this.allCharacters;
query = query.toLowerCase();
return this.allCharacters.filter(char => {
const character = this.characters[char.name];
// Search by name
if (char.name.toLowerCase().includes(query)) return true;
// Search by role
if (char.role.toLowerCase().includes(query)) return true;
// Search by job
if (character.job && character.job.toLowerCase().includes(query)) return true;
// Search by abilities
if (character.abilities && character.abilities.toLowerCase().includes(query)) return true;
// Search by spells
if (character.spells && character.spells.toLowerCase().includes(query)) return true;
return false;
});
}
}
// Create a global instance of the parser
const trustParser = new TrustParser();