haxball / haxball-issues

116 stars 43 forks source link

mute system #1603

Closed Matteussz closed 2 years ago

Matteussz commented 2 years ago

I want to mute the nick, not the id, and keep it silent for the time I set. The penalty for silencing must continue when you leave the room and re-enter.

guguxh commented 2 years ago

Why not the AUTH?

thenorthstar commented 2 years ago

I want to mute the nick, not the id, and keep it silent for the time I set. The penalty for silencing must continue when you leave the room and re-enter.

@Matteussz You can use the following code:

var playerList = {};
var mutedAuths = [];

var playerMessages = ["PLAYER","ADMIN"];
var messagePrefix = "!";
var commands = ["!mute"];

var muteRange = {Min: 1, Max: 10};

var timeObject = {Seconds: 1000, Minutes: 60000, Hours: 3600000};

var colors = {
    Mute:{
    Admin: 0xFF0000,
    AdminCanSee: 0xFFFF00,
    Already: 0xFFFF00,
    InvalidName: 0xFFFF00,
    InvalidNumber: 0xFFFF00,
    NoSuchPlayer: 0xFF0000,
    OutOfRange: 0xFFFF00,
    Removal: 0x00FF00,
    Self: 0xFFFF00,
    Still: 0xFFFF00,
    Success: 0x00FF00
    },
    NoAuthorization:{
    Mute: 0xFF0000
    },
    Player: [0xFFFFFF,0xFFDB72],
    Unmute: 0x00FF00,
    Welcome: 0xFFFFFF
};

var fonts = {
    Mute:{
    Admin: "bold",
    AdminCanSee: "bold",
    Already: "bold",
    InvalidName: "bold",
    InvalidNumber: "bold",
    NoSuchPlayer: "bold",
    OutOfRange: "bold",
    Removal: "normal",
    Self: "bold",
    Still: "small",
    Success: "normal",
    },
    NoAuthorization:{
    Mute: "bold"
    },
    Player: ["normal","bold"],
    Unmute: "normal",
    Welcome: "normal"
};

var sounds = {
    Mute:{
    Admin: 2,
    AdminCanSee: 1,
    Already: 2,
    InvalidName: 2,
    InvalidNumber: 2,
    NoSuchPlayer: 2,
    OutOfRange: 2,
    Removal: 1,
    Self: 2,
    Still: 0,
    Success: 1,
    },
    NoAuthorization:{
    Mute: 2
    },
    Player: [1,1],
    Unmute: 1,
    Welcome: 1
};

var messages = {
    Mute:{
    Admin: "You cannot mute an administrator!",
    Already: "This player is already muted!",
    InvalidName: "Invalid name!",
    InvalidNumber: "Invalid number!",
    NoSuchPlayer: "No such a player with the given name!",
    OutOfRange: `Mute period is bound with ${muteRange.Min} and ${muteRange.Max} minutes`,
    Removal: "has been unmuted!",
    Self: "You cannot mute yourselves!",
    Still: "You are still muted! Only the administration can see your messages",
    Success: ["has been muted by","for"]
    },
    NoAuthorization:{
    Mute: "You have no authorization to mute a player in this room!"
    },
    Unmute: "has been unmuted!",
    Welcome: "Welcome!"
};

var room = HBInit({roomName:"Advanced Mute Script",noPlayer:true,public:true,maxPlayers:12});

function isCommand(string){
    return string.startsWith(messagePrefix) == true && (commands.includes(string) == true || commands.includes(string.split(" ")[0]) == true);
}

room.onPlayerChat = function(player,message){
    console.log(`${player.name}: ${message}`);
    var administrators = room.getPlayerList().filter(p => p.admin == true);

    if(isCommand(message) == true){
    if(player.admin == true){
        var name = message.toLowerCase().split(" ")[1];

        if(name == null){
        room.sendAnnouncement(`${messages.Mute.InvalidName}`,player.id,colors.Mute.InvalidName,fonts.Mute.InvalidName,sounds.Mute.InvalidName);
        return false;
        }
        else{
        var p = room.getPlayerList().find(x => x.name == name);

        if(!p){
            room.sendAnnouncement(`${messages.Mute.NoSuchPlayer}`,player.id,colors.Mute.NoSuchPlayer,fonts.Mute.NoSuchPlayer,sounds.Mute.NoSuchPlayer);
            return false;
        }
        else{
            if(p.id == player.id){
            room.sendAnnouncement(`${messages.Mute.Self}`,player.id,colors.Mute.Self,fonts.Mute.Self,sounds.Mute.Self);
            return false;
            }
            else{
            if(p.admin == true){
                room.sendAnnouncement(`${messages.Mute.Admin}`,player.id,colors.Mute.Admin,fonts.Mute.Admin,sounds.Mute.Admin);
                return false;
            }
            else{
                var time = parseInt(message.toLowerCase().split(" ")[2]);

                if(isNaN(time)){
                room.sendAnnouncement(`${messages.Mute.InvalidNumber}`,player.id,colors.Mute.InvalidNumber,fonts.Mute.InvalidNumber,sounds.Mute.InvalidNumber);
                return false;
                }
                else{
                if(time < muteRange.Min || muteRange.Max < time){
                    room.sendAnnouncement(`${messages.Mute.OutOfRange}`,player.id,colors.Mute.OutOfRange,fonts.Mute.OutOfRange,sounds.Mute.OutOfRange);
                    return false;
                }
                else{
                    if(mutedAuths.includes(playerList[p.name].auth) == true){
                    room.sendAnnouncement(`${messages.Mute.Already}`,player.id,colors.Mute.Already,fonts.Mute.Already,sounds.Mute.Already);
                    return false;
                    }
                    else{
                    room.sendAnnouncement(`${p.name} ${messages.Mute.Success[0]} ${player.name} ${messages.Mute.Success[1]} ${time} ${time == 1 ? "minute." : "minutes."}`,null,colors.Mute.Success,fonts.Mute.Success,sounds.Mute.Success);
                    mutedAuths.push(playerList[p.name].auth);
                    var pname = p.name;
                    var pauth = playerList[pname].auth;
                    setTimeout(function(){
                        if(mutedAuths.includes(pauth) == true){
                        var index = mutedAuths.indexOf(pauth);
                        mutedAuths.splice(index,1);
                        room.sendAnnouncement(`${pname} ${messages.Unmute}`,null,colors.Unmute,fonts.Unmute,sounds.Unmute);
                        }
                    },time*timeObject.Minutes);
                    return false;
                    }
                }
                }
            }
            }
        }
        }
    }
    else{
        room.sendAnnouncement(`${messages.NoAuthorization.Mute}`,player.id,colors.NoAuthorization.Mute,fonts.NoAuthorization.Mute,sounds.NoAuthorization.Mute);
        return false;
    }
    }
    else{
    if(player.admin == false && mutedAuths.includes(playerList[player.name].auth) == true){
        room.sendAnnouncement(`${messages.Mute.Still} (${message})`,player.id,colors.Mute.AdminCanSee,fonts.Mute.AdminCanSee,sounds.Mute.AdminCanSee);
        administrators.forEach(a => {
        room.sendAnnouncement(`[${playerMessages[Number(player.admin)]}] [${player.id}] ${player.name}: ${message}`,a.id,colors.Mute.Still,fonts.Mute.Still,sounds.Mute.Still);
        });
        return false;
    }

    room.sendAnnouncement(`[${playerMessages[Number(player.admin)]}] [${player.id}] ${player.name}: ${message}`,null,colors.Player[Number(player.admin)],fonts.Player[Number(player.admin)],sounds.Player[Number(player.admin)]);
    return false;
    }
}

room.onPlayerJoin = function(player){
    console.log(`${player.name} has joined`);

    room.sendAnnouncement(`${messages.Welcome}`,player.id,colors.Welcome,fonts.Welcome,sounds.Welcome);

    if(playerList[player.name] == undefined){
    playerList[player.name] = {name: player.name, auth: player.auth, conn: player.conn};
    }
    if(mutedAuths.includes(player.auth) == true){
    room.sendAnnouncement(`${messages.Mute.Still}`,player.id,colors.Mute.Still,fonts.Mute.Still,sounds.Mute.Still);
    }
}

Output: https://thehax.pl/forum/powtorki.php?nagranie=ce22163289f3bb68d56348aafeebaa4d

Note: You can also find the same script based on player ID at my GitHub profile: https://github.com/thenorthstar/HaxBall-Example-Scripts/blob/main/Advanced/Mute.js

Matteussz commented 2 years ago

@thenorthstar Thank you so much. Well, if I don't want it to be limited to 10 minutes, should I just change this part:

var muteRange = {Min: 1, Max: 10};

var timeObject = {Seconds: 1000, Minutes: 60000, Hours: 3600000};

and there is no !unmute command

thenorthstar commented 2 years ago

@thenorthstar Thank you so much. Well, if I don't want it to be limited to 10 minutes, should I just change this part:

var muteRange = {Min: 1, Max: 10};

var timeObject = {Seconds: 1000, Minutes: 60000, Hours: 3600000};

and there is no !unmute command

@Matteussz What you should change is the values in muteRange object. timeObject is just for determining the intervals in means of milliseconds. For example, if you want longer mute periods then you can use hours instead of minutes, or shorter, then seconds.

There's no !unmute command because you don't need it here, bot scripts automatically unmute the player after the mute period ends.