Skip to content

Commit 5ef13d5

Browse files
author
Nic Bradley
committed
libSmartAttributes | Fixing Ordering & Naming
1 parent 1839ced commit 5ef13d5

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

libSmartAttributes/0.0.2/libSmartAttributes.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ var libSmartAttributes = (function () {
1313
return legacyAttr.get(type);
1414
}
1515
// Then try for the beacon computed
16-
const beaconName = type === "current" ? name : `${name}_max`;
17-
const beaconAttr = await getSheetItem(characterId, beaconName);
16+
const beaconAttr = await getSheetItem(characterId, name, type);
1817
if (beaconAttr !== null && beaconAttr !== undefined) {
1918
return beaconAttr;
2019
}
2120
// Then try for the user attribute
22-
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
21+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
2322
if (userAttr !== null && userAttr !== undefined) {
2423
return userAttr;
2524
}
26-
log(`Attribute ${beaconName} not found on character ${characterId}`);
25+
log(`Attribute ${name} not found on character ${characterId}`);
2726
return undefined;
2827
}
2928
async function setAttribute(characterId, name, value, type = "current", options) {
@@ -42,19 +41,18 @@ var libSmartAttributes = (function () {
4241
return;
4342
}
4443
// Then try for the beacon computed
45-
const beaconName = type === "current" ? name : `${name}_max`;
46-
const beaconAttr = await getSheetItem(characterId, beaconName);
44+
const beaconAttr = await getSheetItem(characterId, name, type);
4745
if (beaconAttr !== null && beaconAttr !== undefined) {
48-
setSheetItem(characterId, beaconName, value);
46+
setSheetItem(characterId, name, value);
4947
return;
5048
}
5149
// Guard against creating user attributes if noCreate is set
5250
if (options?.noCreate) {
53-
log(`Attribute ${beaconName} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
51+
log(`Attribute ${name} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
5452
return;
5553
}
5654
// Then default to a user attribute
57-
setSheetItem(characterId, `user.${beaconName}`, value);
55+
setSheetItem(characterId, `user.${name}`, value, type);
5856
return;
5957
}
6058
async function deleteAttribute(characterId, name, type = "current") {
@@ -69,21 +67,20 @@ var libSmartAttributes = (function () {
6967
return;
7068
}
7169
// Then try for the beacon computed
72-
const beaconName = type === "current" ? name : `${name}_max`;
73-
const beaconAttr = await getSheetItem(characterId, beaconName);
70+
const beaconAttr = await getSheetItem(characterId, name, type);
7471
if (beaconAttr !== null && beaconAttr !== undefined) {
7572
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
76-
setSheetItem(characterId, name, undefined);
73+
setSheetItem(characterId, name, undefined, type);
7774
return;
7875
}
7976
// Then try for the user attribute
80-
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
77+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
8178
if (userAttr !== null && userAttr !== undefined) {
8279
log(`Deleting user attribute ${name} on character ${characterId}`);
83-
setSheetItem(characterId, `user.${beaconName}`, undefined);
80+
setSheetItem(characterId, `user.${name}`, undefined, type);
8481
return;
8582
}
86-
log(`Attribute ${beaconName} not found on character ${characterId}, nothing to delete`);
83+
log(`Attribute ${type} not found on character ${characterId}, nothing to delete`);
8784
return;
8885
}
8986
var index = {

libSmartAttributes/src/index.ts

Lines changed: 24 additions & 17 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,19 +17,18 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
1317
}
1418

1519
// Then try for the beacon computed
16-
const beaconName = type === "current" ? name : `${name}_max`;
17-
const beaconAttr = await getSheetItem(characterId, beaconName);
20+
const beaconAttr = await getSheetItem(characterId, name, type);
1821
if (beaconAttr !== null && beaconAttr !== undefined) {
1922
return beaconAttr;
2023
}
2124

2225
// Then try for the user attribute
23-
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
26+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
2427
if (userAttr !== null && userAttr !== undefined) {
2528
return userAttr;
2629
}
2730

28-
log(`Attribute ${beaconName} not found on character ${characterId}`);
31+
log(`Attribute ${name} not found on character ${characterId}`);
2932
return undefined;
3033
};
3134

@@ -34,7 +37,13 @@ type SetOptions = {
3437
noCreate?: boolean;
3538
};
3639

37-
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+
) {
3847
// Try for legacy attribute first
3948
const legacyAttr = findObjs({
4049
_type: "attribute",
@@ -53,21 +62,20 @@ async function setAttribute(characterId: string, name: string, value: unknown, t
5362
}
5463

5564
// Then try for the beacon computed
56-
const beaconName = type === "current" ? name : `${name}_max`;
57-
const beaconAttr = await getSheetItem(characterId, beaconName);
65+
const beaconAttr = await getSheetItem(characterId, name, type);
5866
if (beaconAttr !== null && beaconAttr !== undefined) {
59-
setSheetItem(characterId, beaconName, value);
67+
setSheetItem(characterId, name, value);
6068
return;
6169
}
6270

6371
// Guard against creating user attributes if noCreate is set
6472
if (options?.noCreate) {
65-
log(`Attribute ${beaconName} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
73+
log(`Attribute ${name} not found on character ${characterId}, and noCreate option is set. Skipping creation.`);
6674
return;
6775
}
6876

6977
// Then default to a user attribute
70-
setSheetItem(characterId, `user.${beaconName}`, value);
78+
setSheetItem(characterId, `user.${name}`, value, type);
7179
return;
7280
};
7381

@@ -85,23 +93,22 @@ async function deleteAttribute(characterId: string, name: string, type: Attribut
8593
}
8694

8795
// Then try for the beacon computed
88-
const beaconName = type === "current" ? name : `${name}_max`;
89-
const beaconAttr = await getSheetItem(characterId, beaconName);
96+
const beaconAttr = await getSheetItem(characterId, name, type);
9097
if (beaconAttr !== null && beaconAttr !== undefined) {
9198
log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`);
92-
setSheetItem(characterId, name, undefined);
99+
setSheetItem(characterId, name, undefined, type);
93100
return;
94101
}
95102

96103
// Then try for the user attribute
97-
const userAttr = await getSheetItem(characterId, `user.${beaconName}`);
104+
const userAttr = await getSheetItem(characterId, `user.${name}`, type);
98105
if (userAttr !== null && userAttr !== undefined) {
99106
log(`Deleting user attribute ${name} on character ${characterId}`);
100-
setSheetItem(characterId, `user.${beaconName}`, undefined);
107+
setSheetItem(characterId, `user.${name}`, undefined, type);
101108
return;
102109
}
103110

104-
log(`Attribute ${beaconName} not found on character ${characterId}, nothing to delete`);
111+
log(`Attribute ${type} not found on character ${characterId}, nothing to delete`);
105112
return;
106113
};
107114

0 commit comments

Comments
 (0)