Wazarr94 / haxball_bot_headless

Ready-to-go scripts and functions for the HaxBall Headless API !
MIT License
46 stars 74 forks source link

balancing teams problem when players kicked #26

Closed huseyinsoyluu closed 2 years ago

huseyinsoyluu commented 2 years ago

Hi, When 1v1 match going on and 1 spectators, if a player joins (which will be banned / kicked for some reason in bot file like bad nickname) and kicked / banned immediately; there will 2 players in team red and 1 players located in blue. (2v1) It doesn't balancing teams according to players length.

unknown

If we run balanceTeams() in command line on headless page; teams are balancing without any problem. If we are write into onPlayerKick it doesn't work properly.

huseyinsoyluu commented 2 years ago

I checked again and it's not problem for just 1v1 match, it is same in 2v2 and 1 specs as you can see in ss unknown

Wazarr94 commented 2 years ago

Hello, happy to see you experimenting, however there's a simple way to fix the behavior you're describing. When you do the filter in the onPlayerJoin event, you need to exit early to stop any player balancing.

Here's a quick example of what I mean:

function filterPlayerName(player) {
    if (player.name == "test") {
        return true;
    }
}

room.onPlayerJoin = function (player) {
    authArray[player.id] = [player.auth, player.conn];
    if (roomWebhook != '') {
        fetch(roomWebhook, {
            method: 'POST',
            body: JSON.stringify({
                content: `[${getDate()}] ➡️ JOIN (${playersAll.length + 1}/${maxPlayers})\n**` +
                         `${player.name}** [${authArray[player.id][0]}] {${authArray[player.id][1]}}`,
                username: roomName,
            }),
            headers: {
                'Content-Type': 'application/json',
            },
        }).then((res) => res);
    }
    if (filterPlayerName(player)) {
        room.kickPlayer(player.id, "Invalid name", false);
        return;
    }
    room.sendAnnouncement(
        `👋 Welcome ${player.name} !\nEnter "t" before your message to use team chat and "@@" followed by a player name to PM him !`,
        player.id,
        welcomeColor,
        'bold',
        HaxNotification.CHAT
    );
    updateTeams();
    updateAdmins();
    if (masterList.findIndex((auth) => auth == player.auth) != -1) {
        room.sendAnnouncement(
            `Master ${player.name} has connected to the room !`,
            null,
            announcementColor,
            'bold',
            HaxNotification.CHAT
        );
        room.setPlayerAdmin(player.id, true);
    } else if (adminList.map((a) => a[0]).findIndex((auth) => auth == player.auth) != -1) {
        room.sendAnnouncement(
            `Admin ${player.name} has connected to the room !`,
            null,
            announcementColor,
            'bold',
            HaxNotification.CHAT
        );
        room.setPlayerAdmin(player.id, true);
    }
    var sameAuthCheck = playersAll.filter((p) => p.id != player.id && authArray[p.id][0] == player.auth);
    if (sameAuthCheck.length > 0 && !debugMode) {
        var oldPlayerArray = playersAll.filter((p) => p.id != player.id && authArray[p.id][0] == player.auth);
        for (let oldPlayer of oldPlayerArray) {
            ghostKickHandle(oldPlayer, player);
        }
    }
    handlePlayersJoin();
};

I added the name filter after the discord log so it shows the player entering and instantly being kicked in the logs. Since the player is going to be instantly kicked, you don't need to balance teams, check for ghost kick, check if he's an admin or send him a welcome message.