Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getRepIds = (charId, sectionName, ignoreBlanks = false) => {
- if (getObj('character', charId)) {
- let repSecParts = sectionName.trim().split(/_[$*].*_/);
- let regex = new RegExp(`${repSecParts[0]}_(-.*)_${repSecParts[1]}`)
- if (repSecParts.length !== 2 || repSecParts[0].search(/repeating/i) === -1) {
- // log(`Bad repeating section name: ${sectionName}, aborting...`,null,null,'error');
- return;
- }
- // log(`repeating section: ${repSecParts[0]} -- ${repSecParts[1]}`);
- let idArray = [];
- findObjs({type: 'attribute', characterid: charId}).filter(attr => {
- if (attr.get('name').search(repSecParts[0]) !== -1 && attr.get('name').search(repSecParts[1]) !== -1 && attr.get('name').match(regex) && attr.get('current')) {
- if (!ignoreBlanks || attr.get('current').trim() !== '') idArray.push(attr.get('name').match(regex)[1]);
- else log(`Row found, but name field blank - skipping ${charId} -- ${attr.get('name').current}`);
- }
- })
- let reporder = findObjs({type: 'attribute', characterid: charId, name: `_reporder_${repSecParts[0]}`})[0];
- if (reporder) {
- reporder = reporder.get('current').trim().split(',');
- idArray = [...new Set(reporder.filter((id) => idArray.includes(id)).concat(idArray))];
- }
- idArray.filter((attr) => attr.current != '')
- return idArray;
- }
- }
- /*Rep Attr. getter, returns an array with one object per repeating row. Each object contains a key for each
- Attr field grabbed - attrSuffix: {id: UUID of *attribute*, current: current value, max: max value}. Each
- row Object also contains two keys at the start for repeating_prefix and $rowID. Example output for running function
- on a sheet with a single row on the npcaction section, using getRepAttrs(charId, 'repeating_npcaction', idArray, ['description', 'name']):
- [{
- "prefix": "repeating_npcaction_",
- "rowId": "-MKk9QEjtVCV6fuJBru3",
- "name": {
- "id": "-MKk9QEnMidxsAPpfeYJ",
- "current": "Bite",
- "max": ""
- },
- "description": {
- "id": "-MKk9QEpdYeC9UIILty2",
- "current": "",
- "max": ""
- },
- },
- {
- "prefix": "repeating_npcaction_",
- "rowId": "-MO0BpSyp9VPnNESbpNs",
- "name": {
- "id": "-MO0BpT3gEEqX_E6pIlq",
- "current": "Claw",
- "max": ""
- },
- "description": {
- "id": "-MO0BpT3gEEqX_E6pIlq",
- "current": "",
- "max": ""
- },
- }]*/
- const getRepAttrs = (charId, sectionName, rowIds, attrNamesArray, createMissing = false) => {
- if (getObj('character', charId) && sectionName.search(/repeating.*_\$/i) !== -1) {
- let sectionPrefix = sectionName.match(/(repeating.*_)\$/i)[1];
- attrNamesArray = (Array.isArray(attrNamesArray)) ? attrNamesArray : [attrNamesArray];
- attrNamesArray = attrNamesArray.filter((a) => (a) && a != '0')
- rowIds = (Array.isArray(rowIds)) ? rowIds : [rowIds];
- let attrArray = [], index = 0;
- rowIds.forEach(row => {
- let rowObj = {prefix: sectionPrefix, rowId: row};
- attrNamesArray.forEach(attrName => {
- if (findObjs({type:'attribute', characterid: charId, name: `${sectionPrefix}${row}_${attrName}`}).length < 1) {
- // log(`Can't find attribute: ${sectionPrefix}$${index}_${attrName}`)
- if (createMissing) {
- let x = createObj('attribute', {characterid: charId, name: `${sectionPrefix}${row}_${attrName}`, current: '', max: ''});
- // log(`Missing attribute created!`);
- rowObj[attrName] = {id: x.id, current: '', max: ''}
- }
- } else {
- let currentAttr = findObjs({type:'attribute', characterid: charId, name: `${sectionPrefix}${row}_${attrName}`})[0];
- rowObj[attrName] = {id: currentAttr.get('_id'), current: currentAttr.get('current'), max: currentAttr.get('max')}
- }
- })
- attrArray.push(rowObj);
- index ++;
- })
- return attrArray;
- } else {log(`invalid charId (${charId}) or repeating section name (${sectionName}), aborting...`)}
- }
- /* Attribute setter requires same format as getter: array of objects, each Repeating Row is an object,
- each key is an Attribute - attrName: {id: UUID of attr, current: current value, max: max value}*/
- const setRepAttrs = (charId, attrArray) =>{
- if (!Array.isArray(attrArray)) attrArray = [attrArray];
- if (getObj('character', charId)) {
- attrArray.forEach(row => {
- for (let attr in row) {
- if (row[attr].hasOwnProperty('id')) {
- if (row[attr].hasOwnProperty('current')) getObj('attribute', row[attr].id).set('current', row[attr].current)
- if (row[attr].hasOwnProperty('max')) {getObj('attribute', row[attr].id).set('max', row[attr].max)}
- }
- }
- })
- }
- }
Add Comment
Please, Sign In to add comment