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.

117 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. return txt.match(/\d+d\d+(\+\d+)?/)[0] || 0;
  34. },
  35. rollHitDice = function(obj) {
  36. var sets = {},
  37. bar = 'bar'+state.HitDice.config.bar,
  38. hdAttrib,
  39. hdExpression = 0,
  40. bonus = 0
  41. ;
  42. if(_.contains(tokenIds,obj.id)){
  43. tokenIds=_.without(tokenIds,obj.id);
  44. if('graphic' === obj.get('type') &&
  45. 'token' === obj.get('subtype') &&
  46. '' !== obj.get('represents')
  47. ) {
  48. if( obj && '' === obj.get(bar+'_link') ) {
  49. hdAttrib = findObjs({
  50. type: 'attribute',
  51. characterid: obj.get('represents'),
  52. name: state.HitDice.config.hitDiceAttribute
  53. })[0];
  54. if( hdAttrib ) {
  55. //sendChat('', 'HERE WE ARE');
  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. });