208 lines
7.2 KiB
JavaScript
208 lines
7.2 KiB
JavaScript
import { LESOUBLIES_CONFIG } from "./les-oublies-config.js"
|
|
import { LesOubliesUtility } from "./les-oublies-utility.js"
|
|
import { LesOubliesRolls } from "./les-oublies-rolls.js"
|
|
|
|
export class LesOubliesActor extends Actor {
|
|
static CREATION_ITEM_TYPES = new Set(["race", "tribu", "metier"])
|
|
|
|
prepareDerivedData() {
|
|
super.prepareDerivedData()
|
|
|
|
if (this.type === "personnage") {
|
|
const system = this.system
|
|
const sizeValue = Math.clamp(Number(system.size?.value ?? 1), 1, 4)
|
|
const hpMax = Math.max(sizeValue * 4 + Number(system.hp?.bonus ?? 0), 0)
|
|
system.hp.max = hpMax
|
|
system.hp.value = Math.min(Number(system.hp.value ?? hpMax), hpMax)
|
|
|
|
const songesValue = Number(system.songes?.value ?? 0)
|
|
const cauchemarValue = Number(system.cauchemar?.value ?? 0)
|
|
const totals = LesOubliesUtility.computeDreamPointTotals(songesValue, cauchemarValue)
|
|
system.songes.max = totals.songesPoints
|
|
system.cauchemar.max = totals.cauchemarPoints
|
|
system.songes.points = Math.clamp(Number(system.songes.points ?? totals.songesPoints), 0, totals.songesPoints)
|
|
system.cauchemar.points = Math.clamp(Number(system.cauchemar.points ?? totals.cauchemarPoints), 0, totals.cauchemarPoints)
|
|
return
|
|
}
|
|
|
|
if (this.type !== "creature") return
|
|
|
|
const system = this.system
|
|
const hpValue = Math.max(Number(system.hp?.value ?? 0), 0)
|
|
const hpMax = Math.max(Number(system.hp?.max ?? hpValue), hpValue, 0)
|
|
system.hp.max = hpMax
|
|
system.hp.value = Math.min(hpValue, hpMax)
|
|
const songesPoints = Math.max(Number(system.songes?.points ?? 0), 0)
|
|
const cauchemarPoints = Math.max(Number(system.cauchemar?.points ?? 0), 0)
|
|
system.songes.max = Math.max(Number(system.songes?.max ?? songesPoints), songesPoints)
|
|
system.cauchemar.max = Math.max(Number(system.cauchemar?.max ?? cauchemarPoints), cauchemarPoints)
|
|
system.songes.points = Math.min(songesPoints, system.songes.max)
|
|
system.cauchemar.points = Math.min(cauchemarPoints, system.cauchemar.max)
|
|
}
|
|
|
|
getProfileValue(profileKey) {
|
|
return Number(this.system.profils?.[profileKey] ?? 0)
|
|
}
|
|
|
|
getCreationItem(type) {
|
|
if (!LesOubliesActor.CREATION_ITEM_TYPES.has(type)) return this.items.find((item) => item.type === type) ?? null
|
|
const referenceId = this.system.references?.[`${type}Id`] ?? ""
|
|
if (referenceId) {
|
|
const referencedItem = this.items.get(referenceId)
|
|
if (referencedItem?.type === type) return referencedItem
|
|
}
|
|
return this.items.find((item) => item.type === type) ?? null
|
|
}
|
|
|
|
getEmbeddedItems(type) {
|
|
const items = this.itemTypes?.[type] ?? this.items.filter((item) => item.type === type)
|
|
return LesOubliesUtility.sortByName(items)
|
|
}
|
|
|
|
async assignCreationItem(sourceItem) {
|
|
if (!sourceItem || !LesOubliesActor.CREATION_ITEM_TYPES.has(sourceItem.type)) return null
|
|
|
|
const itemData = sourceItem.toObject()
|
|
delete itemData._id
|
|
|
|
const existingIds = this.getEmbeddedItems(sourceItem.type).map((item) => item.id)
|
|
if (existingIds.length) {
|
|
await this.deleteEmbeddedDocuments("Item", existingIds, { renderSheet: false })
|
|
}
|
|
|
|
const [createdItem] = await this.createEmbeddedDocuments("Item", [itemData], { renderSheet: false })
|
|
if (!createdItem) return null
|
|
|
|
await this.update({
|
|
[`system.references.${sourceItem.type}Id`]: createdItem.id,
|
|
})
|
|
|
|
return createdItem
|
|
}
|
|
|
|
async clearCreationItem(type) {
|
|
if (!LesOubliesActor.CREATION_ITEM_TYPES.has(type)) return
|
|
|
|
const existingIds = this.getEmbeddedItems(type).map((item) => item.id)
|
|
if (existingIds.length) {
|
|
await this.deleteEmbeddedDocuments("Item", existingIds, { renderSheet: false })
|
|
}
|
|
|
|
await this.update({
|
|
[`system.references.${type}Id`]: "",
|
|
})
|
|
}
|
|
|
|
getCompagnie() {
|
|
const compagnieId = this.system.references?.compagnieId
|
|
return compagnieId ? game.actors.get(compagnieId) ?? null : null
|
|
}
|
|
|
|
getCompetenceByKey(skillKey) {
|
|
return this.getEmbeddedItems("competence").find((item) => item.system.key === skillKey) ?? null
|
|
}
|
|
|
|
getSkillScoreByKey(skillKey) {
|
|
const competence = this.getCompetenceByKey(skillKey)
|
|
return competence ? this.computeSkillValue(competence) : 0
|
|
}
|
|
|
|
computeSkillValue(item) {
|
|
const base = Number(item.system.base ?? 0)
|
|
const profileValue = this.getProfileValue(item.system.profileKey)
|
|
if (item.system.closed && base === 0) return 0
|
|
return base + profileValue
|
|
}
|
|
|
|
getCompetences() {
|
|
return this.getEmbeddedItems("competence").map((item) => ({
|
|
item,
|
|
finalValue: this.computeSkillValue(item),
|
|
profileLabel: LESOUBLIES_CONFIG.profileLabels[item.system.profileKey] ?? item.system.profileKey,
|
|
}))
|
|
}
|
|
|
|
getGroupedCompetences() {
|
|
return LESOUBLIES_CONFIG.profiles.map((profile) => ({
|
|
...profile,
|
|
profileValue: this.getProfileValue(profile.id),
|
|
items: this.getCompetences().filter((entry) => entry.item.system.profileKey === profile.id),
|
|
}))
|
|
}
|
|
|
|
getDerivedOverview() {
|
|
const hpValue = Number(this.system.hp?.value ?? 0)
|
|
const hpMax = Number(this.system.hp?.max ?? 0)
|
|
const hpDisplay = this.type === "creature"
|
|
? (this.system.hp?.display || (hpValue === hpMax ? String(hpValue) : `${hpValue}/${hpMax}`))
|
|
: `${hpValue}/${hpMax}`
|
|
|
|
return {
|
|
sizeLabel: LESOUBLIES_CONFIG.sizes[this.system.size?.value] ?? this.system.size?.value,
|
|
hpMax,
|
|
hpValue,
|
|
hpDisplay,
|
|
songesMax: this.system.songes?.max ?? this.system.songes?.points ?? 0,
|
|
cauchemarMax: this.system.cauchemar?.max ?? this.system.cauchemar?.points ?? 0,
|
|
songesPoints: this.system.songes?.points ?? 0,
|
|
cauchemarPoints: this.system.cauchemar?.points ?? 0,
|
|
race: this.getCreationItem("race"),
|
|
tribu: this.getCreationItem("tribu"),
|
|
metier: this.getCreationItem("metier"),
|
|
compagnie: this.getCompagnie(),
|
|
}
|
|
}
|
|
|
|
async openTestRollDialog(preset = {}) {
|
|
return LesOubliesRolls.openTestDialog(this, preset)
|
|
}
|
|
|
|
async openConfrontationRollDialog() {
|
|
return LesOubliesRolls.openConfrontationDialog(this)
|
|
}
|
|
|
|
async openInitiativeRollDialog() {
|
|
return LesOubliesRolls.openInitiativeDialog(this)
|
|
}
|
|
|
|
async openAttackRollDialog({ itemId = null, mode = null } = {}) {
|
|
return LesOubliesRolls.openAttackDialog(this, { itemId, mode })
|
|
}
|
|
|
|
async openDamageDialog({ itemId = null } = {}) {
|
|
return LesOubliesRolls.openDamageDialog(this, { itemId })
|
|
}
|
|
|
|
async openSpellActivationDialog(itemId) {
|
|
return LesOubliesRolls.openSpellDialog(this, itemId)
|
|
}
|
|
|
|
async openCombatPresetDialog(actionKey) {
|
|
return LesOubliesRolls.openCombatPresetDialog(this, actionKey)
|
|
}
|
|
|
|
async openThreadHarvestDialog() {
|
|
return LesOubliesRolls.openThreadHarvestDialog(this)
|
|
}
|
|
|
|
async rollProfile(profileKey) {
|
|
return this.openTestRollDialog({
|
|
label: LESOUBLIES_CONFIG.profileLabels[profileKey] ?? profileKey,
|
|
score: this.getProfileValue(profileKey),
|
|
difficulty: 0,
|
|
rollMode: LesOubliesRolls.getDefaultRollMode(this),
|
|
})
|
|
}
|
|
|
|
async rollCompetence(itemId) {
|
|
const item = this.items.get(itemId)
|
|
if (!item) return null
|
|
return this.openTestRollDialog({
|
|
label: item.name,
|
|
score: this.computeSkillValue(item),
|
|
difficulty: 0,
|
|
rollMode: LesOubliesRolls.getDefaultRollMode(this),
|
|
})
|
|
}
|
|
}
|