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.
 
 
 

52 lines
946 B

  1. const rollValues = (rollsDefinition, result) => {
  2. for (let p in rollsDefinition) {
  3. const entry = rollsDefinition[p];
  4. if (typeof(entry) === 'object' && !Array.isArray(entry) && entry !== null) {
  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. if (!entry.push) {
  14. result[fieldName] = range;
  15. continue;
  16. }
  17. let value = range[0] + (Math.random() * (range[1] - range[0]));
  18. if (isInt)
  19. value = ~~value;
  20. result[fieldName] = value;
  21. }
  22. };
  23. module.exports = {
  24. generate: function (item, blueprint) {
  25. if (!blueprint.effects)
  26. return;
  27. item.effects = blueprint.effects.map(function (e) {
  28. let rolls = e.rolls;
  29. let newRolls = {};
  30. rollValues(rolls, newRolls);
  31. return {
  32. type: e.type,
  33. properties: e.properties,
  34. rolls: newRolls
  35. };
  36. });
  37. }
  38. };