foundryvtt-shadows-over-sol/module/sos-flip-dialog.js

126 lines
4.2 KiB
JavaScript

/* -------------------------------------------- */
import { SoSUtility } from "./sos-utility.js";
/* -------------------------------------------- */
export class SoSFlipDialog extends Dialog {
/* -------------------------------------------- */
constructor(flipData, html) {
let conf = {
title: 'Flip Dialog',
content: html,
buttons: {
'flip-close': { label: 'Cancel and Close', callback: html => this.onFlipClose() }
},
default: 'flip'
};
super(conf, { classes: ["sosdialog"], width: 800 });
this.flipData = flipData;
}
/* -------------------------------------------- */
onFlipClose( ) {
this.close();
}
/* -------------------------------------------- */
updateScoreBase( ) {
let scoreBase = 0;
if ( this.flipData.mode == 'skill' || this.flipData.mode == 'weapon' ) {
let statKey = $('#statSelect').val();
this.flipData.stat = duplicate( this.flipData.statList[ statKey ] );
scoreBase = Math.floor(this.flipData.statList[ statKey ].value / 2) + this.flipData.skill.data.value;
} else { //Stat mode
let statKey = $('#statSelect').val();
scoreBase = this.flipData.stat.value;
}
scoreBase += this.flipData.malusConsequence;
$('#score-base').text( scoreBase);
}
/* -------------------------------------------- */
async updateFlip( flipData ) {
//console.log("UPDATE !!!", flipData);
$('.view-deck').remove();
$("#view-deck").append(await flipData.actor.cardDeck.getDeckHTML());
$('.view-edge').remove();
$("#view-edge").append(await flipData.actor.cardDeck.getEdgeHTML());
this.updateScoreBase();
$('.edge-card').click((event) => {
let flipData = this.flipData;
flipData.modifier = $('#modifier').val();
flipData.tn = (flipData.target) ? flipData.target.actor.data.data.scores.defense.value : $('#tn').val();
flipData.edgeName = event.currentTarget.attributes['data-edge-card'].value;
flipData.cardOrigin = "Edge";
if ( flipData.mode == 'skill' || flipData.mode == 'weapon') {
flipData.stat = duplicate( flipData.statList[ $('#statSelect').val() ] );
}
console.log("CLICK:", flipData);
this.flipData.actor.cardDeck.doFlipFromDeckOrEdge(flipData);
this.onFlipClose();
});
}
/* -------------------------------------------- */
updateConsequence(event) {
this.flipData.consequencesSelected = $('#consequenceSelect').val();
let malusConsequence = 0;
for (let consequenceId of this.flipData.consequencesSelected) {
let consequence = this.flipData.consequencesList.find( item => item._id == consequenceId);
console.log(consequence, consequenceId);
malusConsequence += SoSUtility.getConsequenceMalus( consequence.data.severity );
}
$('#consequence-malus').text(malusConsequence);
this.flipData.malusConsequence = malusConsequence;
this.updateScoreBase();
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
this.bringToTop();
var dialog = this;
function onLoad() {
let flipData = dialog.flipData;
//RdDItemSort.setCoutReveReel(rollData.selectedSort);
//$("#diffLibre").val(Misc.toInt(rollData.diffLibre));
//$("#diffConditions").val(Misc.toInt(rollData.diffConditions));
dialog.updateFlip(flipData);
}
// Setup everything onload
$(function () { onLoad(); });
html.find('#statSelect').change((event) => {
this.updateFlip(dialog.flipData );
} );
html.find('#consequenceSelect').change((event) => {
this.updateConsequence( event );
} );
html.find('.class-view-deck').click((event) => {
let flipData = this.flipData;
flipData.modifier = html.find('#modifier').val();
if ( flipData.mode == 'skill' || flipData.mode == 'weapon') {
let statKey = $('#statSelect').val();
flipData.stat = duplicate( flipData.statList[ statKey ] );
}
flipData.cardOrigin = "Deck";
flipData.tn = (flipData.target) ? flipData.target.actor.data.data.scores.defense.value : $('#tn').val();
dialog.flipData.actor.cardDeck.doFlipFromDeckOrEdge(flipData);
dialog.onFlipClose();
});
}
}