Simplification des actionPrincipales

Préparation pour supporter une liste d'actions sur les items
This commit is contained in:
Vincent Vandemeulebrouck 2025-01-15 19:12:39 +01:00
parent 8ebd6ec771
commit 4281f76dfb
3 changed files with 113 additions and 89 deletions

View File

@ -1200,22 +1200,27 @@ export class RdDActor extends RdDBaseActorSang {
async actionPrincipale(item, onActionItem = async () => { }) {
let result = await super.actionPrincipale(item, onActionItem)
if (result) { return result }
result = await this.actionNourritureboisson(item, onActionItem)
if (result) { return result }
if (!result){
result = await this.actionNourritureboisson(item)
}
if (!result){
result = await this.itemActionPrincipale(item)
}
await onActionItem()
return result
}
async itemActionPrincipale(item) {
switch (item.type) {
case ITEM_TYPES.potion: return await this.consommerPotion(item, onActionItem);
case ITEM_TYPES.potion: return await this.consommerPotion(item);
case ITEM_TYPES.livre: return await this.actionLire(item);
case ITEM_TYPES.conteneur: return await item.sheet.render(true);
case ITEM_TYPES.herbe: return await this.actionHerbe(item, onActionItem);
case ITEM_TYPES.herbe: return await this.actionHerbe(item);
case ITEM_TYPES.queue: case ITEM_TYPES.ombre: return await this.actionRefoulement(item);
}
return undefined
}
async actionNourritureboisson(item, onActionItem) {
async actionNourritureboisson(item) {
switch (item.getUtilisationCuisine()) {
case 'brut': {
const utilisation = new Dialog({
@ -1223,19 +1228,19 @@ export class RdDActor extends RdDBaseActorSang {
content: `Que faire de votre ${item.name}`,
buttons: {
'cuisiner': { icon: '<i class="fa-solid fa-utensils"></i>', label: 'Cuisiner', callback: async () => await this.preparerNourriture(item) },
'manger': { icon: '<i class="fa-solid fa-drumstick-bite"></i>', label: 'Manger cru', callback: async () => await this.mangerNourriture(item, onActionItem) }
'manger': { icon: '<i class="fa-solid fa-drumstick-bite"></i>', label: 'Manger cru', callback: async () => await this.mangerNourriture(item) }
}
});
return utilisation.render(true);
}
case 'pret':
return await this.mangerNourriture(item, onActionItem);
return await this.mangerNourriture(item);
}
return undefined;
}
async mangerNourriture(item, onActionItem) {
return (await DialogConsommer.create(this, item, onActionItem)).render(true);
async mangerNourriture(item) {
return (await DialogConsommer.create(this, item)).render(true);
}
async actionLire(item) {
@ -2829,6 +2834,59 @@ export class RdDActor extends RdDBaseActorSang {
return guerisonData;
}
/* -------------------------------------------- */
async fabriquerPotion(herbeData) {
let newPotion = {
name: `Potion de ${herbeData.system.categorie} (${herbeData.name})`, type: 'potion',
img: "systems/foundryvtt-reve-de-dragon/icons/objets/fiole_verre.webp",
system: {
quantite: 1, cout: 0, encombrement: 0.1,
categorie: herbeData.system.categorie,
herbe: herbeData.name,
rarete: herbeData.system.rarete,
herbebrins: herbeData.nbBrins,
herbebonus: herbeData.herbebonus,
description: ""
}
}
await this.createEmbeddedDocuments('Item', [newPotion]);
let newQuantite = herbeData.system.quantite - herbeData.nbBrins;
let messageData = {
alias: this.getAlias(),
nbBrinsReste: newQuantite,
potion: newPotion,
herbe: herbeData
}
this.diminuerQuantiteObjet(herbeData._id, herbeData.nbBrins);
ChatMessage.create({
whisper: ChatUtility.getOwners(this),
content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-fabriquer-potion-base.html`, messageData)
});
}
/* -------------------------------------------- */
async diminuerQuantiteObjet(id, nb, options = { supprimerSiZero: false }) {
const item = this.getItem(id);
if (item) {
await item.diminuerQuantite(nb, options);
}
}
/* -------------------------------------------- */
async consommerPotion(potion) {
if (potion.system.categorie.includes('Soin')) {
await this.consommerPotionSoin(potion);
} else if (potion.system.categorie.includes('Repos')) {
await this.consommerPotionRepos(potion);
} else {
await this.consommerPotionGenerique(potion);
}
await this.diminuerQuantiteObjet(potion.id, 1, { supprimerSiZero: potion.supprimer });
}
/* -------------------------------------------- */
async consommerPotionSoin(potionData) {
potionData.alias = this.name;
@ -2890,77 +2948,22 @@ export class RdDActor extends RdDBaseActorSang {
}
/* -------------------------------------------- */
async fabriquerPotion(herbeData) {
let newPotion = {
name: `Potion de ${herbeData.system.categorie} (${herbeData.name})`, type: 'potion',
img: "systems/foundryvtt-reve-de-dragon/icons/objets/fiole_verre.webp",
system: {
quantite: 1, cout: 0, encombrement: 0.1,
categorie: herbeData.system.categorie,
herbe: herbeData.name,
rarete: herbeData.system.rarete,
herbebrins: herbeData.nbBrins,
herbebonus: herbeData.herbebonus,
description: ""
}
}
await this.createEmbeddedDocuments('Item', [newPotion], { renderSheet: true });
async consommerPotionGenerique(potion) {
potion.alias = this.name;
let newQuantite = herbeData.system.quantite - herbeData.nbBrins;
let messageData = {
alias: this.getAlias(),
nbBrinsReste: newQuantite,
potion: newPotion,
herbe: herbeData
}
this.diminuerQuantiteObjet(herbeData._id, herbeData.nbBrins);
ChatMessage.create({
whisper: ChatUtility.getOwners(this),
content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-fabriquer-potion-base.html`, messageData)
});
}
/* -------------------------------------------- */
async diminuerQuantiteObjet(id, nb, options = { supprimerSiZero: false }) {
const item = this.getItem(id);
if (item) {
await item.diminuerQuantite(nb, options);
}
}
/* -------------------------------------------- */
async consommerPotionGenerique(potionData) {
potionData.alias = this.name;
if (potionData.system.magique) {
if (potion.system.magique) {
// Gestion de la résistance:
potionData.rolled = await RdDResolutionTable.roll(this.getReveActuel(), -8);
if (potionData.rolled.isEchec) {
potion.rolled = await RdDResolutionTable.roll(this.getReveActuel(), -8);
if (potion.rolled.isEchec) {
await this.reveActuelIncDec(-1);
}
}
ChatMessage.create({
whisper: ChatUtility.getOwners(this),
content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-consommer-potion-generique.html`, potionData)
content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-consommer-potion-generique.html`, potion)
});
}
/* -------------------------------------------- */
async consommerPotion(potion, onActionItem = async () => { }) {
const potionData = potion
if (potionData.system.categorie.includes('Soin')) {
this.consommerPotionSoin(potionData);
} else if (potionData.system.categorie.includes('Repos')) {
this.consommerPotionRepos(potionData);
} else {
this.consommerPotionGenerique(potionData);
}
await this.diminuerQuantiteObjet(potion.id, 1, { supprimerSiZero: potionData.supprimer });
await onActionItem()
}
/* -------------------------------------------- */
async onUpdateActor(update, options, actorId) {
const updatedEndurance = update?.system?.sante?.endurance

View File

@ -192,7 +192,6 @@ export class RdDItemSheet extends ItemSheet {
})
this.html.find('.creer-tache-livre').click((event) => this._getEventActor(event).creerTacheDepuisLivre(this.item));
this.html.find('.consommer-potion').click((event) => this._getEventActor(event).consommerPotion(this.item, this.getActionRenderItem()));
this.html.find('.creer-potion-base').click((event) => this._getEventActor(event).actionHerbe(this.item));
this.html.find('input[name="system.cacher_points_de_tache"]').change(async event => await this.item.update({ 'system.cacher_points_de_tache': event.currentTarget.checked }));
@ -200,12 +199,13 @@ export class RdDItemSheet extends ItemSheet {
this.html.find('.chat-roll-text').click(async event => await RdDTextEditor.chatRollText(event))
if (this.actor) {
this.html.find('.item-split').click(async event => RdDSheetUtility.splitItem(RdDSheetUtility.getItem(event, this.actor), this.actor, this.getActionRenderItem()));
this.html.find('.consommer-potion').click(event => this.itemActionConsommer(event))
this.html.find('.item-action').click(event => this.itemAction(event))
this.html.find('.item-split').click( event => this.itemActionSplit(event))
this.html.find('.item-edit').click(async event => RdDSheetUtility.getItem(event, this.actor)?.sheet.render(true));
this.html.find('.item-delete').click(async event => RdDUtility.confirmActorItemDelete(this, RdDSheetUtility.getItem(event, this.actor)));
this.html.find('.item-vendre').click(async event => RdDSheetUtility.getItem(event, this.actor)?.proposerVente());
this.html.find('.item-montrer').click(async event => RdDSheetUtility.getItem(event, this.actor)?.postItemToChat());
this.html.find('.item-action').click(async event => RdDSheetUtility.getItem(event, this.actor)?.actionPrincipale(this.actor, this.getActionRenderItem()));
this.html.find('.item-quantite-plus').click(async event => {
await this.actor.itemQuantiteIncDec(RdDSheetUtility.getItemId(event), 1)
@ -223,14 +223,28 @@ export class RdDItemSheet extends ItemSheet {
RdDTimestamp.handleTimestampEditor(this.html, 'system.temporel.fin', updateItemTimestamp);
}
getActionRenderItem() {
return async () => {
let item = this.item;
while (item) {
await item.sheet?.render()
item = this.actor.getContenant(item)
async itemActionConsommer(event) {
const item = RdDSheetUtility.getItem(event, this.actor)
if (item) {
await actor.consommerPotion(item)
await RdDSheetUtility.renderItemBranch(this.actor, item)
}
}
async itemActionSplit(event) {
const item = RdDSheetUtility.getItem(event, this.actor)
if (item) {
await RdDSheetUtility.splitItem(item, this.actor)
await RdDSheetUtility.renderItemBranch(this.actor, item)
}
}
async itemAction(event) {
const item = RdDSheetUtility.getItem(event, this.actor);
if (item) {
await item.actionPrincipale(this.actor)
await RdDSheetUtility.renderItemBranch(this.actor, item)
}
}
_getEventActor(event) {

View File

@ -70,7 +70,7 @@ export class RdDSheetUtility {
await RdDSheetUtility._onSplitItem(item, split, actor);
onSplit();
});
dialog.render(true);
dialog.render(true)
}
static async _onSplitItem(item, split, actor) {
@ -82,4 +82,11 @@ export class RdDSheetUtility {
await actor.createEmbeddedDocuments('Item', [splitItem])
}
}
static async renderItemBranch(actor, item) {
while (item) {
await item.sheet?.render()
item = actor.getContenant(item)
}
}
}