Montée TMR sur méditation
ajout d'un bouton pour aller dans les TMR lors d'une méditation réussie
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
|
||||
const ACTOR_EMBEDDED_DOCTYPES = ['Item', 'ActiveEffect']
|
||||
/**
|
||||
* class designed to store actor modification instructions, to apply them in a single operation, and have the ability to revert these
|
||||
*/
|
||||
export class ActorImpacts {
|
||||
static $newDocumentImpacts(docType) {
|
||||
return { creates: [], deletes: [], updates: [], docType: docType }
|
||||
}
|
||||
static $checkDocType(docType) {
|
||||
if (!ACTOR_EMBEDDED_DOCTYPES.includes(docType)) {
|
||||
throw `Unsupported document type ${docType}`
|
||||
}
|
||||
}
|
||||
|
||||
constructor(actorToken) {
|
||||
this.actorToken = actorToken
|
||||
this.updates = []
|
||||
this.deltas = []
|
||||
this.itemCreates = []
|
||||
this.itemUpdates = []
|
||||
this.itemDeletes = []
|
||||
ACTOR_EMBEDDED_DOCTYPES.forEach(
|
||||
docType => this[docType] = ActorImpacts.$newDocumentImpacts(docType)
|
||||
)
|
||||
}
|
||||
|
||||
addActorUpdate(path, value) {
|
||||
@@ -27,46 +37,55 @@ export class ActorImpacts {
|
||||
}
|
||||
}
|
||||
|
||||
addDeletedItem(item) {
|
||||
this.itemDeletes.push(item)
|
||||
}
|
||||
addCreatedItem(item) {
|
||||
this.itemCreates.push(item)
|
||||
addDeleted(docType, document) {
|
||||
ActorImpacts.$checkDocType(docType)
|
||||
this[docType].deletes.push(document)
|
||||
}
|
||||
|
||||
addItemUpdate(item, path, value) {
|
||||
const existing = this.itemUpdates.find(it => it.id == item.id)
|
||||
addCreated(docType, document) {
|
||||
ActorImpacts.$checkDocType(docType)
|
||||
this[docType].creates.push(document)
|
||||
}
|
||||
|
||||
addUpdate(docType, document, path, value) {
|
||||
ActorImpacts.$checkDocType(docType)
|
||||
const update = [path, value]
|
||||
const existing = this[docType].updates.find(it => it.id == document.id)
|
||||
if (existing) {
|
||||
existing.updates.push(update)
|
||||
}
|
||||
else {
|
||||
this.itemUpdates.push({ id: item.id, updates: [update], deltas: [] })
|
||||
this[docType].updates.push({ id: document.id, updates: [update], deltas: [] })
|
||||
}
|
||||
}
|
||||
|
||||
addItemDelta(item, path, value) {
|
||||
addDelta(document, path, value) {
|
||||
ActorImpacts.$checkDocType(document)
|
||||
const intValue = Number.parseInt(value)
|
||||
if (Number.isInteger(intValue) && intValue != 0) {
|
||||
const delta = [path, intValue]
|
||||
const existing = this.itemUpdates.find(it => it.id == item.id)
|
||||
const existing = this[docType].updates.find(it => it.id == document.id)
|
||||
if (existing) {
|
||||
existing.deltas.push(delta)
|
||||
}
|
||||
else {
|
||||
this.itemUpdates.push({ id: item.id, updates: [], deltas: [delta] })
|
||||
this[docType].updates.push({ id: document.id, updates: [], deltas: [delta] })
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.error('Cannot use non integer value {} for delta update', value)
|
||||
console.error('Cannot use non-integer value {} for delta update', value)
|
||||
}
|
||||
}
|
||||
|
||||
reverseImpacts() {
|
||||
const reverse = ActorImpacts.$computeReverts(new ActorImpacts(this.actorToken), this, __ => this.actorToken.actor)
|
||||
reverse.itemCreates = this.itemDeletes.map(it => foundry.utils.duplicate(it))
|
||||
reverse.itemDeletes = this.itemCreates.map(it => { return { id: it.id } })
|
||||
reverse.itemUpdates = this.itemUpdates.map(it => ActorImpacts.$computeReverts({ id: it.id }, it, id => this.$getActorItem(id)))
|
||||
ACTOR_EMBEDDED_DOCTYPES.forEach(
|
||||
docType => {
|
||||
reverse[docType].creates = this[docType].deletes.map(it => foundry.utils.duplicate(it))
|
||||
reverse[docType].deletes = this[docType].creates.map(it => { return { id: it.id } })
|
||||
reverse[docType].updates = this[docType].updates.map(it => ActorImpacts.$computeReverts({ id: it.id }, it, id => this.$getEmbeddedDocument(docType, id)))
|
||||
}
|
||||
)
|
||||
return reverse
|
||||
}
|
||||
|
||||
@@ -77,40 +96,44 @@ export class ActorImpacts {
|
||||
|
||||
async applyImpacts() {
|
||||
const actor = this.actorToken.actor
|
||||
const isItemsDelete = this.itemDeletes.length > 0
|
||||
const isItemsCreate = this.itemCreates.length > 0
|
||||
const isItemsUpdate = this.itemUpdates.length > 0
|
||||
const isActorUpdate = this.updates.length > 0 || this.deltas.length > 0
|
||||
await Promise.all(ACTOR_EMBEDDED_DOCTYPES.map(async docType => await this.$applyDocumentsImpacts(actor, docType)))
|
||||
const updates = ActorImpacts.$computeUpdates(this, id => actor)
|
||||
await actor.update(updates, { render: true })
|
||||
}
|
||||
|
||||
if (isItemsDelete) {
|
||||
const deletes = this.itemDeletes.map(it => it.id)
|
||||
await actor.deleteEmbeddedDocuments('Item', deletes, { render: !(isItemsCreate || isItemsUpdate || isActorUpdate) })
|
||||
|
||||
async $applyDocumentsImpacts(actor, docType) {
|
||||
if (this[docType].deletes.length > 0) {
|
||||
const deletes = this[docType].deletes.map(it => it.id)
|
||||
await actor.deleteEmbeddedDocuments(docType, deletes, { render: false })
|
||||
}
|
||||
|
||||
if (isItemsCreate) {
|
||||
const creates = this.itemCreates
|
||||
const created = await actor.createEmbeddedDocuments('Item', creates, { render: !(isItemsUpdate || isActorUpdate)})
|
||||
for (let i=0; i<creates.length; i++){
|
||||
if (this[docType].creates.length > 0) {
|
||||
const creates = this[docType].creates
|
||||
const created = await actor.createEmbeddedDocuments(docType, creates, { render: false })
|
||||
for (let i = 0; i < creates.length; i++) {
|
||||
creates[i].createdId = created[i].id
|
||||
}
|
||||
}
|
||||
|
||||
if (isItemsUpdate) {
|
||||
const updates = this.itemUpdates.map(u => ActorImpacts.$computeUpdates(u, id => this.$getActorItem(id)))
|
||||
await actor.updateEmbeddedDocuments('Item', updates, { render: !isActorUpdate })
|
||||
}
|
||||
|
||||
if (isActorUpdate) {
|
||||
const updates = ActorImpacts.$computeUpdates(this, id => actor)
|
||||
await actor.update(updates, { render: true })
|
||||
if (this[docType].updates.length > 0) {
|
||||
const updates = this[docType].updates.map(u => ActorImpacts.$computeUpdates(u, id => this.$getEmbeddedDocument(docType, id)))
|
||||
await actor.updateEmbeddedDocuments(docType, updates, { render: false })
|
||||
}
|
||||
}
|
||||
|
||||
$getActorItem(id) {
|
||||
return this.actorToken.actor.items.get(id)
|
||||
findCreatedId(docType, origId){
|
||||
return this[docType].creates.find(it => it.id = origId)?.createdId
|
||||
}
|
||||
|
||||
$getEmbeddedDocument(docType, id) {
|
||||
return this.actorToken.actor.getEmbeddedDocument(docType, id)
|
||||
}
|
||||
|
||||
static $computeUpdates(u, getSource) {
|
||||
if (u.updates.length == 0 && u.deltas.length == 0) {
|
||||
return {}
|
||||
}
|
||||
const source = getSource(u.id)
|
||||
const instruction = { _id: u.id }
|
||||
u.updates.forEach(u => instruction[u[0]] = u[1])
|
||||
@@ -124,5 +147,4 @@ export class ActorImpacts {
|
||||
target.deltas = u.deltas.map(d => [d[0], -d[1]])
|
||||
return target
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user