Multiplayer Piano Optimizations [Sounds]

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

目前為 2025-07-14 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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."
        });
    });
})();