foundryvtt-wh4-lang-fr-fr/modules/import-stat-2.js

516 lines
23 KiB
JavaScript
Raw Normal View History

2020-11-08 00:22:04 +01:00
/************************************************************************************/
2020-11-08 23:12:52 +01:00
// Some internal test strings
let strfr = `LA TERREUR DE LA TEUFEL, TROLL DE RIVIÈRE RUSÉ
M
CC CT
F
E
I
Ag Dex Int FM Soc B
4
40 15
55
45
20
15 15 30 20 5 38
Traits : Amphibie, Arme +9, Armure (2), Dur à Cuire, Insensible
à la douleur, Morsure +8, Régénération, Taille (Grande), Vision
Nocturne, Vomissement`
2020-11-08 00:22:04 +01:00
let str1 = `JABBERSLYTHE
2020-11-07 13:46:15 +01:00
M WS BS S T I Agi Dex
Int WP Fel W
7 45 40 55
50 20 35 - 10 20 - 20
Traits: Armour 3, Bestial, Bite+9, Bounce, Corrosive
Blood, Distracting, Infected, Maddening Aura (see
page 17), Night Vision, Size (Enormous), Tail +8,
Tongue Attack +5 (12), Venom, Weapon +9.
`;
2020-11-08 00:22:04 +01:00
let str = `REINER AND DIETER LEDERMANN
SMUGGLERS (BRASS 3)
M WS BS S
T
I
Agi Dex Int WP Fel W
4
33 33 32 35 38 41 39 33 37 38 12
Traits: Weapon (Dagger +5, Sword +7)
Skills: Bribery 43, Charm 43, Cool 42,
Consume Alcohol 45, Gossip 43, Haggle 43,
Lore (Local 38), Perception 43,
Secret Signs (Smuggler) 37
Talents: Briber, Criminal, Dealmaker,
Etiquette (Criminals, Doktor, Guilder)
Trappings: Dagger, Hand Weapon (Sword)
`
2022-09-01 08:18:00 +02:00
//import ItemWfrp4e from "/systems/wfrp4e/modules/item/item-wfrp4e.js"
//import ItemWfrp4e from "/systems/wfrp4e/wfrp4e.js"
2020-11-07 13:46:15 +01:00
2020-11-08 23:12:52 +01:00
/************************************************************************************/
2020-11-07 13:46:15 +01:00
import "./xregexp-all.js";
const us_carac = 'm\\s+ws\\s+bs\\s+s\\s+t\\s+i\\s+agi?\\s+dex\\s+int\\s+\\wp\\s+fel\\s+w';
const fr_carac = 'm\\s+cc\\s+ct\\s+f\\s+e\\s+i\\s+agi?\\s+dex\\s+int\\s+fm\\s+soc\\s+b';
2022-01-30 09:18:49 +01:00
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\\-\*]+)';
2023-10-12 20:10:13 +02:00
const name_val = '(?<name>[a-zA-Z\\s\\-,\']*)[\\s\\r\\na-zA-Z]*(?<tiers>.*|[\\(\\)a-z0-9]+)';
let sectionDataFR = [
2023-09-10 22:22:47 +02:00
{ 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 },
2023-10-12 08:31:55 +02:00
{ name: "talent", toFind: "Talents\\s*:", secondParse: '(?<name>[a-zöàéè\\-\\!\\(\\)\\s\\/\']*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-09-10 22:22:47 +02:00
{ name: "mutation", toFind: "Mutations\\s*:", secondParse: '(?<name>[a-zöàéè\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-10-12 08:31:55 +02:00
{ name: "trapping", toFind: "Equipement\\s*:", secondParse: '(?<name>[a-zöàéè0-9\\s(\\)\\-]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-09-10 22:22:47 +02:00
{ name: "spell", toFind: "Sorts\\s*\\([a-z\\s]*\\)*:", secondParse: '(?<name>[a-zöàéè\\s]*)', index: -1 },
{ name: "spellpetty", toFind: "Spells\\s*\\(Magie Mineure\\)*:", secondParse: '(?<name>[a-zö\\-\\s]*)', index: -1 },
{ name: "spellarcane", toFind: "Spells\\s*\\(Arcane[a-z\\s]*\\)*:", secondParse: '(?<name>[a-zö\\-\\s]*)', index: -1 },
{ name: "spellarcane", toFind: "Spells\\s*\\(Domaine\\s*(?<lore>[a-z\\s]*)\\)*:", secondParse: '(?<name>[a-zö\\-\\s]*)', index: -1 }
];
let sectionDataUS = [
2021-11-02 21:03:04 +01:00
{ name: "trait", toFind: "Traits\\s*:", secondParse: '(?<name>[a-z\\s]*)[\\s\\+]*(?<value>.*|[\\+0-9]+)', index: -1 },
2021-07-29 22:53:35 +02:00
{ name: "skill", toFind: "Skills\\s*:", secondParse: '(?<name>[a-z\\s\\(\\)]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-10-12 08:31:55 +02:00
{ name: "talent", toFind: "Talents\\s*:", secondParse: '(?<name>[a-z\\-\\s\\!\\(\\)\\/\']*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-09-10 22:22:47 +02:00
{ name: "mutation", toFind: "Mutations\\s*:", secondParse: '(?<name>[a-zö\\s]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-10-12 08:31:55 +02:00
{ name: "trapping", toFind: "Trappings\\s*:", secondParse: '(?<name>[a-zö0-9\\s\\(\\)\\-]*)[\\s\\+]*(?<value>.*|[0-9]+)', index: -1 },
2023-09-10 22:22:47 +02:00
{ name: "spellpetty", toFind: "Spells\\s*\\(Petty\\s*[a-z\\s]*\\)*:", secondParse: '(?<name>[a-zö\\-\\s]*)', index: -1 },
{ name: "spellarcane", toFind: "Spells\\s*\\(Arcane\\s*[a-z\\s]*\\)*:", secondParse: '(?<name>[a-zö\\-\\s]*)', index: -1 },
{ name: "spelllore", toFind: "Spells\\s*\\(Lore\\s*of\\s*(?<lore>[a-z\\s]*)\\)*:", secondParse: '(?<name>[a-zö\\-\\s]*)', index: -1 }
2023-09-08 08:01:54 +02:00
]
2023-10-12 08:31:55 +02:00
let moneyUS = [ {name:" gold crown", key: "gc"}, {name: " gc", key: "gc"},
2023-10-12 20:10:13 +02:00
{name:" silver shilling", key:"ss"}, {name:" ss", key: "ss"}, {name:"/-", key: "ss"}, {name:" brass penn", key: "bp"}, {name:" bp", key: "bp"} ]
2023-10-12 08:31:55 +02:00
let moneyFR = [ {name:" couronnes d", key: "gc"}, {name: " co", key: "gc"},
{name:" pistoles ", key:"ss"}, {name:" pa", key: "ss"}, {name:"/-", key: "ss"}, {name:" sous de cuivre", key: "bp"}, {name:" sc", key: "bp"} ]
2020-11-08 00:22:04 +01:00
let regSep = XRegExp('\\s*,\\s*', 'gi'); // Term separator, with auto trim
2020-11-08 23:39:47 +01:00
let regLine1 = XRegExp('[\\r\\n\\.]', 'gi'); // Term separator, with auto trim
2020-11-08 23:12:52 +01:00
let regName = XRegExp(name_val, 'gi');
2020-11-07 13:46:15 +01:00
2023-09-10 22:22:47 +02:00
// Used to detect/manage the skill groupings in the statblock
const __SkillGroupsUS = ["Melee", "Lore", "Trade"]
const __SkillGroupsFR = ["Corps à corps", "Domaine", "Métier"]
// Used to auto update the system.tests.value field
const __hasTestValue = {
"etiquette": true,
"resistance": true,
"acute sense": true,
"strider": true,
"savant": true,
"craftsman": true
}
2020-11-08 00:22:04 +01:00
/************************************************************************************/
2020-11-08 23:12:52 +01:00
async function __findItem(itemName, itemType, location = null) {
2021-07-29 22:53:35 +02:00
let toSearch = itemName.toLowerCase().trim();
let items = game.items.contents.filter(i => i.type == itemType)
2020-11-08 23:12:52 +01:00
2023-09-10 22:22:47 +02:00
console.log("Searching for", toSearch, itemType)
2020-11-08 23:12:52 +01:00
// Search imported items first
for (let i of items) {
2023-09-10 22:22:47 +02:00
if (i.name.toLowerCase() == toSearch.toLowerCase() && i.type == itemType)
2023-09-08 08:01:54 +02:00
return i.toObject();
2020-11-08 23:12:52 +01:00
}
let itemList
// find pack -> search pack -> return entity
if (location) {
let pack = game.packs.find(p => {
2023-09-10 22:22:47 +02:00
location.split(".")[0] == p.metadata.package && location.split(".")[1] == p.metadata.name
2020-11-08 23:12:52 +01:00
})
if (pack) {
2021-07-29 22:53:35 +02:00
await pack.getIndex().then(index => itemList = index);
2023-09-10 22:22:47 +02:00
let searchResult = itemList.find(t => (t.translated && t.type == itemType && (t.originalName.toLowerCase() == toSearch || s.originalName.toLowerCase().split("(")[0].trim() == toSearch)) || (t.type == itemType && (t.name.toLowerCase() == toSearch || t.name.toLowerCase().split("(")[0].trim() == toSearch)));
2023-09-08 08:01:54 +02:00
if (searchResult) {
let item = await pack.getDocument(searchResult._id)
return item.toObject()
}
2020-11-08 23:12:52 +01:00
}
}
// If all else fails, search each pack
2021-07-29 22:53:35 +02:00
for (let p of game.wfrp4e.tags.getPacksWithTag(itemType)) {
await p.getIndex().then(index => itemList = index);
2023-09-10 22:22:47 +02:00
console.log("Seatch", itemType, toSearch);
let searchResult = itemList.find(t => (t.translated && t.type == itemType && (t.originalName.toLowerCase() == toSearch || s.originalName.toLowerCase().split("(")[0].trim() == toSearch)) || (t.type == itemType && (t.name.toLowerCase() == toSearch || t.name.toLowerCase().split("(")[0].trim() == toSearch)));
2023-09-08 08:01:54 +02:00
if (searchResult) {
let item = await p.getDocument(searchResult._id)
return item.toObject()
}
2020-11-08 23:12:52 +01:00
}
}
2020-11-07 13:46:15 +01:00
2021-07-29 22:53:35 +02:00
2020-11-08 23:12:52 +01:00
/************************************************************************************/
2021-07-29 22:53:35 +02:00
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'));
2020-11-08 23:12:52 +01:00
// First try world items
2023-09-10 22:22:47 +02:00
let item
2021-07-29 22:53:35 +02:00
let worldItem = game.items.contents.filter(i => i.type == "skill" && i.name.toLowerCase() == toSearch)[0];
2023-09-10 22:22:47 +02:00
if (worldItem) {
item = worldItem.toObject()
} else {
let packs = game.wfrp4e.tags.getPacksWithTag("skill");
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.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.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);
item = dbSkill.toObject();
2020-11-08 23:12:52 +01:00
}
}
}
2023-09-10 22:22:47 +02:00
if (item) {
//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(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 item
}
2020-11-08 23:12:52 +01:00
}
/************************************************************************************/
async function __findTalent(talentName) {
2023-10-12 08:31:55 +02:00
let parseStr = '(?<name>[a-z\\s\\!\\/\']*)[\\s\\+]*(?<specialized>[a-z\\s\\(\\)\']*)';
2021-07-29 22:53:35 +02:00
let talentSplit = XRegExp.exec(talentName, XRegExp(parseStr, 'gi'));
let toSearch = talentSplit.name.toLowerCase().trim();
2023-09-10 22:22:47 +02:00
//console.log("Talent name", toSearch, talentName, talentSplit)
2021-07-29 22:53:35 +02:00
2020-11-08 23:12:52 +01:00
// First try world items
2023-09-10 22:22:47 +02:00
let item
2021-07-29 22:53:35 +02:00
let worldItem = game.items.contents.filter(i => i.type == "talent" && i.name.toLowerCase() == toSearch)[0];
2023-09-10 22:22:47 +02:00
if (worldItem) {
item = worldItem.toObject()
} else {
let packs = game.wfrp4e.tags.getPacksWithTag("talent");
for (let pack of packs) {
let talentList = await pack.getIndex();
// Search for specific talent (won't find unlisted specializations)
let searchResult = talentList.find(s => (s.type == "talent" && s.translated && s.originalName.toLowerCase() == toSearch) || (s.type == "talent" && s.name.toLowerCase() == toSearch));
if (!searchResult) {
let toSearchClean = toSearch.split("(")[0].trim();
searchResult = talentList.find(s => (s.type == "talent" && s.translated && s.originalName.toLowerCase().split("(")[0].trim() == toSearchClean) ||
(s.type == "talent" && s.name.toLowerCase().split("(")[0].trim() == toSearchClean));
}
if (searchResult) {
let dbTalent;
//console.log("Talent name1", talentSplit)
await pack.getDocument(searchResult._id).then(packTalent => dbTalent = packTalent);
item = dbTalent.toObject();
}
2021-07-29 22:53:35 +02:00
}
2023-09-10 22:22:47 +02:00
}
if (item) {
if (talentSplit.specialized) {
let spec = XRegExp.replace(talentSplit.specialized, "(", "");
spec = XRegExp.replace(spec, ")", "");
spec = spec.trim()
let addToName = true
//console.log("Talent name2", dbTalent.name, spec, game.i18n.localize( spec.trim()) )
if (toSearch == 'doomed') {
item.system.description.value += `<br><br><em>${spec}</em>`;
addToName = false // Very specific case
2020-11-08 23:12:52 +01:00
}
2023-09-10 22:22:47 +02:00
if (__hasTestValue[toSearch]) {
item.system.tests.value = game.i18n.localize(spec);
}
item.name = talentSplit.name
if (addToName) {
item.name += '(' + game.i18n.localize(spec) + ')'
}
item.system.advances.value = 1 // Set 1 advance
2020-11-08 23:12:52 +01:00
}
2023-09-10 22:22:47 +02:00
// Specific Talent post-processing
return item;
2020-11-08 23:12:52 +01:00
}
}
2021-11-02 21:03:04 +01:00
/************************************************************************************/
2023-09-10 22:22:47 +02:00
function __patchName(name) {
if (name.toLowerCase == 'magic sense')
name = 'Magical Sense'
return name
2021-11-02 21:03:04 +01:00
}
2020-11-08 23:12:52 +01:00
/************************************************************************************/
2021-07-29 22:53:35 +02:00
export default async function statParserFR(statString, type = "npc") {
2020-11-08 23:12:52 +01:00
let model = duplicate(game.system.model.Actor[type]);
2020-11-08 00:22:04 +01:00
2022-01-30 09:18:49 +01:00
// Patch wront/strange carac value before processing
statString = statString.replace(/ /g, " 0")
2023-10-12 08:31:55 +02:00
let moneys = { gc: 0, ss: 0, bp: 0 }
2022-01-30 09:18:49 +01:00
let statNameReg = us_carac
2023-10-12 08:31:55 +02:00
let sectionData = duplicate(sectionDataUS)
2023-09-10 22:22:47 +02:00
let skillGrouping = __SkillGroupsUS
2023-10-12 08:31:55 +02:00
let moneyLang = moneyUS
// Detect French stat block
if (statString.includes('CC') && statString.includes('CT') && statString.includes('FM')) {
2023-09-01 13:30:43 +02:00
//ui.notifications.warn("Le parsing de stablock en Français n'est pas encore prêt")
statNameReg = fr_carac
2023-10-12 08:31:55 +02:00
sectionData = duplicate(sectionDataFR)
2023-09-10 22:22:47 +02:00
skillGrouping = __SkillGroupsFR
2023-10-12 08:31:55 +02:00
moneyLang = moneyFR
}
let reg1 = XRegExp(statNameReg, 'gi')
let res = reg1.test(statString)
2020-11-08 00:22:04 +01:00
if (res) { //stat block identified go on
2023-09-10 22:22:47 +02:00
let globalItemList = []
2020-11-08 23:12:52 +01:00
// Extract the name
let res1 = XRegExp.exec(statString, reg1)
console.log("REG", res1)
let pnjStr = statString.substring(0, res1.index)
let nameRes = XRegExp.exec(pnjStr, regName)
2023-09-10 22:22:47 +02:00
//console.log(nameRes)
2021-07-29 22:53:35 +02:00
if (nameRes.tiers && nameRes.tiers.length > 0 && hasProperty(model, "details.status.value")) {
2020-11-08 23:12:52 +01:00
let regTiers = XRegExp("(?<name>[A-Za-z]*)\\s+(?<level>[0-9]*)");
let resTiers = XRegExp.exec(nameRes.tiers, regTiers);
console.log(resTiers);
2021-07-29 22:53:35 +02:00
model.details.status.value = game.i18n.localize(resTiers.name.trim()) + " " + resTiers.level;
2020-11-08 23:12:52 +01:00
}
2023-09-10 22:22:47 +02:00
let baseName = nameRes.name.split("\n")
2020-11-08 23:12:52 +01:00
// Compute the PNJ name
2023-09-10 22:22:47 +02:00
let pnjName = baseName[0].split("—")[0].split(" ").filter(f => !!f)
2020-11-08 23:12:52 +01:00
pnjName = pnjName.map(word => {
2021-07-29 22:53:35 +02:00
if (word == "VON")
return word.toLowerCase();
2020-11-08 23:12:52 +01:00
2021-07-29 22:53:35 +02:00
word = word.toLowerCase();
word = word[0].toUpperCase() + word.substring(1, word.length);
return word;
2020-11-08 23:12:52 +01:00
})
pnjName = pnjName.join(" ")
2023-09-10 22:22:47 +02:00
if (baseName[1]) {
let careerName = baseName[1].split(",")[0]
//console.log("CAREER", careerName)
let career = await __findItem(careerName, "career")
if (career) {
globalItemList.push(career)
}
}
2020-11-08 23:12:52 +01:00
// Get the carac values
let reg2 = XRegExp(carac_val, 'gi')
let resCarac = XRegExp.exec(statString, reg2) // resr contains all carac found
2021-07-29 22:53:35 +02:00
2020-11-08 23:12:52 +01:00
// Setup carac
2022-01-30 09:18:49 +01:00
//console.log("CARAC", resCarac)
if (resCarac["Agi"]) resCarac["Ag"] = resCarac["Agi"] // Auto patch
model.details.move.value = Number(resCarac["m"])
2020-11-08 23:12:52 +01:00
for (let key in model.characteristics) {
if (resCarac[key] === '-') resCarac[key] = 0
model.characteristics[key].initial = Number(resCarac[key])
2020-11-08 23:12:52 +01:00
}
//console.log("CARAC", model.characteristics);
2020-11-08 00:22:04 +01:00
// Search position of skills/talents/...
2021-07-29 22:53:35 +02:00
for (let def of sectionData) {
2020-11-08 00:22:04 +01:00
def.regDef = XRegExp(def.toFind, 'gi');
2020-11-08 23:12:52 +01:00
let res = XRegExp.exec(statString, def.regDef);
2023-09-10 22:22:47 +02:00
if (res) {
def.index = res.index // Position of the string in the whole statblock
def.lore = res.lore // Extraction of the lore, when present
} // Get the index in the string
2020-11-08 00:22:04 +01:00
//console.log(" Parsing", def.name, res);
}
2020-11-08 23:12:52 +01:00
// Sort to split position of various substring
2021-07-29 22:53:35 +02:00
sectionData.sort(function (a, b) { return a.index - b.index; });
2020-11-08 23:12:52 +01:00
// Then loop again and process each item type
2021-07-29 22:53:35 +02:00
for (let i = 0; i < sectionData.length; i++) {
2020-11-08 00:22:04 +01:00
let def = sectionData[i];
2021-07-29 22:53:35 +02:00
if (def.index > -1) {
let maxIndex = statString.length
2023-09-10 22:22:47 +02:00
if (sectionData[i + 1] && sectionData[i + 1].index > -1) {
maxIndex = sectionData[i + 1].index
2023-09-10 22:22:47 +02:00
}
def.substring = statString.substring(def.index, maxIndex)
def.substring = XRegExp.replace(def.substring, def.regDef, "")
def.substring = XRegExp.replace(def.substring, regLine1, " ")
2020-11-08 23:12:52 +01:00
// At this point, def.substring contains the items list as a string
2021-07-29 22:53:35 +02:00
2020-11-08 23:12:52 +01:00
// Then create a table of it in termList, with specific sub-parsing rules
2020-11-08 00:22:04 +01:00
let termList = XRegExp.split(def.substring, regSep);
2023-09-10 22:22:47 +02:00
//console.log("Term list identified", termList)
2023-09-08 08:01:54 +02:00
let lastSkillName
2020-11-08 00:22:04 +01:00
for (let name of termList) {
2021-07-29 22:53:35 +02:00
let itemFound, subres, value;
if (def.secondParse) {
subres = XRegExp.exec(name, XRegExp(def.secondParse, 'gi'))
2023-09-08 08:01:54 +02:00
//console.log("Second pars", def, name, subres)
2023-09-10 22:22:47 +02:00
name = subres.name.trim().replace("\n", "").replace("\r", "")
2023-09-08 08:01:54 +02:00
if (subres.value) {
value = XRegExp.replace(subres.value, "(", "")
2023-09-10 22:22:47 +02:00
value = XRegExp.replace(value, ")", "")
2023-09-08 08:01:54 +02:00
} else {
value = 0
}
2021-11-02 21:03:04 +01:00
}
name = __patchName(name)
2021-07-29 22:53:35 +02:00
if (def.name == 'trait') {
try {
itemFound = await __findItem(name, "trait")
2020-11-08 23:12:52 +01:00
}
2021-07-29 22:53:35 +02:00
catch { }
if (itemFound && value && value.length > 0) {
2023-09-10 22:22:47 +02:00
let number = value.match(/\d+/g);
if (number && (name.toLowerCase() == 'ranged' || name.toLowerCase() == 'weapon' || name.toLowerCase() == "bite" || name.toLowerCase() == "tail" ||
name.toLowerCase() == 'arme' || name.toLowerCase() == "morsure" || name.toLowerCase() == "queue")) {
2023-09-08 08:01:54 +02:00
//console.log(itemFound)
2023-09-10 22:22:47 +02:00
number = number[0] // Take first number ....
itemFound.system.specification.value = Number(number) - ((name.toLowerCase() == 'ranged') ? 0 : Math.floor(Number(model.characteristics.s.initial) / 10))
2021-11-02 21:03:04 +01:00
} else {
2022-09-30 20:56:12 +02:00
itemFound.system.specification.value = game.i18n.localize(value)
2021-11-02 21:03:04 +01:00
}
2023-10-12 20:10:13 +02:00
//itemFound.name += "(" + value + ")"
2020-11-08 23:12:52 +01:00
}
if (!itemFound)
2021-07-29 22:53:35 +02:00
ui.notifications.error("Trait non trouvé, à ajouter manuellement : " + name, { permanent: true })
2023-09-10 22:22:47 +02:00
2021-07-29 22:53:35 +02:00
} else if (def.name == 'skill') {
2020-11-08 23:12:52 +01:00
try {
2021-07-29 22:53:35 +02:00
itemFound = await __findSkill(name, value);
2020-11-08 23:12:52 +01:00
}
2021-07-29 22:53:35 +02:00
catch { }
2023-09-10 22:22:47 +02:00
let newName = name
2023-09-08 08:01:54 +02:00
if (!itemFound && lastSkillName) {
2023-09-10 22:22:47 +02:00
newName = lastSkillName + " (" + name + ")"
itemFound = await __findSkill(newName, value)
2023-09-08 08:01:54 +02:00
}
2021-07-29 22:53:35 +02:00
if (itemFound && subres && value) {
2022-09-30 20:56:12 +02:00
itemFound.system.advances.value = Number(value) - Number(model.characteristics[itemFound.system.characteristic.value].initial);
2020-11-08 13:53:15 +01:00
}
2023-09-08 08:01:54 +02:00
lastSkillName = undefined
2023-09-10 22:22:47 +02:00
for (let keySkillGroup of skillGrouping) {
if (newName.includes(keySkillGroup)) { // useful to handle skills grouping
lastSkillName = keySkillGroup
}
}
if (!itemFound) {
2020-11-08 23:12:52 +01:00
ui.notifications.error("Compétence non trouvée, à ajouter manuellement : " + name, { permanent: true })
2023-09-10 22:22:47 +02:00
}
2020-11-08 23:12:52 +01:00
} else if (def.name == 'talent') {
try {
itemFound = await __findTalent(name);
}
2021-07-29 22:53:35 +02:00
catch { }
if (itemFound && subres && value)
2022-09-30 20:56:12 +02:00
itemFound.system.advances.value = Number(value);
2023-09-10 22:22:47 +02:00
if (!itemFound) {
2020-11-08 23:12:52 +01:00
ui.notifications.error("Talent non trouvé, à ajouter manuellement : " + name, { permanent: true })
2023-09-10 22:22:47 +02:00
}
2023-10-12 08:31:55 +02:00
2020-11-08 23:12:52 +01:00
} else if (def.name == 'trapping') {
2023-09-10 22:22:47 +02:00
itemFound = await __findItem(name, "trapping");
if (!itemFound) {
itemFound = await __findItem(name, "weapon");
}
if (!itemFound) {
itemFound = await __findItem(name, "armor");
2020-11-08 23:12:52 +01:00
}
2021-11-02 21:03:04 +01:00
if (!itemFound && name) {
2023-09-10 22:22:47 +02:00
if (name.toLowerCase().includes("armor") || name.toLowerCase().includes("armure") || name.toLowerCase().includes("armour")) {
2023-10-12 08:31:55 +02:00
itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "armour", system: game.system.model.Item.armor })
2023-09-10 22:22:47 +02:00
} else if (name.toLowerCase().includes("weapon") || name.toLowerCase().includes("arme")) {
itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "weapon", system: game.system.model.Item.weapon })
} else {
itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name, type: "trapping", system: game.system.model.Item.trapping })
itemFound.system.trappingType.value = "misc"
if (name.toLowerCase().includes("cloth")) {
itemFound.system.trappingType.value = "clothingAccessories"
}
2023-09-08 08:01:54 +02:00
}
2023-09-10 22:22:47 +02:00
itemFound = itemFound.toObject()
}
2023-10-12 08:31:55 +02:00
// Searching money stuff
for (let mondeyDef of moneyLang) {
if (name.toLowerCase().includes(mondeyDef.name)) {
2023-10-12 20:10:13 +02:00
let regMoney = XRegExp("(\\d+)\\s*" + mondeyDef.name.trim(), 'gi')
2023-10-12 08:31:55 +02:00
let moneyParsed = XRegExp.exec(name.toLowerCase(), regMoney)
console.log("Parsing money", name, moneyParsed)
moneys[mondeyDef.key] += (moneyParsed && moneyParsed[1]) ? Number(moneyParsed[1]) : 0
}
}
2023-09-10 22:22:47 +02:00
} else if (def.name.includes('spell')) {
console.log("Found spells section!!!!", name, def, def.lore || "NO LORE")
// Lore management, firs pass
2023-10-12 08:31:55 +02:00
if (def.lore) {
2023-09-10 22:22:47 +02:00
let newName = name + " (" + def.lore + ")"
itemFound = await __findItem(newName, "spell");
2023-10-12 08:31:55 +02:00
//console.log("Trying to find ", newName, itemFound)
2023-09-10 22:22:47 +02:00
}
if (!itemFound) { // If not found with Lore, try without the lore
itemFound = await __findItem(name, "spell");
}
if (!itemFound && name) { // Auto-create the spell name
itemFound = new ItemWfrp4e({ img: "systems/wfrp4e/icons/blank.png", name: name + "(To be checked/completed)", type: "spell", system: game.system.model.Item.spell })
itemFound = itemFound.toObject();
}
2021-11-02 21:03:04 +01:00
} else if (def.name == 'mutation') {
try {
itemFound = await __findItem(name, "mutation");
2021-07-29 22:53:35 +02:00
}
2021-11-02 21:03:04 +01:00
catch { }
2020-11-08 00:22:04 +01:00
}
2020-11-08 23:12:52 +01:00
if (itemFound)
2021-07-29 22:53:35 +02:00
globalItemList.push(itemFound);
2020-11-08 00:22:04 +01:00
}
}
}
2020-11-08 23:12:52 +01:00
let moneyItems = await game.wfrp4e.utility.allMoneyItems() || [];
2022-09-30 20:56:12 +02:00
moneyItems = moneyItems.sort((a, b) => (a.system.coinValue.value > b.system.coinValue.value) ? -1 : 1);
2023-10-12 08:31:55 +02:00
for (let m of moneyItems) {
m.system.quantity.value = 0
if (m.system.coinValue.value == 1) {
m.system.quantity.value += moneys.bp
}
if (m.system.coinValue.value == 240) {
m.system.quantity.value += moneys.gc
}
if (m.system.coinValue.value == 12) {
m.system.quantity.value += moneys.ss
}
}
2020-11-08 23:12:52 +01:00
globalItemList = globalItemList.concat(moneyItems);
2023-10-12 08:31:55 +02:00
//DEBUG : console.log("My liste :", moneyItems, moneys);
2020-11-08 23:12:52 +01:00
let name = pnjName;
2021-07-29 22:53:35 +02:00
2022-09-30 20:56:12 +02:00
let effects = globalItemList.reduce((total, globItem) => total.concat(globItem.effects), [])
2021-07-29 22:53:35 +02:00
effects = effects.filter(e => !!e)
effects = effects.filter(e => e.transfer)
2023-10-12 08:31:55 +02:00
for (let e of effects) {
for (let c of e.changes) {
// Charac management stuff
if (c.key?.includes("characteristics")) {
let cKey = c.key.split(".")[2]
model.characteristics[cKey].initial -= Number(c.value)
}
// Move management
if (c.key?.includes("move")) {
model.details.move.value -= Number(c.value)
}
}
}
//DEBUG : console.log("EFFECTS", effects)
2021-07-29 22:53:35 +02:00
return { name, type, data: model, items: globalItemList, effects }
2020-11-07 13:46:15 +01:00
}
2020-11-08 23:12:52 +01:00
// If the carac string has not been found
2021-07-29 22:53:35 +02:00
ui.notifications.error("Impossible de convertir ces statitiques, les caractéristiques n'ont pas été trouvées", { permanent: true })
2020-11-08 23:12:52 +01:00
}