0% found this document useful (0 votes)
18 views10 pages

Final Code

Uploaded by

Saurabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Final Code

Uploaded by

Saurabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Final Code - 02-07-2024 - 6.

20 AM

const room = HBInit({


roomName: " [🇭🇦🇽🇮🇳🇩🇮🇦] Pub Room",
maxPlayers: 30,
public: true,
noPlayer: true,
geo: { "code": "IN", "lat": 20.0000, "lon": 73.7833 }
});

// Variables to manage room state and players


let admins = [];
let superAdmin = null;
let lastTouchPlayer = null;
let secondLastTouchPlayer = null; // Track the second last player who touched the
ball
let lastMovementTime = {}; // Track last movement time for each player
let previousTeams = {}; // Track previous team of AFK players
let afkStatus = {}; // Track AFK status of players
let mutedPlayers = {}; // Track temporarily muted players with timeout
let permanentlyMutedPlayers = []; // Track permanently muted players
let lastMessageTime = {}; // Track last message time for each player
let slowDownMode = false; // Track if slow down mode is enabled
let slowDownModeTimeout; // Timeout handle for slow down mode
let bannedWords = []; // Track banned words
let wordWarnings = {}; // Track word warnings for players
const discordLink = "https://discord.gg/rErwfRtszW";
let kickCounts = {}; // Track the number of kicks by each admin

// Utility Functions
function sendAnnouncement(message, playerId = null, color = 0x00FF00) {
room.sendAnnouncement(message, playerId, color);
}

function updateLastMovement(player) {
lastMovementTime[player.id] = Date.now();
}

function isAdmin(player) {
return admins.includes(player.id);
}

function isSuperAdmin(player) {
return player.id === superAdmin;
}

function grantAdmin(player) {
room.setPlayerAdmin(player.id, true);
if (!admins.includes(player.id)) {
admins.push(player.id);
}
sendAnnouncement("You are now an admin!", player.id, 0xACE1AF);
}

function removeAdmin(player) {
room.setPlayerAdmin(player.id, false);
admins = admins.filter(id => id !== player.id);
sendAnnouncement("Admin rights removed.", player.id, 0xFF0000);
}

function revokeAllAdmins(exceptPlayer) {
room.getPlayerList().forEach(player => {
if (player.id !== exceptPlayer.id) {
room.setPlayerAdmin(player.id, false);
}
});
admins = [exceptPlayer.id];
}

function updateAdmins() {
admins = room.getPlayerList().filter(player => player.admin).map(player =>
player.id);
}

// Mute Functions
function mutePlayer(player, duration = 300000) { // Default duration is 5 minutes
(300000 ms)
if (!mutedPlayers[player.id]) {
const unmuteTime = Date.now() + duration;
mutedPlayers[player.id] = unmuteTime;
setTimeout(() => {
delete mutedPlayers[player.id];
sendAnnouncement(player.name + " is now unmuted.", null, 0x00FF00);
}, duration);
sendAnnouncement(player.name + " is muted for 5 minutes.", null, 0xFF0000);
}
}

function permMutePlayer(player) {
if (!permanentlyMutedPlayers.includes(player.id)) {
permanentlyMutedPlayers.push(player.id);
sendAnnouncement(player.name + " is permanently muted.", null, 0xFF0000);
}
}

function unmutePlayer(player) {
delete mutedPlayers[player.id];
permanentlyMutedPlayers = permanentlyMutedPlayers.filter(id => id !==
player.id);
sendAnnouncement(player.name + " is now unmuted.", null, 0x00FF00);
}

function enableSlowDownMode() {
slowDownMode = true;
sendAnnouncement("Slow Mode on for 3 minutes.", null, 0xFF0000);
slowDownModeTimeout = setTimeout(() => {
slowDownMode = false;
sendAnnouncement("Slow Mode off.", null, 0x00FF00);
}, 3 * 60 * 1000);
}

function disableSlowDownMode() {
slowDownMode = false;
clearTimeout(slowDownModeTimeout);
sendAnnouncement("Slow Mode off.", null, 0x00FF00);
}

// Function to send Discord link periodically


function sendDiscordLink() {
sendAnnouncement("🔊 Discord: " + discordLink);
}
sendDiscordLink();
setInterval(sendDiscordLink, 5 * 60 * 1000); // Send Discord link every 5 minutes

function normalizeName(name) {
return name.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
}

function findPlayerByName(partialName) {
let normalizedSearchName = normalizeName(partialName);
return room.getPlayerList().find(player =>
normalizeName(player.name).includes(normalizedSearchName));
}

// Swap all players' teams


function swapTeams() {
let players = room.getPlayerList().filter(p => p.team !== 0);
players.forEach(player => {
let newTeam = player.team === 1 ? 2 : 1; // 1: Red, 2: Blue
room.setPlayerTeam(player.id, newTeam);
});
sendAnnouncement("Teams swapped!", null, 0xACE1AF);
}

// Function to show banned players


function showBans(player) {
if (!isAdmin(player)) {
sendAnnouncement("Only admins can view banned players.", player.id,
0xFF0000);
return;
}
room.getPlayerList().forEach(player => {
if (player.conn === "BANNED") {
sendAnnouncement("Banned players: " + player.name, player.id,
0x00FF00);
}
});
}

// Function to clear banned players


function clearBans(player) {
if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admins can clear bans.", player.id, 0xFF0000);
return;
}
room.clearBans();
sendAnnouncement("All bans have been cleared.", null, 0x00FF00);
}

// Simplified Event Handlers


room.onPlayerJoin = function (player) {
updateAdmins();
sendAnnouncement("Welcome " + player.name + "!", null, 0xFFFF80);
if (!superAdmin && admins.length === 0) {
grantAdmin(player);
}
updateLastMovement(player); // Initialize last movement time
}

room.onPlayerLeave = function (player) {


if (isAdmin(player)) {
removeAdmin(player);
}
if (superAdmin === player.id) {
superAdmin = null;
}
updateAdmins();
delete lastMovementTime[player.id]; // Clean up last movement time tracking
delete previousTeams[player.id]; // Clean up previous team tracking
delete wordWarnings[player.id]; // Clean up word warnings tracking
delete kickCounts[player.id]; // Clean up kick count tracking
}

// Simplified AFK Check


function checkAFKStatus() {
let currentTime = Date.now();
room.getPlayerList().forEach(player => {
if (player.team === 0) return;

let inactivityTime = currentTime - lastMovementTime[player.id];


if (inactivityTime > 30000 && inactivityTime <= 35000) {
if (afkStatus[player.id] !== "warning30") {
sendAnnouncement(player.name + " AFK, moving in 5s.", player.id,
0xFFFF00);
afkStatus[player.id] = "warning30";
}
} else if (inactivityTime > 15000 && inactivityTime <= 20000) {
if (afkStatus[player.id] !== "warning15") {
sendAnnouncement(player.name + " move to avoid AFK.", player.id,
0xFFFF00);
afkStatus[player.id] = "warning15";
}
} else if (inactivityTime > 35000) {
previousTeams[player.id] = player.team;
room.setPlayerTeam(player.id, 0);
sendAnnouncement(player.name + " AFK, moved to spec. Type 'add' to
return in 30s.", player.id, 0xFF0000);
if (isAdmin(player)) {
removeAdmin(player);
}
if (isSuperAdmin(player)) {
superAdmin = null;
sendAnnouncement("Super admin rights have been revoked.",
player.id, 0xFF0000);
}
delete afkStatus[player.id];
setTimeout(() => {
delete previousTeams[player.id];
}, 30000);
}
});
}
setInterval(checkAFKStatus, 5000);
// Command Handling
room.onPlayerChat = function (player, message) {
// Bypass banned word checks for admin commands
if (message.startsWith("!")) {
if (message === "!showbans") {
if (!isAdmin(player)) {
sendAnnouncement("Only admins can execute this command.",
player.id, 0xFF0000);
return false;
}
showBans(player);
return false;
}

if (message === "!clearbans") {


if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
clearBans(player);
return false;
}

if (message.startsWith("!remword ")) {
if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
let word = message.substring(9).trim().toLowerCase();
if (bannedWords.includes(word)) {
bannedWords = bannedWords.filter(w => w !== word);
sendAnnouncement(`${word} removed from banned words.`, null,
0x00FF00);
} else {
sendAnnouncement(`${word} not found in banned words.`, player.id,
0xFF0000);
}
return false;
}

if (message === "!swap") {


if (!isAdmin(player) && !isSuperAdmin(player)) {
sendAnnouncement("Only admins can execute this command.",
player.id, 0xFF0000);
return false;
}
swapTeams();
return false;
}

if (message === "!admin") {


if (superAdmin && superAdmin !== player.id) {
sendAnnouncement("Super admin is present.", player.id, 0xE88D67);
} else if (admins.length < 2 && !isAdmin(player)) {
grantAdmin(player);
} else {
sendAnnouncement(admins.length >= 2 ? "Max 2 admins." : "You are
already an admin.", player.id, 0xE88D67);
}
return false;
}

if (message === "!super 69") {


if (superAdmin && superAdmin !== player.id) {
sendAnnouncement("There is already a super admin in the room.",
player.id, 0xE88D67);
} else {
superAdmin = player.id;
revokeAllAdmins(player);
room.setPlayerAdmin(player.id, true);
sendAnnouncement("Super admin assigned!", null, 0xFFD700);
}
return false;
}

if (message === "!help") {


let helpMessage = "[ ";
if (isSuperAdmin(player)) {
helpMessage += "!admin !discord !afk !bb !showbans !clearbans !swap
!mute <playerName> !permmute <playerName> !unmute <playerName> !slowmode !
endslowmode !addword <word> !remword <word> !showwords ";
} else if (isAdmin(player)) {
helpMessage += "!admin !discord !afk !bb !swap ";
} else {
helpMessage += "!discord !afk !bb ";
}
helpMessage += "]";
sendAnnouncement(helpMessage, player.id, 0x00FF00);
return false;
}

if (message === "!discord") {


sendAnnouncement("Discord: " + discordLink, null, 0xE88D67);
return false;
}

if (message === "!afk") {


afkStatus[player.id] = !afkStatus[player.id];
if (afkStatus[player.id]) {
previousTeams[player.id] = player.team;
room.setPlayerTeam(player.id, 0);
sendAnnouncement(player.name + " is AFK.", null, 0xACE1AF);
if (isAdmin(player)) {
removeAdmin(player);
}
if (isSuperAdmin(player)) {
superAdmin = null;
sendAnnouncement("Super admin rights have been revoked.",
player.id, 0xFF0000);
}
} else {
room.setPlayerTeam(player.id, previousTeams[player.id] || 1); //
Default to red team if no previous team
delete previousTeams[player.id];
sendAnnouncement(player.name + " is back.", null, 0xACE1AF);
}
return false;
}

if (message === "!bb") {


room.kickPlayer(player.id, "See you soon 👋", false);
return false;
}

// Mute Commands
if (message.startsWith("!mute ")) {
if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
let playerName = message.substring(6).trim();
let targetPlayer = findPlayerByName(playerName);
if (targetPlayer) {
mutePlayer(targetPlayer);
} else {
sendAnnouncement("Player not found: " + playerName, player.id,
0xFF0000);
}
return false;
}

if (message.startsWith("!permmute ")) {
if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
let playerName = message.substring(10).trim();
let targetPlayer = findPlayerByName(playerName);
if (targetPlayer) {
permMutePlayer(targetPlayer);
} else {
sendAnnouncement("Player not found: " + playerName, player.id,
0xFF0000);
}
return false;
}

if (message.startsWith("!unmute ")) {
if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
let playerName = message.substring(8).trim();
let targetPlayer = findPlayerByName(playerName);
if (targetPlayer) {
unmutePlayer(targetPlayer);
} else {
sendAnnouncement("Player not found: " + playerName, player.id,
0xFF0000);
}
return false;
}

if (message === "!slowmode") {


if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
enableSlowDownMode();
return false;
}

if (message === "!endslowmode") {


if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
disableSlowDownMode();
return false;
}

// Banned Words Commands


if (message.startsWith("!addword ")) {
if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
let word = message.substring(9).trim().toLowerCase();
if (!bannedWords.includes(word)) {
bannedWords.push(word);
sendAnnouncement(`${word} added to banned words.`, null, 0x00FF00);
} else {
sendAnnouncement(`${word} already banned.`, player.id, 0xFF0000);
}
return false;
}

if (message === "!showwords") {


if (!isSuperAdmin(player)) {
sendAnnouncement("Only super admin can execute this command.",
player.id, 0xFF0000);
return false;
}
sendAnnouncement(`Banned words: ${bannedWords.join(", ") || "None"}`,
player.id, 0x00FF00);
return false;
}

return true; // Allow other commands to proceed


}

// Regular banned word checks


let normalizedMessage = normalizeName(message);
let playerWarnings = wordWarnings[player.id] || 0;
for (let word of bannedWords) {
if (normalizedMessage.includes(word)) {
playerWarnings += 1;
wordWarnings[player.id] = playerWarnings;
if (playerWarnings >= 2) {
room.kickPlayer(player.id, "Kicked for using banned words.",
false);
return false;
} else {
sendAnnouncement("Warning: Do not use banned words!", player.id,
0xFF0000);
return false;
}
}
}

if (message === "add" && previousTeams[player.id]) {


room.setPlayerTeam(player.id, previousTeams[player.id]);
delete previousTeams[player.id];
updateLastMovement(player);
return false;
}

if (slowDownMode) {
const now = Date.now();
const lastMessage = lastMessageTime[player.id] || 0;
const timeDiff = now - lastMessage;
if (timeDiff < 3000) { // 3 seconds cooldown
sendAnnouncement(`Wait ${(3000 - timeDiff) / 1000}s to message.`,
player.id, 0xFF0000);
return false;
}
lastMessageTime[player.id] = now;
}

if (mutedPlayers[player.id] && mutedPlayers[player.id] > Date.now()) {


sendAnnouncement("You are muted and cannot send messages.", player.id,
0xFF0000);
return false;
}

if (permanentlyMutedPlayers.includes(player.id)) {
sendAnnouncement("You are permanently muted and cannot send messages.",
player.id, 0xFF0000);
return false;
}

return true;
}

// Restore Other Event Handlers


room.onPlayerAdminChange = function (changedPlayer, byPlayer) {
if (!changedPlayer.admin && byPlayer && byPlayer.id !== 0 && !
isSuperAdmin(byPlayer)) {
room.setPlayerAdmin(changedPlayer.id, true);
sendAnnouncement("Admins can't remove other admins. Type !afk to give up
admin rights.", byPlayer.id, 0xFF0000);
}
updateAdmins();
}

room.onTeamGoal = function (team) {


let scores = room.getScores();
let timestamp = `${Math.floor(scores.time / 60)}:${Math.floor(scores.time %
60).toString().padStart(2, '0')}`;
let scorerName = lastTouchPlayer ? lastTouchPlayer.name : "Unknown";
let assistName = secondLastTouchPlayer ? secondLastTouchPlayer.name : "-";

if (lastTouchPlayer && lastTouchPlayer.id === secondLastTouchPlayer?.id) {


// Goal announcement without assist
sendAnnouncement("═════════════════════════════", null, 0xFFFF00);
sendAnnouncement(`💥 ${timestamp} 💥`, null, 0xFFFF00);
sendAnnouncement(`🌟 Goal - ${scorerName}`, null, 0xFFFF00);
sendAnnouncement(`🎯 Score: 🟥 ${scores.red} - ${scores.blue} 🟦`, null,
0xFFFF00);
sendAnnouncement("═════════════════════════════", null, 0xFFFF00);
} else {
// Regular goal announcement with assist
sendAnnouncement("═════════════════════════════", null, 0xFFFF00);
sendAnnouncement(`💥 ${timestamp} 💥`, null, 0xFFFF00);
sendAnnouncement(`🌟 Goal - ${scorerName}`, null, 0xFFFF00);
sendAnnouncement(`✨ Assist: ${assistName}`, null, 0xFFFF00);
sendAnnouncement(`🎯 Score: 🟥 ${scores.red} - ${scores.blue} 🟦`, null,
0xFFFF00);
sendAnnouncement("═════════════════════════════", null, 0xFFFF00);
}

lastTouchPlayer = null; // Reset the last touch player after a goal


secondLastTouchPlayer = null; // Reset the second last touch player after a
goal
}

room.onPlayerBallKick = function (player) {


secondLastTouchPlayer = lastTouchPlayer;
lastTouchPlayer = player;
updateLastMovement(player);
}

room.onPlayerTeamChange = function (player) {


updateLastMovement(player);
}

room.onPlayerActivity = function (player) {


updateLastMovement(player);
}

You might also like