remove warpgate dependency

This commit is contained in:
2024-05-18 00:03:02 -05:00
parent a1246bf758
commit b6c16ed0f5
13 changed files with 123 additions and 112 deletions
+82 -61
View File
@@ -1,72 +1,93 @@
const requestRollFromTokens = game.modules.get('swade-mb-helpers').api.requestRollFromTokens
const requestRollFromTokens = game.modules.get('swade-mb-helpers').api.requestRollFromTokens;
async function main () {
let tokens = Array.from(game.user.targets)
async function main() {
let tokens = Array.from(game.user.targets);
if (tokens.length < 1) {
tokens = canvas.tokens.controlled
tokens = canvas.tokens.controlled;
}
if (tokens.length < 1) {
ui.notifications.error('Please target or select some tokens')
return
ui.notifications.error('Please target or select some tokens');
return;
}
const menuData = {
inputs: [
{ type: 'info', label: `Requesting roll from ${tokens.map(t => t.name).join(', ')}` },
{
type: 'select',
label: 'Trait to roll',
options: []
},
{ type: 'number', label: 'Roll Modifier', options: 0 },
{ type: 'text', label: 'Roll Modifier Description', options: 'Roll Modifier' },
{ type: 'number', label: 'Target Number', options: 4 }
],
buttons: [
{ label: 'Request roll', value: 'ok', default: true },
{ label: 'Cancel', value: 'cancel' }
]
}
const menuConfig = {
title: 'Request roll...'
}
for (const attribute of ['Agility', 'Smarts', 'Spirit', 'Strength', 'Vigor']) {
menuData.inputs[1].options.push(
{ html: `Attribute | ${attribute}`, value: `a|${attribute}` }
)
}
const skillSet = new Set()
const attributes = ['Agility', 'Smarts', 'Spirit', 'Strength', 'Vigor'];
const skillSet = new Set();
for (const token of tokens) {
const skills = token.actor.items.filter(i => i.type === 'skill' &&
!['Untrained', 'Unskilled Attempt'].includes(i.name))
for (const skill of skills) {
skillSet.add(skill.name)
const tokenSkills = token.actor.items.filter(
(i) => i.type === 'skill' && !['Untrained', 'Untrained Attempt'].includes(i.name),
);
for (const skill of tokenSkills) {
skillSet.add(skill.name);
}
}
for (const skill of Array.from(skillSet).sort()) {
menuData.inputs[1].options.push(
{ html: `Skill | ${skill}`, value: `s|${skill}` })
}
menuData.inputs[1].options.push(
{ html: 'Skill | Untrained', value: 's|NOSKILL' })
const result = await warpgate.menu(menuData, menuConfig)
if (result.buttons !== 'ok') {
return
}
console.log(result)
const rollMod = result.inputs[2]
const rollModDesc = result.inputs[3]
const rollParts = result.inputs[1].split('|')
const rollType = (rollParts[0] === 'a' ? 'attribute' : 'skill')
const rollDesc = rollParts[1]
const targetNumber = result.inputs[4] || 4
const options = { targetNumber }
if (rollMod !== 0) {
options.mods = [{ label: rollModDesc, value: rollMod }]
}
requestRollFromTokens(tokens, rollType, rollDesc, options)
const attributeOptions = attributes
.map(
(a) => `
<option ${a === 'Agility' ? 'selected ' : ''} value="a|${a}">${a}</option>`,
)
.join('');
const skillOptions = Array.from(skillSet)
.sort()
.map(
(s) => `
<option value="s|${s}">${s}</option>`,
)
.join('');
const content = `
<form>
<p>Requesting roll from ${tokens.map((t) => t.name).join(', ')}.</p>
<div class="form-group">
<label for="trait">Trait to roll</label>
<select name="trait">
<optgroup label="Attributes">${attributeOptions}</optgroup>
<optgroup label="Skills">
${skillOptions}
<option value="s|NOSKILL">Untrained</option>
</optgroup>
</select>
</div>
<div class="form-group">
<label for="mod">Roll Modifier:</label>
<input type="number" value="0" name="mod">
</div>
<div class="form-group">
<label for="modDesc">Roll Modifier Description:</label>
<input type="text" value="Roll Modifier" name="modDesc">
</div>
<div class="form-group">
<label for="tn">Target Number</label>
<input type="number" value="4" name="tn">
</div>
</form>
`;
const buttons = {
ok: {
label: 'Request Roll',
callback: (html) => {
const form = html[0].querySelector('form');
const formDataObject = new FormDataExtended(form).object;
const rollMod = parseInt(formDataObject.mod);
const rollModDesc = formDataObject.modDesc;
const rollParts = formDataObject.trait.split('|');
const rollType = rollParts[0] === 'a' ? 'attribute' : 'skill';
const rollDesc = rollParts[1];
const targetNumber = parseInt(formDataObject.tn);
const options = { targetNumber };
if (rollMod !== 0) {
options.mods = [{ label: rollModDesc, value: rollMod }];
}
requestRollFromTokens(tokens, rollType, rollDesc, options);
},
},
cancel: {
label: 'Cancel',
},
};
new Dialog({
title: 'Request roll',
content,
buttons,
}).render(true);
}
main()
main();