Advertisement
Guest User

Untitled

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