@@ -92,7 +92,7 @@ export class APIWrapper {
9292 _characterid : character ?. id ,
9393 } ;
9494 const newAttr = createObj ( "attribute" , newObjProps ) ;
95- await APIWrapper . setAttribute ( character . id , name , value , max ) ;
95+ await APIWrapper . setAttribute ( character , name , value , max ) ;
9696 globalSubscribeManager . publish ( "add" , newAttr ) ;
9797 // This is how the previous script was doing it
9898 globalSubscribeManager . publish ( "change" , newAttr , newAttr ) ;
@@ -110,12 +110,18 @@ export class APIWrapper {
110110 const messages : string [ ] = [ ] ;
111111 const attr = await APIWrapper . getAttribute ( character , name )
112112 if ( ! attr ) {
113- errors . push ( `updateAttribute: Attribute ${ name } does not exist for character with ID ${ character ?. id } .` ) ;
113+ errors . push ( `Attribute <strong> ${ name } </strong> does not exist for character <strong> ${ character . get ( "name" ) } </strong> .` ) ;
114114 return [ { messages, errors } ] ;
115115 }
116116 const oldAttr = JSON . parse ( JSON . stringify ( attr ) ) ;
117- await APIWrapper . setAttribute ( character . id , name , value , max ) ;
118- messages . push ( `Setting <strong>${ name } </strong> to <strong>${ value } </strong> for character <strong>${ character ?. get ( "name" ) } </strong>.` ) ;
117+ await APIWrapper . setAttribute ( character , name , value , max ) ;
118+ if ( value && max ) {
119+ messages . push ( `Setting <strong>${ name } </strong> to <strong>${ value } </strong> and <strong>${ name } _max</strong> to <strong>${ max } </strong> for character <strong>${ character ?. get ( "name" ) } </strong>.` ) ;
120+ } else if ( value ) {
121+ messages . push ( `Setting <strong>${ name } </strong> to <strong>${ value } </strong> for character <strong>${ character ?. get ( "name" ) } </strong>, max remains unchanged.` ) ;
122+ } else if ( max ) {
123+ messages . push ( `Setting <strong>${ name } _max</strong> to <strong>${ max } </strong> for character <strong>${ character ?. get ( "name" ) } </strong>, current remains unchanged.` ) ;
124+ }
119125 globalSubscribeManager . publish ( "change" , { name, current : value , max } , { name, current : oldAttr . value , max : oldAttr . max } ) ;
120126 return [ { messages, errors } ] ;
121127 } ;
@@ -164,22 +170,22 @@ export class APIWrapper {
164170 } ;
165171
166172 private static async setAttribute (
167- characterID : string ,
173+ character : Roll20Character ,
168174 attr : string ,
169175 value ?: string ,
170176 max ?: string
171177 ) : Promise < void > {
172178 if ( state . ChatSetAttr . useWorkers ) {
173- await APIWrapper . setWithWorker ( characterID , attr , value , max ) ;
179+ await APIWrapper . setWithWorker ( character . id , attr , value , max ) ;
174180 return ;
175181 }
176182 if ( value ) {
177- const newValue = await setSheetItem ( characterID , attr , value , "current" ) ;
178- log ( `setAttribute: Setting ${ attr } to ${ JSON . stringify ( newValue ) } for character with ID ${ characterID } .` ) ;
183+ await setSheetItem ( character . id , attr , value , "current" ) ;
184+ log ( `Setting <strong> ${ attr } </strong> to <strong> ${ value } </strong> for character with ID <strong> ${ character . get ( "name" ) } </strong> .` ) ;
179185 }
180186 if ( max ) {
181- const newMax = await setSheetItem ( characterID , attr , max , "max" ) ;
182- log ( `setAttribute: Setting ${ attr } max to ${ JSON . stringify ( newMax ) } for character with ID ${ characterID } .` ) ;
187+ await setSheetItem ( character . id , attr , max , "max" ) ;
188+ log ( `Setting <strong> ${ attr } </strong> max to <strong> ${ max } </strong> for character <strong> ${ character . get ( "name" ) } </strong> .` ) ;
183189 }
184190 } ;
185191
@@ -193,7 +199,7 @@ export class APIWrapper {
193199 for ( const [ key , value ] of entries ) {
194200 const attribute = await APIWrapper . getAttribute ( character , key ) ;
195201 if ( ! attribute ?. value ) {
196- errors . push ( `setAttribute: ${ key } does not exist for character with ID ${ character ?. id } .` ) ;
202+ errors . push ( `Attribute <strong> ${ key } </strong> does not exist for character <strong> ${ character . get ( "name" ) } </strong> .` ) ;
197203 return [ { messages, errors } ] ;
198204 }
199205 const stringValue = value . value ? value . value . toString ( ) : undefined ;
@@ -237,15 +243,46 @@ export class APIWrapper {
237243 const errors : string [ ] = [ ] ;
238244 const messages : string [ ] = [ ] ;
239245 for ( const attribute of attributes ) {
246+ const { section, repeatingID, attribute : attrName } = APIWrapper . extractRepeatingDetails ( attribute ) || { } ;
247+ if ( section && repeatingID && ! attrName ) {
248+ return APIWrapper . deleteRepeatingRow ( character , section , repeatingID ) ;
249+ }
240250 const attr = await APIWrapper . getAttr ( character , attribute ) ;
241251 if ( ! attr ) {
242- errors . push ( `deleteAttributes: Attribute ${ attribute } does not exist for character with ID ${ character ?. id } .` ) ;
252+ errors . push ( `Attribute <strong> ${ attribute } </strong> does not exist for character <strong> ${ character . get ( "name" ) } </strong> .` ) ;
243253 continue ;
244254 }
245255 const oldAttr = JSON . parse ( JSON . stringify ( attr ) ) ;
246256 attr . remove ( ) ;
247257 globalSubscribeManager . publish ( "destroy" , oldAttr ) ;
248- messages . push ( `Attribute ${ attribute } deleted for character with ID ${ character ?. id } .` ) ;
258+ messages . push ( `Attribute <strong>${ attribute } </strong> deleted for character <strong>${ character . get ( "name" ) } </strong>.` ) ;
259+ }
260+ return [ { messages, errors } ] ;
261+ } ;
262+
263+ public static async deleteRepeatingRow (
264+ character : Roll20Character ,
265+ section : string ,
266+ repeatingID : string
267+ ) : Promise < [ ErrorResponse ] > {
268+ const errors : string [ ] = [ ] ;
269+ const messages : string [ ] = [ ] ;
270+ const repeatingAttrs = findObjs < "attribute" > ( {
271+ _type : "attribute" ,
272+ _characterid : character . id ,
273+ } ) . filter ( attr => {
274+ const name = attr . get ( "name" ) ;
275+ return name . startsWith ( `repeating_${ section } _${ repeatingID } _` ) ;
276+ } ) ;
277+ if ( repeatingAttrs . length === 0 ) {
278+ errors . push ( `No repeating attributes found for section <strong>${ section } </strong> and ID <strong>${ repeatingID } </strong> for character <strong>${ character . get ( "name" ) } </strong>.` ) ;
279+ return [ { messages, errors } ] ;
280+ }
281+ for ( const attr of repeatingAttrs ) {
282+ const oldAttr = JSON . parse ( JSON . stringify ( attr ) ) ;
283+ attr . remove ( ) ;
284+ globalSubscribeManager . publish ( "destroy" , oldAttr ) ;
285+ messages . push ( `Repeating attribute <strong>${ attr . get ( "name" ) } </strong> deleted for character <strong>${ character . get ( "name" ) } </strong>.` ) ;
249286 }
250287 return [ { messages, errors } ] ;
251288 } ;
@@ -260,17 +297,17 @@ export class APIWrapper {
260297 const attr = await APIWrapper . getAttribute ( character , attribute ) ;
261298 const value = attr ?. value ;
262299 if ( ! value ) {
263- errors . push ( `resetAttributes: Attribute ${ attribute } does not exist for character with ID ${ character ?. id } .` ) ;
300+ errors . push ( `Attribute <strong> ${ attribute } </strong> does not exist for character <strong> ${ character . get ( "name" ) } </strong> .` ) ;
264301 continue ;
265302 }
266303 const max = attr . max ;
267304 if ( ! max ) {
268305 continue ;
269306 }
270307 const oldAttr = JSON . parse ( JSON . stringify ( attr ) ) ;
271- APIWrapper . setAttribute ( character . id , attribute , max ) ;
308+ APIWrapper . setAttribute ( character , attribute , max ) ;
272309 globalSubscribeManager . publish ( "change" , attr , oldAttr ) ;
273- messages . push ( `Attribute ${ attribute } reset for character with ID ${ character ?. id } .` ) ;
310+ messages . push ( `Attribute <strong> ${ attribute } </strong> reset for character <strong> ${ character . get ( "name" ) } </strong> .` ) ;
274311 }
275312 return [ { messages, errors } ] ;
276313 } ;
@@ -281,13 +318,10 @@ export class APIWrapper {
281318 public static extractRepeatingDetails ( attributeName : string ) {
282319 const [ , section , repeatingID , ...attributeParts ] = attributeName . split ( "_" ) ;
283320 const attribute = attributeParts . join ( "_" ) ;
284- if ( ! section || ! repeatingID || ! attribute ) {
285- return null ;
286- }
287321 return {
288- section,
289- repeatingID,
290- attribute,
322+ section : section || undefined ,
323+ repeatingID : repeatingID || undefined ,
324+ attribute : attribute || undefined ,
291325 } ;
292326 } ;
293327
0 commit comments