MWI_DamageTracker

MWI伤害统计

  1. // ==UserScript==
  2. // @name MWI_DamageTracker
  3. // @namespace destiny
  4. // @version 0.0.5
  5. // @description MWI伤害统计
  6. // @author ryle798
  7. // @match https://www.milkywayidle.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14. // 代理 Websocket
  15. const OriginalWebSocket = window.WebSocket;
  16. const handlerQueue = [];
  17. function MyWebSocket(url, protocols) {
  18. const ws = new OriginalWebSocket(url, protocols);
  19. ws.addEventListener('message', function (event) {
  20. const msgData = JSON.parse(event.data);
  21. handlerQueue.reduce((prev, handler) => {
  22. return handler(prev);
  23. }, msgData);
  24. });
  25. return ws;
  26. }
  27. window.WebSocket = MyWebSocket;
  28. let totalDamage = [];
  29. let monstersHP = [];
  30. let dragging = false;
  31. const getStatisticsDom = () => {
  32. if (!document.querySelector('.statistics-panel')) {
  33. let panel = document.createElement('div');
  34. panel.style.position = 'fixed';
  35. panel.style.top = '50px';
  36. panel.style.left = '50px';
  37. panel.style.background = '#f0f0f0';
  38. panel.style.border = '1px solid #ccc';
  39. panel.style.zIndex = '9999';
  40. panel.style.cursor = 'move';
  41. panel.style.fontSize = '12px';
  42. panel.style.padding = '4px';
  43. panel.innerHTML = '<div style="padding: 10px;">还未开始统计</div>';
  44. panel.className = 'statistics-panel';
  45. let offsetX, offsetY;
  46. // Mouse events
  47. panel.addEventListener('mousedown', function (e) {
  48. dragging = true;
  49. offsetX = e.clientX - panel.offsetLeft;
  50. offsetY = e.clientY - panel.offsetTop;
  51. });
  52. document.addEventListener('mousemove', function (e) {
  53. if (dragging) {
  54. var newX = e.clientX - offsetX;
  55. var newY = e.clientY - offsetY;
  56. panel.style.left = newX + 'px';
  57. panel.style.top = newY + 'px';
  58. }
  59. });
  60. document.addEventListener('mouseup', function () {
  61. dragging = false;
  62. updateStatisticsPanel()
  63. });
  64. // Touch events
  65. panel.addEventListener('touchstart', function (e) {
  66. dragging = true;
  67. let touch = e.touches[0];
  68. offsetX = touch.clientX - panel.offsetLeft;
  69. offsetY = touch.clientY - panel.offsetTop;
  70. });
  71. document.addEventListener('touchmove', function (e) {
  72. if (dragging) {
  73. let touch = e.touches[0];
  74. var newX = touch.clientX - offsetX;
  75. var newY = touch.clientY - offsetY;
  76. panel.style.left = newX + 'px';
  77. panel.style.top = newY + 'px';
  78. }
  79. });
  80. document.addEventListener('touchend', function () {
  81. dragging = false;
  82. updateStatisticsPanel()
  83. });
  84. document.body.appendChild(panel);
  85. }
  86. return document.querySelector('.statistics-panel');
  87. };
  88. const updateStatisticsPanel = () => {
  89. if(dragging)return false;
  90. const panel = getStatisticsDom();
  91. const damageDoms = totalDamage.reduce((prev, cur, index) => {
  92. return prev + `<div>${index + 1}号位:${cur}</div>`;
  93. }, '');
  94. panel.innerHTML = damageDoms;
  95. };
  96. const calculateDamage = (msgData) => {
  97. if (msgData.type === 'new_battle') {
  98. monstersHP = msgData.monsters.map((monster) => monster.currentHitpoints);
  99. if (!totalDamage.length) {
  100. totalDamage = new Array(Object.keys(msgData.players).length).fill(0);
  101. }
  102. } else if (msgData.type === 'battle_updated' && monstersHP.length) {
  103. const mMap = msgData.mMap;
  104. const userIndex = Object.keys(msgData.pMap);
  105. monstersHP.forEach((mHP, mIndex) => {
  106. const monster = mMap[mIndex];
  107. const userIndex = Object.keys(msgData.pMap)[0];
  108. if (monster) {
  109. if (userIndex !== void 0) {
  110. const hpDiff = mHP - monster.cHP;
  111. hpDiff > 0 && (totalDamage[userIndex] += hpDiff);
  112. }
  113. monstersHP[mIndex] = monster.cHP;
  114. }
  115. });
  116. updateStatisticsPanel();
  117. } else if (msgData.type === 'actions_updated') {
  118. monstersHP = [];
  119. totalDamage = [];
  120. }
  121. };
  122.  
  123. handlerQueue.push(calculateDamage);
  124. })();