Bloble.io NoobScript V3 ChatCommands Fragment

A fragment of code from NoobScript V3 - Chat commands.

目前為 2018-02-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bloble.io NoobScript V3 ChatCommands Fragment
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  A fragment of code from NoobScript V3 - Chat commands.
// @author       NoobishHacker
// @match        http://bloble.io/*
// @grant        none
// ==/UserScript==

window.UIList = window.UIList || [];
window.initFuncs = window.initFuncs || [];
window.statusItems = window.statusItems || [];
window.overrideSocketEvents = window.overrideSocketEvents || [];

window.chatCommands = window.chatCommands || {};

window.resetCamera = function () { // Override
    camX = camXS = camY = camYS = 0;
    cameraKeys = {
        l: 0,
        r: 0,
        u: 0,
        d: 0
    }

    if (socket && window.overrideSocketEvents && window.overrideSocketEvents.length) {
        window.overrideSocketEvents.forEach((item) => {
            socket.removeAllListeners(item.name)
            socket.on(item.name, item.func);

        });

    }
}

var muted = [];
window.overrideSocketEvents.push({
    name: "ch",
    description: "Chat Muter",
    func: function (a, d, c) {
        if (!muted[a])
            addChatLine(a, d, c)
    }

})

window.addChat = function (msg, from, color) {
    color = color || "#fff";
    var b = document.createElement("li");
    b.className = "chatother";
    b.innerHTML = '<span style="color:' + color + '">[' + from + ']</span> <span class="chatText">' + msg + "</span>";
    10 < chatList.childNodes.length && chatList.removeChild(chatList.childNodes[0]);
    chatList.appendChild(b)
}


window.chatCommands.mute = function (split) {
    if (!split[1]) {
        addChat('Please specify a username or "all" for 1rst arg.')
    } else if (split[1] === 'all') {
        users.forEach((user) => {
            muted[user.sid] = true;
        });
        addChat('Muted ' + users.length + ' users', 'Client');

    } else {
        var len = 0;
        users.forEach((user) => {
            if (user.name === split[1]) {
                muted[user.sid] = true;
                len++;
            }

        });
        addChat('Muted ' + len + ' users with the name ' + split[1], 'Client');
    }
}
window.chatCommands.unmute = function (split) {
    if (!split[1]) {
        addChat('Please specify a username or "all" for 1rst arg.')
    } else if (split[1] === 'all') {
        addChat('Unmuted ' + Object.keys(mute).length + ' users', 'Client');
        muted = {};
    } else {
        var len = 0;
        users.forEach((user) => {
            if (user.name === split[1]) {
                muted[user.sid] = false;
                len++;
            }
        });
        addChat('Unmuted ' + len + ' users with the name ' + split[1], 'Client');
    }
}
window.chatCommands.help = function (split) {
    var avail = Object.keys(window.chatCommands);
    addChat('There are ' + avail.length + ' commands available.', 'Client')
    addChat(avail.join(', '), 'Client');
}

window.chatCommands.playerlist = function (split) {
    var page = parseInt(split[1]) || 1;
    var total = Math.ceil(users.length / 5);
    addChat('There are ' + users.length + ' players. Page ' + page + ' out of ' + total, 'Client')
    var offset = page * 5;
    for (var i = 0; i < 5; i++) {
        if (!users[i + offset]) break;
        addChat(users[i + offset].name, 'Client', playerColors[users[i + offset].color])
    }
}


window.chatCommands.clear = function () {
    while (chatList.hasChildNodes()) {
        chatList.removeChild(chatList.lastChild);
    }
}
var modsShown = true;
window.chatCommands.toggle = function () {
    var element = document.getElementById('noobscriptUI')
    if (modsShown) {
        modsShown = false;
        element.style.display = 'none';
        addChat('Mod Menu disabled', 'Client')
    } else {
        modsShown = true;
        element.style.display = 'block';
        addChat('Mod Menu enabled', 'Client')
    }
}

var chatHist = [];
var chatHistInd = -1;
var prevText = '';



setTimeout(function () {
    var old = chatInput
    chatInput = old.cloneNode(true);
    old.parentNode.replaceChild(chatInput, old);
    chatInput.onclick = function () {
        toggleChat(!0)
    };

    chatInput.addEventListener("keyup", function (a) {
        var b = a.which || a.keyCode;
        if (b === 38) { // up
            if (chatHistInd === -1) {
                prevText = chatInput.value;
                chatHistInd = chatHist.length;
            }
            if (chatHistInd > 0) chatHistInd--;
            chatInput.value = prevText + (chatHist[chatHistInd] || '')

        } else if (b === 40) {
            if (chatHistInd !== -1) {

                if (chatHistInd < chatHist.length) chatHistInd++;
                else chatHistInd = -1;
                chatInput.value = prevText + (chatHist[chatHistInd] || '')
            }
        } else
        if (gameState && socket && 13 === (a.which || a.keyCode) && "" != chatInput.value) {
            var value = chatInput.value;
            chatInput.value = ""
            mainCanvas.focus()
        
            if (value.charAt(0) === '/') {

                var split = value.split(' ');
                var name = split[0].substr(1);
                if (window.chatCommands[name]) window.chatCommands[name](split);
                else {
                    addChat("Command '" + name + "' not found. Please do /help for a list of commands.")
                }
            } else {
                socket.emit("ch", value)
            }
            if (chatHist[chatHist.length - 1] !== value) {
                var ind = chatHist.indexOf(value);
                if (ind !== -1) {
                    chatHist.splice(ind, 1);
                }
                chatHist.push(value);
            }
            chatHistInd = -1;
        }
    })
},1000)