|
| 1 | +// APISmartAttributes v0.0.1 by GUD Team | APISmartAttributes provides an interface for managing beacon attributes in a slightly smarter way. |
| 2 | +var APISmartAttributes = (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); |
| 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}`); |
| 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") { |
| 29 | + // Try for legacy attribute first |
| 30 | + const legacyAttr = findObjs({ |
| 31 | + _type: "attribute", |
| 32 | + _characterid: characterId, |
| 33 | + name: name, |
| 34 | + })[0]; |
| 35 | + if (legacyAttr) { |
| 36 | + return legacyAttr.set({ [type]: value }); |
| 37 | + } |
| 38 | + // Then try for the beacon computed |
| 39 | + const beaconAttr = await getSheetItem(characterId, name); |
| 40 | + if (beaconAttr !== null && beaconAttr !== undefined) { |
| 41 | + return setSheetItem(characterId, name, value); |
| 42 | + } |
| 43 | + // Then default to a user attribute |
| 44 | + return setSheetItem(characterId, `user.${name}`, value); |
| 45 | + } |
| 46 | + var index = { |
| 47 | + getAttribute, |
| 48 | + setAttribute, |
| 49 | + }; |
| 50 | + |
| 51 | + return index; |
| 52 | + |
| 53 | +})(); |
0 commit comments