11type 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,13 +17,13 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
1317 }
1418
1519 // Then try for the beacon computed
16- const beaconAttr = await getSheetItem ( characterId , name ) ;
20+ const beaconAttr = await getSheetItem ( characterId , name , type ) ;
1721 if ( beaconAttr !== null && beaconAttr !== undefined ) {
1822 return beaconAttr ;
1923 }
2024
2125 // Then try for the user attribute
22- const userAttr = await getSheetItem ( characterId , `user.${ name } ` ) ;
26+ const userAttr = await getSheetItem ( characterId , `user.${ name } ` , type ) ;
2327 if ( userAttr !== null && userAttr !== undefined ) {
2428 return userAttr ;
2529 }
@@ -30,9 +34,16 @@ async function getAttribute(characterId: string, name: string, type: AttributeTy
3034
3135type SetOptions = {
3236 setWithWorker ?: boolean ;
37+ noCreate ?: boolean ;
3338} ;
3439
35- 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+ ) {
3647 // Try for legacy attribute first
3748 const legacyAttr = findObjs ( {
3849 _type : "attribute" ,
@@ -41,24 +52,34 @@ async function setAttribute(characterId: string, name: string, value: unknown, t
4152 } ) [ 0 ] ;
4253
4354 if ( legacyAttr && options ?. setWithWorker ) {
44- return legacyAttr . setWithWorker ( { [ type ] : value } ) ;
55+ legacyAttr . setWithWorker ( { [ type ] : value } ) ;
56+ return ;
4557 }
4658
4759 else if ( legacyAttr ) {
48- return legacyAttr . set ( { [ type ] : value } ) ;
60+ legacyAttr . set ( { [ type ] : value } ) ;
61+ return ;
4962 }
5063
5164 // Then try for the beacon computed
52- const beaconAttr = await getSheetItem ( characterId , name ) ;
65+ const beaconAttr = await getSheetItem ( characterId , name , type ) ;
5366 if ( beaconAttr !== null && beaconAttr !== undefined ) {
54- return setSheetItem ( characterId , name , value ) ;
67+ setSheetItem ( characterId , name , value ) ;
68+ return ;
69+ }
70+
71+ // Guard against creating user attributes if noCreate is set
72+ if ( options ?. noCreate ) {
73+ log ( `Attribute ${ name } not found on character ${ characterId } , and noCreate option is set. Skipping creation.` ) ;
74+ return ;
5575 }
5676
5777 // Then default to a user attribute
58- return setSheetItem ( characterId , `user.${ name } ` , value ) ;
78+ setSheetItem ( characterId , `user.${ name } ` , value , type ) ;
79+ return ;
5980} ;
6081
61- async function deleteAttribute ( characterId : string , name : string ) {
82+ async function deleteAttribute ( characterId : string , name : string , type : AttributeType = "current" ) {
6283 // Try for legacy attribute first
6384 const legacyAttr = findObjs ( {
6485 _type : "attribute" ,
@@ -67,15 +88,28 @@ async function deleteAttribute(characterId: string, name: string) {
6788 } ) [ 0 ] ;
6889
6990 if ( legacyAttr ) {
70- return legacyAttr . remove ( ) ;
91+ legacyAttr . remove ( ) ;
92+ return ;
7193 }
7294
7395 // Then try for the beacon computed
74- const beaconAttr = await getSheetItem ( characterId , name ) ;
96+ const beaconAttr = await getSheetItem ( characterId , name , type ) ;
7597 if ( beaconAttr !== null && beaconAttr !== undefined ) {
7698 log ( `Cannot delete beacon computed attribute ${ name } on character ${ characterId } . Setting to undefined instead` ) ;
77- return setSheetItem ( characterId , name , undefined ) ;
99+ setSheetItem ( characterId , name , undefined , type ) ;
100+ return ;
78101 }
102+
103+ // Then try for the user attribute
104+ const userAttr = await getSheetItem ( characterId , `user.${ name } ` , type ) ;
105+ if ( userAttr !== null && userAttr !== undefined ) {
106+ log ( `Deleting user attribute ${ name } on character ${ characterId } ` ) ;
107+ setSheetItem ( characterId , `user.${ name } ` , undefined , type ) ;
108+ return ;
109+ }
110+
111+ log ( `Attribute ${ type } not found on character ${ characterId } , nothing to delete` ) ;
112+ return ;
79113} ;
80114
81115export default {
0 commit comments