Skip to content

Commit

Permalink
Normalize compendium data names when matching
Browse files Browse the repository at this point in the history
  • Loading branch information
346184 committed Dec 3, 2021
1 parent 59fee83 commit 7780fbd
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import { default as ItemData } from '../data/items.js'
import { default as MagicItemData } from '../data/magicitems.js'

var module_id = '';
var setup_done = false;

var monsterDataDB = {}
var itemDataDB = {}
var magicItemDataDB = {}

export default function registerConverters(id) {
module_id = id;

if (typeof Babele === 'undefined' ||
if (setup_done || typeof Babele === 'undefined' ||
!game.settings.get(module_id, 'enableCompendiumTranslation')) {
return
}
setup_done = true;

monsterDataDB = setupDataParsing(MonsterData);
itemDataDB = setupDataParsing(ItemData);
magicItemDataDB = setupDataParsing(MagicItemData);

Babele.get().registerConverters({
'alignment': convertAlignment,
Expand All @@ -28,6 +38,18 @@ export default function registerConverters(id) {
});
}

function setupDataParsing(d) {
var out = {}
for (var key in d.data) {
out[normalize(key)] = d.data[key];
}
return out;
}

function normalize(s) {
return s.toString().toLowerCase();
}

// Alignments

var alignments = {
Expand Down Expand Up @@ -341,11 +363,11 @@ var source_book_replacements = {
}

function convertMonsterSource(m, translation, data) {
if (!MonsterData.data[data.name]) {
if (!monsterDataDB[normalize(data.name)]) {
return m;
}

var new_src = MonsterData.data[data.name].src + ' S. ' + MonsterData.data[data.name].src_pg;
var new_src = monsterDataDB[normalize(data.name)].src + ' S. ' + monsterDataDB[normalize(data.name)].src_pg;
new_src = new_src.replace(', SRD', '');
new_src = new_src.replace('SRD', '');

Expand All @@ -363,32 +385,32 @@ function convertMonsterSource(m, translation, data) {
}

function convertMonsterEnvironment(m, translation, data) {
if (!MonsterData.data[data.name]) {
if (!monsterDataDB[normalize(data.name)]) {
return m;
}

return MonsterData.data[data.name].env;
return monsterDataDB.data[normalize(data.name)].env;
}


function convertItemName(m, translation, data) {
if (ItemData.data[m]) {
return ItemData.data[m].name;
if (itemDataDB[normalize(m)]) {
return itemDataDB[normalize(m)].name;
}

if (MagicItemData.data[m]) {
return MagicItemData.data[m].name;
if (magicItemDataDB[normalize(m)]) {
return magicItemDataDB[normalize(m)].name;
}

var mod = getMagicalItemModifier(m);
if (mod.length > 0) {
var basename = m.substring(0, m.length - mod.length).trim()
if (ItemData.data[basename]) {
return ItemData.data[basename].name + " " + mod;
if (itemDataDB[normalize(basename)]) {
return itemDataDB[normalize(basename)].name + " " + mod;
}

if (MagicItemData.data[basename]) {
return MagicItemData.data[basename].name + " " + mod;
if (magicItemDataDB[normalize(m)]) {
return magicItemDataDB[normalize(basename)].name + " " + mod;
}
}

Expand Down

0 comments on commit 7780fbd

Please sign in to comment.