kopparhavet/module/kh-main.js

231 lines
9.5 KiB
JavaScript

import { ItemSheetKH } from "./items/item-sheet.js";
import { ItemKH } from "./items/item.js";
import KHHooks from "./kh-hooks.js";
import { ActorKH } from "./actors/actor.js";
import { ActorSheetKH } from "./actors/actor-sheet.js";
import { KH } from "./kh-config.js";
import * as migrations from "./helpers/migration-helper.js";
Hooks.once("init", () => {
CONFIG.Combat.initiative = { formula: "(@combat.init)d6kh2", decimals: 0 };
CONFIG.Actor.entityClass = ActorKH;
CONFIG.Item.entityClass = ItemKH;
// Give global access to FFG config.
CONFIG.KH = KH;
game.settings.register("kopparhavet", "worldSchemaVersion", {
name: "World Version",
hint: "Used to automatically upgrade worlds data when the system is upgraded.",
scope: "world",
config: true,
default: 0,
type: String,
});
game.settings.register("kopparhavet", "gameSystem", {
name: "Game System",
hint: "Select what game system is being used..",
scope: "world",
config: true,
default: "kopparhavet",
choices: {
"kopparhavet": "Kopparhavets Hjältar",
"hjaltarnas-tid": "Hjältarnas Tid"
},
type: String,
onChange: (rule) => {
window.location.reload();
},
});
game.settings.register("kopparhavet", "applySpellCost", {
name: "Apply Spell Cost",
hint: "Automatically add spell cost to the character when rolled.",
scope: "world",
config: true,
default: false,
type: Boolean,
});
//registerFonts();
registerSheets();
if(game.settings.get("kopparhavet", "gameSystem") === "hjaltarnas-tid") {
preloadHandlebarsTemplatesHT();
} else {
preloadHandlebarsTemplates();
}
registerHandlebarsHelpers();
});
Hooks.once("ready", () => {
migrateWorld();
});
/* POPULATE CHARACTER WITH DEFAULT SKILLS */
Hooks.on("createActor", async (actor, options, userId) => KHHooks.onCreateActor(actor, options, userId));
function registerSheets() {
// Register sheet application classes
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("kopparhavet", ActorSheetKH, { types: ["character"], makeDefault: true });
Actors.registerSheet("kopparhavet", ActorSheetKH, { types: ["adversary"], makeDefault: true });
Actors.registerSheet("kopparhavet", ActorSheetKH, { types: ["ship"], makeDefault: true });
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["weapon"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["armor"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["gear"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["skill"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["spell"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["talent"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["adversaryAttack"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["shiptalent"], makeDefault: true });
Items.registerSheet("kopparhavet", ItemSheetKH, { types: ["shipweapon"], makeDefault: true });
}
function preloadHandlebarsTemplates() {
const templatePaths = [
"systems/kopparhavet/templates/chat/item-card.html",
"systems/kopparhavet/templates/actors/adversary-sheet.html",
"systems/kopparhavet/templates/actors/character-sheet.html",
"systems/kopparhavet/templates/actors/ship-sheet.html",
"systems/kopparhavet/templates/dice/roll.html",
"systems/kopparhavet/templates/items/adversaryAttack-sheet.html",
"systems/kopparhavet/templates/items/armor-sheet.html",
"systems/kopparhavet/templates/items/gear-sheet.html",
"systems/kopparhavet/templates/items/skill-sheet.html",
"systems/kopparhavet/templates/items/spell-sheet.html",
"systems/kopparhavet/templates/items/talent-sheet.html",
"systems/kopparhavet/templates/items/weapon-sheet.html",
"systems/kopparhavet/templates/items/shipweapon-sheet.html",
"systems/kopparhavet/templates/items/shiptalent-sheet.html",
"systems/kopparhavet/templates/parts/actor/main.html",
"systems/kopparhavet/templates/parts/actor/bio.html",
"systems/kopparhavet/templates/parts/actor/combat.html",
"systems/kopparhavet/templates/parts/actor/gear.html",
"systems/kopparhavet/templates/parts/actor/skills.html",
"systems/kopparhavet/templates/parts/actor/talent.html",
"systems/kopparhavet/templates/parts/shared/modifications.html",
"systems/kopparhavet/templates/parts/actor/ship-main.html",
"systems/kopparhavet/templates/parts/actor/ship-combat.html",
];
return loadTemplates(templatePaths);
}
function preloadHandlebarsTemplatesHT() {
const templatePaths = [
"systems/kopparhavet/templates/chat/item-card.html",
"systems/kopparhavet/templates/actors/ht-adversary-sheet.html",
"systems/kopparhavet/templates/actors/ht-character-sheet.html",
"systems/kopparhavet/templates/actors/ht-ship-sheet.html",
"systems/kopparhavet/templates/dice/roll.html",
"systems/kopparhavet/templates/items/adversaryAttack-sheet.html",
"systems/kopparhavet/templates/items/armor-sheet.html",
"systems/kopparhavet/templates/items/gear-sheet.html",
"systems/kopparhavet/templates/items/skill-sheet.html",
"systems/kopparhavet/templates/items/spell-sheet.html",
"systems/kopparhavet/templates/items/talent-sheet.html",
"systems/kopparhavet/templates/items/weapon-sheet.html",
"systems/kopparhavet/templates/items/shipweapon-sheet.html",
"systems/kopparhavet/templates/items/shiptalent-sheet.html",
"systems/kopparhavet/templates/parts/actor/ht-main.html",
"systems/kopparhavet/templates/parts/actor/ht-combat.html",
"systems/kopparhavet/templates/parts/actor/gear.html",
"systems/kopparhavet/templates/parts/actor/ht-skills.html",
"systems/kopparhavet/templates/parts/actor/talent.html",
"systems/kopparhavet/templates/parts/shared/modifications.html",
"systems/kopparhavet/templates/parts/actor/ship-main.html",
"systems/kopparhavet/templates/parts/actor/ship-combat.html",
];
return loadTemplates(templatePaths);
}
function normalize(data, defaultValue) {
if (data) {
return data.toLowerCase();
} else {
return defaultValue;
}
}
function registerHandlebarsHelpers() {
Handlebars.registerHelper("weaponCategory", function (category) {
category = normalize(category, "melee");
switch (category) {
case "melee":
return game.i18n.localize("WEAPON.MELEE");
case "ranged":
return game.i18n.localize("WEAPON.RANGED");
}
});
Handlebars.registerHelper("rollDifficulty", function (difficulty) {
difficulty = normalize(difficulty, "average");
switch (difficulty) {
case "simple":
return game.i18n.localize("DIFFICULTY.SIMPLE");
case "easy":
return game.i18n.localize("DIFFICULTY.EASY");
case "average":
return game.i18n.localize("DIFFICULTY.AVERAGE");
case "hard":
return game.i18n.localize("DIFFICULTY.HARD");
case "daunting":
return game.i18n.localize("DIFFICULTY.DAUNTING");
}
});
Handlebars.registerHelper("skillType", function (skillType) {
skillType = normalize(skillType, "base");
switch (skillType) {
case "base":
return game.i18n.localize("SKILL.BASE");
case "adventure":
return game.i18n.localize("SKILL.ADVENTURE");
case "combat":
return game.i18n.localize("SKILL.COMBAT");
}
});
Handlebars.registerHelper("spellRoll", function (roll) {
roll = normalize(roll, "roll");
switch (roll) {
case "roll":
return game.i18n.localize("SPELL.ROLL");
case "attackroll":
return game.i18n.localize("SPELL.ATTACKROLL");
case "opposite":
return game.i18n.localize("SPELL.OPPOSITE");
case "ritual":
return game.i18n.localize("SPELL.RITUAL");
}
});
Handlebars.registerHelper('plaintextToHTML', function(value) {
// strip tags, add <br/> tags
return new Handlebars.SafeString(value.replace(/(<([^>]+)>)/gi, "").replace(/(?:\r\n|\r|\n)/g, '<br/>'));
});
}
function migrateWorld() {
// Determine whether a system migration is required and feasible
const currentVersion = game.settings.get("kopparhavet", "worldSchemaVersion");
const NEEDS_MIGRATION_VERSION = "0.0.3";
const COMPATIBLE_MIGRATION_VERSION = '0' || isNaN('NaN');
let needMigration = currentVersion < NEEDS_MIGRATION_VERSION || currentVersion === null;
// Perform the migration
if (needMigration && game.user.isGM) {
if (currentVersion && currentVersion < COMPATIBLE_MIGRATION_VERSION) {
ui.notifications.error(
`Your system data is from a version that cannot be reliably migrated to the latest version. The process will be attempted, but errors may occur.`,
{ permanent: true }
);
}
migrations.migrateWorld();
}
}