Add crafts into the Combined Hero sheet

This commit is contained in:
Oliver-Akins 2025-02-09 16:48:32 -07:00
parent 1535e07f77
commit f6ff3247e3
6 changed files with 269 additions and 12 deletions

View file

@ -0,0 +1,125 @@
import { deleteItemFromElement, editItemFromElement } from "../utils.mjs";
import { documentSorter, filePath } from "../../consts.mjs";
import { gameTerms } from "../../gameTerms.mjs";
import { GenericAppMixin } from "../GenericApp.mjs";
import { localizer } from "../../utils/Localizer.mjs";
import { Logger } from "../../utils/Logger.mjs";
const { HandlebarsApplicationMixin } = foundry.applications.api;
const { ActorSheetV2 } = foundry.applications.sheets;
const { ContextMenu } = foundry.applications.ui;
export class HeroCraftCardV1 extends GenericAppMixin(HandlebarsApplicationMixin(ActorSheetV2)) {
// #region Options
static DEFAULT_OPTIONS = {
classes: [
`ripcrypt--actor`,
`ripcrypt--HeroCraftCardV1`,
],
position: {
width: `auto`,
height: `auto`,
},
window: {
resizable: false,
},
actions: {
},
form: {
submitOnChange: true,
closeOnSubmit: false,
},
};
static PARTS = {
content: {
template: filePath(`templates/Apps/HeroCraftCardV1/content.hbs`),
},
};
// #endregion
// #region Lifecycle
async _onRender(context, options) {
await super._onRender(context, options);
HeroCraftCardV1._onRender.bind(this)(context, options);
};
static async _onRender(_context, options) {
const {
element = this.element,
isEditable = this.isEditable,
} = options;
new ContextMenu(
element,
`[data-ctx-menu="craft"]`,
[
{
name: localizer(`RipCrypt.common.edit`),
condition: (el) => {
const itemId = el.dataset.itemId;
return isEditable && itemId !== ``;
},
callback: editItemFromElement,
},
{
name: localizer(`RipCrypt.common.delete`),
condition: (el) => {
const itemId = el.dataset.itemId;
return isEditable && itemId !== ``;
},
callback: deleteItemFromElement,
},
],
{ jQuery: false, fixed: true },
);
};
async _preparePartContext(partId, ctx, opts) {
ctx = await super._preparePartContext(partId, ctx, opts);
ctx.actor = this.document;
ctx = await HeroCraftCardV1.prepareCraft(ctx);
Logger.debug(`Context:`, ctx);
return ctx;
};
static async prepareCraft(ctx) {
ctx.craft = {};
const aspects = Object.values(gameTerms.Aspects);
const heroRank = ctx.actor.system.level.rank;
const embeddedCrafts = ctx.actor.itemTypes.craft;
const limit = 4;
for (const aspect of aspects) {
let crafts = [];
for (const craft of embeddedCrafts) {
if (craft.system.aspect !== aspect) { continue };
crafts.push({
uuid: craft.uuid,
name: craft.name,
sort: craft.sort,
use: craft.system.advances[heroRank],
});
};
// Ensure limit isn't surpassed
const length = crafts.length;
if (length >= limit) {
crafts = crafts.slice(0, limit);
} else {
crafts = crafts
.concat(Array(limit - length).fill(null))
.slice(0, limit);
};
ctx.craft[aspect] = crafts.sort(documentSorter);
}
return ctx;
};
// #endregion
// #region Actions
// #endregion
};