|
| 1 | +// libSmartAttributes v0.0.3 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 a legacy attribute or beacon computed |
| 7 | + const attr = await getSheetItem(characterId, name, type); |
| 8 | + if (attr !== null && attr !== undefined) { |
| 9 | + return attr; |
| 10 | + } |
| 11 | + // Then try for the user attribute |
| 12 | + const userAttr = await getSheetItem(characterId, `user.${name}`, type); |
| 13 | + if (userAttr !== null && userAttr !== undefined) { |
| 14 | + return userAttr; |
| 15 | + } |
| 16 | + log(`Attribute ${name} not found on character ${characterId}`); |
| 17 | + return undefined; |
| 18 | + } |
| 19 | + async function setAttribute(characterId, name, value, type = "current", options) { |
| 20 | + try { |
| 21 | + await setSheetItem(characterId, name, value, type, { allowThrow: true }); |
| 22 | + return; |
| 23 | + } |
| 24 | + catch { |
| 25 | + // throw will happen on beacon sheets if the computed doesn't exist or is read-only |
| 26 | + } |
| 27 | + // Guard against creating user attributes if noCreate is set |
| 28 | + if (options?.noCreate) { |
| 29 | + log(`Attribute ${name} not found on character ${characterId}, and noCreate option is set. Skipping creation.`); |
| 30 | + return; |
| 31 | + } |
| 32 | + // Then default to a user attribute |
| 33 | + setSheetItem(characterId, `user.${name}`, value, type); |
| 34 | + return; |
| 35 | + } |
| 36 | + async function deleteAttribute(characterId, name, type = "current") { |
| 37 | + // Try for legacy attribute first |
| 38 | + const legacyAttr = findObjs({ |
| 39 | + _type: "attribute", |
| 40 | + _characterid: characterId, |
| 41 | + name: name, |
| 42 | + })[0]; |
| 43 | + if (legacyAttr) { |
| 44 | + legacyAttr.remove(); |
| 45 | + return; |
| 46 | + } |
| 47 | + // Then try for the beacon computed |
| 48 | + const beaconAttr = await getSheetItem(characterId, name, type); |
| 49 | + if (beaconAttr !== null && beaconAttr !== undefined) { |
| 50 | + log(`Cannot delete beacon computed attribute ${name} on character ${characterId}. Setting to undefined instead`); |
| 51 | + setSheetItem(characterId, name, undefined, type); |
| 52 | + return; |
| 53 | + } |
| 54 | + // Then try for the user attribute |
| 55 | + const userAttr = await getSheetItem(characterId, `user.${name}`, type); |
| 56 | + if (userAttr !== null && userAttr !== undefined) { |
| 57 | + log(`Deleting user attribute ${name} on character ${characterId}`); |
| 58 | + setSheetItem(characterId, `user.${name}`, undefined, type); |
| 59 | + return; |
| 60 | + } |
| 61 | + log(`Attribute ${type} not found on character ${characterId}, nothing to delete`); |
| 62 | + return; |
| 63 | + } |
| 64 | + var index = { |
| 65 | + getAttribute, |
| 66 | + setAttribute, |
| 67 | + deleteAttribute, |
| 68 | + }; |
| 69 | + |
| 70 | + return index; |
| 71 | + |
| 72 | +})(); |
0 commit comments