MCDM-Productions / mcdm-core

Common driver for MCDM-specific functionality introduced by their dnd5e supplements for FoundryVTT
Other
3 stars 2 forks source link

BH: Companion Link #5

Closed trioderegion closed 2 years ago

trioderegion commented 2 years ago

Template macro:

//find all owned Beastheart characters
const hearts = game.actors.filter( actor => actor.isOwner && !!actor.items.find(item => item.data.system.identifier == 'beastheart'));

const caregivers = hearts.reduce( (acc, actor) => acc += `<option value="${actor.id}">${actor.name}</option>`,'');

//find all owned Monstrous Companions
const comps = game.actors.filter( actor => actor.isOwner && !!actor.items.find(item => item.data.system.identifier == 'moncom'));

const companions = comps.reduce( (acc, actor) => acc += `<option value="${actor.id}">${actor.name}</option>`,'');

console.log(hearts, comps)

const content = `
<form class="flexrow" style="margin-bottom:1em;">
  <div class="flexcol" style="align-items:center;">
    <label for="caregiver"><h3>Caregiver</h3></label>
    <select name="caregiver">${caregivers}</select>
  </div>
  <div class="flexcol" style="align-items:center;">
    <label for="companion"><h3>Companion</h3></label>
    <select name="companion">${companions}</select>
  </div>
</form>`;

const callback = (html) => {
  let caregiver = html.find('[name="caregiver"]').val();
  let companion = html.find('[name="companion"]').val();
  return {caregiver, companion}
};

const choices = await Dialog.prompt({content, title: "Assign Caregiver to Companion", callback, rejectClose: false})

console.log(choices)

if(choices) {

  const caregiver = game.actors.get(choices.caregiver);
  const companion = game.actors.get(choices.companion);

  const prevCare = companion.getFlag('mcdm-beastheart','ferolink');
  if (prevCare) await game.actors.get(prevCare).unsetFlag('mcdm-beastheart','ferolink');

  const prevComp = caregiver.getFlag('mcdm-beastheart','ferolink');
  if (prevComp) await game.actors.get(prevComp).unsetFlag('mcdm-beastheart','ferolink');

  await caregiver.setFlag('mcdm-beastheart','ferolink',choices.companion);
  await companion.setFlag('mcdm-beastheart','ferolink',choices.caregiver);
}