Advertisement
Guest User

Untitled

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