Skip to content

Commit

Permalink
Merge pull request #42 from p4535992/master
Browse files Browse the repository at this point in the history
add some helpers
  • Loading branch information
p4535992 authored Jan 17, 2024
2 parents e4ac719 + 6d29fa9 commit 20996e9
Show file tree
Hide file tree
Showing 4 changed files with 957 additions and 170 deletions.
29 changes: 27 additions & 2 deletions src/scripts/API/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { warn } from "../lib/lib.js";
import { RetrieveHelpers } from "../lib/retrieve-helpers.js";
import { MagicItemTab } from "../magicItemtab.js";
import { MagicItemActor } from "../magicitemactor.js";
import { MagicItemSheet } from "../magicitemsheet.js";
Expand All @@ -7,10 +8,22 @@ import { MagicItemSheet } from "../magicitemsheet.js";
* Create a new API class and export it as default
*/
const API = {
actor: function (id) {
return MagicItemActor.get(id);
/**
* Method for create and register a new MagicItemActor.
* @param {string/Actor/UUID} id The actor id to use for retrieve the Actor
* @returns {Actor}
*/
actor: async function (actor) {
const actorTmp = await RetrieveHelpers.getActorAsync(actor);
return MagicItemActor.get(actorTmp);
},

/**
* Method for roll and show a chat message on the chat console
* @param {string} magicItemName The name of the magic item to use
* @param {string} innerChildMagicItemName The name of the inner child "magic item" to use
* @returns {void} Return no response
*/
roll: function (magicItemName, innerChildMagicItemName) {
const speaker = ChatMessage.getSpeaker();
let actor;
Expand All @@ -28,10 +41,22 @@ const API = {
magicItemActor.rollByName(magicItemName, innerChildMagicItemName);
},

/**
* Method to bind magic item behavior to the item sheet
* @param {*} app
* @param {*} html
* @param {*} data
*/
bindItemSheet: function (app, html, data) {
MagicItemTab.bind(app, html, data);
},

/**
* Method to bind magic actor behavior to the item sheet
* @param {*} app
* @param {*} html
* @param {*} data
*/
bindCharacterSheet: function (app, html, data) {
MagicItemSheet.bind(app, html, data);
},
Expand Down
123 changes: 123 additions & 0 deletions src/scripts/lib/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import CONSTANTS from "../constants/constants";

// ================================
// Logger utility
// ================================
export default class Logger {
static get DEBUG() {
return (
game.settings.get(CONSTANTS.MODULE_ID, "debug") ||
game.modules.get("_dev-mode")?.api?.getPackageDebugValue(CONSTANTS.MODULE_ID, "boolean")
);
}
// export let debugEnabled = 0;
// 0 = none, warnings = 1, debug = 2, all = 3

static debug(msg, ...args) {
try {
if (
game.settings.get(CONSTANTS.MODULE_ID, "debug") ||
game.modules.get("_dev-mode")?.api?.getPackageDebugValue(CONSTANTS.MODULE_ID, "boolean")
) {
console.log(`DEBUG | ${CONSTANTS.MODULE_ID} | ${msg}`, ...args);
}
} catch (e) {
console.error(e.message);
}
return msg;
}

static logObject(...args) {
return this.log("", args);
}

static log(message, ...args) {
try {
message = `${CONSTANTS.MODULE_ID} | ${message}`;
console.log(message.replace("<br>", "\n"), ...args);
} catch (e) {
console.error(e.message);
}
return message;
}

static notify(message, ...args) {
try {
message = `${CONSTANTS.MODULE_ID} | ${message}`;
ui.notifications?.notify(message);
console.log(message.replace("<br>", "\n"), ...args);
} catch (e) {
console.error(e.message);
}
return message;
}

static info(info, notify = false, ...args) {
try {
info = `${CONSTANTS.MODULE_ID} | ${info}`;
if (notify) {
ui.notifications?.info(info);
}
console.log(info.replace("<br>", "\n"), ...args);
} catch (e) {
console.error(e.message);
}
return info;
}

static warn(warning, notify = false, ...args) {
try {
warning = `${CONSTANTS.MODULE_ID} | ${warning}`;
if (notify) {
ui.notifications?.warn(warning);
}
console.warn(warning.replace("<br>", "\n"), ...args);
} catch (e) {
console.error(e.message);
}
return warning;
}

static errorObject(...args) {
return this.error("", false, args);
}

static error(error, notify = true, ...args) {
try {
error = `${CONSTANTS.MODULE_ID} | ${error}`;
if (notify) {
ui.notifications?.error(error);
}
console.error(error.replace("<br>", "\n"), ...args);
} catch (e) {
console.error(e.message);
}
return new Error(error.replace("<br>", "\n"));
}

static timelog(message) {
warn(Date.now(), message);
}

static i18n = (key) => {
return game.i18n.localize(key)?.trim();
};

static i18nFormat = (key, data = {}) => {
return game.i18n.format(key, data)?.trim();
};

// setDebugLevel = (debugText): void => {
// debugEnabled = { none: 0, warn: 1, debug: 2, all: 3 }[debugText] || 0;
// // 0 = none, warnings = 1, debug = 2, all = 3
// if (debugEnabled >= 3) CONFIG.debug.hooks = true;
// };

static dialogWarning(message, icon = "fas fa-exclamation-triangle") {
return `<p class="${CONSTANTS.MODULE_ID}-dialog">
<i style="font-size:3rem;" class="${icon}"></i><br><br>
<strong style="font-size:1.2rem;">${CONSTANTS.MODULE_ID}</strong>
<br><br>${message}
</p>`;
}
}
Loading

0 comments on commit 20996e9

Please sign in to comment.