kopparhavet/module/kh-main.js

198 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-11-28 19:12:19 +01:00
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";
2020-12-01 06:30:17 +01:00
import * as migrations from "./helpers/migration-helper.js";
2020-11-28 19:12:19 +01:00
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,
2020-12-01 06:43:25 +01:00
type: String,
2020-11-28 19:12:19 +01:00
});
2020-11-30 06:23:35 +01:00
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();
},
});
//registerFonts();
registerSheets();
if(game.settings.get("kopparhavet", "gameSystem") === "hjaltarnas-tid") {
preloadHandlebarsTemplatesHT();
} else {
preloadHandlebarsTemplates();
}
registerHandlebarsHelpers();
2020-11-28 19:12:19 +01:00
});
Hooks.once("ready", () => {
2020-11-30 06:23:35 +01:00
migrateWorld();
2020-11-28 19:12:19 +01:00
});
/* POPULATE CHARACTER WITH DEFAULT SKILLS */
Hooks.on("createActor", async (actor, options, userId) => KHHooks.onCreateActor(actor, options, userId));
function registerSheets() {
// Register sheet application classes
console.log("Registerting sheets")
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("kopparhavet", ActorSheetKH, { types: ["character"], makeDefault: true });
Actors.registerSheet("kopparhavet", ActorSheetKH, { types: ["adversary"], 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 });
}
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/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",
2020-11-30 06:23:35 +01:00
"systems/kopparhavet/templates/parts/actor/main.html",
2020-11-28 19:12:19 +01:00
"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",
];
return loadTemplates(templatePaths);
}
2020-11-30 06:23:35 +01:00
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/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/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",
];
return loadTemplates(templatePaths);
}
2020-11-28 19:12:19 +01:00
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('plaintextToHTML', function(value) {
// strip tags, add <br/> tags
return new Handlebars.SafeString(value.replace(/(<([^>]+)>)/gi, "").replace(/(?:\r\n|\r|\n)/g, '<br/>'));
});
2020-11-30 06:23:35 +01:00
}
function migrateWorld() {
2020-12-01 06:30:17 +01:00
// Determine whether a system migration is required and feasible
const currentVersion = game.settings.get("kopparhavet", "worldSchemaVersion");
2020-12-01 06:43:25 +01:00
const NEEDS_MIGRATION_VERSION = "0.0.3";
const COMPATIBLE_MIGRATION_VERSION = '0' || isNaN('NaN');
2020-12-01 06:30:17 +01:00
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 }
);
2020-11-30 06:23:35 +01:00
}
2020-12-01 06:30:17 +01:00
migrations.migrateWorld();
}
2020-11-28 19:12:19 +01:00
}