Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions libSmartAttributes/0.0.1/index.d.ts

This file was deleted.

97 changes: 97 additions & 0 deletions libSmartAttributes/0.0.2/libSmartAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// libSmartAttributes v0.0.2 by GUD Team | libSmartAttributes provides an interface for managing beacon attributes in a slightly smarter way.
var libSmartAttributes = (function () {
'use strict';

async function getAttribute(characterId, name, type = "current") {
// Try for legacy attribute first
const legacyAttr = findObjs({
_type: "attribute",
_characterid: characterId,
name: name,
})[0];
if (legacyAttr) {
return legacyAttr.get(type);
}
// Then try for the beacon computed
const beaconName = type === "current" ? name : `${name}_max`;
const beaconAttr = await getSheetItem(characterId, beaconName);
if (beaconAttr !== null && beaconAttr !== undefined) {
return beaconAttr;
}
// Then try for the user attribute
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
if (userAttr !== null && userAttr !== undefined) {
return userAttr;
}
log(`Attribute ${beaconName} not found on character ${characterId}`);
return undefined;
}
async function setAttribute(characterId, name, value, type = "current", options) {
// Try for legacy attribute first
const legacyAttr = findObjs({
_type: "attribute",
_characterid: characterId,
name: name,
})[0];
if (legacyAttr && options?.setWithWorker) {
legacyAttr.setWithWorker({ [type]: value });
return;
}
else if (legacyAttr) {
legacyAttr.set({ [type]: value });
return;
}
// Then try for the beacon computed
const beaconName = type === "current" ? name : `${name}_max`;
const beaconAttr = await getSheetItem(characterId, beaconName);
if (beaconAttr !== null && beaconAttr !== undefined) {
setSheetItem(characterId, beaconName, value);
return;
}
// Guard against creating user attributes if noCreate is set
if (options?.noCreate) {
log(`Attribute ${beaconName} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
return;
}
// Then default to a user attribute
setSheetItem(characterId, `user.${beaconName}`, value);
return;
}
async function deleteAttribute(characterId, name, type = "current") {
// Try for legacy attribute first
const legacyAttr = findObjs({
_type: "attribute",
_characterid: characterId,
name: name,
})[0];
if (legacyAttr) {
legacyAttr.remove();
return;
}
// Then try for the beacon computed
const beaconName = type === "current" ? name : `${name}_max`;
const beaconAttr = await getSheetItem(characterId, beaconName);
if (beaconAttr !== null && beaconAttr !== undefined) {
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
setSheetItem(characterId, name, undefined);
return;
}
// Then try for the user attribute
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
if (userAttr !== null && userAttr !== undefined) {
log(`Deleting user attribute ${name} on character ${characterId}`);
setSheetItem(characterId, `user.${beaconName}`, undefined);
return;
}
log(`Attribute ${beaconName} not found on character ${characterId}, nothing to delete`);
return;
}
var index = {
getAttribute,
setAttribute,
deleteAttribute,
};

return index;

})();
6 changes: 4 additions & 2 deletions libSmartAttributes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "lib-smart-attributes",
"version": "1.0.0",
"type": "module",
"main": "src/index.ts",
"types": "0.0.1/index.d.ts",
"exports": {
".": "./src/index.ts",
"./types": "./src/types.d.ts"
},
"scripts": {
"lint": "eslint",
"lint:fix": "eslint --fix",
Expand Down
7 changes: 2 additions & 5 deletions libSmartAttributes/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ export default defineConfig({

output: {
file: `${json.version}/${json.name}.js`,
format: "iife",
name: json.name,
format: "iife",
sourcemap: false,
banner: `// ${json.name} v${json.version} by ${json.authors} | ${json.description}`,
},

plugins: [
del({ targets: `${json.version}/*`, runOnce: true }),
typescript({
declaration: true,
declarationDir: `${json.version}`,
}),
typescript({}),
]
});
6 changes: 4 additions & 2 deletions libSmartAttributes/script.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libSmartAttributes",
"version": "0.0.1",
"version": "0.0.2",
"description": "libSmartAttributes provides an interface for managing beacon attributes in a slightly smarter way.",
"authors": "GUD Team",
"roll20userid": "8705027",
Expand All @@ -9,5 +9,7 @@
"conflicts": [],
"script": "libSmartAttributes.js",
"useroptions": [],
"previousversions": []
"previousversions": [
"0.0.1"
]
}
51 changes: 39 additions & 12 deletions libSmartAttributes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
}

// Then try for the beacon computed
const beaconAttr = await getSheetItem(characterId, name);
const beaconName = type === "current" ? name : `${name}_max`;
const beaconAttr = await getSheetItem(characterId, beaconName);
if (beaconAttr !== null && beaconAttr !== undefined) {
return beaconAttr;
}

// Then try for the user attribute
const userAttr = await getSheetItem(characterId, `user.${name}`);
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
if (userAttr !== null && userAttr !== undefined) {
return userAttr;
}

log(`Attribute ${name} not found on character ${characterId}`);
log(`Attribute ${beaconName} not found on character ${characterId}`);
return undefined;
};

type SetOptions = {
setWithWorker?: boolean;
noCreate?: boolean;
};

async function setAttribute(characterId: string, name: string, value: unknown, type: AttributeType = "current", options?: SetOptions) {
Expand All @@ -41,24 +43,35 @@ async function setAttribute(characterId: string, name: string, value: unknown, t
})[0];

if (legacyAttr && options?.setWithWorker) {
return legacyAttr.setWithWorker({ [type]: value });
legacyAttr.setWithWorker({ [type]: value });
return;
}

else if (legacyAttr) {
return legacyAttr.set({ [type]: value });
legacyAttr.set({ [type]: value });
return;
}

// Then try for the beacon computed
const beaconAttr = await getSheetItem(characterId, name);
const beaconName = type === "current" ? name : `${name}_max`;
const beaconAttr = await getSheetItem(characterId, beaconName);
if (beaconAttr !== null && beaconAttr !== undefined) {
return setSheetItem(characterId, name, value);
setSheetItem(characterId, beaconName, value);
return;
}

// Guard against creating user attributes if noCreate is set
if (options?.noCreate) {
log(`Attribute ${beaconName} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
return;
}

// Then default to a user attribute
return setSheetItem(characterId, `user.${name}`, value);
setSheetItem(characterId, `user.${beaconName}`, value);
return;
};

async function deleteAttribute(characterId: string, name: string) {
async function deleteAttribute(characterId: string, name: string, type: AttributeType = "current") {
// Try for legacy attribute first
const legacyAttr = findObjs({
_type: "attribute",
Expand All @@ -67,15 +80,29 @@ async function deleteAttribute(characterId: string, name: string) {
})[0];

if (legacyAttr) {
return legacyAttr.remove();
legacyAttr.remove();
return;
}

// Then try for the beacon computed
const beaconAttr = await getSheetItem(characterId, name);
const beaconName = type === "current" ? name : `${name}_max`;
const beaconAttr = await getSheetItem(characterId, beaconName);
if (beaconAttr !== null && beaconAttr !== undefined) {
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
return setSheetItem(characterId, name, undefined);
setSheetItem(characterId, name, undefined);
return;
}

// Then try for the user attribute
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
if (userAttr !== null && userAttr !== undefined) {
log(`Deleting user attribute ${name} on character ${characterId}`);
setSheetItem(characterId, `user.${beaconName}`, undefined);
return;
}

log(`Attribute ${beaconName} not found on character ${characterId}, nothing to delete`);
return;
};

export default {
Expand Down
5 changes: 5 additions & 0 deletions libSmartAttributes/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare namespace SmartAttributes {
function getAttribute(characterId: string, name: string, type?: "current" | "max"): Promise<string | number | undefined>;
function setAttribute(characterId: string, name: string, value: unknown, type?: "current" | "max", options?: { setWithWorker?: boolean, noCreate?: boolean }): Promise<void>;
function deleteAttribute(characterId: string, name: string, type?: "current" | "max", options?: { setWithWorker?: boolean }): Promise<void>;
}
Loading
Loading