Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
69
0
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. }
  269.  
  270.  
  271. function pointDistance(p1, p2) {
  272.     var d1 = p1.x - p2.x;
  273.     var d2 = p1.y - p2.y;
  274.     return Math.sqrt(d1 * d1 + d2 * d2);
  275. }
  276.  
  277. function getPlayersNotWithinLine()
  278. {
  279.     var players = room.getPlayerList();
  280.     var found = false;
  281.     if(crossed)
  282.     {
  283.         for(var i = 0; i < players.length; i++) {
  284.             if(players[i].position != null) {
  285.                 if(players[i].team != lastTeamTouched && players[i].team!= lastCall)
  286.                 {
  287.                     if((players[i].position.y > greenLine || players[i].position.y < -greenLine) && pointDistance(room.getBallPosition(), players[i].position)<770)
  288.                     {
  289.                         for(var j = 0; j < lineCrossedPlayers.length; j++)
  290.                         {
  291.                             if(lineCrossedPlayers[j].name==players[i].name)
  292.                             {
  293.                                 lineCrossedPlayers[j].times = lineCrossedPlayers[j].times + 1;
  294.                                 room.sendChat("Cizgiyi Geçme Sünnetçi Gelir Haaa - " + players[i].name + " {" + lineCrossedPlayers[j].times + "}");
  295.                                 found = true;
  296.                             }
  297.  
  298.                         }
  299.                         if(!found)
  300.                         {
  301.                             lineCrossedPlayers.push({name: players[i].name, times: 1});
  302.                             room.sendChat("Cizgiyi Geçme Sünnetçi Gelir Haaa - " + players[i].name + " {1}");
  303.                         }
  304.                     }
  305.                 }
  306.  
  307.             }
  308.         }
  309.     }
  310.     crossed = false;
  311. }
  312.  
  313. function isBackRequired()
  314. {
  315.     var ballPosition = room.getBallPosition();
  316.     if(lastCall=="1")
  317.     {
  318.         if((ballPosition.x - exitingPos > throwInLeeway) && backMSG==true && isOutsideStadium(ballPosition))
  319.         {
  320.             backMSG = false;
  321.             room.sendChat("Yerinden kullan La Ayı");
  322.  
  323.         }
  324.         if((ballPosition.x - exitingPos < -throwInLeeway) && backMSG==true && isOutsideStadium(ballPosition))
  325.         {
  326.             backMSG = false;
  327.  
  328.  
  329.             room.sendChat("Yerinden kullan La Ayı");
  330.  
  331.  
  332.         }
  333.     }
  334.     if(lastCall=="2")
  335.     {
  336.         if((ballPosition.x - exitingPos > throwInLeeway) && backMSG==true && isOutsideStadium(ballPosition))
  337.         {
  338.             backMSG = false;
  339.             room.sendChat("Yerinden kullan La Ayı");
  340.  
  341.  
  342.         }
  343.         if((ballPosition.x - exitingPos < -throwInLeeway) && backMSG==true && isOutsideStadium(ballPosition))
  344.         {
  345.             backMSG = false;
  346.             room.sendChat("Yerinden kullan La Ayı");
  347.  
  348.         }
  349.     }
  350.  
  351.  
  352. }
  353. function isThrowInCorrect()
  354. {
  355.     var ballPosition = room.getBallPosition();
  356.     var boolCrossing = isBallCrossingTheLine();
  357.     var string = lastTeamTouched.toString();
  358.     if(boolCrossing && !isBallKickedOutside && string==lastCall && (lastCall=="1" || lastCall=="2"))
  359.     {
  360.  
  361.         if(lastCall=="2")
  362.         {
  363.             room.sendChat("[UYARI] Taçı sürükleyerek kullanma Leyn")
  364.         }
  365.         if(lastCall=="1")
  366.         {
  367.             room.sendChat("[UYARI] Taçı sürükleyerek kullanma Leyn");
  368.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement