Fix char import

This commit is contained in:
sladecraven 2021-07-29 22:53:35 +02:00
parent 4bf5b4ec0f
commit 31ef890408
4 changed files with 136 additions and 111 deletions

View File

@ -380,7 +380,7 @@ Hooks.once('init', () => {
"trapping_qualities_flaws": (value) => { "trapping_qualities_flaws": (value) => {
if ( value ) { if ( value ) {
let newQF = []; let newQF = [];
console.log("ATOUTS", value); //console.log("ATOUTS", value);
var i=0; var i=0;
//var re = /(.*) (\d+)/i; //var re = /(.*) (\d+)/i;
for (i=0; i<value.length; i++) { for (i=0; i<value.length; i++) {

View File

@ -2,7 +2,7 @@
"name": "WH4-fr-translation", "name": "WH4-fr-translation",
"title": "WFRP4e - Core Module en français.", "title": "WFRP4e - Core Module en français.",
"description": "Traduction Française pour Warhammer v4.", "description": "Traduction Française pour Warhammer v4.",
"version": "4.0.11", "version": "4.0.12",
"minimumCoreVersion" : "0.8.0", "minimumCoreVersion" : "0.8.0",
"compatibleCoreVersion": "0.8.8", "compatibleCoreVersion": "0.8.8",
"author": "LeRatierBretonnien", "author": "LeRatierBretonnien",

View File

@ -27,6 +27,8 @@ Talents: Briber, Criminal, Dealmaker,
Etiquette (Criminals, Doktor, Guilder) Etiquette (Criminals, Doktor, Guilder)
Trappings: Dagger, Hand Weapon (Sword) Trappings: Dagger, Hand Weapon (Sword)
` `
import ItemWfrp4e from "/systems/wfrp4e/modules/item/item-wfrp4e.js";
import WFRP_Utility from "/systems/wfrp4e/modules/system/utility-wfrp4e.js";
/************************************************************************************/ /************************************************************************************/
import "./xregexp-all.js"; import "./xregexp-all.js";
@ -35,19 +37,19 @@ const fr_carac = 'm\\s+ws\\s+bs\\s+s\\s+t\\s+i\\s+agi?\\s+dex\\s+int\\s+\\wp\\s+
const carac_val = '(?<m>[0-9-]+)\\s+(?<ws>[0-9-]+)\\s+(?<bs>[0-9-]+)\\s+(?<s>[0-9-]+)\\s+(?<t>[0-9-]+)\\s+(?<i>[0-9-]+)\\s+(?<ag>[0-9-]+)\\s+(?<dex>[0-9-]+)\\s+(?<int>[0-9-]+)\\s+(?<wp>[0-9-]+)\\s+(?<fel>[0-9-]+)\\s+(?<w>[0-9-\*]+)'; const carac_val = '(?<m>[0-9-]+)\\s+(?<ws>[0-9-]+)\\s+(?<bs>[0-9-]+)\\s+(?<s>[0-9-]+)\\s+(?<t>[0-9-]+)\\s+(?<i>[0-9-]+)\\s+(?<ag>[0-9-]+)\\s+(?<dex>[0-9-]+)\\s+(?<int>[0-9-]+)\\s+(?<wp>[0-9-]+)\\s+(?<fel>[0-9-]+)\\s+(?<w>[0-9-\*]+)';
const name_val = '(?<name>[a-zA-Z\\s\\-,]*)[\\s\\r\\na-zA-Z]*(?<tiers>.*|[\\(\\)a-z0-9]+)'; const name_val = '(?<name>[a-zA-Z\\s\\-,]*)[\\s\\r\\na-zA-Z]*(?<tiers>.*|[\\(\\)a-z0-9]+)';
let sectionData = [ let sectionData = [
{ name: "trait", toFind:"Traits\\s*:", secondParse: '(?<name>[a-z\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index:-1 }, { name: "trait", toFind: "Traits\\s*:", secondParse: '(?<name>[a-z\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
{ name: "skill", toFind:"Skills\\s*:", secondParse: '(?<name>[a-z\\s\\(\\)]*)[\\s\\+]*(?<value>.*|[0-9]+)', index:-1 }, { name: "skill", toFind: "Skills\\s*:", secondParse: '(?<name>[a-z\\s\\(\\)]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
{ name: "talent", toFind:"Talents\\s*:", secondParse: '(?<name>[a-z\\-\\s!/]*)[\\s\\+]*(?<value>.*|[0-9]+)', index:-1 }, { name: "talent", toFind: "Talents\\s*:", secondParse: '(?<name>[a-z\\-\\s!/]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
{ name: "trapping", toFind:"Trappings\\s*:", secondParse: '(?<name>[a-z\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index:-1 } { name: "trapping", toFind: "Trappings\\s*:", secondParse: '(?<name>[a-z\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 }
]; ];
let regSep = XRegExp('\\s*,\\s*', 'gi'); // Term separator, with auto trim let regSep = XRegExp('\\s*,\\s*', 'gi'); // Term separator, with auto trim
let regLine1 = XRegExp('[\\r\\n\\.]', 'gi'); // Term separator, with auto trim let regLine1 = XRegExp('[\\r\\n\\.]', 'gi'); // Term separator, with auto trim
let regName = XRegExp(name_val, 'gi'); let regName = XRegExp(name_val, 'gi');
/************************************************************************************/ /************************************************************************************/
async function __findItem(itemName, itemType, location = null) { async function __findItem(itemName, itemType, location = null) {
itemName = itemName.trim(); let toSearch = itemName.toLowerCase().trim();
let items = game.items.entities.filter(i => i.type == itemType) let items = game.items.contents.filter(i => i.type == itemType)
// Search imported items first // Search imported items first
for (let i of items) { for (let i of items) {
@ -63,52 +65,54 @@ async function __findItem(itemName, itemType, location = null) {
location.split(".")[1] == p.metadata.name location.split(".")[1] == p.metadata.name
}) })
if (pack) { if (pack) {
if (pack.translations[itemName]) { await pack.getIndex().then(index => itemList = index);
let translItemName = pack.translations[itemName].name; let searchResult = itemList.find(t => (t.translated && t.originalName.toLowerCase() == toSearch) || (t.name.toLowerCase() == toSearch) );
await pack.getIndex().then(index => itemList = index); if (searchResult)
let searchResult = itemList.find(t => t.name == translItemName) return await pack.getDocument(searchResult._id)
if (searchResult)
return await pack.getEntity(searchResult._id)
}
} }
} }
// If all else fails, search each pack // If all else fails, search each pack
for (let p of game.packs.filter(p => p.metadata.tags && p.metadata.tags.includes(itemType))) { for (let p of game.wfrp4e.tags.getPacksWithTag(itemType)) {
if (p.translations[itemName]) { await p.getIndex().then(index => itemList = index);
let translItemName = p.translations[itemName].name; let searchResult = itemList.find(t => (t.translated && t.originalName.toLowerCase() == toSearch) || (t.name.toLowerCase() == toSearch) );
await p.getIndex().then(index => itemList = index); if (searchResult)
let searchResult = itemList.find(t => t.name == translItemName) return await p.getDocument(searchResult._id)
if (searchResult)
return await p.getEntity(searchResult._id)
}
} }
} }
/************************************************************************************/
async function __findSkill(skillName) {
skillName = skillName.trim();
// First try world items
let worldItem = game.items.entities.filter(i => i.type == "skill" && i.name == skillName)[0];
if (worldItem) return worldItem
let skillList = []; /************************************************************************************/
let packs = game.packs.filter(p => p.metadata.tags && p.metadata.tags.includes("skill")) async function __findSkill(skillName, value = undefined) {
let toSearch = skillName.toLowerCase().trim();
let parseStr = '(?<name>[a-z\\s]*)[\\s\\+]*(?<specialized>[a-z\\s\\(\\)]*)';
let skillSplit = XRegExp.exec(skillName, XRegExp(parseStr, 'gi'));
// First try world items
let worldItem = game.items.contents.filter(i => i.type == "skill" && i.name.toLowerCase() == toSearch)[0];
if (worldItem) return worldItem;
let packs = game.wfrp4e.tags.getPacksWithTag("skill");
for (let pack of packs) { for (let pack of packs) {
//console.log("SEARCH : ", skillName, pack); let skillList = await pack.getIndex();
if ( pack.translations[skillName] ) { // Search for specific skill (won't find unlisted specializations)
let translSkillName = pack.translations[skillName].name; let searchResult = skillList.find(s => (s.translated && s.originalName.toLowerCase() == toSearch) || (s.name.toLowerCase() == toSearch ) );
skillList = await pack.getIndex() if (!searchResult) {
// Search for specific skill (won't find unlisted specializations) let toSearchClean = toSearch.split("(")[0].trim();
let searchResult = skillList.find(s => s.name == translSkillName) searchResult = skillList.find(s => (s.translated && s.originalName.toLowerCase().split("(")[0].trim() == toSearchClean) ||
if (!searchResult) (s.name.toLowerCase().split("(")[0].trim() == toSearchClean) );
searchResult = skillList.find(s => s.name.split("(")[0].trim() == skillName.split("(")[0].trim()) }
if (searchResult) { if (searchResult) {
let dbSkill; let dbSkill;
await pack.getEntity(searchResult._id).then(packSkill => dbSkill = packSkill); await pack.getDocument(searchResult._id).then(packSkill => dbSkill = packSkill);
dbSkill.data.name = translSkillName; // This is important if a specialized skill wasn't found. Without it, <Skill ()> would be added instead of <Skill (Specialization)> if (skillSplit.specialized && ( dbSkill.name.includes('()') || dbSkill.name.includes('( )' ) ) ) {
return dbSkill; let spec = XRegExp.replace(skillSplit.specialized, "(", "");
spec = XRegExp.replace(spec, ")", "");
let skillSplit2 = XRegExp.exec(dbSkill.name, XRegExp(parseStr, 'gi'));
dbSkill.data.update( { name: skillSplit2.name + '(' + game.i18n.localize( spec.trim() ) + ')' } );
} }
//game.babele.translate('wfrp4e-core.skills', dbSkill);
return dbSkill;
} }
} }
throw "Could not find skill (or specialization of) " + skillName + " in compendum or world" throw "Could not find skill (or specialization of) " + skillName + " in compendum or world"
@ -116,35 +120,40 @@ async function __findSkill(skillName) {
/************************************************************************************/ /************************************************************************************/
async function __findTalent(talentName) { async function __findTalent(talentName) {
talentName = talentName.trim(); let parseStr = '(?<name>[a-z\\s]*)[\\s\\+]*(?<specialized>[a-z\\s\\(\\)]*)';
let talentSplit = XRegExp.exec(talentName, XRegExp(parseStr, 'gi'));
let toSearch = talentSplit.name.toLowerCase().trim();
// First try world items // First try world items
let worldItem = game.items.entities.filter(i => i.type == "talent" && i.name == talentName)[0]; let worldItem = game.items.contents.filter(i => i.type == "talent" && i.name.toLowerCase() == toSearch)[0];
if (worldItem) return worldItem if (worldItem) return worldItem;
let talentList = []; let packs = game.wfrp4e.tags.getPacksWithTag("talent");
let packs = game.packs.filter(p => p.metadata.tags && p.metadata.tags.includes("talent"))
for (let pack of packs) { for (let pack of packs) {
if ( pack.translations[talentName] ) { let talentList = await pack.getIndex();
let translTalentName = pack.translations[talentName].name; // Search for specific skill (won't find unlisted specializations)
talentList = await pack.getIndex() let searchResult = talentList.find(s => (s.translated && s.originalName.toLowerCase() == toSearch) || (s.name.toLowerCase() == toSearch ) );
// Search for specific talent (won't find unlisted specializations) if (!searchResult) {
let searchResult = talentList.find(t => t.name == translTalentName) let toSearchClean = toSearch.split("(")[0].trim();
if (!searchResult) searchResult = talentList.find(s => (s.translated && s.originalName.toLowerCase().split("(")[0].trim() == toSearchClean) ||
searchResult = talentList.find(t => t.name.split("(")[0].trim() == talentName.split("(")[0].trim()) (s.name.toLowerCase().split("(")[0].trim() == toSearchClean) );
}
if (searchResult) { if (searchResult) {
let dbTalent; let dbTalent;
await pack.getEntity(searchResult._id).then(packTalent => dbTalent = packTalent); await pack.getDocument(searchResult._id).then(packTalent => dbTalent = packTalent);
dbTalent.data.name = translTalentName; // This is important if a specialized talent wasn't found. Without it, <Talent ()> would be added instead of <Talent (Specialization)> if ( talentSplit.specialized ) {
return dbTalent; let spec = XRegExp.replace(talentSplit.specialized, "(", "");
spec = XRegExp.replace(spec, ")", "");
dbTalent.data.update( { name: talentSplit.name + '(' + game.i18n.localize( spec.trim() ) + ')' } );
} }
return dbTalent;
} }
} }
throw "Could not find talent (or specialization of) " + talentName + " in compendium or world" throw "Could not find talent (or specialization of) " + talentName + " in compendium or world"
} }
/************************************************************************************/ /************************************************************************************/
export default async function statParserFR( statString, type = "npc") { export default async function statParserFR(statString, type = "npc") {
let model = duplicate(game.system.model.Actor[type]); let model = duplicate(game.system.model.Actor[type]);
let reg1 = XRegExp(us_carac, 'gi'); let reg1 = XRegExp(us_carac, 'gi');
@ -154,23 +163,23 @@ export default async function statParserFR( statString, type = "npc") {
let res1 = XRegExp.exec(statString, reg1); let res1 = XRegExp.exec(statString, reg1);
console.log("REG", res1); console.log("REG", res1);
let pnjStr = statString.substring(0, res1.index); let pnjStr = statString.substring(0, res1.index);
let nameRes = XRegExp.exec(pnjStr, regName ); let nameRes = XRegExp.exec(pnjStr, regName);
console.log(nameRes); console.log(nameRes);
if ( nameRes.tiers && nameRes.tiers.length > 0 && hasProperty(model, "details.status.value") ) { if (nameRes.tiers && nameRes.tiers.length > 0 && hasProperty(model, "details.status.value")) {
let regTiers = XRegExp("(?<name>[A-Za-z]*)\\s+(?<level>[0-9]*)"); let regTiers = XRegExp("(?<name>[A-Za-z]*)\\s+(?<level>[0-9]*)");
let resTiers = XRegExp.exec(nameRes.tiers, regTiers); let resTiers = XRegExp.exec(nameRes.tiers, regTiers);
console.log(resTiers); console.log(resTiers);
model.details.status.value = game.i18n.localize(resTiers.name.trim()) + " " + resTiers.level; model.details.status.value = game.i18n.localize(resTiers.name.trim()) + " " + resTiers.level;
} }
// Compute the PNJ name // Compute the PNJ name
let pnjName = nameRes.name.split("—")[0].split(" ").filter(f => !!f); let pnjName = nameRes.name.split("—")[0].split(" ").filter(f => !!f);
pnjName = pnjName.map(word => { pnjName = pnjName.map(word => {
if (word == "VON") if (word == "VON")
return word.toLowerCase(); return word.toLowerCase();
word = word.toLowerCase(); word = word.toLowerCase();
word = word[0].toUpperCase() + word.substring(1, word.length); word = word[0].toUpperCase() + word.substring(1, word.length);
return word; return word;
}) })
pnjName = pnjName.join(" ") pnjName = pnjName.join(" ")
@ -188,23 +197,23 @@ export default async function statParserFR( statString, type = "npc") {
//console.log("CARAC", model.characteristics); //console.log("CARAC", model.characteristics);
// Search position of skills/talents/... // Search position of skills/talents/...
for( let def of sectionData ) { for (let def of sectionData) {
def.regDef = XRegExp(def.toFind, 'gi'); def.regDef = XRegExp(def.toFind, 'gi');
let res = XRegExp.exec(statString, def.regDef); let res = XRegExp.exec(statString, def.regDef);
if (res ) def.index = res.index; // Get the index in the string if (res) def.index = res.index; // Get the index in the string
//console.log(" Parsing", def.name, res); //console.log(" Parsing", def.name, res);
} }
// Sort to split position of various substring // Sort to split position of various substring
sectionData.sort( function(a, b) { return a.index - b.index; } ); sectionData.sort(function (a, b) { return a.index - b.index; });
let globalItemList = []; let globalItemList = [];
// Then loop again and process each item type // Then loop again and process each item type
for(let i=0; i< sectionData.length; i++ ) { for (let i = 0; i < sectionData.length; i++) {
let def = sectionData[i]; let def = sectionData[i];
if ( def.index > -1) { if (def.index > -1) {
let maxIndex = statString.length; let maxIndex = statString.length;
if ( sectionData[i+1] && sectionData[i+1].index > -1 ) if (sectionData[i + 1] && sectionData[i + 1].index > -1)
maxIndex = sectionData[i+1].index; maxIndex = sectionData[i + 1].index;
def.substring = statString.substring(def.index, maxIndex); def.substring = statString.substring(def.index, maxIndex);
def.substring = XRegExp.replace(def.substring, def.regDef, ""); def.substring = XRegExp.replace(def.substring, def.regDef, "");
def.substring = XRegExp.replace(def.substring, regLine1, " "); def.substring = XRegExp.replace(def.substring, regLine1, " ");
@ -213,30 +222,30 @@ export default async function statParserFR( statString, type = "npc") {
// Then create a table of it in termList, with specific sub-parsing rules // Then create a table of it in termList, with specific sub-parsing rules
let termList = XRegExp.split(def.substring, regSep); let termList = XRegExp.split(def.substring, regSep);
for (let name of termList) { for (let name of termList) {
let itemFound, subres; let itemFound, subres, value;
if (def.secondParse) { if (def.secondParse) {
subres = XRegExp.exec( name, XRegExp(def.secondParse, 'gi') ); subres = XRegExp.exec(name, XRegExp(def.secondParse, 'gi'));
name = subres.name.trim(); name = subres.name.trim();
} value = XRegExp.replace(subres.value, "(", "");
if ( def.name == 'trait') { value = XRegExp.replace(subres.value, ")", "");
}
if (def.name == 'trait') {
try { try {
itemFound = await __findItem(name, "trait"); itemFound = await __findItem(name, "trait");
} }
catch {} catch { }
if ( itemFound && subres && subres.value.length > 0 ) { if (itemFound && subres && subres.value.length > 0) {
subres.value = XRegExp.replace(subres.value, "(", ""); itemFound.data.data.specification.value = game.i18n.localize(value);
subres.value = XRegExp.replace(subres.value, ")", "");
itemFound.data.data.specification.value = game.i18n.localize( subres.value);
} }
if (!itemFound) if (!itemFound)
ui.notifications.error("Trait non trouvé, à ajouter manuellemen : " + name, { permanent: true }) ui.notifications.error("Trait non trouvé, à ajouter manuellement : " + name, { permanent: true })
} else if ( def.name == 'skill') { } else if (def.name == 'skill') {
try { try {
itemFound = await __findSkill(name); itemFound = await __findSkill(name, value);
} }
catch {} catch { }
if ( itemFound && subres && subres.value) { if (itemFound && subres && value) {
itemFound.data.data.advances.value = Number(subres.value) - Number(resCarac[itemFound.data.data.characteristic.value]); itemFound.data.data.advances.value = Number(value) - Number(model.characteristics[itemFound.data.data.characteristic.value].initial);
} }
if (!itemFound) if (!itemFound)
ui.notifications.error("Compétence non trouvée, à ajouter manuellement : " + name, { permanent: true }) ui.notifications.error("Compétence non trouvée, à ajouter manuellement : " + name, { permanent: true })
@ -244,23 +253,23 @@ export default async function statParserFR( statString, type = "npc") {
try { try {
itemFound = await __findTalent(name); itemFound = await __findTalent(name);
} }
catch {} catch { }
if ( itemFound && subres && subres.value) if (itemFound && subres && value)
itemFound.data.data.advances.value = Number(subres.value); itemFound.data.data.advances.value = Number(value);
if (!itemFound) if (!itemFound)
ui.notifications.error("Talent non trouvé, à ajouter manuellement : " + name, { permanent: true }) ui.notifications.error("Talent non trouvé, à ajouter manuellement : " + name, { permanent: true })
} else if (def.name == 'trapping') { } else if (def.name == 'trapping') {
try { try {
itemFound = await __findItem(name, "trapping"); itemFound = await __findItem(name, "trapping");
} }
catch {} catch { }
if (!itemFound) { if (!itemFound) {
itemFound = new game.wfrp4e.entities.ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "trapping", data: game.system.model.Item.trapping }) itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "trapping", data: game.system.model.Item.trapping })
itemFound.data.data.trappingType.value = "misc" itemFound.data.data.trappingType.value = "misc"
} }
} }
if (itemFound) if (itemFound)
globalItemList.push( itemFound ); globalItemList.push(itemFound);
} }
} }
} }
@ -270,11 +279,16 @@ export default async function statParserFR( statString, type = "npc") {
globalItemList = globalItemList.concat(moneyItems); globalItemList = globalItemList.concat(moneyItems);
//console.log("My liste :", globalItemList); //console.log("My liste :", globalItemList);
let name = pnjName; let name = pnjName;
return { name, type, data: model, items: globalItemList }
let effects = globalItemList.reduce((total, globItem) => total.concat(globItem.data.effects), [])
effects = effects.filter(e => !!e)
effects = effects.filter(e => e.transfer)
return { name, type, data: model, items: globalItemList, effects }
} }
// If the carac string has not been found // If the carac string has not been found
ui.notifications.error("Impossible de convertir ces statitiques, les caractéristiques n'ont pas été trouvées", { permanent: true } ) ui.notifications.error("Impossible de convertir ces statitiques, les caractéristiques n'ont pas été trouvées", { permanent: true })
} }
/************************************************************************************/ /************************************************************************************/
@ -283,5 +297,5 @@ Hooks.once('ready', () => {
//var fullskills = game.packs.get('wfrp4e-core.skills'); //var fullskills = game.packs.get('wfrp4e-core.skills');
//console.log("Skills", game.wfrp4e.apps.StatBlockParser.prototype); //console.log("Skills", game.wfrp4e.apps.StatBlockParser.prototype);
} ) })

11
workspace.code-workspace Normal file
View File

@ -0,0 +1,11 @@
{
"folders": [
{
"path": "."
},
{
"path": "../WFRP4e-FoundryVTT"
}
],
"settings": {}
}