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,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