MWI_DamageTracker

MWI伤害统计

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         MWI_DamageTracker
// @namespace    destiny
// @version      0.0.5
// @description  MWI伤害统计
// @author       ryle798
// @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 = [];
  let dragging = false;
  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 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;
        updateStatisticsPanel()
      });
  
      // 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;
        updateStatisticsPanel()
      });
  
      document.body.appendChild(panel);
    }
    return document.querySelector('.statistics-panel');
  };
  
  const updateStatisticsPanel = () => {
    if(dragging)return false;
    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.currentHitpoints);
      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);
})();