SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var room = HBInit({ roomName: "[BOT]", maxPlayers: 16, playerName : "BOT", public : false});
  2. room.setDefaultStadium("Classic");
  3. room.setScoreLimit(3);
  4. room.setTimeLimit(3);
  5.  
  6.  
  7.  
  8. /*
  9.     Functions
  10. */
  11. // If there are no admins left in the room give admin to one of the remaining players.
  12. function updateAdmins() {
  13.   // Get all players except the host (id = 0 is always the host)
  14.   var players = room.getPlayerList().filter((player) => player.id != 0 );
  15.   if ( players.length == 0 ) return; // No players left, do nothing.
  16.   if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  17.   room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
  18. }
  19.  
  20. function initPlayerStats(id){
  21.     stats.set(id, [0, 0, 0, 0, 0]) // goals, assists, wins, loses, og
  22. }
  23.  
  24.  
  25. // Gives the last player who touched the ball, works only if the ball has the same
  26. // size than in classics maps.
  27. var radiusBall = 10;
  28. var triggerDistance = radiusBall + 15 + 0.1;
  29. function getLastTouchTheBall(lastPlayerTouched) {
  30.     var ballPosition = room.getBallPosition();
  31.     var players = room.getPlayerList();
  32.     for(var i = 0; i < players.length; i++) {
  33.         if(players[i].position != null) {
  34.                         var distanceToBall = pointDistance(players[i].position, ballPosition);
  35.             if(distanceToBall < triggerDistance) {
  36.                 lastPlayerTouched = players[i];
  37.             }
  38.         }
  39.     }
  40.         return lastPlayerTouched;
  41.  
  42. }
  43.  
  44. // Calculate the distance between 2 points
  45. function pointDistance(p1, p2) {
  46.     var d1 = p1.x - p2.x;
  47.     var d2 = p1.y - p2.y;
  48.     return Math.sqrt(d1 * d1 + d2 * d2);
  49. }
  50.  
  51.  
  52. // return: the name of the team who took a goal
  53. var team_name = team => team == 1 ? "blue" : "red";
  54.  
  55. // return: whether it's an OG
  56. var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
  57.  
  58. // return: a better display of the second when a goal is scored
  59. var floor = s => s < 10 ? "0" + s : s;
  60.  
  61. // return: whether there's an assist
  62. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
  63.  
  64.  
  65.  
  66. /*
  67. Events
  68. */
  69. var stats = new Map(); // map where will be set all player stats
  70. var mutedPlayers = []; // Array where will be added muted players
  71. var init = "init"; // Smth to initialize smth
  72. init.id = 0 // Faster than getting host's id with the method
  73. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  74. var whoTouchedLast; // var representing the last player who touched the ball
  75. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  76.  
  77.  
  78. room.onPlayerLeave = function(player) {
  79.   updateAdmins();
  80. }
  81.  
  82.  
  83.  
  84. room.onPlayerJoin = function(player) {
  85.  
  86.     updateAdmins(); // Gives admin to the first player who join the room if there's no one
  87.     initPlayerStats(player.id); // Set new player's stat (happens if he refreshs)
  88.     room.sendChat("Hi " + player.name + " ! Write !help or !adminhelp if needed." )
  89. }
  90.  
  91. room.onPlayerChat = function(player, message) {
  92.     if (message == "p") { // Set pause when someone say "p"
  93.         room.pauseGame(true);
  94.     }
  95.     else if (message == "!p"){ // Unpause when someone say "!p"
  96.         room.pauseGame(false);
  97.     }
  98.     else if (message == "!help") {
  99.         room.sendChat('Write "p" to pause the game, "!p" to unpause it, ' +
  100.         '"!stats" to see some stats about yourself in the room. This room is under development. ')
  101.     }
  102.     else if (message == "!adminhelp"){
  103.         room.sendChat('Write "!mute Player to mute somebody in the room, "!unmute Player "' +
  104.         'to unmute him, "!clear" to reset all bans')
  105.     }
  106.     else if (message == "!stats") {
  107.         ps = stats.get(player.id); // stands for playerstats
  108.         room.sendChat(player.name + ": goals: " + ps[0] + " ,assists: " + ps[1]
  109.         + " ,og: " + ps[4]);
  110.         // + " wins: "  + ps[2] + " loses: " + ps[3]
  111.     }
  112.     else if (message == "!admin Anddyisthebest"){
  113.         // Gives admin to the person why type this password
  114.         room.setPlayerAdmin(player.id, true);
  115.         return false; // The message won't be displayed
  116.     }
  117.     else if (player.admin == true && message.substr(0, 5) == "!mute" ){
  118.         // Prevent somebody to talk in the room (uses the nickname, not the id)
  119.         // need to be admin
  120.         // ex: !mute Anddy
  121.         if (!(mutedPlayers.includes(message.substr(6)))) mutedPlayers.push(message.substr(6));
  122.     }
  123.     else if (mutedPlayers.includes(player.name)){
  124.         return false;
  125.     }
  126.     else if (player.admin == true && message.substr(0,7) == "!unmute"){
  127.         // Allow somebody to talk if he has been muted
  128.         // need to be admin
  129.         // ex: !unmute Anddy
  130.         pos = mutedPlayers.indexOf(message.substr(9));
  131.         mutedPlayers.splice(pos, 1);
  132.     }
  133.     else if (player.admin == true && message == "!clear") room.clearBans();
  134.     // reset the banned players
  135.     // need to be admin
  136.  
  137. }
  138.  
  139. room.onPlayerBallKick = function(player){
  140.     // Gets the last one who kicked the ball
  141.     lastKicker = [player, room.getScores().time]
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. room.onGameTick = function() {
  149.     // A situation can happen where a player touch the ball very slightly by directly kicking
  150.     // and it can lead to an error
  151.  
  152.     whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  153.     if (whoTouchedLast != undefined && whoTouchedLast.id != whoTouchedBall[0].id) {
  154.         whoTouchedBall[1] = whoTouchedBall[0];
  155.         whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  156.     }
  157. }
  158.  
  159.  
  160. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  161.  
  162.  
  163.     var time = room.getScores().time;
  164.     var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  165.     time = m + ":" + floor(s); // MM:SS format
  166.     var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  167.     var assist = "";
  168.     if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  169.  
  170.  
  171.     room.sendChat("A goal has been scored by " + whoTouchedBall[0].name +
  172.      assist + ownGoal + " at " +
  173.      time + " against team " + team_name(team));
  174.  
  175.      if (ownGoal != "") {
  176.          stats.get(whoTouchedBall[0].id)[4] += 1;
  177.      } else {
  178.          stats.get(whoTouchedBall[0].id)[0] += 1;
  179.      }
  180.  
  181.     if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].id)[1] += 1;
  182.  
  183.  
  184.     if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  185.     scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  186.     whoTouchedBall = [init, init];
  187.     whoTouchedLast = undefined;
  188. }
  189.  
  190.  
  191.  
  192. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  193.  
  194.     room.sendChat("Scored goals:")
  195.     for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  196.         room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  197.     }
  198. }
  199.  
  200. room.onGameStop = function(){
  201.     scorers = undefined;
  202.     whoTouchedBall = [init, init];
  203.     whoTouchedLast = undefined;
  204. }
  205.  
  206. function isOutsideStadium(ballPosition) {
  207.     return ballPosition.x > stadiumWidth || ballPosition.x < -stadiumWidth || ballPosition.y > stadiumHeight || ballPosition.y < -stadiumHeight;
  208. }
  209.  
  210. var isBallOutsideStadium = false;
  211.  
  212. function checkBallPosition() {
  213.     var ballPosition = room.getBallPosition();
  214.     if(isOutsideStadium(ballPosition)) {
  215.         if(!isBallOutsideStadium) {
  216.             isBallOutsideStadium = true;
  217.             exitingPos = ballPosition.x;
  218.             var totalScores = room.getScores().red + room.getScores().blue;
  219.             if(lastScores != totalScores) {
  220.                 lastScores = totalScores;
  221.                 return false;
  222.             }
  223.             if(ballPosition.x > stadiumWidth && lastTeamTouched == Team.RED || ballPosition.x < -stadiumWidth && lastTeamTouched == Team.BLUE) {
  224.                 lastCall = "[A] Kale Vuruşu";
  225.                 room.sendChat("[A] Kale Vuruşu Dokunmayın Karşı Takım");
  226.             }
  227.             else if(ballPosition.x > stadiumWidth && lastTeamTouched == Team.BLUE || ballPosition.x < -stadiumWidth && lastTeamTouched == Team.RED) {
  228.                 room.sendChat("[K] Köşe Vuruşu İyi Hizala Kısa Diyerek Aut Kullanma");
  229.                 lastCall = "[K] Köşe Vuruşu İyi Hizala Kısa Diyerek Aut Kullanma";
  230.             }
  231.             else {
  232.                 isBallKickedOutside = false;
  233.                 room.sendChat(lastTeamTouched == Team.RED ? "[B] Taç mavi takımda kırmızı sakın dokunma" : "[R] Taç kırmızı takımda mavi sakın dokunma");
  234.                 lastCall = lastTeamTouched == Team.RED ? "2" : "1";
  235.             }
  236.  
  237.         }
  238.     }
  239.     else {
  240.         isBallOutsideStadium = false;
  241.         backMSG = true;
  242.  
  243.     }
  244.     return true;
  245. }
  246.  
  247. function getLastTouchTheBall() {
  248.     var ballPosition = room.getBallPosition();
  249.     var players = room.getPlayerList();
  250.     for(var i = 0; i < players.length; i++) {
  251.         if(players[i].position != null) {
  252.             var distanceToBall = pointDistance(players[i].position, ballPosition);
  253.             if(distanceToBall < triggerDistance) {
  254.                 if(lastPlayerTouched!=players[i].name)
  255.                 {
  256.                     if(lastTeamTouched==players[i].team)
  257.                     {
  258.                         assistingTouch = lastPlayerTouched;
  259.                     }else assistingTouch = "";
  260.                 }
  261.                 lastTeamTouched = players[i].team;
  262.                 previousPlayerTouched == lastPlayerTouched;
  263.                 lastPlayerTouched = players[i].name;
  264.             }
  265.         }
  266.     }
  267.     return lastPlayerTouched;
  268. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top