ooshbin

Repeating section functions

Jan 11th, 2021 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. const getRepIds = (charId, sectionName, ignoreBlanks = false) => {
  2. if (getObj('character', charId)) {
  3. let repSecParts = sectionName.trim().split(/_[$*].*_/);
  4. let regex = new RegExp(`${repSecParts[0]}_(-.*)_${repSecParts[1]}`)
  5. if (repSecParts.length !== 2 || repSecParts[0].search(/repeating/i) === -1) {
  6. // log(`Bad repeating section name: ${sectionName}, aborting...`,null,null,'error');
  7. return;
  8. }
  9. // log(`repeating section: ${repSecParts[0]} -- ${repSecParts[1]}`);
  10. let idArray = [];
  11. findObjs({type: 'attribute', characterid: charId}).filter(attr => {
  12. if (attr.get('name').search(repSecParts[0]) !== -1 && attr.get('name').search(repSecParts[1]) !== -1 && attr.get('name').match(regex) && attr.get('current')) {
  13. if (!ignoreBlanks || attr.get('current').trim() !== '') idArray.push(attr.get('name').match(regex)[1]);
  14. else log(`Row found, but name field blank - skipping ${charId} -- ${attr.get('name').current}`);
  15. }
  16. })
  17. let reporder = findObjs({type: 'attribute', characterid: charId, name: `_reporder_${repSecParts[0]}`})[0];
  18. if (reporder) {
  19. reporder = reporder.get('current').trim().split(',');
  20. idArray = [...new Set(reporder.filter((id) => idArray.includes(id)).concat(idArray))];
  21. }
  22. idArray.filter((attr) => attr.current != '')
  23. return idArray;
  24. }
  25. }
  26.  
  27. /*Rep Attr. getter, returns an array with one object per repeating row. Each object contains a key for each
  28. Attr field grabbed - attrSuffix: {id: UUID of *attribute*, current: current value, max: max value}. Each
  29. row Object also contains two keys at the start for repeating_prefix and $rowID. Example output for running function
  30. on a sheet with a single row on the npcaction section, using getRepAttrs(charId, 'repeating_npcaction', idArray, ['description', 'name']):
  31.  
  32. [{
  33. "prefix": "repeating_npcaction_",
  34. "rowId": "-MKk9QEjtVCV6fuJBru3",
  35. "name": {
  36. "id": "-MKk9QEnMidxsAPpfeYJ",
  37. "current": "Bite",
  38. "max": ""
  39. },
  40. "description": {
  41. "id": "-MKk9QEpdYeC9UIILty2",
  42. "current": "",
  43. "max": ""
  44. },
  45. },
  46. {
  47. "prefix": "repeating_npcaction_",
  48. "rowId": "-MO0BpSyp9VPnNESbpNs",
  49. "name": {
  50. "id": "-MO0BpT3gEEqX_E6pIlq",
  51. "current": "Claw",
  52. "max": ""
  53. },
  54. "description": {
  55. "id": "-MO0BpT3gEEqX_E6pIlq",
  56. "current": "",
  57. "max": ""
  58. },
  59. }]*/
  60.  
  61. const getRepAttrs = (charId, sectionName, rowIds, attrNamesArray, createMissing = false) => {
  62. if (getObj('character', charId) && sectionName.search(/repeating.*_\$/i) !== -1) {
  63. let sectionPrefix = sectionName.match(/(repeating.*_)\$/i)[1];
  64. attrNamesArray = (Array.isArray(attrNamesArray)) ? attrNamesArray : [attrNamesArray];
  65. attrNamesArray = attrNamesArray.filter((a) => (a) && a != '0')
  66. rowIds = (Array.isArray(rowIds)) ? rowIds : [rowIds];
  67. let attrArray = [], index = 0;
  68. rowIds.forEach(row => {
  69. let rowObj = {prefix: sectionPrefix, rowId: row};
  70. attrNamesArray.forEach(attrName => {
  71. if (findObjs({type:'attribute', characterid: charId, name: `${sectionPrefix}${row}_${attrName}`}).length < 1) {
  72. // log(`Can't find attribute: ${sectionPrefix}$${index}_${attrName}`)
  73. if (createMissing) {
  74. let x = createObj('attribute', {characterid: charId, name: `${sectionPrefix}${row}_${attrName}`, current: '', max: ''});
  75. // log(`Missing attribute created!`);
  76. rowObj[attrName] = {id: x.id, current: '', max: ''}
  77. }
  78. } else {
  79. let currentAttr = findObjs({type:'attribute', characterid: charId, name: `${sectionPrefix}${row}_${attrName}`})[0];
  80. rowObj[attrName] = {id: currentAttr.get('_id'), current: currentAttr.get('current'), max: currentAttr.get('max')}
  81. }
  82. })
  83. attrArray.push(rowObj);
  84. index ++;
  85. })
  86. return attrArray;
  87. } else {log(`invalid charId (${charId}) or repeating section name (${sectionName}), aborting...`)}
  88. }
  89.  
  90. /* Attribute setter requires same format as getter: array of objects, each Repeating Row is an object,
  91. each key is an Attribute - attrName: {id: UUID of attr, current: current value, max: max value}*/
  92. const setRepAttrs = (charId, attrArray) =>{
  93. if (!Array.isArray(attrArray)) attrArray = [attrArray];
  94. if (getObj('character', charId)) {
  95. attrArray.forEach(row => {
  96. for (let attr in row) {
  97. if (row[attr].hasOwnProperty('id')) {
  98. if (row[attr].hasOwnProperty('current')) getObj('attribute', row[attr].id).set('current', row[attr].current)
  99. if (row[attr].hasOwnProperty('max')) {getObj('attribute', row[attr].id).set('max', row[attr].max)}
  100. }
  101. }
  102. })
  103. }
  104. }
Add Comment
Please, Sign In to add comment