Skip to content

Commit

Permalink
Added conversion for lang file and prep for batch conversion PF2E
Browse files Browse the repository at this point in the history
  • Loading branch information
HadaIonut committed Jun 24, 2021
1 parent 3fdb9c2 commit ba248ed
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 3,416 deletions.
3 changes: 1 addition & 2 deletions scripts/Dnd5e/BatchConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
labelConverter
} from "../Utils/ConversionEngineNew.js";
import {createErrorMessage} from "../Utils/ErrorHandler.js";
import {createNewCompendium, typeSelector} from "./Compendium5eConverter.js";
import {loading} from "../Utils/Utils.js";

const itemUpdater = (item, loading) => {
Expand Down Expand Up @@ -103,4 +102,4 @@ const batchConversionMap = {
const initBatchConversion = (elements, type) => () => batchConversion(elements, batchConversionMap[type]);


export {initBatchConversion}
export {initBatchConversion}
98 changes: 49 additions & 49 deletions scripts/MetricModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ const entityUpdater = {
}
}

const batchConversionManager = (data, type, button) => {
let batchConvert = initBatchConversion(data, type);
button.on('click', () => createWarningDialog(batchConvert));
}

const batchCompendiumManager = (data, type, button) => {
let batchConvert = batchCompendiumUpdater(game.packs.keys());
button.on('click', () => createWarningDialog(batchConvert));
}

const batchCompendiumUpdaterMap = {
'dnd5e': {
'scenes': batchConversionManager,
'actors': batchConversionManager,
'items': batchConversionManager,
'tables': batchConversionManager,
'journal': batchConversionManager,
'compendium': batchCompendiumManager
},
'pf2e': {

}
}

const onRenderSideBar = (app, html) => {
if (!game.user.hasRole(4)) return;
let button = $(`<button><i class='fas fa-exchange-alt'></i>Metrify all the ${app?.options?.id}</button>`);
const type = app?.options?.id;
if (batchCompendiumUpdaterMap[game.system.id][type])
batchCompendiumUpdaterMap[game.system.id][type](type === 'compendium' ? game.packs.keys() : game[type], type, button);
if (app?.options?.id !== 'combat' && app?.options?.id !== 'playlists' && !app?.options?.id.includes('popout'))
html.find(".directory-footer").append(button);
}

const addButton = (element, entity, type, html) => {
if (!game.user.hasRole(4)) return;
if (element.length !== 1) return;
Expand Down Expand Up @@ -56,38 +90,21 @@ const onRenderJurnalSheet = (obj, html) => {
addButton(element, obj.object, "sheet");
}

const onRenderSideBar = (app, html) => {
if (!game.user.hasRole(4)) return;
let button = $(`<button><i class='fas fa-exchange-alt'></i>Metrify all the ${app?.options?.id}</button>`);
let batchConvert
switch (app?.options?.id) {
case "scenes":
batchConvert = initBatchConversion(game.scenes, app?.options?.id);
button.on('click', () => createWarningDialog(batchConvert));
break;
case "compendium":
batchConvert = batchCompendiumUpdater(game.packs.keys());
button.on('click', () => createWarningDialog(batchConvert));
break;
case "actors":
batchConvert = initBatchConversion(game.actors, app?.options?.id);
button.on('click', () => createWarningDialog(batchConvert));
break;
case "items":
batchConvert = initBatchConversion(game.items, app?.options?.id);
button.on('click', () => createWarningDialog(batchConvert));
break;
case "tables":
batchConvert = initBatchConversion(game.tables, app?.options?.id);
button.on('click', () => createWarningDialog(batchConvert));
break;
case "journal":
batchConvert = initBatchConversion(game.journal, app?.options?.id);
button.on('click', () => createWarningDialog(batchConvert));
break;
}
if (app?.options?.id !== 'combat' && app?.options?.id !== 'playlists' && !app?.options?.id.includes('popout'))
html.find(".directory-footer").append(button);
const onRenderRollTable = (obj, html) => {
let element = html.find(".window-header .window-title");
addButton(element, obj.object, 'rolltable')
}

const onCompendiumRender = (obj, html) => {
let element = html.find(".window-header .window-title");
addButton(element, obj.collection, 'compendium', obj);

/*
Intended for debugging the relinking function
let button = $(`<a class="popout" style><i class="fas fa-ruler"></i>Relink</a>`);
button.on('click', () => relinkCompendium(obj.collection))
element.after(button)
*/
}

const createWarningDialog = (callFunction) => {
Expand All @@ -109,23 +126,6 @@ const createWarningDialog = (callFunction) => {
}).render(true);
}

const onRenderRollTable = (obj, html) => {
let element = html.find(".window-header .window-title");
addButton(element, obj.object, 'rolltable')
}

const onCompendiumRender = (obj, html) => {
let element = html.find(".window-header .window-title");
addButton(element, obj.collection, 'compendium', obj);

/*
Intended for debugging the relinking function
let button = $(`<a class="popout" style><i class="fas fa-ruler"></i>Relink</a>`);
button.on('click', () => relinkCompendium(obj.collection))
element.after(button)
*/
}

export {
onCompendiumRender,
onRenderActorSheet,
Expand Down
61 changes: 58 additions & 3 deletions scripts/Pf2e/Pf2eConverter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {convertStringFromImperialToMetric, convertText, convertValueToMetric} from "../Utils/ConversionEngineNew.js";
import {copyObject} from "../Utils/Utils.js";
import {capitalize, copyObject} from "../Utils/Utils.js";

const convertTrait = (trait) => trait.replace(/reach-([0-9]+)/, (_0, value) => `Reach ${convertValueToMetric(value, 'ft')} m`);

const convertInconsistentText = (speed) => speed?.replace(/([0-9]+)(\W|&nbsp;| cubic |-)?(feet|inch|foot|ft\.|pounds|lbs\.|pound|lbs|lb|ft)?/, (_0, number, separator, label) => {
return `${convertValueToMetric(number, label ? label : 'ft')}${separator || ''}${label ? convertStringFromImperialToMetric(label): ''}`
return `${convertValueToMetric(number, label ? label : 'ft')}${separator || ''}${label ? convertStringFromImperialToMetric(label) : ''}`
})

const speedConverter = (speedObject) => {
Expand All @@ -15,6 +15,61 @@ const speedConverter = (speedObject) => {
return speedObject;
}

const convertTranslations = (text) => {
text = convertText(text);
text = convertStringFromImperialToMetric(text);
return text;
}

const addNewTranslationsForMetric = (object, prop) => {
const convertedProp = convertTranslations(object[prop]);
const match = prop.match(/Trait([A-Za-z]+)([0-9]+)/);
if (match) {
const newProp = `Trait${match[1]}${convertValueToMetric(match[2], 'ft')}`;
game.i18n.translations.PF2EM[newProp] = convertedProp;
return;
}
const weaponMatch = prop.match(/WeaponRange([0-9]+)/);
if (weaponMatch) {
const newProp = `WeaponRange${convertValueToMetric(weaponMatch[1], 'ft')}`;
game.i18n.translations.PF2EM[newProp] = convertedProp;
return;
}
const areaMatch = prop.match(/AreaSize([0-9]+)/);
if (areaMatch) {
const newProp = `AreaSize${String(convertValueToMetric(areaMatch[1], 'ft')).replace('.', ',')}`;
game.i18n.translations.PF2EM[newProp] = convertedProp;
return;
}
object[prop] = convertedProp;
}

const convertI18NObject = (obj) => {
for (const prop in obj) {
const value = obj[prop];
if (typeof value === 'string') addNewTranslationsForMetric(obj, prop);
else convertI18NObject(value);
}
}

const addNewSizes = () => {
Object.keys(CONFIG.PF2E.areaSizes).forEach((key) => {
const convertedKey = convertValueToMetric(key, 'ft');
CONFIG.PF2E.areaSizes[convertedKey] = `PF2EM.AreaSize${String(convertedKey).replace('.', ',')}`;
});
Object.keys(CONFIG.PF2E.weaponRange).forEach((key) => {
if (key === 'melee' || key === 'reach') return;
const convertedKey = convertValueToMetric(key, 'ft');
CONFIG.PF2E.weaponRange[convertedKey] = `PF2EM.WeaponRange${convertedKey}`;
});
Object.keys(CONFIG.PF2E.weaponTraits).forEach((key) => {
const match = key.match(/([a-z]+)-([a-z]+)?-?([0-9]+)/);
if (!match) return;
const convertedKey = `${match[1]}-${match[2] ? match[2] : ''}-${convertValueToMetric(match[3], 'ft')}`;
CONFIG.PF2E.weaponTraits[convertedKey] = `PF2EM.Trait${capitalize(match[1])}${match[2] ? capitalize(match[2]) : ''}${convertValueToMetric(match[3], 'ft')}`;
});
}

const updateItems = async (items) => {
for (const item of items) await updateItem(item);
}
Expand Down Expand Up @@ -45,4 +100,4 @@ const updateActor = async (actor) => {
await updateItems(actor.data.items);
}

export {updateActor, updateItem, speedConverter, convertInconsistentText, convertTrait}
export {updateActor, updateItem, speedConverter, convertInconsistentText, convertTrait, convertI18NObject, addNewSizes}
Loading

0 comments on commit ba248ed

Please sign in to comment.