foundryvtt-reve-de-dragon/module/coeur/rdd-coeur.js

146 lines
5.8 KiB
JavaScript
Raw Normal View History

import { RdDBaseActor } from "../actor/base-actor.js";
import { ChatUtility } from "../chat-utility.js";
import { ReglesOptionnelles } from "../settings/regles-optionnelles.js";
const INFO_COEUR = 'info-coeur';
export class RdDCoeur {
static registerChatCallbacks(html) {
html.on("click", 'a.accepter-tendre-moment', event => {
RdDCoeur.accepterTendreMoment(RdDCoeur.extractInfoCoeur(event))
})
html.on("click", 'a.refuser-tendre-moment', event => {
RdDCoeur.refuserTendreMoment(RdDCoeur.extractInfoCoeur(event))
})
html.on("click", 'a.perdre-point-coeur-douceur', event => {
RdDCoeur.perdreEnDouceur(
RdDCoeur.extractInfoCoeur(event),
event.currentTarget.attributes['data-actor-id'].value)
})
}
static addTagsInfoCoeur(infoCoeur, chatMessage = undefined) {
if (chatMessage) {
infoCoeur.chatMessageId = chatMessage.id
}
else {
chatMessage = game.messages.get(infoCoeur.chatMessageId)
}
ChatUtility.setMessageData(chatMessage, INFO_COEUR, infoCoeur);
}
static extractInfoCoeur(event) {
return ChatUtility.getMessageData(ChatUtility.getChatMessage(event), INFO_COEUR)
}
static getInfoCoeur(sourceActorId, targetActorId) {
const sourceActor = game.actors.get(sourceActorId)
const targetActor = game.actors.get(targetActorId)
if (sourceActor && targetActor) {
return {
source: {
actor: RdDBaseActor.extractActorMin(sourceActor),
coeur: sourceActor.getPointsCoeur(targetActorId),
},
target: {
actor: RdDBaseActor.extractActorMin(targetActor),
coeur: targetActor.getPointsCoeur(sourceActorId),
}
}
}
return {}
}
static async toggleSubActeurCoeur(actorId, subActorId, toggleCoeur) {
const actor = game.actors.get(actorId)
if (ReglesOptionnelles.isUsing("chateau-dormant-gardien") && !actor.system.sommeil.nouveaujour) {
ui.notifications.warn(`Les changements de points de coeur se font juste avant de gérer Château Dormant, juste avant de passer à un nouveau jour`)
return
}
const coeur = actor.getPointsCoeur(subActorId);
if (toggleCoeur <= coeur) {
// TODO: validation?
await actor.moralIncDec(-4);
actor.setPointsCoeur(subActorId, Math.max(0, coeur - 1));
ChatMessage.create({
whisper: ChatUtility.getWhisperRecipientsAndGMs(actor.name),
content: `Perte de points de coeur arbitraire: ${actor.name} perd 4 points de moral, pour finir à ${actor.getMoralTotal()}.`
});
}
else {
actor.setPointsCoeur(subActorId, Math.min(4, toggleCoeur));
}
}
static async startSubActeurTendreMoment(actorId, subActeurId) {
const infoCoeur = RdDCoeur.getInfoCoeur(actorId, subActeurId)
if (infoCoeur.target?.actor.id) {
// TODO: passer par une fenêtre pour saisir sa proposition (lieu, heure, ...)
const chatHtml = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/coeur/chat-proposer-tendre-moment.hbs`, infoCoeur)
const chatMessage = await ChatMessage.create({
whisper: ChatUtility.getWhisperRecipientsAndGMs(infoCoeur.target?.actor.name),
content: chatHtml
})
RdDCoeur.addTagsInfoCoeur(infoCoeur, chatMessage)
}
}
static async accepterTendreMoment(infoCoeur) {
const target = game.actors.get(infoCoeur.target.actor.id)
if (!target.isOwner) {
ui.notifications.warn(`vous ne pouvez pas accepter pour ${infoCoeur.target.actor.name}`)
return
}
ChatUtility.removeChatMessageId(infoCoeur.chatMessageId)
infoCoeur.target.jetTendre = (await (new Roll('1d6').evaluate({ async: true }))).total
infoCoeur.source.jetTendre = (await (new Roll('1d6').evaluate({ async: true }))).total
const diff = Math.abs(infoCoeur.source.jetTendre - infoCoeur.target.jetTendre)
for (let amoureux of [infoCoeur.source, infoCoeur.target]) {
const actorAmoureux = game.actors.get(amoureux.actor.id);
amoureux.situation = diff <= amoureux.coeur ? 'heureux' : 'neutre'
amoureux.gainMoral = await actorAmoureux.jetDeMoral(amoureux.situation)
}
const chatHtml = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/coeur/chat-accepter-tendre-moment.hbs`, infoCoeur)
const chatMessage = await ChatMessage.create({
whisper: ChatUtility.getWhisperRecipientsAndGMs(infoCoeur.source?.actor.name, infoCoeur.target?.actor.name),
content: chatHtml
})
RdDCoeur.addTagsInfoCoeur(infoCoeur, chatMessage)
}
static async refuserTendreMoment(infoCoeur) {
const target = game.actors.get(infoCoeur.target.actor.id)
if (!target.isOwner) {
ui.notifications.warn(`vous ne pouvez pas refuser pour ${infoCoeur.target.actor.name}`)
return
}
ChatUtility.removeChatMessageId(infoCoeur.chatMessageId)
const chatHtml = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/coeur/chat-refuser-tendre-moment.hbs`, infoCoeur)
await ChatMessage.create({
whisper: ChatUtility.getWhisperRecipientsAndGMs(infoCoeur.source?.actor.name, infoCoeur.target?.actor.name),
content: chatHtml
});
}
static async perdreEnDouceur(infoCoeur, actorId) {
const [amoureux, partenaire] = (infoCoeur.source.actor.id == actorId
? [infoCoeur.source, infoCoeur.target]
: (infoCoeur.target.actor.id == actorId
? [infoCoeur.target, infoCoeur.source]
: [undefined, undefined]))
const subActorId = partenaire?.actor.id;
if (amoureux.perteCoeur) {
ui.notifications.warn(`Un point de coeur a déjà été perdu`)
}
else if (amoureux.coeur > 0) {
const actor = game.actors.get(actorId)
if (actor.isOwner) {
await actor.setPointsCoeur(subActorId, amoureux.coeur - 1)
amoureux.perteCoeur = true
RdDCoeur.addTagsInfoCoeur(infoCoeur)
}
}
}
}