2022-01-16 22:06:49 +01:00
import { BoLDefaultRoll } from "../controllers/bol-rolls.js" ;
2021-12-29 19:15:06 +01:00
2022-01-23 09:25:09 +01:00
// Spell circle to min PP cost
2022-02-23 20:39:58 +01:00
const _ _circle2minpp = { 0 : 0 , 1 : 2 , 2 : 6 , 3 : 11 }
2022-01-23 09:25:09 +01:00
2021-12-29 19:15:06 +01:00
export class BoLUtility {
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
2022-02-23 20:39:58 +01:00
static init ( ) {
2022-01-16 22:06:49 +01:00
this . attackStore = { }
2022-02-23 20:39:58 +01:00
game . settings . register ( "bol" , "rollArmor" , {
name : "Effectuer des jets pour les armures" ,
hint : "Effectue un jet de dés pour les armures (valeur fixe si désactivé)" ,
scope : "world" ,
config : true ,
default : true ,
type : Boolean ,
onChange : lang => window . location . reload ( )
} ) ;
game . settings . register ( "bol" , "useBougette" , {
name : "Utiliser la Bougette (règle fan-made)" ,
hint : "Utilise un indicateur de Bougette, comme décrit dans l'aide de jeu Gold&Glory du RatierBretonnien (https://www.lahiette.com/leratierbretonnien/)" ,
scope : "world" ,
config : true ,
default : false ,
type : Boolean ,
onChange : lang => window . location . reload ( )
} ) ;
this . rollArmor = game . settings . get ( "bol" , "rollArmor" ) // Roll armor or not
this . useBougette = game . settings . get ( "bol" , "useBougette" ) // Use optionnal bougette rules
console . log ( "UTIL" , this )
}
/* -------------------------------------------- */
static getRollArmor ( ) {
return this . rollArmor
}
/* -------------------------------------------- */
static getUseBougette ( ) {
return this . useBougette
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async ready ( ) {
}
2021-12-29 19:15:06 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static templateData ( it ) {
return BoLUtility . data ( it ) ? . data ? ? { }
}
/* -------------------------------------------- */
static data ( it ) {
if ( it instanceof Actor || it instanceof Item || it instanceof Combatant ) {
return it . data ;
}
return it ;
}
2021-12-29 19:15:06 +01:00
2022-01-16 22:06:49 +01:00
/* -------------------------------------------- */
static storeRoll ( roll ) {
this . lastRoll = roll
}
/* -------------------------------------------- */
static getLastRoll ( ) {
return this . lastRoll
}
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static createDirectOptionList ( min , max ) {
2021-11-01 00:28:42 +01:00
let options = { } ;
2021-12-29 19:15:06 +01:00
for ( let i = min ; i <= max ; i ++ ) {
2021-11-01 00:28:42 +01:00
options [ ` ${ i } ` ] = ` ${ i } ` ;
}
return options ;
}
/* -------------------------------------------- */
static buildListOptions ( min , max ) {
2021-11-08 14:40:29 +01:00
let options = [ ] ;
2021-11-01 00:28:42 +01:00
for ( let i = min ; i <= max ; i ++ ) {
2021-11-08 14:40:29 +01:00
options . push ( ` <option value=" ${ i } "> ${ i } </option> ` ) ;
2021-11-01 00:28:42 +01:00
}
2021-11-08 14:40:29 +01:00
return options . join ( "" ) ;
2021-11-01 00:28:42 +01:00
}
2021-11-08 14:40:29 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async showDiceSoNice ( roll , rollMode ) {
if ( game . modules . get ( "dice-so-nice" ) ? . active ) {
if ( game . dice3d ) {
let whisper = null ;
let blind = false ;
rollMode = rollMode ? ? game . settings . get ( "core" , "rollMode" ) ;
switch ( rollMode ) {
case "blindroll" : //GM only
blind = true ;
case "gmroll" : //GM + rolling player
whisper = this . getUsers ( user => user . isGM ) ;
break ;
case "roll" : //everybody
whisper = this . getUsers ( user => user . active ) ;
break ;
case "selfroll" :
whisper = [ game . user . id ] ;
break ;
}
await game . dice3d . showForRoll ( roll , game . user , true , whisper , blind ) ;
}
}
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static getUsers ( filter ) {
return game . users . filter ( filter ) . map ( user => user . data . _id ) ;
}
/* -------------------------------------------- */
static getWhisperRecipients ( rollMode , name ) {
switch ( rollMode ) {
case "blindroll" : return this . getUsers ( user => user . isGM ) ;
case "gmroll" : return this . getWhisperRecipientsAndGMs ( name ) ;
case "selfroll" : return [ game . user . id ] ;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
return undefined ;
}
/* -------------------------------------------- */
static getWhisperRecipientsAndGMs ( name ) {
let recep1 = ChatMessage . getWhisperRecipients ( name ) || [ ] ;
return recep1 . concat ( ChatMessage . getWhisperRecipients ( 'GM' ) ) ;
}
/* -------------------------------------------- */
static blindMessageToGM ( chatOptions ) {
let chatGM = duplicate ( chatOptions ) ;
chatGM . whisper = this . getUsers ( user => user . isGM ) ;
chatGM . content = "Blind message of " + game . user . name + "<br>" + chatOptions . content ;
console . log ( "blindMessageToGM" , chatGM ) ;
2022-03-10 21:05:53 +01:00
game . socket . emit ( "system.bol" , { msg : "msg_gm_chat_message" , data : chatGM } ) ;
2021-12-29 19:15:06 +01:00
}
2022-01-01 23:32:48 +01:00
/* -------------------------------------------- */
static sendAttackSuccess ( attackDef ) {
if ( attackDef . target ) {
// Broadcast to GM or process it directly in case of GM defense
if ( ! game . user . isGM ) {
game . socket . emit ( "system.bol" , { msg : "msg_attack_success" , data : attackDef } ) ;
} else {
BoLUtility . processAttackSuccess ( attackDef ) ;
}
2022-02-23 20:39:58 +01:00
}
2022-01-01 23:32:48 +01:00
}
2022-02-23 20:39:58 +01:00
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static async chatListeners ( html ) {
2022-01-17 23:50:57 +01:00
2021-12-29 19:15:06 +01:00
// Damage handling
2022-02-23 20:39:58 +01:00
html . on ( "click" , '.chat-damage-apply' , event => {
2022-01-17 23:50:57 +01:00
let rollData = BoLUtility . getLastRoll ( )
$ ( ` # ${ rollData . applyId } ` ) . hide ( )
BoLUtility . sendAttackSuccess ( rollData )
2022-01-01 23:32:48 +01:00
} ) ;
2022-02-23 20:39:58 +01:00
html . on ( "click" , '.chat-damage-roll' , event => {
2022-01-17 23:50:57 +01:00
event . preventDefault ( ) ;
let rollData = BoLUtility . getLastRoll ( )
rollData . damageMode = event . currentTarget . attributes [ 'data-damage-mode' ] . value ;
let bolRoll = new BoLDefaultRoll ( rollData )
bolRoll . rollDamage ( )
} ) ;
2022-02-23 20:39:58 +01:00
2022-01-17 23:50:57 +01:00
html . on ( "click" , '.transform-heroic-roll' , event => {
event . preventDefault ( ) ;
let rollData = BoLUtility . getLastRoll ( )
rollData . actor . subHeroPoints ( 1 )
2022-02-23 20:39:58 +01:00
let r = new BoLDefaultRoll ( rollData )
2022-01-17 23:50:57 +01:00
r . upgradeToCritical ( ) ;
2022-02-23 20:39:58 +01:00
} ) ;
2022-01-16 22:06:49 +01:00
html . on ( "click" , '.hero-reroll' , event => {
2022-01-09 13:23:20 +01:00
event . preventDefault ( ) ;
2022-01-16 22:06:49 +01:00
let rollData = BoLUtility . getLastRoll ( )
rollData . actor . subHeroPoints ( 1 )
2022-01-17 23:50:57 +01:00
rollData . reroll = false // Disable reroll option for second roll
2022-02-23 20:39:58 +01:00
let r = new BoLDefaultRoll ( rollData )
2022-01-16 22:06:49 +01:00
r . roll ( ) ;
2022-02-23 20:39:58 +01:00
} ) ;
2022-01-09 13:23:20 +01:00
2022-02-23 20:39:58 +01:00
html . on ( "click" , '.damage-handling' , event => {
2022-01-09 13:23:20 +01:00
event . preventDefault ( ) ;
2021-12-29 19:15:06 +01:00
let attackId = event . currentTarget . attributes [ 'data-attack-id' ] . value ;
let defenseMode = event . currentTarget . attributes [ 'data-defense-mode' ] . value ;
let weaponId = ( event . currentTarget . attributes [ 'data-weapon-id' ] ) ? event . currentTarget . attributes [ 'data-weapon-id' ] . value : - 1
2022-02-23 20:39:58 +01:00
if ( game . user . isGM ) {
2021-12-29 19:15:06 +01:00
BoLUtility . processDamageHandling ( event , attackId , defenseMode , weaponId )
} else {
2022-02-23 20:39:58 +01:00
game . socket . emit ( "system.bol" , { msg : "msg_damage_handling" , data : { event : event , attackId : attackId , defenseMode : defenseMode , weaponId : weaponId } } ) ;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
} ) ;
}
/* -------------------------------------------- */
2022-02-23 20:39:58 +01:00
static async processDamageHandling ( event , attackId , defenseMode , weaponId = - 1 ) {
if ( ! game . user . isGM ) {
2021-12-29 19:15:06 +01:00
return ;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
BoLUtility . removeChatMessageId ( BoLUtility . findChatMessageId ( event . currentTarget ) ) ;
2022-01-08 23:47:48 +01:00
//console.log("Damage Handling", event, attackId, defenseMode, weaponId)
2021-12-29 19:15:06 +01:00
// Only GM process this
2022-01-17 23:50:57 +01:00
let attackDef = this . attackStore [ attackId ]
2021-12-29 19:15:06 +01:00
if ( attackDef ) {
2022-01-08 23:47:48 +01:00
if ( attackDef . defenseDone ) return ; // ?? Why ???
attackDef . defenseDone = true
2021-12-29 19:15:06 +01:00
attackDef . defenseMode = defenseMode ;
2022-01-09 13:23:20 +01:00
2021-12-29 19:15:06 +01:00
if ( defenseMode == 'damage-with-armor' ) {
2022-02-23 20:39:58 +01:00
let armorFormula = attackDef . defender . getArmorFormula ( )
2021-12-29 19:15:06 +01:00
attackDef . rollArmor = new Roll ( armorFormula )
2022-02-23 20:39:58 +01:00
attackDef . rollArmor . roll ( { async : false } )
attackDef . armorProtect = ( attackDef . rollArmor . total < 0 ) ? 0 : attackDef . rollArmor . total ;
2022-01-01 23:32:48 +01:00
attackDef . finalDamage = attackDef . damageRoll . total - attackDef . armorProtect ;
2022-02-23 20:39:58 +01:00
attackDef . finalDamage = ( attackDef . finalDamage < 0 ) ? 0 : attackDef . finalDamage ;
2021-12-29 19:15:06 +01:00
attackDef . defender . sufferDamage ( attackDef . finalDamage ) ;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
if ( defenseMode == 'damage-without-armor' ) {
attackDef . finalDamage = attackDef . damageRoll . total ;
attackDef . defender . sufferDamage ( attackDef . finalDamage ) ;
}
if ( defenseMode == 'hero-reduce-damage' ) {
2022-02-23 20:39:58 +01:00
let armorFormula = attackDef . defender . getArmorFormula ( ) ;
2022-01-01 23:32:48 +01:00
attackDef . rollArmor = new Roll ( armorFormula )
2022-02-23 20:39:58 +01:00
attackDef . rollArmor . roll ( { async : false } ) ;
attackDef . armorProtect = ( attackDef . rollArmor . total < 0 ) ? 0 : attackDef . rollArmor . total ;
2021-12-29 19:15:06 +01:00
attackDef . rollHero = new Roll ( "1d6" ) ;
2022-02-23 20:39:58 +01:00
attackDef . rollHero . roll ( { async : false } ) ;
2022-01-01 23:32:48 +01:00
attackDef . finalDamage = attackDef . damageRoll . total - attackDef . rollHero . total - attackDef . armorProtect ;
2022-02-23 20:39:58 +01:00
attackDef . finalDamage = ( attackDef . finalDamage < 0 ) ? 0 : attackDef . finalDamage ;
2021-12-29 19:15:06 +01:00
attackDef . defender . sufferDamage ( attackDef . finalDamage ) ;
attackDef . defender . subHeroPoints ( 1 ) ;
}
if ( defenseMode == 'hero-in-extremis' ) {
attackDef . finalDamage = 0 ;
attackDef . weaponHero = attackDef . defender . weapons . find ( item => item . _id == weaponId ) ;
2022-02-23 20:39:58 +01:00
attackDef . defender . deleteEmbeddedDocuments ( "Item" , [ weaponId ] ) ;
2021-12-29 19:15:06 +01:00
}
ChatMessage . create ( {
alias : attackDef . defender . name ,
whisper : BoLUtility . getWhisperRecipientsAndGMs ( attackDef . defender . name ) ,
content : await renderTemplate ( 'systems/bol/templates/chat/rolls/defense-result-card.hbs' , {
attackId : attackDef . id ,
attacker : attackDef . attacker ,
rollArmor : attackDef . rollArmor ,
rollHero : attackDef . rollHero ,
2022-02-23 20:39:58 +01:00
weaponHero : attackDef . weaponHero ,
2022-01-01 23:32:48 +01:00
armorProtect : attackDef . armorProtect ,
2021-12-29 19:15:06 +01:00
defender : attackDef . defender ,
defenseMode : attackDef . defenseMode ,
finalDamage : attackDef . finalDamage
} )
} )
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
static createChatMessage ( name , rollMode , chatOptions ) {
switch ( rollMode ) {
case "blindroll" : // GM only
if ( ! game . user . isGM ) {
this . blindMessageToGM ( chatOptions ) ;
chatOptions . whisper = [ game . user . id ] ;
chatOptions . content = "Message only to the GM" ;
}
else {
chatOptions . whisper = this . getUsers ( user => user . isGM ) ;
}
break ;
default :
chatOptions . whisper = this . getWhisperRecipients ( rollMode , name ) ;
break ;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
chatOptions . alias = chatOptions . alias || name ;
ChatMessage . create ( chatOptions ) ;
}
/* -------------------------------------------- */
static createChatWithRollMode ( name , chatOptions ) {
this . createChatMessage ( name , game . settings . get ( "core" , "rollMode" ) , chatOptions ) ;
}
2021-11-01 23:06:34 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static isRangedWeapon ( weapon ) {
2021-11-01 23:06:34 +01:00
return weapon . data . type == 'ranged' || weapon . data . thrown ;
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static removeChatMessageId ( messageId ) {
2022-02-23 20:39:58 +01:00
if ( messageId ) {
2021-12-29 19:15:06 +01:00
game . messages . get ( messageId ) ? . delete ( ) ;
}
}
2022-02-23 20:39:58 +01:00
2021-12-29 19:15:06 +01:00
static findChatMessageId ( current ) {
return BoLUtility . getChatMessageId ( BoLUtility . findChatMessage ( current ) ) ;
}
static getChatMessageId ( node ) {
return node ? . attributes . getNamedItem ( 'data-message-id' ) ? . value ;
}
static findChatMessage ( current ) {
return BoLUtility . findNodeMatching ( current , it => it . classList . contains ( 'chat-message' ) && it . attributes . getNamedItem ( 'data-message-id' ) ) ;
}
static findNodeMatching ( current , predicate ) {
if ( current ) {
if ( predicate ( current ) ) {
return current ;
}
return BoLUtility . findNodeMatching ( current . parentElement , predicate ) ;
}
return undefined ;
}
2021-11-01 22:23:43 +01:00
/* -------------------------------------------- */
static getTarget ( ) {
if ( game . user . targets && game . user . targets . size == 1 ) {
for ( let target of game . user . targets ) {
return target ;
}
}
return undefined ;
}
2021-12-29 19:15:06 +01:00
2021-12-25 23:26:27 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static async processAttackSuccess ( attackDef ) {
if ( ! game . user . isGM ) { // Only GM process this
return ;
}
// Build and send the defense message to the relevant people (ie GM + defender)
let defenderWeapons = attackDef . defender . weapons ;
console . log ( "DEF WEP" , attackDef )
this . attackStore [ attackDef . id ] = attackDef ; // Store !
ChatMessage . create ( {
alias : attackDef . defender . name ,
whisper : BoLUtility . getWhisperRecipientsAndGMs ( attackDef . defender . name ) ,
content : await renderTemplate ( 'systems/bol/templates/chat/rolls/defense-request-card.hbs' , {
attackId : attackDef . id ,
attacker : attackDef . attacker ,
defender : attackDef . defender ,
defenderWeapons : defenderWeapons ,
2022-03-10 21:05:53 +01:00
damageTotal : attackDef . damageRoll . total ,
damagesIgnoresArmor : attackDef . damagesIgnoresArmor ,
2021-12-29 19:15:06 +01:00
} )
2022-03-10 21:05:53 +01:00
} )
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
static onSocketMessage ( sockmsg ) {
if ( sockmsg . name == "msg_attack_success" ) {
BoLUtility . processAttackSuccess ( sockmsg . data ) ;
}
if ( sockmsg . name == "msg_damage_handling" ) {
BoLUtility . processDamageHandling ( sockmsg . data . event , sockmsg . data . attackId , sockmsg . data . defenseMode )
}
2022-02-23 20:39:58 +01:00
}
2022-01-23 09:25:09 +01:00
/* -------------------------------------------- */
2022-02-23 20:39:58 +01:00
static computeSpellCost ( spell , nbOptCond = 0 ) {
2022-01-23 09:25:09 +01:00
let pp = spell . data . data . properties . ppcost
let minpp = _ _circle2minpp [ spell . data . data . properties . circle ]
2022-02-23 20:39:58 +01:00
pp = ( pp - nbOptCond < minpp ) ? minpp : pp - nbOptCond
2022-01-23 09:25:09 +01:00
return pp
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
2022-03-10 21:05:53 +01:00
static getDamageFormula ( weaponData , fightOption ) {
let upgradeDamage = ( fightOption && fightOption . data . properties . fightoptiontype == "twoweaponsatt" )
2022-02-23 20:39:58 +01:00
let damageString = weaponData . properties . damage
let modifier = weaponData . properties . damageModifiers ? ? 0
let multiplier = weaponData . properties . damageMultiplier ? ? 1
2021-12-29 19:15:06 +01:00
if ( damageString [ 0 ] == 'd' ) { damageString = "1" + damageString } // Help parsing
2022-01-09 20:00:11 +01:00
if ( modifier == null ) modifier = 0 ;
2022-02-23 20:39:58 +01:00
let reroll = ( weaponData . properties . damageReroll1 ) ? "r1" : "" // Reroll 1 option
2022-01-19 21:57:34 +01:00
let formula = damageString
2022-02-23 20:39:58 +01:00
if ( damageString . includes ( "d" ) || damageString . includes ( "D" ) ) {
2022-03-10 21:05:53 +01:00
var myReg = new RegExp ( '(\\d+)[dD]([\\d]+)([MB]*)?([\\+\\d]*)?' , 'g' )
let res = myReg . exec ( damageString )
let nbDice = parseInt ( res [ 1 ] )
let postForm = 'kh' + nbDice
let modIndex = 3
// Upgrade damage if needed
if ( upgradeDamage && ( ! res [ 3 ] || res [ 3 ] == "" ) ) {
res [ 3 ] = "B" // Upgrade to bonus
}
2022-01-19 21:57:34 +01:00
if ( res [ 3 ] ) {
2022-03-10 21:05:53 +01:00
if ( upgradeDamage && res [ 3 ] == 'M' ) {
res [ 3 ] = "" // Disable lamlus for upgradeDamage
}
2022-01-19 21:57:34 +01:00
if ( res [ 3 ] == 'M' ) {
2022-03-10 21:05:53 +01:00
postForm = 'kl' + nbDice
nbDice ++
modIndex = 4
2022-01-19 21:57:34 +01:00
}
if ( res [ 3 ] == 'B' ) {
2022-03-10 21:05:53 +01:00
postForm = 'kh' + nbDice
nbDice ++
modIndex = 4
2022-01-19 21:57:34 +01:00
}
2021-12-25 23:26:27 +01:00
}
2022-03-10 21:05:53 +01:00
formula = "(" + nbDice + "d" + res [ 2 ] + reroll + postForm + "+" + modifier + ") *" + multiplier
2021-12-25 23:26:27 +01:00
}
2022-03-10 21:05:53 +01:00
return formula
2021-12-25 23:26:27 +01:00
}
2022-02-16 09:11:49 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async confirmDelete ( actorSheet , li ) {
let itemId = li . data ( "item-id" ) ;
let msgTxt = "<p>Are you sure to remove this Item ?" ;
let buttons = {
delete : {
2021-12-29 19:15:06 +01:00
icon : '<i class="fas fa-check"></i>' ,
label : "Yes, remove it" ,
callback : ( ) => {
actorSheet . actor . deleteEmbeddedDocuments ( "Item" , [ itemId ] ) ;
li . slideUp ( 200 , ( ) => actorSheet . render ( false ) ) ;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
} ,
cancel : {
icon : '<i class="fas fa-times"></i>' ,
label : "Cancel"
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
}
msgTxt += "</p>" ;
let d = new Dialog ( {
title : "Confirm removal" ,
content : msgTxt ,
buttons : buttons ,
default : "cancel"
} ) ;
d . render ( true ) ;
2021-11-01 00:28:42 +01:00
}
}