forked from public/foundryvtt-reve-de-dragon
RollV2, tchat et appel chance
gestion des appels à la chance pour tout jet V2 correction de soucis forçage du jet continuation des messages de défense
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import { ChatUtility } from "../chat-utility.js"
|
||||
import RollDialog from "./roll-dialog.mjs"
|
||||
import { RdDCarac } from "../rdd-carac.js";
|
||||
import { RdDCarac } from "../rdd-carac.js"
|
||||
import { RdDCombat } from "../rdd-combat.js"
|
||||
import { ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE } from "./roll-constants.mjs"
|
||||
import { RdDResolutionTable } from "../rdd-resolution-table.js"
|
||||
|
||||
export class ChatRollResult {
|
||||
export default class ChatRollResult {
|
||||
static init() {
|
||||
ChatRollResult.instance = new ChatRollResult()
|
||||
|
||||
Hooks.on('renderChatLog', (log, html, chatLog) => ChatRollResult.instance.chatListeners(html))
|
||||
}
|
||||
|
||||
static onReady() {
|
||||
foundry.applications.handlebars.loadTemplates({
|
||||
@@ -14,10 +22,7 @@ export class ChatRollResult {
|
||||
}
|
||||
|
||||
async display(roll) {
|
||||
roll.show = roll.show || {};
|
||||
roll.show.chance = this.isAppelChancePossible(roll)
|
||||
roll.show.encaissement = this.isShowEncaissement(roll)
|
||||
roll.show.recul = this.isShowReculChoc(roll)
|
||||
this.prepareDisplay(roll)
|
||||
|
||||
const chatMessage = await ChatUtility.createChatWithRollMode(
|
||||
{
|
||||
@@ -26,14 +31,18 @@ export class ChatRollResult {
|
||||
roll.active.actor,
|
||||
roll.current?.rollmode?.key
|
||||
)
|
||||
if (roll.show.chance) {
|
||||
const save = RollDialog.saveParts(roll)
|
||||
console.log("Store message roll", save)
|
||||
ChatUtility.setMessageData(chatMessage, 'rollData', save)
|
||||
}
|
||||
const save = RollDialog.saveParts(roll)
|
||||
ChatUtility.setMessageData(chatMessage, 'rollData', save)
|
||||
return chatMessage
|
||||
}
|
||||
|
||||
prepareDisplay(roll) {
|
||||
roll.show = roll.show || {}
|
||||
roll.show.chance = this.isAppelChancePossible(roll)
|
||||
roll.show.encaissement = this.isShowEncaissement(roll)
|
||||
roll.show.recul = this.isShowReculChoc(roll)
|
||||
}
|
||||
|
||||
isAppelChancePossible(roll) {
|
||||
return roll.active.actor.isPersonnage() &&
|
||||
roll.rolled.isEchec &&
|
||||
@@ -46,14 +55,87 @@ export class ChatRollResult {
|
||||
}
|
||||
|
||||
isShowReculChoc(roll) {
|
||||
return roll.rolled.isEchec &&
|
||||
roll.attackerRoll &&
|
||||
!roll.current.defense.isEsquive &&
|
||||
(roll.attackerRoll.particuliere == 'force' || 'charge' == attackerRoll.tactique?.key)
|
||||
const attaque = roll.attackerRoll
|
||||
return attaque &&
|
||||
(roll.rolled.isEchec || !roll.current.defense.isEsquive) &&
|
||||
(attaque.particuliere == 'force' || 'charge' == attaque.tactique?.key)
|
||||
}
|
||||
|
||||
async buildRollHtml(roll) {
|
||||
const template = `systems/foundryvtt-reve-de-dragon/templates/roll/result/chat-${roll.type.current}.hbs`
|
||||
return await foundry.applications.handlebars.renderTemplate(template, roll)
|
||||
}
|
||||
|
||||
async chatListeners(html) {
|
||||
$(html).on("click", '.appel-chance', event => this.onClickAppelChance(event))
|
||||
$(html).on("click", '.appel-destinee', event => this.onClickAppelDestinee(event))
|
||||
}
|
||||
|
||||
onClickAppelChance(event) {
|
||||
const chatMessage = ChatUtility.getChatMessage(event)
|
||||
const savedRoll = ChatUtility.getMessageData(chatMessage, 'rollData')
|
||||
const actor = game.actors.get(savedRoll.ids.actorId)
|
||||
actor.rollAppelChance(
|
||||
() => this.onAppelChanceSuccess(savedRoll, chatMessage),
|
||||
() => this.onAppelChanceEchec(savedRoll, chatMessage))
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
getCombat(roll) {
|
||||
switch (roll.type.current) {
|
||||
case ROLL_TYPE_DEFENSE:
|
||||
return RdDCombat.rddCombatForAttackerAndDefender(roll.ids.opponentId, roll.ids.opponentTokenId, roll.ids.actorId)
|
||||
case ROLL_TYPE_ATTAQUE:
|
||||
return RdDCombat.rddCombatForAttackerAndDefender(roll.ids.actorId, roll.ids.actorTokenId, roll.ids.opponentId)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
onAppelChanceSuccess(savedRoll, chatMessage) {
|
||||
const reRoll = foundry.utils.duplicate(savedRoll)
|
||||
reRoll.type.retry = true
|
||||
// TODO: annuler les effets
|
||||
switch (reRoll.type.current) {
|
||||
case ROLL_TYPE_DEFENSE:
|
||||
this.getCombat(reRoll)?.doRollDefense(reRoll)
|
||||
break
|
||||
case ROLL_TYPE_ATTAQUE:
|
||||
this.getCombat(reRoll)?.doRollAttaque(reRoll)
|
||||
break
|
||||
default: {
|
||||
RollDialog.create(reRoll)
|
||||
}
|
||||
}
|
||||
ChatUtility.removeChatMessageId(chatMessage.id)
|
||||
}
|
||||
|
||||
async onAppelChanceEchec(savedRoll, chatMessage) {
|
||||
savedRoll.type.retry = true
|
||||
await this.updateChatMessage(chatMessage, savedRoll)
|
||||
}
|
||||
|
||||
async updateChatMessage(chatMessage, savedRoll) {
|
||||
ChatUtility.setMessageData(chatMessage, 'rollData', savedRoll)
|
||||
const copy = foundry.utils.duplicate(savedRoll)
|
||||
RollDialog.loadRollData(copy)
|
||||
this.prepareDisplay(copy)
|
||||
chatMessage.update({ content: await this.buildRollHtml(copy) })
|
||||
chatMessage.render(true)
|
||||
}
|
||||
|
||||
onClickAppelDestinee(event) {
|
||||
const chatMessage = ChatUtility.getChatMessage(event)
|
||||
const savedRoll = ChatUtility.getMessageData(chatMessage, 'rollData')
|
||||
const actor = game.actors.get(savedRoll.ids.actorId)
|
||||
|
||||
actor.appelDestinee(async () => {
|
||||
const reRoll = foundry.utils.duplicate(savedRoll)
|
||||
reRoll.type.retry = true
|
||||
RdDResolutionTable.significativeRequise(reRoll.rolled)
|
||||
await this.updateChatMessage(chatMessage, savedRoll)
|
||||
})
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user