2023-03-10 22:37:21 +01:00
import { RdDItem } from "../item.js" ;
2023-03-14 02:38:21 +01:00
import { Misc } from "../misc.js" ;
2023-03-29 22:53:40 +02:00
import { RdDTimestamp } from "../time/rdd-timestamp.js" ;
2024-01-28 20:41:00 +01:00
import { ChatUtility } from "../chat-utility.js" ;
2023-03-10 22:37:21 +01:00
2023-03-10 22:41:11 +01:00
const BASE _TACHE _SOIN _BLESSURE = {
type : "tache" ,
img : 'systems/foundryvtt-reve-de-dragon/icons/competence_chirurgie.webp' ,
system : { carac : "dexterite" , competence : "Chirurgie" , periodicite : "1 round" , fatigue : 0 , }
}
const TACHES _SOIN _BLESSURE = {
6 : { name : 'Blessure critique' , system : { difficulte : - 6 , points _de _tache : 6 } } ,
4 : { name : 'Blessure grave' , system : { difficulte : - 4 , points _de _tache : 4 } } ,
2 : { name : 'Blessure légère' , system : { difficulte : - 2 , points _de _tache : 2 } } ,
}
2023-03-14 02:38:21 +01:00
const definitionsBlessures = [
2024-01-28 20:41:00 +01:00
{ type : "contusion" , gravite : 0 , endurance : "1d4" , vie : 0 , label : 'Contusion/éraflure' , max : 100 , icon : "systems/foundryvtt-reve-de-dragon/icons/sante/eraflure.webp" } ,
{ type : "legere" , gravite : 2 , endurance : "1d6" , vie : 0 , label : 'Légère' , max : 5 , icon : "systems/foundryvtt-reve-de-dragon/icons/sante/blessure.webp" } ,
{ type : "grave" , gravite : 4 , endurance : "2d6" , vie : - 2 , label : 'Grave' , max : 2 , icon : "systems/foundryvtt-reve-de-dragon/icons/sante/blessure.webp" } ,
{ type : "critique" , gravite : 6 , endurance : "-100" , vie : - 4 , label : 'Critique' , max : 1 , icon : "systems/foundryvtt-reve-de-dragon/icons/sante/blessure.webp" } ,
2023-03-30 00:19:12 +02:00
{ type : "mort" , gravite : 8 , label : 'Mort' , max : 1 , icon : "systems/foundryvtt-reve-de-dragon/icons/sante/mort.webp" }
2023-03-14 02:38:21 +01:00
]
2023-03-10 22:37:21 +01:00
export class RdDItemBlessure extends RdDItem {
static get defaultIcon ( ) {
return "systems/foundryvtt-reve-de-dragon/icons/sante/blessure.webp" ;
}
2023-03-14 02:38:21 +01:00
prepareDerivedData ( ) {
super . prepareDerivedData ( ) ;
2025-01-18 22:54:10 +01:00
this . system . label = RdDItemBlessure . getLabelGravite ( this . system . gravite )
2023-03-14 02:38:21 +01:00
}
2023-03-10 22:41:11 +01:00
static prepareTacheSoin ( gravite ) {
const tache = TACHES _SOIN _BLESSURE [ gravite ]
if ( ! tache ) {
ui . notifications . warn ( ` Pas de tâche de soins pour une blessure ${ gravite } ` )
return undefined ;
}
2024-06-01 01:53:14 +02:00
return foundry . utils . mergeObject ( BASE _TACHE _SOIN _BLESSURE , tache , { inplace : false } )
2023-03-10 22:41:11 +01:00
}
2024-01-28 20:41:00 +01:00
static async applyFullBlessure ( actor , gravite ) {
2025-01-18 22:54:10 +01:00
const definition = foundry . utils . duplicate ( RdDItemBlessure . getDefinition ( gravite ) )
2024-01-28 20:41:00 +01:00
if ( definition . vie ) {
2025-01-18 22:54:10 +01:00
await actor . santeIncDec ( "vie" , definition . vie )
}
const lostEndurance = await RdDItemBlessure . rollLostEndurance ( definition . endurance )
if ( lostEndurance ) {
await actor . santeIncDec ( "endurance" , - Number ( lostEndurance ) ) ;
2024-01-28 20:41:00 +01:00
}
await this . createBlessure ( actor , gravite )
ChatMessage . create ( {
2025-01-18 22:54:10 +01:00
//TODO: hbs
content : ` Blessure ${ definition . label } appliquée à ${ actor . name } <br>Perte d'endurance : ${ lostEndurance } ( ${ definition . endurance } )<br>Perte de Vie : ${ definition . vie } ` ,
2024-10-16 23:18:15 +02:00
whisper : ChatUtility . getOwners ( actor )
2024-01-28 20:41:00 +01:00
} ) ;
2025-01-18 22:54:10 +01:00
actor . sheet ? . render ( )
}
2024-01-28 20:41:00 +01:00
2025-01-18 22:54:10 +01:00
static async rollLostEndurance ( formula ) {
if ( formula ) {
const roll = new Roll ( formula )
await roll . evaluate ( )
return roll . total
}
return 0
2024-01-28 20:41:00 +01:00
}
2024-10-27 08:47:44 +01:00
static async createBlessure ( actor , gravite , localisation = '' , attackerToken ) {
2023-03-14 02:38:21 +01:00
const definition = RdDItemBlessure . getDefinition ( gravite )
const blessure = {
2023-03-30 00:19:12 +02:00
name : definition . label ,
2023-03-14 02:38:21 +01:00
type : 'blessure' ,
img : definition . icon ,
system : {
gravite : gravite ,
difficulte : - gravite ,
2023-03-30 00:19:12 +02:00
localisation : localisation ,
2024-10-27 08:47:44 +01:00
origine : attackerToken ? . name ? ? ""
2023-03-14 02:38:21 +01:00
}
}
const blessures = await actor . createEmbeddedDocuments ( 'Item' , [ blessure ] )
return blessures [ 0 ]
}
2023-03-10 22:41:11 +01:00
static async createTacheSoinBlessure ( actor , gravite ) {
const tache = RdDItemBlessure . prepareTacheSoin ( gravite )
if ( tache ) {
const taches = await actor . createEmbeddedDocuments ( 'Item' , [ tache ] , { renderSheet : false } ) ;
return taches [ 0 ] ;
}
return undefined
}
async updateTacheSoinBlessure ( tache ) {
if ( tache ) {
await tache . update ( {
system : {
itemId : this . id ,
difficulte : Math . min ( this . system . difficulte , tache . system . difficulte ) ,
points _de _tache _courant : Math . max ( 0 , this . system . premierssoins . tache )
}
} ) ;
}
}
2023-03-14 02:38:21 +01:00
async setSoinsBlessure ( systemUpdate = { } ) {
2024-06-01 01:53:14 +02:00
systemUpdate = foundry . utils . mergeObject ( systemUpdate , this . system , { overwrite : false } )
systemUpdate . soinscomplets . done = systemUpdate . premierssoins . done && systemUpdate . soinscomplets . done
2023-03-14 02:38:21 +01:00
await this . update ( {
img : this . getImgSoins ( systemUpdate . gravite , systemUpdate . soinscomplets . done ) ,
system : systemUpdate
2024-06-01 01:53:14 +02:00
} )
2023-03-14 02:38:21 +01:00
}
async recuperationBlessure ( { actor , timestamp , message , isMaladeEmpoisonne , blessures } ) {
if ( this . parent != actor || actor == undefined ) {
2023-03-20 23:28:25 +01:00
return
2023-03-14 02:38:21 +01:00
}
2023-03-20 23:28:25 +01:00
if ( new RdDTimestamp ( this . system . temporel . fin ) . compare ( timestamp ) > 0 ) {
2023-03-14 02:38:21 +01:00
// attente periode
return
}
if ( this . system . gravite > 0 ) {
2023-03-20 23:28:25 +01:00
const update = { system : { premierssoins : { bonus : 0 } , soinscomplets : { bonus : 0 } } }
2025-01-18 22:54:10 +01:00
const gravite = this . system . gravite
const graviteMoindre = gravite - 2
2023-03-14 02:38:21 +01:00
const moindres = blessures . filter ( it => it . system . gravite == graviteMoindre , 'blessures' ) . length
2025-01-18 22:54:10 +01:00
const label = RdDItemBlessure . getLabelGravite ( this . system . gravite )
2023-03-14 02:38:21 +01:00
2023-03-20 23:28:25 +01:00
let rolled = await actor . jetRecuperationConstitution ( this . system . soinscomplets . bonus , message ) ;
2023-03-14 02:38:21 +01:00
if ( rolled . isETotal ) {
2023-03-30 00:19:12 +02:00
message . content += ` -- une blessure ${ label } s'infecte (temps de guérison augmenté de ${ gravite } jours, perte de vie) ` ;
2023-03-14 02:38:21 +01:00
await actor . santeIncDec ( "vie" , - 1 ) ;
2024-05-01 09:13:21 +02:00
foundry . utils . mergeObject ( update , {
2023-03-20 23:28:25 +01:00
system : { fin : { indexDate : timestamp . addJours ( gravite ) . indexDate } }
} ) ;
2023-03-14 02:38:21 +01:00
}
else {
if ( ! isMaladeEmpoisonne && rolled . isSuccess && this . peutRetrograder ( graviteMoindre , moindres ) ) {
2023-03-30 00:19:12 +02:00
message . content += ` -- une blessure ${ label } cicatrise ` ;
2024-05-01 09:13:21 +02:00
foundry . utils . mergeObject ( update , {
2023-11-12 19:52:57 +01:00
system : {
gravite : graviteMoindre ,
temporel : { fin : { indexDate : timestamp . addJours ( graviteMoindre ) . indexDate } }
}
2023-03-20 23:28:25 +01:00
} ) ;
2023-03-14 02:38:21 +01:00
}
else {
2023-03-30 00:19:12 +02:00
message . content += ` -- une blessure ${ label } reste stable ` ;
2023-03-14 02:38:21 +01:00
}
}
await this . update ( update ) ;
}
}
peutRetrograder ( graviteMoindre , moindres ) {
2025-01-18 22:54:10 +01:00
return moindres < RdDItemBlessure . maxBlessures ( graviteMoindre )
2023-03-14 02:38:21 +01:00
}
2023-03-10 22:37:21 +01:00
async calculerFinPeriodeTemporel ( debut ) {
return await debut . nouveauJour ( ) . addJours ( this . system . gravite ) ;
}
async onFinPeriode ( oldTimestamp , newTimestamp ) {
if ( this . system . gravite <= 0 ) {
await super . onFinPeriode ( oldTimestamp , newTimestamp )
}
}
2023-03-14 02:38:21 +01:00
getImgSoins ( gravite , soins ) {
let img = 'blessure'
if ( gravite > 6 ) {
img = 'mort'
}
if ( gravite <= 0 ) {
img = 'eraflure'
}
return ` systems/foundryvtt-reve-de-dragon/icons/sante/ ${ soins ? 'blessure-soins' : img } .webp `
2023-03-10 22:41:11 +01:00
}
2025-01-18 22:54:10 +01:00
static getLabelGravite ( gravite ) {
return definitionsBlessures . find ( it => it . gravite >= gravite ) . label
2023-03-10 22:41:11 +01:00
}
2023-03-14 02:38:21 +01:00
static getDefinition ( gravite ) {
2025-01-18 22:54:10 +01:00
return definitionsBlessures . find ( it => it . gravite >= gravite )
2023-03-14 02:38:21 +01:00
}
2025-01-18 22:54:10 +01:00
2023-03-14 02:38:21 +01:00
static maxBlessures ( gravite ) {
2025-01-18 22:54:10 +01:00
return definitionsBlessures . find ( it => it . gravite >= gravite ) . max
2023-03-10 22:41:11 +01:00
}
2023-03-14 02:38:21 +01:00
isContusion ( ) {
return this . system . gravite <= 0
}
isLegere ( ) {
return this . system . gravite > 0 && this . system . gravite <= 2
}
isGrave ( ) {
return this . system . gravite > 2 && this . system . gravite <= 4
2023-03-10 22:41:11 +01:00
}
2023-03-14 02:38:21 +01:00
isCritique ( ) {
return this . system . gravite > 4 && this . system . gravite <= 6
}
isMort ( ) {
return this . system . gravite > 6
}
2023-03-30 00:19:12 +02:00
getProprietes ( ) {
return [
RdDItem . propertyIfDefined ( 'Causée par' , this . system . origine , this . system . origine ) ,
` <b>Heure et Date</b>: ${ new RdDTimestamp ( this . system . temporel . debut ) . formatDateHeure ( ) } ` ,
RdDItem . propertyIfDefined ( 'Blessé' , this . parent ? . name , this . parent ) ,
` <b>Localisation</b>: ${ this . system . localisation } ` ,
2025-01-18 22:54:10 +01:00
` <b>Gravité</b>: ${ this . system . label } ` ,
2023-03-30 00:19:12 +02:00
` <b>Difficulté des soins</b>: ${ this . system . difficulte } ` ,
( this . system . soinscomplets . done ?
` <b>Bonus soins complets</b>: ${ this . system . soinscomplets . bonus } ` :
( this . system . premierssoins . done ?
` <b>Bonus premiers soins</b>: ${ this . system . premierssoins . bonus } ` :
` <b>Points de tâche</b>: ${ this . system . premierssoins . tache } `
)
) ,
] ;
}
2023-03-14 02:38:21 +01:00
}