Skip to content

Commit 21f5b8c

Browse files
authored
Merge pull request #2160 from Roll20/beacon/smartattributes
libSmartAttributes | 0.0.2 | Improve Behavior
2 parents 50625cb + a1c0e0e commit 21f5b8c

File tree

9 files changed

+218
-99
lines changed

9 files changed

+218
-99
lines changed

libSmartAttributes/0.0.1/index.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// libSmartAttributes v0.0.2 by GUD Team | libSmartAttributes provides an interface for managing beacon attributes in a slightly smarter way.
2+
var libSmartAttributes = (function () {
3+
'use strict';
4+
5+
async function getAttribute(characterId, name, type = "current") {
6+
// Try for legacy attribute first
7+
const legacyAttr = findObjs({
8+
_type: "attribute",
9+
_characterid: characterId,
10+
name: name,
11+
})[0];
12+
if (legacyAttr) {
13+
return legacyAttr.get(type);
14+
}
15+
// Then try for the beacon computed
16+
const beaconAttr = await getSheetItem(characterId, name, type);
17+
if (beaconAttr !== null && beaconAttr !== undefined) {
18+
return beaconAttr;
19+
}
20+
// Then try for the user attribute
21+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
22+
if (userAttr !== null && userAttr !== undefined) {
23+
return userAttr;
24+
}
25+
log(`Attribute ${name} not found on character ${characterId}`);
26+
return undefined;
27+
}
28+
async function setAttribute(characterId, name, value, type = "current", options) {
29+
// Try for legacy attribute first
30+
const legacyAttr = findObjs({
31+
_type: "attribute",
32+
_characterid: characterId,
33+
name: name,
34+
})[0];
35+
if (legacyAttr && options?.setWithWorker) {
36+
legacyAttr.setWithWorker({ [type]: value });
37+
return;
38+
}
39+
else if (legacyAttr) {
40+
legacyAttr.set({ [type]: value });
41+
return;
42+
}
43+
// Then try for the beacon computed
44+
const beaconAttr = await getSheetItem(characterId, name, type);
45+
if (beaconAttr !== null && beaconAttr !== undefined) {
46+
setSheetItem(characterId, name, value);
47+
return;
48+
}
49+
// Guard against creating user attributes if noCreate is set
50+
if (options?.noCreate) {
51+
log(`Attribute ${name} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
52+
return;
53+
}
54+
// Then default to a user attribute
55+
setSheetItem(characterId, `user.${name}`, value, type);
56+
return;
57+
}
58+
async function deleteAttribute(characterId, name, type = "current") {
59+
// Try for legacy attribute first
60+
const legacyAttr = findObjs({
61+
_type: "attribute",
62+
_characterid: characterId,
63+
name: name,
64+
})[0];
65+
if (legacyAttr) {
66+
legacyAttr.remove();
67+
return;
68+
}
69+
// Then try for the beacon computed
70+
const beaconAttr = await getSheetItem(characterId, name, type);
71+
if (beaconAttr !== null && beaconAttr !== undefined) {
72+
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
73+
setSheetItem(characterId, name, undefined, type);
74+
return;
75+
}
76+
// Then try for the user attribute
77+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
78+
if (userAttr !== null && userAttr !== undefined) {
79+
log(`Deleting user attribute ${name} on character ${characterId}`);
80+
setSheetItem(characterId, `user.${name}`, undefined, type);
81+
return;
82+
}
83+
log(`Attribute ${type} not found on character ${characterId}, nothing to delete`);
84+
return;
85+
}
86+
var index = {
87+
getAttribute,
88+
setAttribute,
89+
deleteAttribute,
90+
};
91+
92+
return index;
93+
94+
})();

libSmartAttributes/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"name": "lib-smart-attributes",
33
"version": "1.0.0",
44
"type": "module",
5-
"main": "src/index.ts",
6-
"types": "0.0.1/index.d.ts",
5+
"exports": {
6+
".": "./src/index.ts",
7+
"./types": "./src/types.d.ts"
8+
},
79
"scripts": {
810
"lint": "eslint",
911
"lint:fix": "eslint --fix",

libSmartAttributes/rollup.config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ export default defineConfig({
99

1010
output: {
1111
file: `${json.version}/${json.name}.js`,
12-
format: "iife",
1312
name: json.name,
13+
format: "iife",
1414
sourcemap: false,
1515
banner: `// ${json.name} v${json.version} by ${json.authors} | ${json.description}`,
1616
},
1717

1818
plugins: [
1919
del({ targets: `${json.version}/*`, runOnce: true }),
20-
typescript({
21-
declaration: true,
22-
declarationDir: `${json.version}`,
23-
}),
20+
typescript({}),
2421
]
2522
});

libSmartAttributes/script.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libSmartAttributes",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "libSmartAttributes provides an interface for managing beacon attributes in a slightly smarter way.",
55
"authors": "GUD Team",
66
"roll20userid": "8705027",
@@ -9,5 +9,7 @@
99
"conflicts": [],
1010
"script": "libSmartAttributes.js",
1111
"useroptions": [],
12-
"previousversions": []
12+
"previousversions": [
13+
"0.0.1"
14+
]
1315
}

libSmartAttributes/src/index.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
type AttributeType = "current" | "max";
22

3-
async function getAttribute(characterId: string, name: string, type: AttributeType = "current") {
3+
async function getAttribute(
4+
characterId: string,
5+
name: string,
6+
type: AttributeType = "current"
7+
) {
48
// Try for legacy attribute first
59
const legacyAttr = findObjs({
610
_type: "attribute",
@@ -13,13 +17,13 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
1317
}
1418

1519
// Then try for the beacon computed
16-
const beaconAttr = await getSheetItem(characterId, name);
20+
const beaconAttr = await getSheetItem(characterId, name, type);
1721
if (beaconAttr !== null && beaconAttr !== undefined) {
1822
return beaconAttr;
1923
}
2024

2125
// Then try for the user attribute
22-
const userAttr = await getSheetItem(characterId, `user.${name}`);
26+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
2327
if (userAttr !== null && userAttr !== undefined) {
2428
return userAttr;
2529
}
@@ -30,9 +34,16 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
3034

3135
type SetOptions = {
3236
setWithWorker?: boolean;
37+
noCreate?: boolean;
3338
};
3439

35-
async function setAttribute(characterId: string, name: string, value: unknown, type: AttributeType = "current", options?: SetOptions) {
40+
async function setAttribute(
41+
characterId: string,
42+
name: string,
43+
value: unknown,
44+
type: AttributeType = "current",
45+
options?: SetOptions
46+
) {
3647
// Try for legacy attribute first
3748
const legacyAttr = findObjs({
3849
_type: "attribute",
@@ -41,24 +52,34 @@ async function setAttribute(characterId: string, name: string, value: unknown, t
4152
})[0];
4253

4354
if (legacyAttr && options?.setWithWorker) {
44-
return legacyAttr.setWithWorker({ [type]: value });
55+
legacyAttr.setWithWorker({ [type]: value });
56+
return;
4557
}
4658

4759
else if (legacyAttr) {
48-
return legacyAttr.set({ [type]: value });
60+
legacyAttr.set({ [type]: value });
61+
return;
4962
}
5063

5164
// Then try for the beacon computed
52-
const beaconAttr = await getSheetItem(characterId, name);
65+
const beaconAttr = await getSheetItem(characterId, name, type);
5366
if (beaconAttr !== null && beaconAttr !== undefined) {
54-
return setSheetItem(characterId, name, value);
67+
setSheetItem(characterId, name, value);
68+
return;
69+
}
70+
71+
// Guard against creating user attributes if noCreate is set
72+
if (options?.noCreate) {
73+
log(`Attribute ${name} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
74+
return;
5575
}
5676

5777
// Then default to a user attribute
58-
return setSheetItem(characterId, `user.${name}`, value);
78+
setSheetItem(characterId, `user.${name}`, value, type);
79+
return;
5980
};
6081

61-
async function deleteAttribute(characterId: string, name: string) {
82+
async function deleteAttribute(characterId: string, name: string, type: AttributeType = "current") {
6283
// Try for legacy attribute first
6384
const legacyAttr = findObjs({
6485
_type: "attribute",
@@ -67,15 +88,28 @@ async function deleteAttribute(characterId: string, name: string) {
6788
})[0];
6889

6990
if (legacyAttr) {
70-
return legacyAttr.remove();
91+
legacyAttr.remove();
92+
return;
7193
}
7294

7395
// Then try for the beacon computed
74-
const beaconAttr = await getSheetItem(characterId, name);
96+
const beaconAttr = await getSheetItem(characterId, name, type);
7597
if (beaconAttr !== null && beaconAttr !== undefined) {
7698
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
77-
return setSheetItem(characterId, name, undefined);
99+
setSheetItem(characterId, name, undefined, type);
100+
return;
78101
}
102+
103+
// Then try for the user attribute
104+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
105+
if (userAttr !== null && userAttr !== undefined) {
106+
log(`Deleting user attribute ${name} on character ${characterId}`);
107+
setSheetItem(characterId, `user.${name}`, undefined, type);
108+
return;
109+
}
110+
111+
log(`Attribute ${type} not found on character ${characterId}, nothing to delete`);
112+
return;
79113
};
80114

81115
export default {

libSmartAttributes/src/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare namespace SmartAttributes {
2+
function getAttribute(characterId: string, name: string, type?: "current" | "max"): Promise<string | number | undefined>;
3+
function setAttribute(characterId: string, name: string, value: unknown, type?: "current" | "max", options?: { setWithWorker?: boolean, noCreate?: boolean }): Promise<void>;
4+
function deleteAttribute(characterId: string, name: string, type?: "current" | "max", options?: { setWithWorker?: boolean }): Promise<void>;
5+
}

0 commit comments

Comments
 (0)