Sandbox MooMoo.io FPS and Packet Limiter Display

Displays FPS and Packet Limiter for sandbox.moomoo.io

目前为 2024-07-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         Sandbox MooMoo.io FPS and Packet Limiter Display
// @namespace    http://tampermonkey.net/
// @author       wat
// @version      v1
// @description  Displays FPS and Packet Limiter for sandbox.moomoo.io
// @match        *://sandbox.moomoo.io/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let fps = 0;
    let packetLimiter = 0;
    let lastPacketTime = 0;
    let packetCount = 0;

    // Create the display box
    function createDisplayBox() {
        const box = document.createElement('div');
        box.id = 'fpsPacketBox';
        box.style.position = 'fixed';
        box.style.top = '10px';
        box.style.left = '50%';
        box.style.transform = 'translateX(-50%)';
        box.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        box.style.color = 'white';
        box.style.padding = '10px';
        box.style.borderRadius = '5px';
        box.style.fontFamily = 'Arial, sans-serif';
        box.style.fontSize = '14px';
        box.style.zIndex = '9999';
        document.body.appendChild(box);
    }

    // Update the display
    function updateDisplay() {
        const box = document.getElementById('fpsPacketBox');
        if (box) {
            box.textContent = `FPS: ${fps.toFixed(2)} | Packet Limiter: ${packetLimiter.toFixed(2)}`;
        }
    }

    // Calculate FPS
    let frameCount = 0;
    let lastFpsUpdateTime = performance.now();

    function calculateFps() {
        frameCount++;
        const now = performance.now();
        const elapsed = now - lastFpsUpdateTime;

        if (elapsed >= 1000) {
            fps = (frameCount * 1000) / elapsed;
            frameCount = 0;
            lastFpsUpdateTime = now;
        }

        requestAnimationFrame(calculateFps);
    }

    // Intercept WebSocket messages to calculate packet limiter
    function interceptWebSocket() {
        const oldWSSend = WebSocket.prototype.send;
        WebSocket.prototype.send = function(data) {
            const now = performance.now();
            packetCount++;

            if (now - lastPacketTime >= 1000) {
                packetLimiter = packetCount;
                packetCount = 0;
                lastPacketTime = now;
            }

            oldWSSend.apply(this, arguments);
        };
    }

    // Initialize
    function init() {
        createDisplayBox();
        calculateFps();
        interceptWebSocket();
        setInterval(updateDisplay, 100); // Update display every 100ms
    }

    // Wait for the game to load
    const loadInterval = setInterval(() => {
        if (document.getElementById('gameCanvas')) {
            clearInterval(loadInterval);
            init();
        }
    }, 100);
})();