Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* VARIABLES */
- /* ROOM */
- const roomName = "πͺ POWER BIG 3v3 [24/7]";
- const botName = "PowerBot";
- const maxPlayers = 12;
- const roomPublic = true;
- const geo = [{"code": "HT", "lat": 51.9, "lon": 19.1}];
- const room = HBInit({ roomName: roomName, maxPlayers: maxPlayers, public: roomPublic, playerName: botName, geo: geo[0] });
- const scoreLimit = 3;
- const timeLimit = 3;
- room.setScoreLimit(scoreLimit);
- room.setTimeLimit(timeLimit);
- room.setTeamsLock(true);
- var adminPassword = 100 + getRandomInt(900);
- console.log("adminPassword : " + adminPassword);
- /* OPTIONS */
- var drawTimeLimit = Infinity;
- /* PLAYERS */
- const Team = { SPECTATORS: 0, RED: 1, BLUE: 2 };
- var players;
- var teamR;
- var teamB;
- var teamS;
- /* GAME */
- var lastPlayersTouched;
- var point = [{"x": 0, "y": 0}, {"x": 0, "y": 0}];
- var ballSpeed;
- var goldenGoal = false;
- /* AUXILIARY */
- var checkTimeVariable = false;
- /* FUNCTIONS */
- /* AUXILIARY FUNCTIONS */
- function getRandomInt(max) { // return random number from 0 to max-1
- return Math.floor(Math.random() * Math.floor(max));
- }
- function arrayMin(arr) {
- var len = arr.length;
- var min = Infinity;
- while (len--) {
- if (arr[len] < min) {
- min = arr[len];
- }
- }
- return min;
- }
- function getTime(scores) {
- return "[" + Math.floor(Math.floor(scores.time/60)/10).toString() + Math.floor(Math.floor(scores.time/60)%10).toString() + ":" + Math.floor(Math.floor(scores.time - (Math.floor(scores.time/60) * 60))/10).toString() + Math.floor(Math.floor(scores.time - (Math.floor(scores.time/60) * 60))%10).toString() + "]"
- }
- function pointDistance(p1, p2) {
- var d1 = p1.x - p2.x;
- var d2 = p1.y - p2.y;
- return Math.sqrt(d1 * d1 + d2 * d2);
- }
- /* GAME FUNCTIONS */
- function checkTime() {
- const scores = room.getScores();
- if (Math.abs(scores.time - scores.timeLimit) <= 0.01 && scores.timeLimit != 0) {
- if (scores.red != scores.blue) {
- if (checkTimeVariable == false) {
- checkTimeVariable = true;
- setTimeout(() => { checkTimeVariable = false; }, 3000);
- scores.red > scores.blue ? endGame(Team.RED) : endGame(Team.BLUE);
- setTimeout(() => { room.stopGame(); }, 2000);
- }
- return;
- }
- goldenGoal = true;
- room.sendChat("β½ First goal wins! β½");
- }
- if (Math.abs(drawTimeLimit * 60 - scores.time - 60) <= 0.01 && players.length > 2) {
- if (checkTimeVariable == false) {
- checkTimeVariable = true;
- setTimeout(() => { checkTimeVariable = false; }, 10);
- room.sendChat("β 60 seconds left until draw! β");
- }
- }
- if (Math.abs(scores.time - drawTimeLimit * 60) <= 0.01 && players.length > 2) {
- if (checkTimeVariable == false) {
- checkTimeVariable = true;
- setTimeout(() => { checkTimeVariable = false; }, 10);
- endGame(Team.SPECTATORS);
- room.stopGame();
- goldenGoal = false;
- }
- }
- }
- function endGame(winner) { // no stopGame() function in it
- const scores = room.getScores();
- if (winner == Team.RED) {
- room.sendChat("π΄ Red Team won " + scores.red + "-" + scores.blue + "!");
- }
- else if (winner == Team.BLUE) {
- room.sendChat("π΅ Blue Team won " + scores.blue + "-" + scores.red + "!");
- }
- else {
- room.sendChat("π€ Draw limit reached! π€");
- }
- }
- /* PLAYER FUNCTIONS */
- function updateTeams() {
- players = room.getPlayerList().filter((player) => player.id != 0);
- teamR = players.filter(p => p.team === Team.RED);
- teamB = players.filter(p => p.team === Team.BLUE);
- teamS = players.filter(p => p.team === Team.SPECTATORS);
- }
- function updateAdmins() {
- if (players.length == 0 || players.find((player) => player.admin) != null) {
- return;
- }
- var copie = [];
- players.forEach(function(element) { copie.push(element.id); });
- room.setPlayerAdmin(arrayMin(copie), true); // Give admin to the player who's played the longest on the room
- }
- /* STATS FUNCTIONS */
- function getStats() {
- const ballPosition = room.getBallPosition();
- point[1] = point[0];
- point[0] = ballPosition;
- ballSpeed = (pointDistance(point[0], point[1]) * 60 * 60 * 60)/15000;
- }
- /* EVENTS */
- /* PLAYER MOVEMENT */
- room.onPlayerJoin = function(player) {
- room.sendChat("[PM] π Welcome " + player.name + " ! This 24/7 host is in development, but you can play !", player.id);
- updateTeams();
- updateAdmins();
- }
- room.onPlayerTeamChange = function(changedPlayer, byPlayer) {
- if (changedPlayer.id == 0) {
- room.setPlayerTeam(0, Team.SPECTATORS);
- return;
- }
- updateTeams();
- }
- room.onPlayerLeave = function(player) {
- updateTeams();
- updateAdmins();
- }
- room.onPlayerKicked = function(kickedPlayer, reason, ban, byPlayer) {
- }
- /* PLAYER ACTIVITY */
- room.onPlayerChat = function(player, message) {
- message = message.split(" ");
- if (["!claim"].includes(message[0].toLowerCase())) {
- if (message[1] == adminPassword) {
- room.setPlayerAdmin(player.id, true);
- adminPassword = 100 + getRandomInt(900);
- console.log("adminPassword : " + adminPassword);
- }
- }
- if (message[0][0] == "!") {
- return false;
- }
- }
- room.onPlayerActivity = function(player) {
- }
- room.onPlayerBallKick = function(player) {
- if (lastPlayersTouched[0] == null || player.id != lastPlayersTouched[0].id) {
- lastPlayersTouched[1] = lastPlayersTouched[0];
- lastPlayersTouched[0] = player;
- }
- }
- /* GAME MANAGEMENT */
- room.onGameStart = function(byPlayer) {
- goldenGoal = false;
- lastPlayersTouched = [null, null];
- }
- room.onGameStop = function(byPlayer) {
- }
- room.onGamePause = function(byPlayer) {
- }
- room.onGameUnpause = function(byPlayer) {
- }
- room.onTeamGoal = function(team) {
- const scores = room.getScores();
- if (lastPlayersTouched[0] != null && lastPlayersTouched[0].team == team) {
- if (lastPlayersTouched[1] != null && lastPlayersTouched[1].team == team) {
- room.sendChat("β½ " + getTime(scores) + " Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "π΄" : "π΅"));
- }
- else {
- room.sendChat("β½ " + getTime(scores) + " Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "π΄" : "π΅"));
- }
- }
- else {
- room.sendChat("π " + getTime(scores) + " Own Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "π΄" : "π΅"));
- }
- if (scores.red == scores.scoreLimit || scores.blue == scores.scoreLimit || goldenGoal == true) {
- endGame(team);
- goldenGoal = false;
- setTimeout(() => { room.stopGame(); }, 1000);
- }
- }
- room.onPositionsReset = function() {
- lastPlayersTouched = [null, null];
- }
- /* MISCELLANEOUS */
- room.onRoomLink = function(url) {
- }
- room.onPlayerAdminChange = function(changedPlayer, byPlayer) {
- }
- room.onStadiumChange = function(newStadiumName, byPlayer) {
- }
- room.onGameTick = function() {
- checkTime();
- getStats();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement