foundryvtt-shadows-over-sol/module/sos-utility.js

110 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
import { SoSDialogCombatActions } from "./sos-dialog-combat-actions.js";
2021-01-17 22:09:01 +01:00
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
export class SoSUtility {
2021-01-18 16:11:27 +01:00
/* -------------------------------------------- */
2021-01-17 22:09:01 +01:00
static async preloadHandlebarsTemplates() {
2021-01-18 17:46:39 +01:00
2021-01-17 22:09:01 +01:00
const templatePaths = [
2021-01-18 17:46:39 +01:00
'systems/foundryvtt-shadows-over-sol/templates/actor-sheet.html',
'systems/foundryvtt-shadows-over-sol/templates/editor-notes-gm.html',
'systems/foundryvtt-shadows-over-sol/templates/stat-option-list.html',
2021-01-21 17:16:01 +01:00
'systems/foundryvtt-shadows-over-sol/templates/stat-name-list.html',
2021-01-19 22:54:53 +01:00
'systems/foundryvtt-shadows-over-sol/templates/item-sheet.html',
2021-01-21 17:16:01 +01:00
'systems/foundryvtt-shadows-over-sol/templates/item-geneline-sheet.html',
'systems/foundryvtt-shadows-over-sol/templates/item-subculture-sheet.html',
'systems/foundryvtt-shadows-over-sol/templates/dialog-flip.html'
2021-01-17 22:09:01 +01:00
]
2021-01-21 17:16:01 +01:00
return loadTemplates(templatePaths);
}
2021-01-31 17:23:14 +01:00
/* -------------------------------------------- */
2021-01-21 17:16:01 +01:00
static fillRange (start, end) {
return Array(end - start + 1).fill().map((item, index) => start + index);
2021-01-17 22:09:01 +01:00
}
2021-01-31 17:23:14 +01:00
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
onSocketMesssage( msg ) {
if (msg.name == 'msg_declare_actions' ) {
2021-02-04 22:55:57 +01:00
if (game.user.isGM) {
let combat = game.combats.get( msg.data.combatId); // Get the associated combat
combat.setupActorActions( msg.data );
}
} else if (msg.name == 'msg_close_action') {
if (game.user.isGM) {
game.combat.closeAction( msg.data.uniqId );
}
2021-02-03 15:33:07 +01:00
}
}
2021-01-31 17:23:14 +01:00
/* -------------------------------------------- */
static async loadCompendiumNames(compendium) {
const pack = game.packs.get(compendium);
let competences;
await pack.getIndex().then(index => competences = index);
return competences;
}
/* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) {
let compendiumItems = await SoSUtility.loadCompendiumNames(compendium);
const pack = game.packs.get(compendium);
let list = [];
for (let compendiumItem of compendiumItems) {
await pack.getEntity(compendiumItem._id).then(it => {
const item = it.data;
if (filter(item)) {
list.push(item);
}
});
};
return list;
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
static updateCombat(combat, round, diff, id) {
combat.requestActions();
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
static async openDeclareActions( event) {
event.preventDefault();
let round = event.currentTarget.attributes['data-round'].value;
2021-02-04 08:38:59 +01:00
let combatantId = event.currentTarget.attributes['data-combatant-id'].value;
2021-02-03 15:33:07 +01:00
let combatId = event.currentTarget.attributes['data-combat-id'].value;
2021-02-04 08:38:59 +01:00
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
let d = await SoSDialogCombatActions.create( combatId, combatantId, round, uniqId );
2021-02-03 15:33:07 +01:00
d.render(true);
}
2021-02-04 22:55:57 +01:00
/* -------------------------------------------- */
static closeAction(event) {
let uniqId = event.currentTarget.attributes['data-uniq-id'].value;
// Delete message !
const toDelete = game.messages.filter(it => it.data.content.includes( uniqId ));
toDelete.forEach(it => it.delete());
if ( game.user.isGM ) {
game.combat.closeAction( uniqId );
} else {
game.socket.emit("system.foundryvtt-reve-de-dragon", {
name: "msg_close_action", data: { uniqId: uniqId} } );
}
}
2021-02-03 15:33:07 +01:00
/* -------------------------------------------- */
static async registerChatCallbacks(html) {
html.on("click", '#button-declare-actions', event => {
SoSUtility.openDeclareActions( event );
});
2021-02-04 22:55:57 +01:00
html.on("click", '#button-end-action', event => {
SoSUtility.closeAction( event );
});
2021-02-03 15:33:07 +01:00
}
2021-01-17 22:09:01 +01:00
}