foundryvtt-beam-saber/module/blades.js
2020-08-05 14:01:29 +03:00

179 lines
5.4 KiB
JavaScript

/**
* A simple and flexible system for world-building using an arbitrary collection of character and item attributes
* Author: Atropos
* Software License: GNU GPLv3
*/
// Import Modules
import { registerSystemSettings } from "./settings.js";
import { preloadHandlebarsTemplates } from "./blades-templates.js";
import { bladesRoll, simpleRollPopup } from "./blades-roll.js";
import { BladesHelpers } from "./blades-helpers.js";
import { BladesActor } from "./blades-actor.js";
import { BladesItem } from "./blades-item.js";
import { BladesItemSheet } from "./blades-item-sheet.js";
import { BladesActorSheet } from "./blades-actor-sheet.js";
import { BladesCrewSheet } from "./blades-crew-sheet.js";
import * as migrations from "./migration.js";
window.BladesHelpers = BladesHelpers;
/* -------------------------------------------- */
/* Foundry VTT Initialization */
/* -------------------------------------------- */
Hooks.once("init", async function() {
console.log(`Initializing Blades In the Dark System`);
game.blades = {
dice: bladesRoll
}
// Define Roll template.
// CONFIG.Dice.template = "systems/blades-in-the-dark/templates/blades-roll.html"
// CONFIG.Dice.tooltip = "systems/blades-in-the-dark/templates/blades-roll-tooltip.html"
CONFIG.Item.entityClass = BladesItem;
CONFIG.Actor.entityClass = BladesActor;
// Register System Settings
registerSystemSettings();
// Register sheet application classes
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("blades", BladesActorSheet, { types: ["character"], makeDefault: true });
Actors.registerSheet("blades", BladesCrewSheet, { types: ["crew"], makeDefault: true });
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("blades", BladesItemSheet, {makeDefault: true});
preloadHandlebarsTemplates();
// Multiboxes.
Handlebars.registerHelper('multiboxes', function(selected, options) {
let html = options.fn(this);
if (typeof selected != 'undefined') {
selected.forEach(selected_value => {
if (selected_value !== false) {
const escapedValue = RegExp.escape(Handlebars.escapeExpression(selected_value));
const rgx = new RegExp(' value=\"' + escapedValue + '\"');
html = html.replace(rgx, "$& checked=\"checked\"");
}
});
}
return html;
});
// Trauma Counter
Handlebars.registerHelper('traumacounter', function(selected, options) {
let html = options.fn(this);
var count = selected.length;
if (count > 4) count = 4;
const rgx = new RegExp(' value=\"' + count + '\"');
return html.replace(rgx, "$& checked=\"checked\"");
});
// NotEquals handlebar.
Handlebars.registerHelper('noteq', (a, b, options) => {
return (a !== b) ? options.fn(this) : '';
});
// ReputationTurf handlebar.
Handlebars.registerHelper('repturf', (turfs_amount, options) => {
let html = options.fn(this);
var turfs_amount_int = parseInt(turfs_amount);
// Can't be more than 6.
if (turfs_amount_int > 6) {
turfs_amount_int = 6;
}
for (let i = 13 - turfs_amount_int; i <= 12; i++) {
const rgx = new RegExp(' value=\"' + i + '\"');
html = html.replace(rgx, "$& disabled=\"disabled\"");
}
return html;
});
Handlebars.registerHelper('crew_vault_coins', (max_coins, options) => {
let html = options.fn(this);
for (let i = 1; i <= max_coins; i++) {
html += "<input type=\"radio\" id=\"crew-coins-vault-" + i + "\" name=\"data.vault.value\" value=\"" + i + "\"><label for=\"crew-coins-vault-" + i + "\"></label>";
}
return html;
});
Handlebars.registerHelper('crew_experience', (options) => {
let html = options.fn(this);
for (let i = 1; i <= 10; i++) {
html += '<input type="radio" id="crew-experience-' + i + '" name="data.experience" value="' + i + '" dtype="Radio"><label for="crew-experience-' + i + '"></label>';
}
return html;
});
// Enrich the HTML replace /n with <br>
Handlebars.registerHelper('html', (options) => {
let text = options.hash['text'].replace(/\n/g, "<br />");
return new Handlebars.SafeString(text);;
});
});
/**
* Once the entire VTT framework is initialized, check to see if we should perform a data migration
*/
Hooks.once("ready", function() {
// Determine whether a system migration is required
const currentVersion = game.settings.get("bitd", "systemMigrationVersion");
const NEEDS_MIGRATION_VERSION = 1.0;
let needMigration = (currentVersion < NEEDS_MIGRATION_VERSION) || (currentVersion === null);
// Perform the migration
if ( needMigration && game.user.isGM ) {
migrations.migrateWorld();
}
});
/*
* Hooks
*/
Hooks.on("preCreateOwnedItem", (parent_entity, child_data, options, userId) => {
BladesHelpers.removeDuplicatedItemType(child_data, parent_entity);
return true;
});
Hooks.on("createOwnedItem", (parent_entity, child_data, options, userId) => {
BladesHelpers.callItemLogic(child_data, parent_entity);
return true;
});
Hooks.on("deleteOwnedItem", (parent_entity, child_data, options, userId) => {
BladesHelpers.undoItemLogic(child_data, parent_entity);
return true;
});
// getSceneControlButtons
Hooks.on("renderSceneControls", async (app, html) => {
let dice_roller = $('<li class="scene-control" title="Dice Roll"><i class="fas fa-dice"></i></li>');
dice_roller.click(function() {
simpleRollPopup();
});
html.append(dice_roller);
});