You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

57 lines
1.2 KiB

  1. const rollValues = (rollsDefinition, result) => {
  2. for (let p in rollsDefinition) {
  3. const entry = rollsDefinition[p];
  4. if (typeof(entry) === 'object' && entry !== null && !Array.isArray(entry) ) {
  5. const newResult = {};
  6. result[p] = newResult;
  7. rollValues(entry, newResult);
  8. continue;
  9. }
  10. const range = entry;
  11. const isInt = (p.indexOf('i_') === 0);
  12. const fieldName = p.replace('i_', '');
  13. //Keys that start with s_ indicate that they shouldn't be rolled
  14. // We use this to allow arrays inside rolls to be hardcoded
  15. if (!Array.isArray(entry) || p.indexOf('s_') === 0) {
  16. if (p.indexOf('s_') === 0)
  17. result[p.substr(2)] = range;
  18. else
  19. result[fieldName] = range;
  20. continue;
  21. }
  22. let value = range[0] + (Math.random() * (range[1] - range[0]));
  23. if (isInt)
  24. value = ~~value;
  25. result[fieldName] = value;
  26. }
  27. };
  28. module.exports = {
  29. generate: function (item, blueprint) {
  30. if (!blueprint.effects)
  31. return;
  32. item.effects = blueprint.effects.map(function (e) {
  33. let rolls = e.rolls;
  34. let newRolls = {};
  35. rollValues(rolls, newRolls);
  36. return {
  37. type: e.type,
  38. properties: e.properties,
  39. rolls: newRolls
  40. };
  41. });
  42. }
  43. };