MWI_DamageTracker

MWI伤害统计

当前为 2024-06-17 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         MWI_DamageTracker
// @namespace    ponchain
// @version      0.0.2
// @description  MWI伤害统计
// @author       ponchain
// @match        https://www.milkywayidle.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';
  // 代理 Websocket
  const OriginalWebSocket = window.WebSocket;
  const handlerQueue = [];
  function MyWebSocket(url, protocols) {
    const ws = new OriginalWebSocket(url, protocols);
    ws.addEventListener('message', function (event) {
      const msgData = JSON.parse(event.data);
      handlerQueue.reduce((prev, handler) => {
        return handler(prev);
      }, msgData);
    });
    return ws;
  }
  window.WebSocket = MyWebSocket;
  let totalDamage = [];
  let monstersHP = [];
  const getStatisticsDom = () => {
    if (!document.querySelector('.statistics-panel')) {
      let panel = document.createElement('div');
      panel.style.position = 'fixed';
      panel.style.top = '50px';
      panel.style.left = '50px';
      panel.style.background = '#f0f0f0';
      panel.style.border = '1px solid #ccc';
      panel.style.zIndex = '9999';
      panel.style.cursor = 'move';
      panel.style.fontSize = '12px';
      panel.style.padding = '4px';
      panel.innerHTML = '<div style="padding: 10px;">还未开始统计</div>';
      panel.className = 'statistics-panel';
      let dragging = false;
      let offsetX, offsetY;
  
      // Mouse events
      panel.addEventListener('mousedown', function (e) {
        dragging = true;
        offsetX = e.clientX - panel.offsetLeft;
        offsetY = e.clientY - panel.offsetTop;
      });
  
      document.addEventListener('mousemove', function (e) {
        if (dragging) {
          var newX = e.clientX - offsetX;
          var newY = e.clientY - offsetY;
          panel.style.left = newX + 'px';
          panel.style.top = newY + 'px';
        }
      });
  
      document.addEventListener('mouseup', function () {
        dragging = false;
      });
  
      // Touch events
      panel.addEventListener('touchstart', function (e) {
        dragging = true;
        let touch = e.touches[0];
        offsetX = touch.clientX - panel.offsetLeft;
        offsetY = touch.clientY - panel.offsetTop;
      });
  
      document.addEventListener('touchmove', function (e) {
        if (dragging) {
          let touch = e.touches[0];
          var newX = touch.clientX - offsetX;
          var newY = touch.clientY - offsetY;
          panel.style.left = newX + 'px';
          panel.style.top = newY + 'px';
        }
      });
  
      document.addEventListener('touchend', function () {
        dragging = false;
      });
  
      document.body.appendChild(panel);
    }
    return document.querySelector('.statistics-panel');
  };
  
  const updateStatisticsPanel = () => {
    const panel = getStatisticsDom();
    const damageDoms = totalDamage.reduce((prev, cur, index) => {
      return prev + `<div>${index + 1}号位:${cur}</div>`;
    }, '');
    panel.innerHTML = damageDoms;
  };
  const calculateDamage = (msgData) => {
    if (msgData.type === 'new_battle') {
      monstersHP = msgData.monsters.map((monster) => monster.maxHitpoints);
      if (!totalDamage.length) {
        totalDamage = new Array(Object.keys(msgData.players).length).fill(0);
      }
    } else if (msgData.type === 'battle_updated' && monstersHP.length) {
      const mMap = msgData.mMap;
      const userIndex = Object.keys(msgData.pMap);
      monstersHP.forEach((mHP, mIndex) => {
        const monster = mMap[mIndex];
        const userIndex = Object.keys(msgData.pMap)[0];
        if (monster) {
          if (userIndex !== void 0) {
            const hpDiff = mHP - monster.cHP;
            hpDiff > 0 && (totalDamage[userIndex] += hpDiff);
          }
          monstersHP[mIndex] = monster.cHP;
        }
      });
      updateStatisticsPanel();
    } else if (msgData.type === 'actions_updated') {
      monstersHP = [];
      totalDamage = [];
    }
  };

  handlerQueue.push(calculateDamage);
})();