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 ( )
2022-07-17 18:20:05 +02:00
} )
2022-02-23 20:39:58 +01:00
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 ( )
2022-07-17 18:20:05 +02:00
} )
2022-11-25 20:47:28 +01:00
game . settings . register ( "world" , "character-summary-data" , {
name : "character-summary-data" ,
scope : "world" ,
config : false ,
default : { npcList : [ ] , x : 200 , y : 200 } ,
type : Object
} )
2022-07-17 18:20:05 +02:00
game . settings . register ( "bol" , "logoActorSheet" , {
name : "Chemin du logo des fiches de perso" ,
hint : "Vous pouvez changer le logo BoL des fiches de perso, pour jouer dans un autre univers (idéalement 346 x 200, défaut : /systems/bol/ui/logo.webp)" ,
scope : "world" ,
config : true ,
default : "/systems/bol/ui/logo.webp" ,
type : String ,
onChange : lang => window . location . reload ( )
} )
game . settings . register ( "bol" , "logoTopLeft" , {
name : "Chemin du logo haut gauche" ,
hint : "Vous pouvez changer le logo BoL en haut à gauche de chaque écran (idéalement 718 x 416, défaut : /systems/bol/ui/logo2.webp)" ,
scope : "world" ,
config : true ,
default : "/systems/bol/ui/logo2.webp" ,
type : String ,
onChange : lang => window . location . reload ( )
} )
2022-12-23 23:24:09 +01:00
game . settings . register ( "bol" , "horoscope-group" , {
name : "horoscope-group" ,
scope : "world" ,
config : false ,
default : { } ,
type : Object
} )
2022-02-23 20:39:58 +01:00
this . rollArmor = game . settings . get ( "bol" , "rollArmor" ) // Roll armor or not
this . useBougette = game . settings . get ( "bol" , "useBougette" ) // Use optionnal bougette rules
2022-07-17 18:20:05 +02:00
this . actorSheetLogo = game . settings . get ( "bol" , "logoActorSheet" ) || "/systems/bol/ui/logo.webp"
this . logoTopLeft = game . settings . get ( "bol" , "logoTopLeft" ) || "/systems/bol/ui/logo2.webp"
2022-02-23 20:39:58 +01:00
}
/* -------------------------------------------- */
static getRollArmor ( ) {
return this . rollArmor
}
/* -------------------------------------------- */
static getUseBougette ( ) {
return this . useBougette
2021-11-01 00:28:42 +01:00
}
2022-07-17 18:20:05 +02:00
/* -------------------------------------------- */
static getLogoActorSheet ( ) {
return this . actorSheetLogo
}
/* -------------------------------------------- */
static getLogoTopLeft ( ) {
return this . logoTopLeft
}
2022-11-30 12:12:44 +01:00
/* -------------------------------------------- */
static getActorFromRollData ( rollData ) {
let actor = game . actors . get ( rollData . actorId )
if ( rollData . tokenId ) {
let token = canvas . tokens . placeables . find ( t => t . id == rollData . tokenId )
if ( token ) {
actor = token . actor
}
}
return actor
}
2022-11-25 20:47:28 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async ready ( ) {
2022-07-17 18:20:05 +02:00
//$("#logo").attr("src", this.getLogoTopLeft() )
2022-11-25 20:47:28 +01:00
$ ( "#logo" ) . css ( "content" , ` url( ${ this . getLogoTopLeft ( ) } ) ` )
2022-11-30 12:12:44 +01:00
CONFIG . statusEffects = duplicate ( game . bol . config . statusEffects )
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 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 ) {
2022-06-11 10:21:18 +02:00
this . rollTab [ roll . id ] = roll
2022-01-16 22:06:49 +01:00
}
/* -------------------------------------------- */
2022-06-11 10:21:18 +02:00
static getRoll ( rollId ) {
return this . rollTab [ roll . id ]
2022-01-16 22:06:49 +01:00
}
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 ;
}
2022-03-27 22:56:43 +02:00
/* -------------------------------------------- */
2022-06-11 20:56:35 +02:00
static getOtherWhisperRecipients ( name ) {
2022-03-27 22:56:43 +02:00
let users = [ ]
2022-06-11 20:56:35 +02:00
for ( let user of game . users ) {
2022-11-30 20:58:27 +01:00
if ( ! user . isGM && user . name != name ) {
2022-08-31 22:24:56 +02:00
users . push ( user . id )
2022-03-27 22:56:43 +02:00
}
}
return users
}
2022-06-11 20:56:35 +02:00
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
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-04-08 23:42:01 +02:00
game . socket . emit ( "system.bol" , { name : "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 ) {
2022-04-08 23:42:01 +02:00
if ( attackDef . targetId ) {
2022-01-01 23:32:48 +01:00
// Broadcast to GM or process it directly in case of GM defense
if ( ! game . user . isGM ) {
2022-04-08 23:42:01 +02:00
game . socket . emit ( "system.bol" , { name : "msg_attack_success" , data : duplicate ( attackDef ) } )
2022-01-01 23:32:48 +01:00
} else {
2022-04-08 23:42:01 +02:00
BoLUtility . processAttackSuccess ( attackDef )
2022-01-01 23:32:48 +01:00
}
2022-02-23 20:39:58 +01:00
}
2022-01-01 23:32:48 +01:00
}
2022-02-23 20:39:58 +01:00
2022-03-27 22:56:43 +02:00
/* -------------------------------------------- */
static async chatMessageHandler ( message , html , data ) {
const chatCard = html . find ( '.flavor-text' )
if ( chatCard . length > 0 ) {
// If the user is the message author or the actor owner, proceed
const actor = game . actors . get ( data . message . speaker . actor )
2022-04-08 23:42:01 +02:00
//console.log("FOUND 1!!! ", actor)
2022-03-27 22:56:43 +02:00
if ( actor && actor . isOwner ) return
else if ( game . user . isGM || data . author . id === game . user . id ) return
2022-06-11 20:56:35 +02:00
2022-03-27 22:56:43 +02:00
const divButtons = chatCard . find ( '.actions-section' )
divButtons . hide ( )
}
}
2022-06-11 20:56:35 +02:00
2022-06-11 10:21:18 +02:00
/* -------------------------------------------- */
2022-06-11 20:56:35 +02:00
static getRollDataFromMessage ( event ) {
2022-06-11 10:21:18 +02:00
let messageId = BoLUtility . findChatMessageId ( event . currentTarget )
let message = game . messages . get ( messageId )
2022-06-11 20:56:35 +02:00
return message . getFlag ( "world" , "bol-roll-data" )
}
/* -------------------------------------------- */
static cleanupButtons ( id ) {
$ ( ` # ${ id } ` ) . hide ( ) // Hide the options roll buttons
game . socket . emit ( "system.bol" , { name : "msg_cleanup_buttons" , data : { id : id } } )
2022-06-11 10:21:18 +02:00
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static async chatListeners ( html ) {
2022-06-11 20:56:35 +02: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-06-11 10:21:18 +02:00
let rollData = BoLUtility . getRollDataFromMessage ( event )
2022-06-11 20:56:35 +02:00
BoLUtility . cleanupButtons ( rollData . applyId )
2022-01-17 23:50:57 +01:00
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-06-11 20:56:35 +02:00
event . preventDefault ( )
2022-06-11 10:21:18 +02:00
let rollData = BoLUtility . getRollDataFromMessage ( event )
2022-06-11 20:56:35 +02:00
rollData . damageMode = event . currentTarget . attributes [ 'data-damage-mode' ] . value
2022-01-17 23:50:57 +01:00
let bolRoll = new BoLDefaultRoll ( rollData )
bolRoll . rollDamage ( )
} ) ;
2022-02-23 20:39:58 +01:00
2022-03-27 22:56:43 +02:00
html . on ( "click" , '.transform-legendary-roll' , event => {
event . preventDefault ( ) ;
2022-06-11 10:21:18 +02:00
let rollData = BoLUtility . getRollDataFromMessage ( event )
2022-06-11 20:56:35 +02:00
let actor = game . actors . get ( rollData . actorId )
2022-04-08 23:42:01 +02:00
actor . subHeroPoints ( 1 )
2022-03-27 22:56:43 +02:00
let r = new BoLDefaultRoll ( rollData )
r . upgradeToLegendary ( )
} )
2022-01-17 23:50:57 +01:00
html . on ( "click" , '.transform-heroic-roll' , event => {
event . preventDefault ( ) ;
2022-06-11 10:21:18 +02:00
let rollData = BoLUtility . getRollDataFromMessage ( event )
2022-06-11 20:56:35 +02:00
let actor = game . actors . get ( rollData . actorId )
2022-04-08 23:42:01 +02:00
actor . subHeroPoints ( 1 )
2022-02-23 20:39:58 +01:00
let r = new BoLDefaultRoll ( rollData )
2022-03-27 22:56:43 +02:00
r . upgradeToHeroic ( )
} )
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-06-11 10:21:18 +02:00
let rollData = BoLUtility . getRollDataFromMessage ( event )
2022-06-11 20:56:35 +02:00
let actor = game . actors . get ( rollData . actorId )
2022-04-08 23:42:01 +02:00
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-03-27 12:42:29 +02:00
event . preventDefault ( )
let attackId = event . currentTarget . attributes [ 'data-attack-id' ] . value
let defenseMode = event . currentTarget . attributes [ 'data-defense-mode' ] . value
2021-12-29 19:15:06 +01:00
let weaponId = ( event . currentTarget . attributes [ 'data-weapon-id' ] ) ? event . currentTarget . attributes [ 'data-weapon-id' ] . value : - 1
2022-11-25 20:47:28 +01:00
2022-06-11 20:56:35 +02:00
// Remove message for all
let msgId = BoLUtility . findChatMessageId ( event . currentTarget )
2022-02-23 20:39:58 +01:00
if ( game . user . isGM ) {
2022-06-11 20:56:35 +02:00
BoLUtility . processDamageHandling ( attackId , defenseMode , weaponId , msgId )
2021-12-29 19:15:06 +01:00
} else {
2022-06-11 20:56:35 +02:00
game . socket . emit ( "system.bol" , { name : "msg_damage_handling" , data : { msgId : msgId , attackId : attackId , defenseMode : defenseMode , weaponId : weaponId } } )
2021-11-01 00:28:42 +01:00
}
2022-06-11 20:56:35 +02:00
} )
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
2022-06-11 20:56:35 +02:00
static async processDamageHandling ( attackId , defenseMode , weaponId = - 1 , msgId ) {
2022-02-23 20:39:58 +01:00
if ( ! game . user . isGM ) {
2022-06-11 20:56:35 +02:00
return
2021-11-01 00:28:42 +01:00
}
2022-11-25 20:47:28 +01:00
BoLUtility . removeChatMessageId ( msgId )
2022-06-11 20:56:35 +02:00
console . log ( "Damage Handling" , 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 ]
2022-04-08 23:42:01 +02:00
if ( attackDef && attackDef . defenderId ) {
if ( attackDef . defenseDone ) {
return
} // ?? Why ???
2022-01-08 23:47:48 +01:00
attackDef . defenseDone = true
2022-05-23 18:38:51 +02:00
attackDef . defenseMode = defenseMode
let token = game . scenes . current . tokens . get ( attackDef . targetId )
let defender = token . actor
2022-01-09 13:23:20 +01:00
2021-12-29 19:15:06 +01:00
if ( defenseMode == 'damage-with-armor' ) {
2022-04-08 23:42:01 +02:00
let armorFormula = defender . getArmorFormula ( )
2021-12-29 19:15:06 +01:00
attackDef . rollArmor = new Roll ( armorFormula )
2022-06-11 20:56:35 +02:00
attackDef . rollArmor . roll ( { async : false } )
2022-06-11 10:21:18 +02:00
attackDef . armorProtect = ( attackDef . rollArmor . total < 0 ) ? 0 : attackDef . rollArmor . total
attackDef . finalDamage = attackDef . damageTotal - attackDef . armorProtect
attackDef . finalDamage = ( attackDef . finalDamage < 0 ) ? 0 : attackDef . finalDamage
defender . sufferDamage ( attackDef . finalDamage )
console . log ( "Armor roll -> result " , attackDef )
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
if ( defenseMode == 'damage-without-armor' ) {
2022-06-11 10:21:18 +02:00
attackDef . finalDamage = attackDef . damageTotal
defender . sufferDamage ( attackDef . finalDamage )
2021-12-29 19:15:06 +01:00
}
if ( defenseMode == 'hero-reduce-damage' ) {
2022-05-10 23:04:04 +02:00
let armorFormula = defender . getArmorFormula ( )
2022-01-01 23:32:48 +01:00
attackDef . rollArmor = new Roll ( armorFormula )
2022-05-10 23:04:04 +02:00
attackDef . rollArmor . roll ( { async : false } )
attackDef . armorProtect = ( attackDef . rollArmor . total < 0 ) ? 0 : attackDef . rollArmor . total
attackDef . rollHero = new Roll ( "1d6" )
attackDef . rollHero . roll ( { async : false } )
2022-06-11 10:21:18 +02:00
attackDef . finalDamage = attackDef . damageTotal - attackDef . rollHero . total - attackDef . armorProtect
2022-05-10 23:04:04 +02:00
attackDef . finalDamage = ( attackDef . finalDamage < 0 ) ? 0 : attackDef . finalDamage
defender . sufferDamage ( attackDef . finalDamage )
defender . subHeroPoints ( 1 )
2021-12-29 19:15:06 +01:00
}
if ( defenseMode == 'hero-in-extremis' ) {
attackDef . finalDamage = 0 ;
2022-04-08 23:42:01 +02:00
attackDef . weaponHero = defender . weapons . find ( item => item . _id == weaponId ) ;
defender . deleteEmbeddedDocuments ( "Item" , [ weaponId ] ) ;
2021-12-29 19:15:06 +01:00
}
2022-06-11 20:56:35 +02:00
let defenderUser
for ( let user of game . users ) {
2022-11-25 20:47:28 +01:00
if ( user . character && user . character . id == defender . id ) {
2022-06-11 20:56:35 +02:00
defenderUser = user
}
2022-11-25 20:47:28 +01:00
}
2022-06-11 20:56:35 +02:00
let damageResults = {
attackId : attackDef . id ,
attacker : attackDef . attacker ,
rollArmor : attackDef . rollArmor ,
rollHero : attackDef . rollHero ,
weaponHero : attackDef . weaponHero ,
armorProtect : attackDef . armorProtect ,
name : defender . name ,
defender : defender ,
defenseMode : attackDef . defenseMode ,
finalDamage : attackDef . finalDamage
}
2021-12-29 19:15:06 +01:00
ChatMessage . create ( {
2022-04-08 23:42:01 +02:00
alias : defender . name ,
whisper : BoLUtility . getWhisperRecipientsAndGMs ( defender . name ) ,
2022-06-11 20:56:35 +02:00
content : await renderTemplate ( 'systems/bol/templates/chat/rolls/defense-result-card.hbs' , damageResults )
} )
console . log ( "Defender data : " , defenderUser )
ChatMessage . create ( {
alias : defender . name ,
whisper : BoLUtility . getOtherWhisperRecipients ( defenderUser ? . name ) ,
content : await renderTemplate ( 'systems/bol/templates/chat/rolls/defense-summary-card.hbs' , damageResults )
2021-12-29 19:15:06 +01:00
} )
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 ) {
2022-04-08 23:42:01 +02:00
return target
2021-11-01 22:23:43 +01:00
}
}
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 ) {
2022-04-08 23:42:01 +02:00
console . log ( "Attack success processing" , attackDef )
if ( ! game . user . isGM || ! attackDef . defenderId ) { // Only GM process this
return
2021-12-29 19:15:06 +01:00
}
// Build and send the defense message to the relevant people (ie GM + defender)
2022-04-08 23:42:01 +02:00
let defender = game . actors . get ( attackDef . defenderId )
2022-07-01 16:30:21 +02:00
console . log ( "DEF WEP" , attackDef , defender )
let defenderWeapons = defender . weapons || [ ]
2022-04-08 23:42:01 +02:00
this . attackStore [ attackDef . id ] = attackDef // Store !
2021-12-29 19:15:06 +01:00
ChatMessage . create ( {
2022-04-08 23:42:01 +02:00
alias : defender . name ,
whisper : BoLUtility . getWhisperRecipientsAndGMs ( defender . name ) ,
2021-12-29 19:15:06 +01:00
content : await renderTemplate ( 'systems/bol/templates/chat/rolls/defense-request-card.hbs' , {
attackId : attackDef . id ,
attacker : attackDef . attacker ,
2022-04-08 23:42:01 +02:00
defender : defender ,
2021-12-29 19:15:06 +01:00
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" ) {
2022-04-08 23:42:01 +02:00
BoLUtility . processAttackSuccess ( sockmsg . data )
2021-12-29 19:15:06 +01:00
}
2022-06-11 20:56:35 +02:00
if ( sockmsg . name == "msg_cleanup_buttons" ) {
$ ( ` # ${ sockmsg . data . id } ` ) . hide ( ) // Hide the options roll buttons
}
2021-12-29 19:15:06 +01:00
if ( sockmsg . name == "msg_damage_handling" ) {
2022-06-11 20:56:35 +02:00
BoLUtility . processDamageHandling ( sockmsg . data . attackId , sockmsg . data . defenseMode , sockmsg . data . weaponId , sockmsg . data . msgId )
2021-12-29 19:15:06 +01:00
}
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-06-11 10:21:18 +02:00
let pp = spell . data . properties . ppcost
let minpp = _ _circle2minpp [ spell . 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
2022-06-11 20:56:35 +02:00
if ( upgradeDamage && ( ! res [ 3 ] || res [ 3 ] == "" ) ) {
2022-03-10 21:05:53 +01:00
res [ 3 ] = "B" // Upgrade to bonus
}
2022-01-19 21:57:34 +01:00
if ( res [ 3 ] ) {
2022-06-11 20:56:35 +02:00
if ( upgradeDamage && res [ 3 ] == 'M' ) {
2022-03-10 21:05:53 +01:00
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
}
2022-07-16 11:03:33 +02:00
if ( res [ 3 ] == 'MM' ) {
postForm = 'kl' + nbDice
nbDice += 2
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
}
2022-07-16 11:03:33 +02:00
if ( res [ 3 ] == 'BB' ) {
postForm = 'kh' + nbDice
nbDice += 2
modIndex = 4
}
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
/* -------------------------------------------- */
2022-11-25 20:47:28 +01:00
static async loadCompendiumData ( compendium ) {
const pack = game . packs . get ( compendium ) ;
return await pack ? . getDocuments ( ) ? ? [ ] ;
}
/* -------------------------------------------- */
static async loadCompendium ( compendium , filter = item => true ) {
let compendiumData = await this . loadCompendiumData ( compendium ) ;
return compendiumData . filter ( filter ) ;
}
/* -------------------------------------------- */
static async searchItem ( dataItem ) {
let item
if ( dataItem . pack ) {
let id = dataItem . id || dataItem . _id
let items = await this . loadCompendium ( dataItem . pack , item => item . id == id )
item = items [ 0 ] || undefined
} else {
item = game . items . get ( dataItem . id )
2021-12-29 19:15:06 +01:00
}
2022-11-25 20:47:28 +01:00
return item
2021-11-01 00:28:42 +01:00
}
2022-11-25 20:47:28 +01:00
2021-11-01 00:28:42 +01:00
}