Skip to content

Commit 1839ced

Browse files
author
Nic Bradley
committed
Updating
1 parent aad4fea commit 1839ced

File tree

9 files changed

+179
-64
lines changed

9 files changed

+179
-64
lines changed

libSmartAttributes/0.0.1/index.d.ts

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

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: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
1313
}
1414

1515
// Then try for the beacon computed
16-
const beaconAttr = await getSheetItem(characterId, name);
16+
const beaconName = type === "current" ? name : `${name}_max`;
17+
const beaconAttr = await getSheetItem(characterId, beaconName);
1718
if (beaconAttr !== null && beaconAttr !== undefined) {
1819
return beaconAttr;
1920
}
2021

2122
// Then try for the user attribute
22-
const userAttr = await getSheetItem(characterId, `user.${name}`);
23+
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
2324
if (userAttr !== null && userAttr !== undefined) {
2425
return userAttr;
2526
}
2627

27-
log(`Attribute ${name} not found on character ${characterId}`);
28+
log(`Attribute ${beaconName} not found on character ${characterId}`);
2829
return undefined;
2930
};
3031

3132
type SetOptions = {
3233
setWithWorker?: boolean;
34+
noCreate?: boolean;
3335
};
3436

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

4345
if (legacyAttr && options?.setWithWorker) {
44-
return legacyAttr.setWithWorker({ [type]: value });
46+
legacyAttr.setWithWorker({ [type]: value });
47+
return;
4548
}
4649

4750
else if (legacyAttr) {
48-
return legacyAttr.set({ [type]: value });
51+
legacyAttr.set({ [type]: value });
52+
return;
4953
}
5054

5155
// Then try for the beacon computed
52-
const beaconAttr = await getSheetItem(characterId, name);
56+
const beaconName = type === "current" ? name : `${name}_max`;
57+
const beaconAttr = await getSheetItem(characterId, beaconName);
5358
if (beaconAttr !== null && beaconAttr !== undefined) {
54-
return setSheetItem(characterId, name, value);
59+
setSheetItem(characterId, beaconName, value);
60+
return;
61+
}
62+
63+
// Guard against creating user attributes if noCreate is set
64+
if (options?.noCreate) {
65+
log(`Attribute ${beaconName} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
66+
return;
5567
}
5668

5769
// Then default to a user attribute
58-
return setSheetItem(characterId, `user.${name}`, value);
70+
setSheetItem(characterId, `user.${beaconName}`, value);
71+
return;
5972
};
6073

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

6982
if (legacyAttr) {
70-
return legacyAttr.remove();
83+
legacyAttr.remove();
84+
return;
7185
}
7286

7387
// Then try for the beacon computed
74-
const beaconAttr = await getSheetItem(characterId, name);
88+
const beaconName = type === "current" ? name : `${name}_max`;
89+
const beaconAttr = await getSheetItem(characterId, beaconName);
7590
if (beaconAttr !== null && beaconAttr !== undefined) {
7691
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
77-
return setSheetItem(characterId, name, undefined);
92+
setSheetItem(characterId, name, undefined);
93+
return;
7894
}
95+
96+
// Then try for the user attribute
97+
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
98+
if (userAttr !== null && userAttr !== undefined) {
99+
log(`Deleting user attribute ${name} on character ${characterId}`);
100+
setSheetItem(characterId, `user.${beaconName}`, undefined);
101+
return;
102+
}
103+
104+
log(`Attribute ${beaconName} not found on character ${characterId}, nothing to delete`);
105+
return;
79106
};
80107

81108
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)