diff --git a/src/module/entities/TwodsixActor.ts b/src/module/entities/TwodsixActor.ts index 7194a1a51..638219ed1 100644 --- a/src/module/entities/TwodsixActor.ts +++ b/src/module/entities/TwodsixActor.ts @@ -7,8 +7,16 @@ import {CharacteristicType, UpdateData} from "../../types/twodsix"; import {TWODSIX} from "../config"; import { TwodsixRollSettings } from "../utils/TwodsixRollSettings"; import { TwodsixDiceRoll } from "../utils/TwodsixDiceRoll"; +import TwodsixItem from "./TwodsixItem"; export default class TwodsixActor extends Actor { + public static async create(data: Record, options?: Record): Promise { + const actor = await super.create(data, options) as TwodsixActor; + if (actor.data.type == "traveller") { + actor.createUntrainedSkill(); + } + return actor; + } /** * Augment the basic actor data with additional dynamic data. @@ -133,4 +141,20 @@ export default class TwodsixActor extends Actor { await this.update(updateData); } + public getUntrainedSkill():TwodsixItem { + return this.getOwnedItem(this.data.data.untrainedSkill) as TwodsixItem; + } + + private async createUntrainedSkill() { + const data = { + "name": game.i18n.localize("TWODSIX.Actor.Skills.Untrained") + "WAZZAA", + "data": { + "value": game.settings.get('twodsix', 'untrainedSkillValue') + }, + "type": "skills", + "flags": {'twodsix.hide': true} + }; + const untrainedSkill = await this.createOwnedItem(data) as TwodsixItem; + await this.update({"data.untrainedSkill": untrainedSkill._id}); + } } diff --git a/src/module/handlebars.ts b/src/module/handlebars.ts index 3b069c605..1f17ac502 100644 --- a/src/module/handlebars.ts +++ b/src/module/handlebars.ts @@ -88,6 +88,15 @@ export default function registerHandlebarsHelpers():void { return TwodsixItem.burstBonusDamage(number); }); + Handlebars.registerHelper('twodsix_filterSkills', (skill) => { + return !skill.data.flags.twodsix?.hide && skill.type === "skills"; + }); + + // Handy for debugging + Handlebars.registerHelper('json', function(context) { + return JSON.stringify(context); + }); + //From https://discord.com/channels/732325252788387980/732328233630171188/790507540818690068 //Not used yet Handlebars.registerHelper("iff", function (v1, operator, v2, options) { diff --git a/src/module/sheets/AbstractTwodsixActorSheet.ts b/src/module/sheets/AbstractTwodsixActorSheet.ts index b28929622..ff14f7b40 100644 --- a/src/module/sheets/AbstractTwodsixActorSheet.ts +++ b/src/module/sheets/AbstractTwodsixActorSheet.ts @@ -109,6 +109,7 @@ export abstract class AbstractTwodsixActorSheet extends ActorSheet { itemData.data.value = String(0); } } + itemData.data.skill = this.actor.getUntrainedSkill()._id; // Finally, create the item! return this.actor.createOwnedItem(itemData); } diff --git a/src/module/sheets/TwodsixActorSheet.ts b/src/module/sheets/TwodsixActorSheet.ts index ed88b4e17..edf9712e4 100644 --- a/src/module/sheets/TwodsixActorSheet.ts +++ b/src/module/sheets/TwodsixActorSheet.ts @@ -142,29 +142,9 @@ export class TwodsixActorSheet extends AbstractTwodsixActorSheet { * @private */ private async _onRollUntrained(event:Event, showTrowDiag:boolean):Promise { - const data = { - "name": game.i18n.localize("TWODSIX.Actor.Skills.Untrained"), - "data": { - "value": game.settings.get('twodsix', 'untrainedSkillValue') - }, - "type": "skills" - }; - - // this feels very hacky, but creating temporary TwodsixItems only returns a plain js object.. - // however the rest of the handling of this roll then behaves exactly like a normal skill, so it feels - // somewhat worth it. - let skill:TwodsixItem; - try { - skill = TwodsixItem.createOwned(data, this.actor) as TwodsixItem; - await skill.skillRoll(showTrowDiag); - } finally { - if (skill) { - skill.delete(); - } - } + this.actor.getUntrainedSkill().skillRoll(showTrowDiag); } - /** * Handle clickable damage rolls. * @param {Event} event The originating click event diff --git a/src/module/utils/TwodsixDiceRoll.ts b/src/module/utils/TwodsixDiceRoll.ts index 030f5da28..991c9c758 100644 --- a/src/module/utils/TwodsixDiceRoll.ts +++ b/src/module/utils/TwodsixDiceRoll.ts @@ -16,6 +16,20 @@ export class TwodsixDiceRoll { effect: number; roll: Roll; + constructor (settings: TwodsixRollSettings, actor: TwodsixActor, skill: TwodsixItem=null, item: TwodsixItem=null) { + this.settings = settings; + this.actor = actor; + this.skill = skill; + this.item = item; + + this.createRoll(); + + this.naturalTotal = this.roll.dice[0].results.reduce((total:number, dice) => { + return dice["active"] ? total + dice["result"] : total; + }, 0); + + this.calculateEffect(); + } private createRoll(): void { const difficultiesAsTargetNumber = game.settings.get('twodsix', 'difficultiesAsTargetNumber'); @@ -50,23 +64,7 @@ export class TwodsixDiceRoll { data["difficultyMod"] = this.settings.difficulty.mod; } - this.roll = new Roll(formula, data); - this.roll.roll(); - } - - constructor (settings: TwodsixRollSettings, actor: TwodsixActor, skill: TwodsixItem=null, item: TwodsixItem=null) { - this.settings = settings; - this.actor = actor; - this.skill = skill; - this.item = item; - - this.createRoll(); - - this.naturalTotal = this.roll.dice[0].results.reduce((total:number, dice) => { - return dice["active"] ? total + dice["result"] : total; - }, 0); - - this.calculateEffect(); + this.roll = new Roll(formula, data).roll(); } public getCrit(): Crit { diff --git a/static/template.json b/static/template.json index 1e4f46a6a..629718286 100644 --- a/static/template.json +++ b/static/template.json @@ -32,6 +32,7 @@ "contacts": "", "allies": "", "enemies": "", + "untrainedSkill": null, "description": "", "bio": "", "notes": "", diff --git a/static/templates/items/parts/common-parts.html b/static/templates/items/parts/common-parts.html index c70c514bd..1e8d07418 100644 --- a/static/templates/items/parts/common-parts.html +++ b/static/templates/items/parts/common-parts.html @@ -38,9 +38,9 @@