@@ -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
3132type SetOptions = {
3233 setWithWorker ?: boolean ;
34+ noCreate ?: boolean ;
3335} ;
3436
3537async 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
81108export default {
0 commit comments