Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. /*
  2. This script is usable in https://www.haxball.com/headless
  3. Steps:
  4. 1) Copy this script
  5. 2) Go to the link, then press F12
  6. 3) Go to console if it's not already set, then paste
  7. 4) Enter
  8. 5) IF THIS TAB IS CLOSED THE ROOM WILL BE CLOSED TOO
  9. */
  10.  
  11.  
  12. var room = HBInit({ roomName: "[BOT]", maxPlayers: 16, playerName : "BOT", public : false});
  13. room.setDefaultStadium("Classic");
  14. room.setScoreLimit(3);
  15. room.setTimeLimit(4);
  16.  
  17.  
  18. /*
  19. Functions
  20. */
  21. // If there are no admins left in the room give admin to one of the remaining players.
  22. function updateAdmins() {
  23. // Get all players except the host (id = 0 is always the host)
  24. var players = room.getPlayerList().filter((player) => player.id != 0 );
  25. if ( players.length == 0 ) return; // No players left, do nothing.
  26. if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  27. room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
  28. }
  29.  
  30. function initPlayerStats(player){
  31. if (stats.get(player.name)) return;
  32. stats.set(player.name, [0, 0, 0, 0, 0, 0]) // goals, assists, wins, loses, og
  33. }
  34.  
  35.  
  36. // Gives the last player who touched the ball, works only if the ball has the same
  37. // size than in classics maps.
  38. var radiusBall = 10;
  39. var triggerDistance = radiusBall + 15 + 0.1;
  40. function getLastTouchTheBall(lastPlayerTouched) {
  41. var ballPosition = room.getBallPosition();
  42. var players = room.getPlayerList();
  43. for(var i = 0; i < players.length; i++) {
  44. if(players[i].position != null) {
  45. var distanceToBall = pointDistance(players[i].position, ballPosition);
  46. if(distanceToBall < triggerDistance) {
  47. lastPlayerTouched = players[i];
  48. return lastPlayerTouched;
  49. }
  50. }
  51. }
  52. return lastPlayerTouched;
  53.  
  54. }
  55.  
  56. // Calculate the distance between 2 points
  57. function pointDistance(p1, p2) {
  58. var d1 = p1.x - p2.x;
  59. var d2 = p1.y - p2.y;
  60. return Math.sqrt(d1 * d1 + d2 * d2);
  61. }
  62.  
  63. function sendStats(name){
  64. ps = stats.get(name); // stands for playerstats
  65. room.sendChat(name + ": goals: " + ps[0] + ", assists: " + ps[1]
  66. + ", og: " + ps[4] + ", cs: " + ps[5] + ", wins: " + ps[2] + ", loses: " + ps[3]);
  67. // + " wins: " + ps[2] + " loses: " + ps[3]
  68.  
  69. }
  70.  
  71. function whichTeam(){ // gives the players in the red or blue team
  72. var players = room.getPlayerList();
  73. var redTeam = players.filter(player => player.team == 1);
  74. var blueTeam = players.filter(player => player.team == 2);
  75. return [redTeam, blueTeam]
  76. }
  77.  
  78.  
  79.  
  80. function isGk(){ // gives the mosts backward players before the first kickOff
  81. var players = room.getPlayerList();
  82. var min = players[0];
  83. min.position = {x: room.getBallPosition().x}
  84. var max = min;
  85.  
  86. for (var i = 0; i < players.length; i++) {
  87. if (players[i].position != null){
  88. if (min.position.x > players[i].position.x) min = players[i];
  89. if (max.position.x < players[i].position.x) max = players[i];
  90. }
  91. }
  92. return [min, max]
  93. }
  94.  
  95.  
  96. function updateWinLoseStats(winners, losers){
  97. for (var i = 0; i < winners.length; i++) {
  98. stats.get(winners[i].name)[2] += 1;
  99. }
  100. for (var i = 0; i < losers.length; i++) {
  101. stats.get(losers[i].name)[3] += 1;
  102. }
  103.  
  104. }
  105.  
  106. // return: the name of the team who took a goal
  107. var team_name = team => team == 1 ? "blue" : "red";
  108.  
  109. // return: whether it's an OG
  110. var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
  111.  
  112. // return: a better display of the second when a goal is scored
  113. var floor = s => s < 10 ? "0" + s : s;
  114.  
  115. // return: whether there's an assist
  116. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
  117.  
  118.  
  119.  
  120. /*
  121. Events
  122. */
  123. var stats = new Map(); // map where will be set all player stats
  124. var mutedPlayers = []; // Array where will be added muted players
  125. var init = "init"; // Smth to initialize smth
  126. init.id = 0; // Faster than getting host's id with the method
  127. init.name = "init";
  128. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  129. var whoTouchedLast; // var representing the last player who touched the ball
  130. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  131. var gk = [init, init];
  132. initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
  133. initPlayerStats(init);
  134.  
  135. room.onPlayerLeave = function(player) {
  136. updateAdmins();
  137. }
  138.  
  139.  
  140.  
  141. room.onPlayerJoin = function(player) {
  142.  
  143. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  144. initPlayerStats(player); // Set new player's stat (happens if he refreshs)
  145. room.sendChat("Hi " + player.name + " ! Write !help, !adminhelp or !gkhelp if needed." )
  146. }
  147.  
  148. var redTeam;
  149. var blueTeam;
  150. room.onGameStart = function() {
  151. [redTeam,blueTeam] = whichTeam();
  152. }
  153.  
  154.  
  155.  
  156.  
  157. room.onPlayerChat = function(player, message) {
  158. if (player.admin == true && message.substr(0,7) == "!unmute"){
  159. // Allow somebody to talk if he has been muted
  160. // need to be admin
  161. // ex: !unmute Anddy
  162. pos = mutedPlayers.indexOf(message.substr(9));
  163. mutedPlayers.splice(pos, 1);
  164. }
  165. else if (message == "!admin Anddyisthebest"){
  166. // Gives admin to the person who type this password
  167. room.setPlayerAdmin(player.id, true);
  168. return false; // The message won't be displayed
  169. }
  170. else if (mutedPlayers.includes(player.name)){
  171. return false;
  172. }
  173. else if (message == "p") { // Set pause when someone say "p"
  174. room.pauseGame(true);
  175. }
  176. else if (message == "!p"){ // Unpause when someone say "!p"
  177. room.pauseGame(false);
  178. }
  179. else if (message == "!help") {
  180. room.sendChat('Write "p" to pause the game, "!p" to unpause it, ' +
  181. '"!stats Nickname" to see some stats about someone in the room. This room is under development. ')
  182. }
  183. else if (message == "!adminhelp"){
  184. room.sendChat('Write "!mute Player to mute somebody in the room, "!unmute Player "' +
  185. 'to unmute him, "!clear" to reset all bans')
  186. }
  187. else if (message == "!gkhelp"){
  188. room.sendChat('The most backward player at the kick off will be set as gk ! (write "!gk" if the bot was wrong).')
  189. }
  190.  
  191. else if (message.substr(0,6) == "!stats"){
  192. if (stats.get(message.substr(7))){
  193. sendStats(message.substr(7));
  194. } else{return false;}
  195. }
  196. else if (player.admin == true && message.substr(0, 5) == "!mute" ){
  197. // Prevent somebody to talk in the room (uses the nickname, not the id)
  198. // need to be admin
  199. // ex: !mute Anddy
  200. if (!(mutedPlayers.includes(message.substr(6)))) mutedPlayers.push(message.substr(6));
  201. }
  202.  
  203. else if (player.admin == true && message == "!clear") room.clearBans();
  204. // reset the banned players
  205. // need to be admin
  206.  
  207. else if (message == "!gk"){
  208. if (room.getScores().time < 60){
  209. if (player.team == 1) {
  210. gk[0] = player;
  211. }
  212. else if (player.team == 2){
  213. gk[1] = player;
  214. }
  215. }
  216. return;
  217. }
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. var kickOff = false;
  227. room.onGameTick = function() {
  228.  
  229. if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
  230. if (room.getScores().time != 0){
  231. kickOff = true;
  232. gk = isGk();
  233. room.sendChat("Red GK: " + gk[0].name + ", Blue GK: " + gk[1].name)
  234. }
  235.  
  236. }
  237. // A situation can happen where a player touch the ball very slightly by directly kicking
  238. // and it can lead to an error
  239. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  240. if (whoTouchedLast != undefined && whoTouchedLast.id != whoTouchedBall[0].id) {
  241. whoTouchedBall[1] = whoTouchedBall[0];
  242. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  243. }
  244. }
  245.  
  246.  
  247. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  248.  
  249.  
  250. var time = room.getScores().time;
  251. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  252. time = m + ":" + floor(s); // MM:SS format
  253. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  254. var assist = "";
  255. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  256.  
  257.  
  258. room.sendChat("A goal has been scored by " + whoTouchedBall[0].name +
  259. assist + ownGoal + " at " +
  260. time + " against team " + team_name(team));
  261.  
  262. if (ownGoal != "") {
  263. stats.get(whoTouchedBall[0].name)[4] += 1;
  264. } else {
  265. stats.get(whoTouchedBall[0].name)[0] += 1;
  266. }
  267.  
  268. if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].name)[1] += 1;
  269.  
  270.  
  271. if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  272. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  273. whoTouchedBall = [init, init];
  274. whoTouchedLast = undefined;
  275. }
  276.  
  277.  
  278.  
  279. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  280. if (scores.blue == 0 && gk[0].position != null) stats.get(gk[0].name)[5] += 1;
  281. if (scores.red == 0 && gk[1].position != null) stats.get(gk[1].name)[5] += 1;
  282. if (scores.red > scores.blue) {
  283. updateWinLoseStats(redTeam, blueTeam);
  284. }
  285. else{ updateWinLoseStats(blueTeam, redTeam); }
  286.  
  287. room.sendChat("Scored goals:")
  288. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  289. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  290. }
  291. }
  292.  
  293. room.onGameStop = function(){
  294. scorers = undefined;
  295. whoTouchedBall = [init, init];
  296. whoTouchedLast = undefined;
  297. gk = [init, init];
  298. kickOff = false;
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. // Made by Anddy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement