forked from public/foundryvtt-wh4-lang-fr-fr
		
	Various fixes
This commit is contained in:
		| @@ -56,17 +56,19 @@ const name_val = '(?<name>[a-zA-Z\\s\\-,]*)[\\s\\r\\na-zA-Z]*(?<tiers>.*|[\\(\\) | ||||
| let sectionDataFR = [ | ||||
|   { name: "trait", toFind: "Traits\\s*:", secondParse: '(?<name>[a-zàéè\\s]*)[\\s\\+]*(?<value>.*|[\\+0-9]+)', index: -1 }, | ||||
|   { name: "skill", toFind: "Compétences\\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: "mutation", toFind: "Mutations\\s*:", secondParse: '(?<name>[a-zàéè\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 }, | ||||
|   { name: "trapping", toFind: "Equiement\\s*:", secondParse: '(?<name>[a-zàéè\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 } | ||||
|   { name: "trapping", toFind: "Equipement\\s*:", secondParse: '(?<name>[a-zàéè\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 }, | ||||
|   { name: "spell", toFind: "Sorts\\s*\\([a-z\\s]*\\)*:", secondParse: '(?<name>[a-zàéè\\s]*)', index: -1 } | ||||
| ]; | ||||
| let sectionDataUS = [ | ||||
|   { 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: "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: "mutation", toFind: "Mutations\\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 }, | ||||
|   { name: "spell", toFind: "Spells\\s*\\([a-z\\s]*\\)*:", secondParse: '(?<name>[a-z\\s]*)', index: -1 }  | ||||
| ] | ||||
| let regSep = XRegExp('\\s*,\\s*', 'gi'); // Term separator, with auto trim | ||||
| let regLine1 = XRegExp('[\\r\\n\\.]', 'gi'); // Term separator, with auto trim | ||||
| let regName = XRegExp(name_val, 'gi'); | ||||
| @@ -79,7 +81,7 @@ async function __findItem(itemName, itemType, location = null) { | ||||
|   // Search imported items first | ||||
|   for (let i of items) { | ||||
|     if (i.name.toLowerCase() == itemName.toLowerCase() && i.type == itemType)  | ||||
|       return i; | ||||
|       return i.toObject(); | ||||
|   } | ||||
|   let itemList | ||||
|  | ||||
| @@ -92,8 +94,10 @@ async function __findItem(itemName, itemType, location = null) { | ||||
|     if (pack) { | ||||
|       await pack.getIndex().then(index => itemList = index); | ||||
|       let searchResult = itemList.find(t => (t.translated && t.type == itemType && t.originalName.toLowerCase() == toSearch) || (t.type == itemType && t.name.toLowerCase() == toSearch) ); | ||||
|       if (searchResult) | ||||
|         return await pack.getDocument(searchResult._id) | ||||
|       if (searchResult) { | ||||
|         let item = await pack.getDocument(searchResult._id) | ||||
|         return item.toObject() | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
| @@ -101,8 +105,10 @@ async function __findItem(itemName, itemType, location = null) { | ||||
|   for (let p of game.wfrp4e.tags.getPacksWithTag(itemType)) { | ||||
|     await p.getIndex().then(index => itemList = index); | ||||
|     let searchResult = itemList.find(t => (t.translated && t.type == itemType && t.originalName.toLowerCase() == toSearch) || (t.type == itemType && t.name.toLowerCase() == toSearch) ); | ||||
|     if (searchResult) | ||||
|       return await p.getDocument(searchResult._id) | ||||
|     if (searchResult) { | ||||
|       let item = await p.getDocument(searchResult._id) | ||||
|       return item.toObject() | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| @@ -121,24 +127,26 @@ async function __findSkill(skillName, value = undefined) { | ||||
|   for (let pack of packs) { | ||||
|     let skillList = await pack.getIndex(); | ||||
|     // Search for specific skill (won't find unlisted specializations) | ||||
|     let searchResult = skillList.find(s => (s.translated && s.originalName.toLowerCase() == toSearch) || (s.name.toLowerCase() == toSearch ) ); | ||||
|     let searchResult = skillList.find(s => (s.type == "skill" && s.translated && s.originalName.toLowerCase() == toSearch) || (s.type == "skill" && s.name.toLowerCase() == toSearch ) ); | ||||
|     if (!searchResult) { | ||||
|       let toSearchClean = toSearch.split("(")[0].trim(); | ||||
|       searchResult = skillList.find(s => (s.translated && s.originalName.toLowerCase().split("(")[0].trim() == toSearchClean) ||  | ||||
|                 (s.name.toLowerCase().split("(")[0].trim() == toSearchClean)   ); | ||||
|       searchResult = skillList.find(s => (s.type == "skill" && s.translated && s.originalName.toLowerCase().split("(")[0].trim() == toSearchClean) ||  | ||||
|                 (s.type == "skill" && s.name.toLowerCase().split("(")[0].trim() == toSearchClean)   ); | ||||
|     } | ||||
|     if (searchResult) { | ||||
|       let dbSkill; | ||||
|       await pack.getDocument(searchResult._id).then(packSkill => dbSkill = packSkill); | ||||
|       if (skillSplit.specialized && ( dbSkill.name.includes('()') || dbSkill.name.includes('( )' ) ) ) { | ||||
|       let item = dbSkill.toObject(); | ||||
|       //console.log("Skill name1", skillSplit) | ||||
|       if (skillSplit.specialized && ( item.name.includes('()') || item.name.includes('( )' ) ) ) { | ||||
|         let spec = XRegExp.replace(skillSplit.specialized, "(", ""); | ||||
|         spec = XRegExp.replace(spec, ")", ""); | ||||
|         let skillSplit2 = XRegExp.exec(dbSkill.name, XRegExp(parseStr, 'gi')); | ||||
|         dbSkill.name = skillSplit2.name + '(' + game.i18n.localize( spec.trim() ) + ')'  | ||||
|         let skillSplit2 = XRegExp.exec(item.name, XRegExp(parseStr, 'gi')); | ||||
|         item.name = skillSplit2.name + '(' + game.i18n.localize( spec.trim() ) + ')'  | ||||
|         //dbSkill.update( { name: } ); | ||||
|       } | ||||
|       //game.babele.translate('wfrp4e-core.skills', dbSkill); | ||||
|       return dbSkill; | ||||
|       return item | ||||
|     } | ||||
|   } | ||||
|   throw "Could not find skill (or specialization of) " + skillName + " in compendum or world" | ||||
| @@ -146,9 +154,11 @@ async function __findSkill(skillName, value = undefined) { | ||||
|  | ||||
| /************************************************************************************/ | ||||
| async function __findTalent(talentName) { | ||||
|   let parseStr = '(?<name>[a-z\\s]*)[\\s\\+]*(?<specialized>[a-z\\s\\(\\)]*)'; | ||||
|   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(); | ||||
|    | ||||
|   //console.log("Talent name", talentName, talentSplit) | ||||
|  | ||||
|   // First try world items | ||||
|   let worldItem = game.items.contents.filter(i => i.type == "talent" && i.name.toLowerCase() == toSearch)[0]; | ||||
| @@ -166,13 +176,16 @@ async function __findTalent(talentName) { | ||||
|     } | ||||
|     if (searchResult) { | ||||
|       let dbTalent; | ||||
|       //console.log("Talent name1", talentSplit) | ||||
|       await pack.getDocument(searchResult._id).then(packTalent => dbTalent = packTalent); | ||||
|       let item = dbTalent.toObject(); | ||||
|       if ( talentSplit.specialized  ) { | ||||
|         let spec = XRegExp.replace(talentSplit.specialized, "(", ""); | ||||
|         spec = XRegExp.replace(spec, ")", ""); | ||||
|         dbTalent.update( { name: talentSplit.name + '(' + game.i18n.localize( spec.trim() ) + ')' } ); | ||||
|         item.name = talentSplit.name + '(' + game.i18n.localize( spec.trim() ) + ')' | ||||
|         //console.log("Talent name2", dbTalent.name, spec, game.i18n.localize( spec.trim()) ) | ||||
|       } | ||||
|       return dbTalent; | ||||
|       return item; | ||||
|     } | ||||
|   } | ||||
|   throw "Could not find talent (or specialization of) " + talentName + " in compendium or world" | ||||
| @@ -267,13 +280,19 @@ export default async function statParserFR(statString, type = "npc") { | ||||
|  | ||||
|         // Then create a table of it in termList, with specific sub-parsing rules | ||||
|         let termList = XRegExp.split(def.substring, regSep); | ||||
|         let lastSkillName | ||||
|         for (let name of termList) { | ||||
|           let itemFound, subres, value; | ||||
|           if (def.secondParse) { | ||||
|             subres = XRegExp.exec(name, XRegExp(def.secondParse, 'gi')) | ||||
|             //console.log("Second pars", def, name, subres) | ||||
|             name  = subres.name.trim().replace("\n", "").replace("\r", "") | ||||
|             value = XRegExp.replace(subres.value, "(", "") | ||||
|             value = XRegExp.replace(value, ")", "") | ||||
|             if (subres.value) { | ||||
|               value = XRegExp.replace(subres.value, "(", "") | ||||
|               value = XRegExp.replace(value, ")", "")   | ||||
|             } else { | ||||
|               value = 0 | ||||
|             } | ||||
|           } | ||||
|           name = __patchName(name) | ||||
|           if (def.name == 'trait') { | ||||
| @@ -281,12 +300,10 @@ export default async function statParserFR(statString, type = "npc") { | ||||
|               itemFound = await __findItem(name, "trait") | ||||
|             } | ||||
|             catch { } | ||||
|             if (itemFound) | ||||
|               itemFound = itemFound.toObject() | ||||
|             if (itemFound && value && value.length > 0) { | ||||
|               if (name.toLowerCase() == 'weapon' || name.toLowerCase() == "bite" || name.toLowerCase() == "tail" || | ||||
|                   name.toLowerCase() == 'arme' || name.toLowerCase() == "morsure" || name.toLowerCase() == "queue") { | ||||
|                 console.log(itemFound) | ||||
|                 //console.log(itemFound) | ||||
|                 itemFound.system.specification.value = Number(value) - Math.floor( Number(model.characteristics.s.initial) / 10) | ||||
|               } else { | ||||
|                 itemFound.system.specification.value = game.i18n.localize(value) | ||||
| @@ -300,11 +317,20 @@ export default async function statParserFR(statString, type = "npc") { | ||||
|               itemFound = await __findSkill(name, value); | ||||
|             } | ||||
|             catch { } | ||||
|             if (itemFound) | ||||
|               itemFound = itemFound.toObject(); | ||||
|             if (!itemFound && lastSkillName) { | ||||
|               name = lastSkillName + " (" + name + ")" | ||||
|               itemFound = await __findSkill(name, value); | ||||
|             } | ||||
|             if (itemFound && subres && value) { | ||||
|               itemFound.system.advances.value = Number(value) - Number(model.characteristics[itemFound.system.characteristic.value].initial); | ||||
|             } | ||||
|             lastSkillName = undefined | ||||
|             if ( itemFound?.name?.includes('Trade')) { // useful to handle skills grouping | ||||
|               lastSkillName = "Trade" | ||||
|             }  | ||||
|             if ( itemFound?.name?.includes('Lore')) { | ||||
|               lastSkillName = "Lore" | ||||
|             }  | ||||
|             if (!itemFound) | ||||
|               ui.notifications.error("Compétence non trouvée, à ajouter manuellement : " + name, { permanent: true }) | ||||
|           } else if (def.name == 'talent') { | ||||
| @@ -312,8 +338,6 @@ export default async function statParserFR(statString, type = "npc") { | ||||
|               itemFound = await __findTalent(name); | ||||
|             } | ||||
|             catch { } | ||||
|             if (itemFound) | ||||
|               itemFound = itemFound.toObject(); | ||||
|             if (itemFound && subres && value) | ||||
|               itemFound.system.advances.value = Number(value); | ||||
|             if (!itemFound) | ||||
| @@ -324,18 +348,25 @@ export default async function statParserFR(statString, type = "npc") { | ||||
|             } | ||||
|             catch { } | ||||
|             if (!itemFound && name) { | ||||
|               itemFound = new game.entities.ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "trapping", system: game.system.model.Item.trapping }) | ||||
|               itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "trapping", system: game.system.model.Item.trapping }) | ||||
|               itemFound.system.trappingType.value = "misc" | ||||
|               itemFound = itemFound.toObject() | ||||
|             }  | ||||
|             if (itemFound) | ||||
|               itemFound = itemFound.toObject(); | ||||
|           } else if (def.name == 'spell') { | ||||
|             console.log("Found spells section!!!!") | ||||
|               try { | ||||
|                 itemFound = await __findItem(name, "spell"); | ||||
|               } | ||||
|               catch { } | ||||
|               if (!itemFound && name) { | ||||
|                 itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "spell", system: game.system.model.Item.spell }) | ||||
|                 itemFound = itemFound.toObject();   | ||||
|               }  | ||||
|           } else if (def.name == 'mutation') { | ||||
|             try { | ||||
|               itemFound = await __findItem(name, "mutation"); | ||||
|             } | ||||
|             catch { } | ||||
|             if (itemFound) | ||||
|               itemFound = itemFound.toObject(); | ||||
|           } | ||||
|  | ||||
|           if (itemFound) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user