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.

115 lines
3.4 KiB

  1. var HitDice = HitDice || (function() {
  2. 'use strict';
  3. var tokenIds = [],
  4. configure = function() {
  5. if(!state.HitDice) {
  6. state.HitDice = {
  7. version: 0.1,
  8. config: {
  9. bar: 3,
  10. hitDiceAttribute: 'npc_hitdice',
  11. }
  12. };
  13. }
  14. },
  15. handleInput = function(msg) {
  16. if (msg.type === "api" && /^!mhd(\b|$)/i.test(msg.content) && playerIsGM(msg.playerid) ) {
  17. let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
  18. let count = 0;
  19. // WUSSALLTHISTHEN
  20. (msg.selected || [])
  21. .map(o=>getObj('graphic',o._id))
  22. .filter(g=>undefined !== g)
  23. .forEach( o => {
  24. ++count;
  25. tokenIds.push(o.id);
  26. rollHitDice(o);
  27. })
  28. ;
  29. sendChat('',`/w "${who}" Rolling hit dice for ${count} token(s).`);
  30. }
  31. },
  32. findRoll = function(txt) {
  33. let roll = txt.match(/\d+d\d+([\+\-]\d+)?/);
  34. return roll ? roll[0] : 0;
  35. },
  36. rollHitDice = function(obj) {
  37. var sets = {},
  38. bar = 'bar'+state.HitDice.config.bar,
  39. hdAttrib,
  40. hdExpression = 0,
  41. bonus = 0
  42. ;
  43. if(_.contains(tokenIds,obj.id)){
  44. tokenIds=_.without(tokenIds,obj.id);
  45. if('graphic' === obj.get('type') &&
  46. 'token' === obj.get('subtype') &&
  47. '' !== obj.get('represents')
  48. ) {
  49. if( obj && '' === obj.get(bar+'_link') ) {
  50. hdAttrib = findObjs({
  51. type: 'attribute',
  52. characterid: obj.get('represents'),
  53. name: state.HitDice.config.hitDiceAttribute
  54. })[0];
  55. if(hdAttrib) {
  56. log(hdAttrib);
  57. hdExpression = findRoll(hdAttrib.get('current'));
  58. sendChat('','/r '+hdExpression+'+'+bonus,function(r){
  59. var hp=0;
  60. _.each(r,function(subr){
  61. var val=JSON.parse(subr.content);
  62. if(_.has(val,'total'))
  63. {
  64. hp+=val.total;
  65. }
  66. });
  67. sets[bar+"_value"] = hp||1;
  68. sets[bar+"_max"] = hp||1;
  69. obj.set(sets);
  70. });
  71. }
  72. }
  73. }
  74. }
  75. },
  76. saveTokenId = function(obj){
  77. tokenIds.push(obj.id);
  78. setTimeout((function(id){
  79. return function(){
  80. var token=getObj('graphic',id);
  81. if(token){
  82. rollHitDice(token);
  83. }
  84. };
  85. }(obj.id)),100);
  86. },
  87. registerEventHandlers = function() {
  88. on('chat:message', handleInput);
  89. on('add:graphic', saveTokenId);
  90. on('change:graphic', rollHitDice);
  91. };
  92. return {
  93. configure: configure,
  94. RegisterEventHandlers: registerEventHandlers
  95. };
  96. }());
  97. on('ready',function() {
  98. 'use strict';
  99. HitDice.configure();
  100. HitDice.RegisterEventHandlers();
  101. });