Multiplayer Piano Optimizations [Sounds]

Play sounds when users join, leave, or mention you in Multiplayer Piano

当前为 2025-07-14 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Multiplayer Piano Optimizations [Sounds]
// @namespace    http://tampermonkey.net/
// @version      1.0.5
// @description  Play sounds when users join, leave, or mention you in Multiplayer Piano
// @author       zackiboiz, cheezburger0
// @match        *://multiplayerpiano.com/*
// @match        *://multiplayerpiano.net/*
// @match        *://qmppv2.qwerty0301.repl.co/*
// @match        *://mpp.8448.space/*
// @match        *://mpp.autoplayer.xyz/*
// @match        *://mpp.hyye.xyz/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=multiplayerpiano.net
// @grant        none
// @license      MIT
// ==/UserScript==

(async () => {
    const MPP = window.MPP;
    const version = "1.0.4";
    let users = {};

    if (!MPP.chat.sendPrivate) {
        MPP.chat.sendPrivate = ({ name, color, message }) => {
            MPP.chat.receive({
                m: "a",
                t: Date.now(),
                a: message,
                p: {
                    _id: "usrscr",
                    id: "userscript",
                    name,
                    color
                }
            });
        };
    }

    const SOUNDS = {
        MENTION: "https://files.catbox.moe/f5tzag.mp3",
        JOIN: "https://files.catbox.moe/t3ztlz.mp3",
        LEAVE: "https://files.catbox.moe/kmpz7e.mp3"
    };

    const lastPlayed = {};
    const GAP_MS = 200;

    function play(src) {
        const now = Date.now();
        if (!lastPlayed[src] || now - lastPlayed[src] >= GAP_MS) {
            lastPlayed[src] = now;
            const sfx = new Audio(src);
            sfx.play().catch(() => {});
        }
    }

    function handleMessage(msg) {
        const sender = msg.p ?? msg.sender;
        replyMap[msg.id] = sender._id;

        const you = MPP.client.user._id;
        const isMention = msg.a.includes(`@${you}`);
        const isReplyToYou = msg.r && replyMap[msg.r] === you;

        if ((isMention || isReplyToYou) && !document.hasFocus()) {
            play(SOUNDS.MENTION);
        }
    }

    const replyMap = {};
    MPP.client.on("a", handleMessage);
    MPP.client.on("dm", handleMessage);

    MPP.client.on("ch", (ch) => {
        users = {};
        ch.ppl.forEach(user => {
            users[user._id] = user;
        });
    });
    MPP.client.on("p", (p) => {
        if (!users.hasOwnProperty(p._id)) {
            play(SOUNDS.JOIN);
        }

        users[p._id] = p;
    });
    MPP.client.on("bye", (u) => {
        play(SOUNDS.LEAVE);
        delete users[u.p];
    });

    MPP.client.on("c", (data) => {
        MPP.chat.sendPrivate({
            name: `[MPP Sounds] v${version}`,
            color: "#ffaa00",
            message: "Sound alerts loaded."
        });
    });
})();