Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
2,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.46 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. geo = {"code": "eu", "lat": 52.5192, "lon": 13.4061}
  12. var room = HBInit({ roomName: "[BOT] FS VS NOOB [VPS][Anddy]", maxPlayers: 16, playerName : "BOT", public : false, geo});
  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. for commands
  39. */
  40.  
  41. function swapFun(player){
  42. if (player.admin == true){
  43. if (room.getScores() == null) {
  44. players = room.getPlayerList();
  45. for (i = 0; i < players.length; i++){
  46. if (players[i].team == 1){
  47. room.setPlayerTeam(players[i].id, 2);
  48. }
  49. else if (players[i].team == 2){
  50. room.setPlayerTeam(players[i].id, 1);
  51. }
  52. }
  53. }
  54. }
  55. }
  56.  
  57.  
  58. function pushMuteFun(player, message){ // !mute Anddy
  59. // Prevent somebody to talk in the room (uses the nickname, not the id)
  60. // need to be admin
  61. if (player.admin == true){
  62. if (!(mutedPlayers.includes(message.substr(6)))) mutedPlayers.push(message.substr(6));
  63. }
  64. }
  65.  
  66.  
  67. function gotMutedFun(player){
  68. if (mutedPlayers.includes(player.name)){
  69. return true;
  70. }
  71. }
  72.  
  73. function unmuteFun(player, message){ // !unmute Anddy
  74. // Allow somebody to talk if he has been muted
  75. // need to be admin
  76. if (player.admin == true){
  77. pos = mutedPlayers.indexOf(message.substr(9));
  78. mutedPlayers.splice(pos, 1);
  79. }
  80. }
  81.  
  82. function adminFun(player, message){ // !admin Anddyisthebest
  83. // Gives admin to the person who type this password
  84.  
  85. room.setPlayerAdmin(player.id, true);
  86. return false; // The message won't be displayed
  87. }
  88.  
  89. function putPauseFun() { // p
  90. room.pauseGame(true);
  91. }
  92.  
  93. function unPauseFun() { // !p
  94. room.pauseGame(false);
  95. }
  96.  
  97. function helpFun() { // !help
  98. room.sendChat('Available commands: "p", "!p" , "!stats Nickname", "!ranking", "!poss", "!resetstats", "!adminhelp", "!gkhelp", "!rankhelp"');
  99. }
  100.  
  101. function adminHelpFun() {
  102. room.sendChat('Available commands: "!mute Player", "!unmute Player", ' +
  103. '"!clearbans", "!swap" (to switch reds and blues). You need to be admin.')
  104. }
  105.  
  106.  
  107. function gkHelpFun() { // !gkhelp
  108. room.sendChat('The most backward player at the kick off will be set as gk ! (write "!gk" if the bot was wrong).')
  109. }
  110. function rankHelpFun() { // !gkhelp
  111. 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.")
  112. }
  113.  
  114.  
  115. function statsFun(player, message){ // !stats Anddy
  116. if (stats.get(message.substr(7))){
  117. sendStats(message.substr(7));
  118. } else{ return false;}
  119. }
  120.  
  121. function rankFun() { // !ranking
  122. string = ranking();
  123. room.sendChat("Ranking: " + string);
  124. }
  125.  
  126. function resetStatsFun (player){ // !resetstats
  127. if (rankingCalc(player.name) > 0){
  128. stats.set(player.name, [0,0,0,0,0,0]);
  129. room.sendChat("Your stats have been reseted ! ")
  130. }
  131. else (room.sendChat("You must have positive points to be able to reset it, sorry."))
  132. }
  133.  
  134. function clearFun(player){ // !clear
  135. if (player.admin == true) room.clearBans();
  136. }
  137.  
  138. function resetFun(player){
  139. if (player.admin == true){
  140. room.stopGame();
  141. room.startGame();
  142. }
  143. }
  144.  
  145. function gkFun(player){ // !gk
  146. if (room.getScores().time < 60){
  147. if (player.team == 1) {
  148. gk[0] = player;
  149. }
  150. else if (player.team == 2){
  151. gk[1] = player;
  152. }
  153. }
  154. return;
  155. }
  156.  
  157.  
  158. /*
  159. For ranking
  160. */
  161.  
  162. function rankingCalc(player){
  163. return stats.get(player)[0] * 5 + stats.get(player)[1] * 3 +
  164. stats.get(player)[2] * 3 + stats.get(player)[5] * 6 -
  165. stats.get(player)[3] * 7 - stats.get(player)[4] * 4;
  166. }
  167.  
  168. function ranking(){
  169.  
  170. var overall = [];
  171. players = Array.from(stats.keys());
  172. for (var i = 2; i < players.length; i++) {
  173. score = rankingCalc(players[i])
  174. // Goal: 5 pts, assist: 3 pts, win: 3 pts, cs: 6 pts, lose: -7 pts, og: -4 pts
  175. overall.push({name: players[i], value: score});
  176. }
  177. overall.sort(function(a,b){
  178. return b.value - a.value;
  179. })
  180. string = "";
  181.  
  182. for (var i = 0; i < overall.length; i++) {
  183. if (overall[i].value != 0){
  184. string += i+1 + ") " + overall[i].name + ": " + overall[i].value + " pts, ";
  185. }
  186. }
  187. return string;
  188. }
  189.  
  190. function sendStats(name){
  191. ps = stats.get(name); // stands for playerstats
  192. room.sendChat(name + ": goals: " + ps[0] + ", assists: " + ps[1]
  193. + ", og: " + ps[4] + ", cs: " + ps[5] + ", wins: " + ps[2] + ", loses: " + ps[3] +
  194. " points: " + rankingCalc(name));
  195. }
  196.  
  197.  
  198. function whichTeam(){ // gives the players in the red or blue team
  199. var players = room.getPlayerList();
  200. var redTeam = players.filter(player => player.team == 1);
  201. var blueTeam = players.filter(player => player.team == 2);
  202. return [redTeam, blueTeam]
  203. }
  204.  
  205.  
  206.  
  207. function isGk(){ // gives the mosts backward players before the first kickOff
  208. var players = room.getPlayerList();
  209. var min = players[0];
  210. min.position = {x: room.getBallPosition().x + 60}
  211. var max = min;
  212.  
  213. for (var i = 0; i < players.length; i++) {
  214. if (players[i].position != null){
  215. if (min.position.x > players[i].position.x) min = players[i];
  216. if (max.position.x < players[i].position.x) max = players[i];
  217. }
  218. }
  219. return [min, max]
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. function updateWinLoseStats(winners, losers){
  227. for (var i = 0; i < winners.length; i++) {
  228. stats.get(winners[i].name)[2] += 1;
  229. }
  230. for (var i = 0; i < losers.length; i++) {
  231. stats.get(losers[i].name)[3] += 1;
  232. }
  233. }
  234.  
  235. function initBallCarrying(redTeam, blueTeam){
  236. var ballCarrying = new Map();
  237. var playing = redTeam.concat(blueTeam);
  238. for (var i = 0; i < playing.length; i++) {
  239. ballCarrying.set(playing[i].name, [0, playing[i].team]); // secs, team, %
  240. }
  241. return ballCarrying;
  242. }
  243.  
  244.  
  245.  
  246. function updateTeamPoss(value){
  247. if (value[1] == 1) redPoss += value[0];
  248. if (value[1] == 2) bluePoss += value[0];
  249. }
  250.  
  251. var bluePoss;
  252. var redPoss;
  253. function teamPossFun(){
  254. if (room.getScores() == null) return false;
  255. bluePoss = 0;
  256. redPoss = 0
  257. ballCarrying.forEach(updateTeamPoss);
  258. redPoss = Math.round((redPoss / room.getScores().time) * 100);
  259. bluePoss = Math.round((bluePoss / room.getScores().time) * 100);
  260. room.sendChat("Ball possession: red " + redPoss + " - " + bluePoss + " blue." );
  261.  
  262. }
  263.  
  264.  
  265.  
  266. /*
  267. For the game
  268. */
  269.  
  270. // Gives the last player who touched the ball, works only if the ball has the same
  271. // size than in classics maps.
  272. var radiusBall = 10;
  273. var triggerDistance = radiusBall + 15 + 0.1;
  274. function getLastTouchTheBall(lastPlayerTouched, time) {
  275. var ballPosition = room.getBallPosition();
  276. var players = room.getPlayerList();
  277. for(var i = 0; i < players.length; i++) {
  278. if(players[i].position != null) {
  279. var distanceToBall = pointDistance(players[i].position, ballPosition);
  280. if(distanceToBall < triggerDistance) {
  281. lastPlayerTouched = players[i];
  282. return lastPlayerTouched;
  283. }
  284. }
  285. }
  286. return lastPlayerTouched;
  287.  
  288. }
  289.  
  290.  
  291.  
  292. // Calculate the distance between 2 points
  293. function pointDistance(p1, p2) {
  294. var d1 = p1.x - p2.x;
  295. var d2 = p1.y - p2.y;
  296. return Math.sqrt(d1 * d1 + d2 * d2);
  297. }
  298.  
  299. function isOvertime(){
  300. scores = room.getScores();
  301. if (scores != null){
  302. if (scores.timeLimit != 0){
  303. if (scores.time > scores.timeLimit){
  304. if (scores.red == 0 && hasFinished == false){
  305. stats.get(gk[0].name)[5] += 1;
  306. stats.get(gk[1].name)[5] += 1;
  307. hasFinished = true;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. // return: the name of the team who took a goal
  314. var team_name = team => team == 1 ? "blue" : "red";
  315.  
  316. // return: whether it's an OG
  317. var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
  318.  
  319. // return: a better display of the second when a goal is scored
  320. var floor = s => s < 10 ? "0" + s : s;
  321.  
  322. // return: whether there's an assist
  323. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
  324.  
  325.  
  326.  
  327. /*
  328. Events
  329. */
  330. var stats = new Map(); // map where will be set all player stats
  331. var mutedPlayers = []; // Array where will be added muted players
  332. var init = "init"; // Smth to initialize smth
  333. init.id = 0; // Faster than getting host's id with the method
  334. init.name = "init";
  335. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  336. var whoTouchedLast; // var representing the last player who touched the ball
  337. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  338. var gk = [init, init];
  339.  
  340.  
  341. var commands = {
  342. // Command that doesnt need to know players attributes.
  343. "!help": helpFun,
  344. "!gkhelp": gkHelpFun,
  345. "!adminhelp": adminHelpFun,
  346. "!rankhelp": rankHelpFun,
  347. "!ranking": rankFun,
  348. "p": putPauseFun,
  349. "!p": unPauseFun,
  350. "!poss": teamPossFun,
  351.  
  352. // Command that need to know who is the player.
  353. "!resetstats": resetStatsFun,
  354. "!gk": gkFun,
  355. "!uzvara": adminFun,
  356.  
  357. // Command that need to know if a player is admin.
  358. "!swap": swapFun,
  359. "!rr": resetFun,
  360. "!clear": clearFun,
  361.  
  362. // Command that need to know what's the message.
  363. "!stats": statsFun,
  364.  
  365. // Command that need to know who is the player and what's the message.
  366. "!mute" : pushMuteFun,
  367. "!unmute": unmuteFun
  368.  
  369. }
  370.  
  371. initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
  372. initPlayerStats(init);
  373.  
  374. room.onPlayerLeave = function(player) {
  375. updateAdmins();
  376. }
  377.  
  378.  
  379.  
  380. room.onPlayerJoin = function(player) {
  381. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  382. initPlayerStats(player); // Set new player's stat
  383. room.sendChat("Hi " + player.name + " ! Write !help, !adminhelp, !rankhelp or !gkhelp if needed." )
  384. }
  385.  
  386. var redTeam;
  387. var blueTeam;
  388. room.onGameStart = function() {
  389. [redTeam,blueTeam] = whichTeam();
  390. ballCarrying = initBallCarrying(redTeam, blueTeam);
  391. }
  392.  
  393. room.onPlayerTeamChange = function(player){
  394. if (room.getScores() != null){
  395. if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
  396. }
  397. }
  398.  
  399.  
  400.  
  401. room.onPlayerChat = function(player, message) {
  402. if (mutedPlayers.includes(player.name)) return false;
  403. let spacePos = message.search(" ");
  404. let command = message.substr(0, spacePos !== -1 ? spacePos : message.length);
  405. if (commands.hasOwnProperty(command) == true) return commands[command](player, message);
  406.  
  407. }
  408.  
  409.  
  410.  
  411.  
  412. room.onPlayerBallKick = function (player){
  413. whoTouchedLast = player;
  414. }
  415.  
  416. var kickOff = false;
  417. var hasFinished = false;
  418.  
  419. room.onGameTick = function() {
  420.  
  421. setInterval(isOvertime, 5000, hasFinished);
  422.  
  423. if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
  424. if (room.getScores().time != 0){
  425. kickOff = true;
  426. gk = isGk();
  427. room.sendChat("Red GK: " + gk[0].name + ", Blue GK: " + gk[1].name)
  428. }
  429. }
  430. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  431.  
  432. if (whoTouchedLast != undefined) {
  433.  
  434. if (ballCarrying.get(whoTouchedLast.name)) {
  435. ballCarrying.get(whoTouchedLast.name)[0] += 1/60;
  436. }
  437.  
  438. if ( whoTouchedLast.id != whoTouchedBall[0].id){
  439. whoTouchedBall[1] = whoTouchedBall[0];
  440. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  441. }
  442. }
  443. }
  444.  
  445. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  446.  
  447.  
  448. var time = room.getScores().time;
  449. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  450. time = m + ":" + floor(s); // MM:SS format
  451. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  452. var assist = "";
  453. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  454.  
  455.  
  456. room.sendChat("A goal has been scored by " + whoTouchedBall[0].name +
  457. assist + ownGoal + " at " +
  458. time + " against team " + team_name(team));
  459.  
  460. if (ownGoal != "") {
  461. stats.get(whoTouchedBall[0].name)[4] += 1;
  462. } else {
  463. stats.get(whoTouchedBall[0].name)[0] += 1;
  464. }
  465.  
  466. if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].name)[1] += 1;
  467.  
  468.  
  469. if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  470. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  471. whoTouchedBall = [init, init];
  472. whoTouchedLast = undefined;
  473. }
  474.  
  475.  
  476.  
  477. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  478. if (scores.blue == 0 && gk[0].position != null && hasFinished == false) stats.get(gk[0].name)[5] += 1;
  479. if (scores.red == 0 && gk[1].position != null && hasFinished == false) stats.get(gk[1].name)[5] += 1;
  480. if (scores.red > scores.blue) {
  481. updateWinLoseStats(redTeam, blueTeam);
  482. }
  483. else{ updateWinLoseStats(blueTeam, redTeam); }
  484.  
  485. room.sendChat("Scored goals:")
  486. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  487. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  488. }
  489. }
  490.  
  491. room.onGameStop = function(){
  492. scorers = undefined;
  493. whoTouchedBall = [init, init];
  494. whoTouchedLast = undefined;
  495. gk = [init, init];
  496. kickOff = false;
  497. hasFinished = false;
  498. }
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507. // Made by Anddy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement