foundryvtt-reve-de-dragon/module/apps/rdd-import-stats.js

457 lines
11 KiB
JavaScript
Raw Normal View History

2024-10-30 22:45:47 +01:00
/************************************************************************************/
import "./xregexp-all.js";
import { SystemCompendiums } from "../settings/system-compendiums.js";
import { RdDBaseActorReve } from "../actor/base-actor-reve.js";
import { Grammar } from "../grammar.js";
import { Misc } from "../misc.js";
2024-10-30 22:45:47 +01:00
/************************************************************************************/
// Some internal test strings
let statBlock01 = `+$16(/, baron de Sylvedire, né à lheure du
Roseau, 40 ans, 1m78, 65 kg, Beauté 13.
TAILLE
10
Mêlée
14
APPARENCE
13
Tir
11
CONSTITUTION
12
Lancer
11
FORCE
12
Dérobée
13
AGILITÉ
16
Vie
11
DEXTÉRITÉ
13
Endurance
25
VUE
10
+dom
0
OUÏE
11
Protection
2 ou 4
ODO-GOÛT
9
cuir souple
VOLONTÉ
14
ou cuir / métal
INTELLECT
9
EMPATHIE
11
RÊVE
13
CHANCE
10
niv
init
+dom
Épée dragonne
+5
12
+3
Hache de bataille
+6
13
+3
Bouclier moyen
+5
Dague mêlée
+4
11
+1
Corps à corps
+4
11
(0)
Esquive
+8
Escalade, Saut +4 / Commerce +3 / Équitation
2024-10-30 22:45:47 +01:00
+6 / Chirurgie 0 / Survie en extérieur +4 / Survie fo-
rêt +6 / Acrobatie -2 / Métallurgie +2 / Natation +3 /
Légendes -1 / Écriture -4
`;
let statBlock02 = `/HVJDUGHV
TAILLE
11
Mêlée
12
CONSTITUTION
11
Tir
11
FORCE
12
Lancer
11
AGILITÉ
12
Dérobée
11
DEXTERITÉ
11
Vie
11
VUE
11
Endurance
22
OUÏE
11
Vitesse
12
VOLONTÉ
10
+dom
0
Protection
4
cuir / métal
niv
init
+dom
Hache de bataille
+4
10
+3
Bouclier moyen
+4
Dague mêlée
+3
9
+1
Arc
+5
10
+2
Corps à corps
+3
9
(0)
Esquive avec armure
+2
Course +1/ Vigilance +4
`;
let statBlock03 = `rencontres sont laissées à /HVFKLHQVORXSVGXEDURQ
chaque gardien des rêves.
TAILLE
8
Vie
10
CONSTITUTION FORCE
12
11
Endurance
Vitesse
12/38
21
/HVFKLHQV]RPELV
PERCEPTION 13
+dom
0
VOLONTÉ
10
Protection
0
Les « monstres » apparaîtront un soir, durant
RÊVE
10
lheure du Serpent, et attaqueront les voya-
niv
init
+dom
geurs à leur campement. Si ces derniers ne
Morsure
13
+4
10
+1
campent pas, ils apparaîtront tout de même à
Esquive
11
+3
lheure du Serpent. Le feu ne les effraie pas. Ils
Course, Saut
12
+3
ne sont pas très rapides, mais en revanche, très
Discrétion
12
+3
silencieux : ils naboient pas. Les voyageurs
Vigilance
13
+3
`
// Skill parser depending on the type of actor
const compParser = {
personnage: "(\\D+)*\\s+(?<value>[\\+\\-]?\\d+)",
creature: "\\s+(?<carac>\\d+)\\s+(?<value>[\\+\\-]?\\d+)\\s?(?<init>\\d+)?\\s+?(?<dommages>\\+\\d+)?"
};
2024-10-30 22:45:47 +01:00
// Main class for parsing a stat block
export class RdDStatBlockParser {
2024-10-30 22:45:47 +01:00
static openInputDialog() {
let dialog = new Dialog({
title: "Import de stats de PNJ/Créatures",
content: `
<div>
<p>Coller le texte de la stat ici</p>
<textarea id="statBlock" style="width: 100%; height: 200px;"></textarea>
</div>
`,
buttons: {
ok: {
label: "OK",
callback: async (html) => {
let statBlock = html.find("#statBlock")[0].value;
await RdDStatBlockParser.parseStatBlock(statBlock);
dialog.close();
}
},
cancel: {
label: "Cancel"
}
}
});
dialog.render(true);
}
2024-10-31 23:36:16 +01:00
static fixWeirdPDF(statString) {
// Split the statString into lines
let lines = statString.split("\n");
let newLines = [];
let index = 0;
let nextType = "string";
// Loop through each line
for (let i = 0; i < lines.length; i++) {
// remove trailing spaces
lines[i] = lines[i].trim();
// Is it text ?
if (lines[i].match(/^[a-zA-Zéêè\s]+/)) {
if (nextType == "string") {
2024-10-31 23:36:16 +01:00
newLines[index] = lines[i];
nextType = "number";
2024-10-31 23:36:16 +01:00
} else {
console.log("Wrong sequence string detected...", lines[i], nextType);
}
}
// Is it a number ?
if (lines[i].match(/^[\d\s]+/)) {
if (nextType == "number") {
2024-10-31 23:36:16 +01:00
newLines[index] = newLines[index] + lines[i];
nextType = "string";
index++;
} else {
console.log("Wrong sequence number detected...", lines[i], nextType);
}
}
}
2024-10-30 22:45:47 +01:00
}
2024-10-30 22:45:47 +01:00
static async parseStatBlock(statString, type = "npc") {
//statString = statBlock03;
if (!statString) {
return;
}
2024-10-31 23:36:16 +01:00
// Special function to fix strange/weird copy/paste from PDF readers
// Unused up to now : this.fixWeirdPDF(statString);
2024-10-30 22:45:47 +01:00
// Replace all endline by space in the statString
statString = statString.replace(/\n/g, " ");
// Remove all multiple spaces
statString = statString.replace(/\s{2,}/g, " ");
// Remove all leading and trailing spaces
statString = statString.trim();
let actorType = "personnage";
// TODO: check for entite
let perception = XRegExp.exec(statString, XRegExp("perception\\s+(?<value>\\d+)", 'giu'))
if (perception?.value) {
2024-10-30 22:45:47 +01:00
actorType = "creature";
}
// Now start carac
let actorData = foundry.utils.deepClone(game.model.Actor[actorType]);
2024-10-30 22:45:47 +01:00
for (let key in game.model.Actor.personnage.carac) {
let caracDef = game.model.Actor.personnage.carac[key];
// Parse the stat string for each caracteristic
let carac = XRegExp.exec(statString, XRegExp(caracDef.label + "\\s+(?<value>\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (carac?.value) {
actorData.carac[key].value = Number(carac.value);
}
}
// If creature we need to setup additionnal fields
if (actorType == "creature") {
let plusDom = XRegExp.exec(statString, XRegExp("\\+dom\\s+(?<value>\\+\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (plusDom?.values) {
actorData.attributs.plusdom.value = Number(plusDom.value);
}
let protection = XRegExp.exec(statString, XRegExp("protection\\s+(?<value>\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (protection?.value) {
actorData.attributs.protection.value = Number(protection.value);
}
let endurance = XRegExp.exec(statString, XRegExp("endurance\\s+(?<value>\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (endurance?.value) {
actorData.sante.endurance.value = Number(endurance.value);
actorData.sante.endurance.max = Number(endurance.value);
}
let vie = XRegExp.exec(statString, XRegExp("vie\\s+(?<value>\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (vie.value) {
actorData.sante.vie.value = Number(vie.value);
actorData.sante.vie.max = Number(vie.value);
}
let vitesse = XRegExp.exec(statString, XRegExp("vitesse\\s+(?<value>[\\d\\/]+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (vitesse?.value) {
actorData.attributs.vitesse.value = vitesse.value;
}
}
let items = [];
// Get skills from compendium
const competences = await SystemCompendiums.getCompetences(actorType);
//console.log("Competences : ", competences);
let allComp = competences.map(i => i.toObject())
for (let comp of allComp) {
const compName = comp.name;
let skill = XRegExp.exec(Grammar.toLowerCaseNoAccent(statString), XRegExp(Grammar.toLowerCaseNoAccent(compName) + compParser[actorType], 'giu'));
2024-10-30 22:45:47 +01:00
if (skill) {
comp.system.niveau = Number(skill.value);
if (actorType == "creature") {
comp.system.carac_value = Number(skill.carac);
if (skill.init) {
comp.system.dommages = Number(skill.dommages);
comp.system.iscombat = true;
}
2024-10-31 23:36:16 +01:00
}
2024-10-30 22:45:47 +01:00
}
if (actorType == "creature" && skill.init) {
items.push(comp); // Only selective push
}
2024-10-30 22:45:47 +01:00
if (actorType == "personnage") {
items.push(comp); // Always push
}
}
// Now process weapons
const weapons = await SystemCompendiums.getWorldOrCompendiumItems("arme", "equipement")
//console.log("Equipement : ", equipment);
// TODO: les noms d'armes peuvent avoir un suffixe (à une main, lancée) qui détermine la compétence correspondante
// TODO: une arme peut être spécifique ("fourche"), ajouter une compétence dans ces cas là?
2024-10-30 22:45:47 +01:00
for (let w of weapons) {
let weapon = XRegExp.exec(statString, XRegExp(w.name + "\\s+(?<value>\\+\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (weapon) {
w.system.equipe = 'true'
2024-10-30 22:45:47 +01:00
items.push(w.toObject());
// now process the skill
if (w.system?.competence != "") {
let wComp = items.find(i => Grammar.equalsInsensitive(i.name, w.system.competence))
2024-10-30 22:45:47 +01:00
if (wComp) {
wComp.system.niveau = Number(weapon.value);
}
}
if (w.system?.tir != "") {
let wComp = items.find(i => Grammar.equalsInsensitive(i.name, w.system.tir))
2024-10-30 22:45:47 +01:00
if (wComp) {
wComp.system.niveau = Number(weapon.value);
}
}
if (w.system?.lancer != "") {
let wComp = items.find(i => Grammar.equalsInsensitive(i.name, w.system.lancer))
2024-10-30 22:45:47 +01:00
if (wComp) {
wComp.system.niveau = Number(weapon.value);
}
}
}
}
// Now process armors
const armors = await SystemCompendiums.getWorldOrCompendiumItems("armure", "equipement")
for (let a of armors) {
let armor = XRegExp.exec(statString, XRegExp(a.name, 'giu'));
2024-10-30 22:45:47 +01:00
if (armor) {
a.system.equipe = true
items.push(a.toObject());
}
}
let feminin = XRegExp.exec(statString, XRegExp("né(?<value>e?) à", 'giu'));
actorData.sexe = (feminin?.value == 'e') ? 'féminin' : 'masculin'
2024-10-30 22:45:47 +01:00
// Get hour name : heure du XXXXX
let heure = XRegExp.exec(statString, XRegExp("heure (du|de la|des|de l\')\\s*(?<value>[\\p{Letter}\s]+),", 'giu'));
2024-10-30 22:45:47 +01:00
if (heure?.value) {
actorData.heure = heure.value;
2024-10-30 22:45:47 +01:00
}
// Get age
let age = XRegExp.exec(statString, XRegExp("(?<value>\\d+) ans", 'giu'));
2024-10-30 22:45:47 +01:00
if (age?.value) {
actorData.age = Number(age.value);
2024-10-30 22:45:47 +01:00
}
// Get height
let taille = XRegExp.exec(statString, XRegExp("(?<value>\\d+m\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (taille?.value) {
actorData.taille = taille.value;
2024-10-30 22:45:47 +01:00
}
// Get weight
let poids = XRegExp.exec(statString, XRegExp("(?<value>\\d+) kg", 'giu'));
2024-10-30 22:45:47 +01:00
if (poids?.value) {
actorData.poids = poids.value;
2024-10-30 22:45:47 +01:00
}
// Get beauty
let beaute = XRegExp.exec(statString, XRegExp("beauté\\s+(?<value>\\d+)", 'giu'));
2024-10-30 22:45:47 +01:00
if (beaute?.value) {
actorData.beaute = Number(beaute.value);
2024-10-30 22:45:47 +01:00
}
// Name is all string before ', né'
let name = RdDStatBlockParser.extractName(actorType, statString);
let newActor = RdDBaseActorReve.create({ name: name || "Importé", type: actorType, system: actorData, items: items });
2024-10-30 22:45:47 +01:00
// DUmp....
console.log(actorData);
}
static extractName(actorType, statString) {
switch (actorType) {
case "personnage": return RdDStatBlockParser.extractNamePersonnage(statString);
case "creature": return RdDStatBlockParser.extractNameCreature(statString);
}
return RdDStatBlockParser.extractNameCreature(statString);
}
static extractNamePersonnage(statString) {
let name = XRegExp.exec(statString, XRegExp("(?<value>[\\p{Letter}\\s\\d]+),", 'giu'));
if (!name?.value) {
name = XRegExp.exec(statString, XRegExp("(?<value>.+)\\s+taille", 'giu'));
}
return Misc.upperFirst(name?.value || "Importé");
}
static extractNameCreature(statString) {
const name = XRegExp.exec(statString, XRegExp("(?<value>.+)\\s+taille", 'giu'));
return Misc.upperFirst(name?.value || "Importé");
}
2024-10-30 22:45:47 +01:00
}