chore:fix my merge mistake

This commit is contained in:
WinterMyst
2024-09-22 16:28:59 +02:00
parent e3d83ce4bd
commit ec6d9a881e
30 changed files with 1716 additions and 0 deletions

View File

@ -0,0 +1,59 @@
/**
* Extend the base Actor document by defining a custom roll data structure which is ideal for the Simple system.
* @extends {Actor}
*/
export class KidsOnBroomsActor extends Actor {
/**
* Override getRollData() that's supplied to rolls.
*/
getRollData() {
let data = { ...this.system };
// Wand bonuses
data.wandBonus = {
wood: this._getWandBonus(this.system.wand.wood),
core: this._getWandBonus(this.system.wand.core)
};
return data;
}
_getWandBonus(type) {
const bonuses = {
"Wisteria": { stat: "brains", bonus: 1 },
"Hawthorn": { stat: "brains", bonus: 1 },
"Pine": { stat: "brawn", bonus: 1 },
"Oak": { stat: "brawn", bonus: 1 },
"Crabapple": { stat: "fight", bonus: 1 },
"Dogwood": { stat: "fight", bonus: 1 },
"Birch": { stat: "flight", bonus: 1 },
"Bamboo": { stat: "flight", bonus: 1 },
"Ironwood": { stat: "grit", bonus: 1 },
"Maple": { stat: "grit", bonus: 1 },
"Lilac": { stat: "charm", bonus: 1 },
"Cherry": { stat: "charm", bonus: 1 },
"Parchment": { stat: "brains", bonus: 1 },
"Phoenix Feather": { stat: "brains", bonus: 1 },
"Owl Feather": { stat: "brains", bonus: 1 },
"Gorilla Fur": { stat: "brawn", bonus: 1 },
"Ogres Fingernail": { stat: "brawn", bonus: 1 },
"Hippos Tooth": { stat: "brawn", bonus: 1 },
"Dragons Heartstring": { stat: "fight", bonus: 1 },
"Wolfs Tooth": { stat: "fight", bonus: 1 },
"Elks Antler": { stat: "fight", bonus: 1 },
"Hawks Feather": { stat: "flight", bonus: 1 },
"Bats Bone": { stat: "flight", bonus: 1 },
"Changelings Hair": { stat: "charm", bonus: 1 },
"Gold": { stat: "charm", bonus: 1 },
"Mirror": { stat: "charm", bonus: 1 },
"Steel": { stat: "grit", bonus: 1 },
"Diamond": { stat: "grit", bonus: 1 },
"Lions Mane": { stat: "grit", bonus: 1 }
};
return bonuses[type] || { stat: "", bonus: 0 };
}
}

View File

@ -0,0 +1,7 @@
export const KIDSONBROOMS = {};
// Define constants here, such as:
KIDSONBROOMS.foobar = {
'bas': 'KIDSONBROOMS.bas',
'bar': 'KIDSONBROOMS.bar'
};

View File

@ -0,0 +1,15 @@
/**
* Define a set of template paths to pre-load
* Pre-loaded templates are compiled and cached for fast access when rendering
* @return {Promise}
*/
export const preloadHandlebarsTemplates = async function() {
return loadTemplates([
// Actor partials.
"systems/kids-on-brooms/templates/actor/parts/actor-features.html",
"systems/kids-on-brooms/templates/actor/parts/actor-adversity.html",
"systems/kids-on-brooms/templates/actor/parts/actor-stats.html",
"systems/kids-on-brooms/templates/actor/parts/actor-npc-stats.html",
]);
};

333
module/kidsonbrooms.mjs Normal file
View File

@ -0,0 +1,333 @@
// Import document classes.
import { KidsOnBroomsActor } from "./documents/actor.mjs";
// Import sheet classes.
import { KidsOnBroomsActorSheet } from "./sheets/actor-sheet.mjs";
// Import helper/utility classes and constants.
import { preloadHandlebarsTemplates } from "./helpers/templates.mjs";
import { KIDSONBROOMS } from "./helpers/config.mjs";
/* -------------------------------------------- */
/* Init Hook */
/* -------------------------------------------- */
Hooks.once('init', async function() {
// Register the helper
Handlebars.registerHelper('capitalizeFirst', function(string) {
if (typeof string === 'string') {
return string.charAt(0).toUpperCase() + string.slice(1);
}
return '';
});
// Add utility classes and functions to the global game object so that they're more easily
// accessible in global contexts.
game.kidsonbrooms = {
KidsOnBroomsActor,
_onTakeAdversityToken: _onTakeAdversityToken, // Add the function to the global object
_onSpendAdversityTokens: _onSpendAdversityTokens // Add the function to the global object
};
// Add custom constants for configuration.
CONFIG.KIDSONBROOMS = KIDSONBROOMS;
/**
* Set an initiative formula for the system
* @type {String}
*/
CONFIG.Combat.initiative = {
formula: "1d20",
decimals: 2
};
// Define custom Document classes
CONFIG.Actor.documentClass = KidsOnBroomsActor;
// Register sheet application classes
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("kids-on-brooms", KidsOnBroomsActorSheet, { makeDefault: true });
//If there is a new chat message that is a roll we add the adversity token controls
Hooks.on("renderChatMessage", (message, html, messageData) => {
const adversityControls = html.find('.adversity-controls');
if (adversityControls.length > 0) {
const messageToEdit = adversityControls.data("roll-id");
// Bind event listeners for the controls
adversityControls.find(".take-adversity").off("click").click((event) => {
const actorId = event.currentTarget.dataset.actorId;
const actor = game.actors.get(actorId);
// Check if the current user owns the actor - They can not claim if they are not
if (!actor.testUserPermission(game.user, "owner")) {
ui.notifications.warn("You don't own this character and cannot take adversity tokens.");
return;
}
// Check if the token has already been claimed -- Contigency if the button somehow activates again
if (message.getFlag("kids-on-brooms", "tokenClaimed")) {
ui.notifications.warn("This adversity token has already been claimed.");
return;
}
_onTakeAdversityToken(event, actor);
if (game.user.isGM) {
let tokenControls = game.messages.get(message.id);
console.log(tokenControls);
// Update the chat message content with the button disabled and text changed
const updatedContent = tokenControls.content.replace(
`<button class="take-adversity" data-actor-id="${actor.id}">Take Adversity Token</button>`,
`<button class="take-adversity" data-actor-id="${actor.id}" disabled>Token claimed</button>`
);
console.log("Removing Button");
// Update the message content
tokenControls.update({ content: updatedContent });
// Set the flag on the chat message to indicate that the token has been claimed
tokenControls.setFlag("kids-on-brooms", "tokenClaimed", true);
} else {
// Emit a socket request to update the message to show that the token has been claimed
game.socket.emit('system.kids-on-brooms', {
action: "takeToken",
messageID: message.id,
actorID: actor.id,
});
}
console.log("Send socket message for taking a token");
});
adversityControls.find(".spend-adversity").off("click").click((event) => {
//This entails a lot more, so I offloaded it to a new function
_onSpendAdversityTokens(event, messageToEdit);
});
}
});
// Preload Handlebars templates.
return preloadHandlebarsTemplates();
});
/***
* This handles the incoming socket requests.
* If a player wants to spend tokens on another players roll the gm has to approve first
* if a player wants to claim a token we will update the message since they do not have the permissions
*/
Hooks.once('ready', function() {
game.socket.on('system.kids-on-brooms', async (data) => {
console.log("Socket data received:", data);
if (data.action === "spendTokens") {
console.log(`Request to spend tokens: ${data.tokensToSpend} tokens for ${data.rollActorId} by ${data.spendingActorId}`);
// Only handle the request if the GM is logged in
if (!game.user.isGM) {
console.log("Not GM, ignoring the token spend request.");
return;
}
// The actor who made the roll
const rollActor = game.actors.get(data.rollActorId);
// The actor who is spending tokens
const spendingActor = game.actors.get(data.spendingActorId);
//If these for some reason do not exist
if (!rollActor || !spendingActor) {
console.warn("Actor not found:", data.rollActorId, data.spendingActorId);
return;
}
// Create a confirmation dialog for the GM
new Dialog({
title: "Approve Adversity Token Spending?",
content: `<p>${spendingActor.name} wants to spend ${data.tokenCost} adversity tokens on ${rollActor.name}'s roll to increase it by ${data.tokensToSpend}. Approve?</p>`,
buttons: {
yes: {
label: "Yes",
callback: async () => {
const currentTokens = spendingActor.system.adversityTokens || 0;
// Update the spending actor's adversity token count
await spendingActor.update({ "system.adversityTokens": currentTokens - data.tokenCost });
// Modify the roll message with the new total
await _updateRollMessage(data.rollMessageId, data.tokensToSpend, false);
console.log(`${spendingActor.name} spent ${data.tokensToSpend} tokens, updated roll total to ${roll.cumulativeTotal}`);
ui.notifications.info(`${spendingActor.name} successfully spent ${data.tokensToSpend} tokens.`);
}
},
no: {
label: "No",
callback: () => {
ui.notifications.info(`The GM denied ${spendingActor.name}'s request to spend tokens.`);
}
}
},
default: "yes"
}).render(true);
} else if (data.action === "takeToken") {
// Only handle the request if the GM is logged in
if (!game.user.isGM) {
console.log("Not GM, ignoring the token spend request.");
return;
}
let tokenControls = game.messages.get(data.messageID);
console.log(tokenControls);
// Update the chat message content with the button disabled and text changed
const updatedContent = tokenControls.content.replace(
`<button class="take-adversity" data-actor-id="${data.actorID}">Take Adversity Token</button>`,
`<button class="take-adversity" data-actor-id="${data.actorID}" disabled>Token claimed</button>`
);
console.log("Removing Button");
// Update the message content
tokenControls.update({ content: updatedContent });
// Set the flag on the chat message to indicate that the token has been claimed
tokenControls.setFlag("kids-on-brooms", "tokenClaimed", true);
}
});
});
/***
* This function adds the adversity token to the actor that made the roll and logs it
*
* @param {Event} e - The button click event
* @param {Actor} actor - The actor object that made the roll
*/
async function _onTakeAdversityToken(e, actor) {
e.preventDefault();
// Get the chat message ID (assuming it's stored in the dataset)
const messageId = e.currentTarget.closest('.message').dataset.messageId;
const message = game.messages.get(messageId);
// Add an adversity token to the actor
const currentTokens = actor.system.adversityTokens || 0;
await actor.update({ "system.adversityTokens": currentTokens + 1 });
// Notify the user
ui.notifications.info(`You gained 1 adversity token.`);
console.log(`Gave one adversity token to ${actor.id}`)
}
/***
* This function allows players to spend tokens to change a roll. This will automatically be calculated in their sheet
*
*/
async function _onSpendAdversityTokens(e, rollMessageId) {
e.preventDefault();
// The actor who made the roll
const rollActorId = e.currentTarget.dataset.actorId;
const rollActor = game.actors.get(rollActorId); //technically redundant since it is also done in the main hook, but perfomance is good enuff
// Get the actor of the player who is spending tokens
const spendingPlayerActor = game.actors.get(game.user.character?.id || game.actors.filter(actor => actor.testUserPermission(game.user, "owner"))[0]?.id);
if (!spendingPlayerActor) {
ui.notifications.warn("You don't control any actors.");
return;
}
//Get the tokens to be spend from the input field
const tokenInput = $(e.currentTarget).closest('.adversity-controls').find('.token-input').val();
const tokensToSpend = parseInt(tokenInput, 10);
if (isNaN(tokensToSpend) || tokensToSpend <= 0) {
ui.notifications.warn("Please enter a valid number of tokens.");
return;
}
let tokenCost = tokensToSpend;
// If the player spending tokens is not the owner of the actor who rolled, they spend double
//(note, this is a rule of mine, I have disabled it by default)
if ((!spendingPlayerActor.testUserPermission(game.user, "owner") || spendingPlayerActor.id !== rollActorId) && false) {
tokenCost = tokensToSpend * 2;
}
// Ensure the spending actor has enough adversity tokens
if (spendingPlayerActor.system.adversityTokens < tokenCost) {
ui.notifications.warn(`You do not have enough adversity tokens.`);
return;
}
// Check if the player owns the actor who made the roll
if (spendingPlayerActor.id === rollActorId) {
// The player owns the actor, so they can spend tokens directly without GM approval
const currentTokens = spendingPlayerActor.system.adversityTokens || 0;
// Deduct the tokens from the player
await spendingPlayerActor.update({ "system.adversityTokens": currentTokens - tokenCost });
// Modify the roll message with the new total
await _updateRollMessage(rollMessageId, tokensToSpend, true);
} else {
// The player does not own the actor, so request GM approval to spend the tokens
console.log(`Requesting to spend ${tokensToSpend} tokens for ${rollActor.name} by ${spendingPlayerActor.name} (cost: ${tokenCost})`);
// Emit a socket request to spend tokens
game.socket.emit('system.kids-on-brooms', {
action: "spendTokens",
rollActorId: rollActorId,
spendingActorId: spendingPlayerActor.id, // Send the player's actor who is spending the tokens
tokensToSpend: tokensToSpend,
tokenCost: tokenCost,
rollMessageId: rollMessageId // Pass message ID to update the roll result
});
ui.notifications.info(`Requested to spend ${tokenCost} tokens for ${rollActor.name}`);
}
}
// Helper function to send a new message with the updated roll result
async function _updateRollMessage(rollMessageId, tokensToSpend, isPlayerOfActor) {
const message = game.messages.get(rollMessageId);
if (!message) {
console.error("Message not found with ID:", rollMessageId);
return;
}
// Retrieve current tokens spent from flags, or initialize to 0 if not found
let cumulativeTokensSpent = message.getFlag("kids-on-brooms", "tokensSpent") || 0;
let newTotal = message.getFlag("kids-on-brooms", "newRollTotal") || message.rolls[0].total;
/*if(isPlayerOfActor)
{
// Add the new tokens to the cumulative total
cumulativeTokensSpent += tokensToSpend;
} else {
cumulativeTokensSpent += 2*tokensToSpend;
}*/
cumulativeTokensSpent += tokensToSpend;
newTotal += tokensToSpend;
await message.setFlag("kids-on-brooms", "newRollTotal", newTotal);
// Update the message's flags to store the cumulative tokens spent
await message.setFlag("kids-on-brooms", "tokensSpent", cumulativeTokensSpent);
let newContent = "";
if(cumulativeTokensSpent === 1)
{
newContent = `You have now spent ${cumulativeTokensSpent} token. The new roll total is ${newTotal}.`;
} else {
newContent = `You have now spent ${cumulativeTokensSpent} tokens. The new roll total is ${newTotal}.`;
}
// Create a new chat message to display the updated total
await ChatMessage.create({
speaker: ChatMessage.getSpeaker({ actor: message.speaker.actor }),
content: newContent,
type: CONST.CHAT_MESSAGE_STYLES.OTHER,
});
}

View File

@ -0,0 +1,147 @@
/**
* Extend the basic ActorSheet with some very simple modifications
* @extends {ActorSheet}
*/
export class KidsOnBroomsActorSheet extends ActorSheet {
/** @override */
static get defaultOptions()
{
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["kids-on-brooms", "sheet", "actor"],
width: 800,
height: 800,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "features" }]
});
}
/** @override */
get template()
{
console.log("template", this.actor)
return `systems/kids-on-brooms/templates/actor/actor-${this.actor.type}-sheet.html`;
}
/* -------------------------------------------- */
/** @override */
/** @override */
async getData()
{
// Retrieve the data structure from the base sheet.
const context = super.getData();
// Use a safe clone of the actor data for further operations.
const actorData = this.document.toObject(false);
// Add the actor's data to context.data for easier access, as well as flags.
context.system = actorData.system;
context.flags = actorData.flags;
// Add roll data for TinyMCE editors.
context.rollData = context.actor.getRollData();
// Add roll data for TinyMCE editors.
context.rollData = context.actor.getRollData();
console.log(context);
return context;
}
/* -------------------------------------------- */
/** @override */
activateListeners(html)
{
super.activateListeners(html);
// -------------------------------------------------------------
// Everything below here is only needed if the sheet is editable
if (!this.isEditable) return;
// Rollable abilities.
html.find('.rollable').click(this._onRoll.bind(this));
//If the user changes their wand material save that
html.find('select[name="system.wand.wood"]').change(event => {
const value = event.target.value;
this.actor.update({ "system.wand.wood": value });
});
html.find('select[name="system.wand.core"]').change(event => {
const value = event.target.value;
this.actor.update({ "system.wand.core": value });
});
}
/**
* Handle clickable rolls.
* @param {Event} event The originating click event
* @private
*/
async _onRoll(e) {
e.preventDefault();
const element = e.currentTarget;
const dataset = element.dataset;
// Handle rolls that supply the formula directly
if (dataset.roll) {
let label = dataset.label ? `${dataset.label}` : '';
// Get the roll data and include wand bonuses
let rollData = this.actor.getRollData();
let totalBonus = 0;
console.log(dataset.roll);
// Apply wood bonus if it matches the stat being rolled for
if (rollData.wandBonus.wood.stat === dataset.key) {
totalBonus += rollData.wandBonus.wood.bonus;
}
// Apply core bonus if it matches the stat being rolled for AND it's different from the wood bonus
if (rollData.wandBonus.core.stat === dataset.key && rollData.wandBonus.core.stat !== rollData.wandBonus.wood.stat) {
totalBonus += rollData.wandBonus.core.bonus;
}
let rollFormula = dataset.roll + `+${totalBonus}`;
let roll = new Roll(rollFormula, rollData);
console.log(rollFormula);
console.log(rollData);
// Send the roll message to chat
const rollMessage = await roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
flavor: label,
rollMode: game.settings.get('core', 'rollMode'),
})
// Now send the follow-up message with the adversity controls
await this._sendAdversityControlsMessage(this.actor.id, rollMessage.id);
return roll;
}
}
//This just sends the buttons for the adversity token system
async _sendAdversityControlsMessage(actorId, rollMessageId) {
// Create the content for the adversity controls
const adversityControlsHtml = this._createAdversityControls(actorId, rollMessageId);
// Send the adversity controls as a follow-up message
const controlMessage = await ChatMessage.create({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
content: adversityControlsHtml,
});
return controlMessage;
}
// Create HTML content for adversity controls
_createAdversityControls(actorId, rollMessageId) {
return `
<div class="adversity-controls" data-roll-id="${rollMessageId}">
<button class="take-adversity" data-actor-id="${actorId}">Take Adversity Token</button>
<input type="number" class="token-input" value="1" min="1" />
<button class="spend-adversity" data-actor-id="${actorId}">Spend Adversity Tokens</button>
</div>
`;
}
}