fvtt-mournblade/modules/mournblade-actor.js
2022-06-04 21:06:01 +02:00

229 lines
7.2 KiB
JavaScript

/* -------------------------------------------- */
import { MournbladeUtility } from "./mournblade-utility.js";
import { MournbladeRollDialog } from "./mournblade-roll-dialog.js";
/* -------------------------------------------- */
/* -------------------------------------------- */
/**
* Extend the base Actor entity by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
export class MournbladeActor extends Actor {
/* -------------------------------------------- */
/**
* Override the create() function to provide additional SoS functionality.
*
* This overrided create() function adds initial items
* Namely: Basic skills, money,
*
* @param {Object} data Barebones actor data which this function adds onto.
* @param {Object} options (Unused) Additional options which customize the creation workflow.
*
*/
static async create(data, options) {
// Case of compendium global import
if (data instanceof Array) {
return super.create(data, options);
}
// If the created actor has items (only applicable to duplicated actors) bypass the new actor creation logic
if (data.items) {
let actor = super.create(data, options);
return actor;
}
if (data.type == 'personnage') {
const skills = await MournbladeUtility.loadCompendium("fvtt-mournblade.skills")
data.items = skills.map(i => i.toObject());
}
if (data.type == 'pnj') {
}
return super.create(data, options);
}
/* -------------------------------------------- */
getWeapons() {
return this.data.items.filter(item => item.type == "arme" )
}
/* -------------------------------------------- */
getArmors() {
return this.data.items.filter(item => item.type == "protection" )
}
/* -------------------------------------------- */
getSkills() {
let comp = []
for( let item of this.data.items) {
item = duplicate(item)
if ( item.type == "competence") {
item.data.attribut1total = item.data.niveau + (this.data.data.attributs[item.data.attribut1]?.value || 0)
item.data.attribut2total = item.data.niveau + (this.data.data.attributs[item.data.attribut2]?.value || 0)
item.data.attribut3total = item.data.niveau + (this.data.data.attributs[item.data.attribut3]?.value || 0)
if (item.data.niveau == 0) {
item.data.attribut1total -= 3
item.data.attribut2total -= 3
item.data.attribut3total -= 3
}
item.data.attribut1label = this.data.data.attributs[item.data.attribut1]?.label || ""
item.data.attribut2label = this.data.data.attributs[item.data.attribut2]?.label || ""
item.data.attribut3label = this.data.data.attributs[item.data.attribut3]?.label || ""
comp.push( item )
}
}
return comp
}
/* -------------------------------------------- */
prepareBaseData() {
}
/* -------------------------------------------- */
async prepareData() {
super.prepareData();
}
/* -------------------------------------------- */
prepareDerivedData() {
if (this.type == 'character') {
}
super.prepareDerivedData();
}
/* -------------------------------------------- */
_preUpdate(changed, options, user) {
super._preUpdate(changed, options, user);
}
/* -------------------------------------------- */
getItemById(id) {
let item = this.data.items.find(item => item.id == id);
if (item) {
item = duplicate(item)
}
return item;
}
/* -------------------------------------------- */
async equipItem(itemId) {
let item = this.data.items.find(item => item.id == itemId);
if (item && item.data.data) {
let update = { _id: item.id, "data.equipped": !item.data.data.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
editItemField(itemId, itemType, itemField, dataType, value) {
let item= this.data.items.find(item => item.id == itemId)
if (item) {
console.log("Item ", item, itemField, dataType, value)
if (dataType.toLowerCase() == "number") {
value = Number(value)
} else {
value = String(value)
}
let update = { _id: item.id, [`data.${itemField}`]: value };
this.updateEmbeddedDocuments( "Item", [update])
}
}
/* -------------------------------------------- */
compareName(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
}
/* -------------------------------------------- */
getAttribute(attrKey) {
return this.data.data.attributes[attrKey]
}
/* -------------------------------------------- */
async equipGear(equipmentId) {
let item = this.data.items.find(item => item.id == equipmentId);
if (item && item.data.data) {
let update = { _id: item.id, "data.equipped": !item.data.data.equipped };
await this.updateEmbeddedDocuments('Item', [update]); // Updates one EmbeddedEntity
}
}
/* -------------------------------------------- */
getSubActors() {
let subActors = [];
for (let id of this.data.data.subactors) {
subActors.push(duplicate(game.actors.get(id)));
}
return subActors;
}
/* -------------------------------------------- */
async addSubActor(subActorId) {
let subActors = duplicate(this.data.data.subactors);
subActors.push(subActorId);
await this.update({ 'data.subactors': subActors });
}
/* -------------------------------------------- */
async delSubActor(subActorId) {
let newArray = [];
for (let id of this.data.data.subactors) {
if (id != subActorId) {
newArray.push(id);
}
}
await this.update({ 'data.subactors': newArray });
}
/* -------------------------------------------- */
async incDecQuantity(objetId, incDec = 0) {
let objetQ = this.data.items.get(objetId)
if (objetQ) {
let newQ = objetQ.data.data.quantity + incDec;
const updated = await this.updateEmbeddedDocuments('Item', [{ _id: objetQ.id, 'data.quantity': newQ }]); // pdates one EmbeddedEntity
}
}
/* -------------------------------------------- */
getCommonRollData(attrKey = undefined, compId = undefined) {
let rollData = MournbladeUtility.getBasicRollData()
rollData.alias = this.name
rollData.actorImg = this.img
rollData.actorId = this.id
rollData.img = this.img
if (attrKey) {
rollData.attrKey = attrKey
rollData.attr = duplicate(this.data.data.attributs[attrKey])
}
if (compId) {
rollData.competence = duplicate(this.data.items.get(compId) || {})
}
return rollData
}
/* -------------------------------------------- */
async rollAttribut(attrKey) {
let rollData = this.getCommonRollData(attrKey)
console.log("RollDatra", rollData)
let rollDialog = await MournbladeRollDialog.create(this, rollData)
rollDialog.render(true)
}
/* -------------------------------------------- */
async rollCompetence(attrKey, compId) {
let rollData = this.getCommonRollData(attrKey, compId)
console.log("RollDatra", rollData)
let rollDialog = await MournbladeRollDialog.create(this, rollData)
rollDialog.render(true)
}
}