Advertisement
nick191821971782

Untitled

Jan 26th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.17 KB | None | 0 0
  1. /* VARIABLES */
  2.  
  3. /* ROOM */
  4.  
  5. const roomName = "Bod's Pub";
  6. const botName = "pp";
  7. const maxPlayers = 20;
  8. const roomPublic = true;
  9.  
  10. const room = HBInit({ roomName: roomName, maxPlayers: maxPlayers, public: roomPublic, playerName: botName });
  11.  
  12. const scoreLimit = 3;
  13. const timeLimit = 3;
  14. room.setScoreLimit(scoreLimit);
  15. room.setTimeLimit(timeLimit);
  16. room.setTeamsLock(true);
  17.  
  18. var adminPassword = 100 + getRandomInt(900);
  19. console.log("adminPassword : " + adminPassword);
  20.  
  21. /* STADIUM */
  22.  
  23. const playerRadius = 15;
  24. var ballRadius = 10;
  25. const triggerDistance = playerRadius + ballRadius + 0.01;
  26.  
  27. /* OPTIONS */
  28.  
  29. var drawTimeLimit = Infinity;
  30. var maxTeamSize = 4;
  31.  
  32. /* PLAYERS */
  33.  
  34. const Team = { SPECTATORS: 0, RED: 1, BLUE: 2 };
  35. var players;
  36. var teamR;
  37. var teamB;
  38. var teamS;
  39.  
  40. /* GAME */
  41.  
  42. var lastTeamTouched;
  43. var lastPlayersTouched;
  44. var goldenGoal = false;
  45. var activePlay = false;
  46. var muteList = [];
  47.  
  48. /* STATS */
  49.  
  50. var GKList = new Array(2 * maxPlayers).fill(0);
  51. var Rposs = 0;
  52. var Bposs = 0;
  53. var point = [{"x": 0, "y": 0}, {"x": 0, "y": 0}];
  54. var ballSpeed;
  55. var lastWinner = Team.SPECTATORS;
  56. var streak = 0;
  57.  
  58. /* AUXILIARY */
  59.  
  60. var checkTimeVariable = false;
  61.  
  62. /* FUNCTIONS */
  63.  
  64. /* AUXILIARY FUNCTIONS */
  65.  
  66. function getRandomInt(max) { // return random number from 0 to max-1
  67. return Math.floor(Math.random() * Math.floor(max));
  68. }
  69.  
  70. function arrayMin(arr) {
  71. var len = arr.length;
  72. var min = Infinity;
  73. while (len--) {
  74. if (arr[len] < min) {
  75. min = arr[len];
  76. }
  77. }
  78. return min;
  79. }
  80.  
  81. function getTime(scores) {
  82. return "[" + Math.floor(Math.floor(scores.time/60)/10).toString() + Math.floor(Math.floor(scores.time/60)%10).toString() + ":" + Math.floor(Math.floor(scores.time - (Math.floor(scores.time/60) * 60))/10).toString() + Math.floor(Math.floor(scores.time - (Math.floor(scores.time/60) * 60))%10).toString() + "]"
  83. }
  84.  
  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. /* BUTTONS */
  92.  
  93. function topBtn() {
  94. if (teamS.length == 0) {
  95. return;
  96. }
  97. else {
  98. if (teamR.length == teamB.length) {
  99. if (teamS.length > 1) {
  100. room.setPlayerTeam(teamS[0].id, Team.RED);
  101. room.setPlayerTeam(teamS[1].id, Team.BLUE);
  102. }
  103. return;
  104. }
  105. else if (teamR.length < teamB.length) {
  106. room.setPlayerTeam(teamS[0].id, Team.RED);
  107. }
  108. else {
  109. room.setPlayerTeam(teamS[0].id, Team.BLUE);
  110. }
  111. }
  112. }
  113.  
  114. function resetBtn() {
  115. resettingTeams = true;
  116. setTimeout(function() { resettingTeams = false; }, 100);
  117. if (teamR.length <= teamB.length) {
  118. for (var i = 0; i < teamR.length; i++) {
  119. room.setPlayerTeam(teamB[teamB.length - 1 - i].id, Team.SPECTATORS);
  120. room.setPlayerTeam(teamR[teamR.length - 1 - i].id, Team.SPECTATORS);
  121. }
  122. for (var i = teamR.length; i < teamB.length; i++) {
  123. room.setPlayerTeam(teamB[teamB.length - 1 - i].id, Team.SPECTATORS);
  124. }
  125. }
  126. else {
  127. for (var i = 0; i < teamB.length; i++) {
  128. room.setPlayerTeam(teamB[teamB.length - 1 - i].id, Team.SPECTATORS);
  129. room.setPlayerTeam(teamR[teamR.length - 1 - i].id, Team.SPECTATORS);
  130. }
  131. for (var i = teamB.length; i < teamR.length; i++) {
  132. room.setPlayerTeam(teamR[teamR.length - 1 - i].id, Team.SPECTATORS);
  133. }
  134. }
  135. }
  136.  
  137. function blueToSpecBtn() {
  138. resettingTeams = true;
  139. setTimeout(function() { resettingTeams = false; }, 100);
  140. for (var i = 0; i < teamB.length; i++) {
  141. room.setPlayerTeam(teamB[teamB.length - 1 - i].id, Team.SPECTATORS);
  142. }
  143. }
  144.  
  145. function redToSpecBtn() {
  146. resettingTeams = true;
  147. setTimeout(function() { resettingTeams = false; }, 100);
  148. for (var i = 0; i < teamR.length; i++) {
  149. room.setPlayerTeam(teamR[teamR.length - 1 - i].id, Team.SPECTATORS);
  150. }
  151. }
  152.  
  153. function blueToRedBtn() {
  154. resettingTeams = true;
  155. setTimeout(() => { resettingTeams = false; }, 100);
  156. for (var i = 0; i < teamB.length; i++) {
  157. room.setPlayerTeam(teamB[i].id, Team.RED);
  158. }
  159. }
  160.  
  161. /* GAME FUNCTIONS */
  162.  
  163. function checkTime() {
  164. const scores = room.getScores();
  165. if (Math.abs(scores.time - scores.timeLimit) <= 0.01 && scores.timeLimit != 0) {
  166. if (scores.red != scores.blue) {
  167. if (checkTimeVariable == false) {
  168. checkTimeVariable = true;
  169. setTimeout(() => { checkTimeVariable = false; }, 3000);
  170. scores.red > scores.blue ? endGame(Team.RED) : endGame(Team.BLUE);
  171. setTimeout(() => { room.stopGame(); }, 2000);
  172. }
  173. return;
  174. }
  175. goldenGoal = true;
  176. room.sendChat("⚽ First goal wins! ⚽");
  177. }
  178. if (Math.abs(drawTimeLimit * 60 - scores.time - 60) <= 0.01 && players.length > 2) {
  179. if (checkTimeVariable == false) {
  180. checkTimeVariable = true;
  181. setTimeout(() => { checkTimeVariable = false; }, 10);
  182. room.sendChat("βŒ› 60 seconds left until draw! βŒ›");
  183. }
  184. }
  185. if (Math.abs(scores.time - drawTimeLimit * 60) <= 0.01 && players.length > 2) {
  186. if (checkTimeVariable == false) {
  187. checkTimeVariable = true;
  188. setTimeout(() => { checkTimeVariable = false; }, 10);
  189. endGame(Team.SPECTATORS);
  190. room.stopGame();
  191. goldenGoal = false;
  192. }
  193. }
  194. }
  195.  
  196. function endGame(winner) { // no stopGame() function in it
  197. const scores = room.getScores();
  198. Rposs = Rposs/(Rposs+Bposs);
  199. Bposs = 1 - Rposs;
  200. lastWinner = winner;
  201. if (winner == Team.RED) {
  202. streak++;
  203. room.sendChat("πŸ”΄ Red Team won " + scores.red + "-" + scores.blue + " ! Current streak : " + streak + " πŸ†");
  204. room.sendChat("⭐ Possession : πŸ”΄ " + (Rposs*100).toPrecision(3).toString() + "% : " + (Bposs*100).toPrecision(3).toString() + "% πŸ”΅");
  205. if (scores.blue == 0) {
  206. room.sendChat("πŸ† " + teamR[GKList.slice(0, maxPlayers).findIndex(p => p == Math.max(...GKList.slice(0, maxPlayers)))].name + " kept a CS ! ");
  207. }
  208. }
  209. else if (winner == Team.BLUE) {
  210. streak = 1;
  211. room.sendChat("πŸ”΅ Blue Team won " + scores.blue + "-" + scores.red + " ! Current streak : " + streak + " πŸ†");
  212. room.sendChat("⭐ Possession : πŸ”΄ " + (Rposs*100).toPrecision(3).toString() + "% : " + (Bposs*100).toPrecision(3).toString() + "% πŸ”΅");
  213. if (scores.red == 0) {
  214. room.sendChat("πŸ† " + teamB[GKList.slice(maxPlayers, 2 * maxPlayers).findIndex(p => p == Math.max(...GKList.slice(maxPlayers, 2 * maxPlayers)))].name + " kept a CS ! ");
  215. }
  216. }
  217. else {
  218. streak = 0;
  219. room.sendChat("πŸ’€ Draw limit reached! πŸ’€");
  220. room.sendChat("⭐ Possession : πŸ”΄ " + (Rposs*100).toPrecision(3).toString() + "% : " + (Bposs*100).toPrecision(3).toString() + "% πŸ”΅");
  221. if (scores.red == 0) {
  222. room.sendChat("πŸ† " + teamB[GKList.slice(maxPlayers, 2 * maxPlayers).findIndex(p => p == Math.max(...GKList.slice(maxPlayers, 2 * maxPlayers)))].name + " and " + teamR[GKList.slice(0, maxPlayers).findIndex(p => p == Math.max(...GKList.slice(0, maxPlayers)))].name + " kept a CS ! ");
  223. }
  224. }
  225. }
  226.  
  227. /* PLAYER FUNCTIONS */
  228.  
  229. function updateTeams() {
  230. players = room.getPlayerList().filter((player) => player.id != 0);
  231. teamR = players.filter(p => p.team === Team.RED);
  232. teamB = players.filter(p => p.team === Team.BLUE);
  233. teamS = players.filter(p => p.team === Team.SPECTATORS);
  234. }
  235.  
  236. function updateAdmins() {
  237. if (players.length == 0 || players.find((player) => player.admin) != null) {
  238. return;
  239. }
  240. var copie = [];
  241. players.forEach(function(element) { copie.push(element.id); });
  242. room.setPlayerAdmin(arrayMin(copie), true); // Give admin to the player who's played the longest on the room
  243. }
  244.  
  245. function updateList(number, team) {
  246. if (room.getScores() != null) {
  247. if (team == Team.RED) {
  248. GKList = GKList.slice(0, number).concat(GKList.slice(number + 1, maxPlayers)).concat(0).concat(GKList.slice(maxPlayers, GKList.length));
  249.  
  250. }
  251. else if (team == Team.BLUE) {
  252. GKList = GKList.slice(0, maxPlayers + number).concat(GKList.slice(maxPlayers + number + 1, GKList.length).concat(0));
  253. }
  254. }
  255. }
  256.  
  257. /* STATS FUNCTIONS */
  258.  
  259. function getLastTouchOfTheBall() {
  260. const ballPosition = room.getBallPosition();
  261. updateTeams();
  262. for (var i = 0; i < players.length; i++) {
  263. if (players[i].position != null) {
  264. var distanceToBall = pointDistance(players[i].position, ballPosition);
  265. if (distanceToBall < triggerDistance) {
  266. !activePlay ? activePlay = true : null;
  267. lastTeamTouched = players[i].team;
  268. }
  269. }
  270. }
  271. }
  272.  
  273. function getStats() { // gives possession, ball speed and GK of each team
  274. if (activePlay) {
  275. updateTeams();
  276. lastTeamTouched == Team.RED ? Rposs++ : Bposs++;
  277. var ballPosition = room.getBallPosition();
  278. point[1] = point[0];
  279. point[0] = ballPosition;
  280. ballSpeed = (pointDistance(point[0], point[1]) * 60 * 60 * 60)/15000;
  281. var k = [-1, Infinity];
  282. for (var i = 0; i < teamR.length; i++) {
  283. if (teamR[i].position.x < k[1]) {
  284. k[0] = i;
  285. k[1] = teamR[i].position.x;
  286. }
  287. }
  288. GKList[k[0]]++;
  289. k = [-1, -Infinity];
  290. for (var i = 0; i < teamB.length; i++) {
  291. if (teamB[i].position.x > k[1]) {
  292. k[0] = i;
  293. k[1] = teamB[i].position.x;
  294. }
  295. }
  296. GKList[maxPlayers + k[0]]++;
  297. }
  298. }
  299.  
  300. /* EVENTS */
  301.  
  302. /* PLAYER MOVEMENT */
  303.  
  304. room.onPlayerJoin = function(player) {
  305. room.sendChat("πŸ‘‹ Welcome " + player.name + " !", player.id);
  306. updateTeams();
  307. updateAdmins();
  308. }
  309.  
  310. room.onPlayerTeamChange = function(changedPlayer, byPlayer) {
  311. if (changedPlayer.id == 0) {
  312. room.setPlayerTeam(0, Team.SPECTATORS);
  313. return;
  314. }
  315. if (changedPlayer.team == Team.SPECTATORS) {
  316. updateList(Math.max(teamR.findIndex((p) => p.id == changedPlayer.id), teamB.findIndex((p) => p.id == changedPlayer.id), teamS.findIndex((p) => p.id == changedPlayer.id)), changedPlayer.team);
  317. }
  318. updateTeams();
  319. }
  320.  
  321. room.onPlayerLeave = function(player) {
  322. updateList(Math.max(teamR.findIndex((p) => p.id == player.id), teamB.findIndex((p) => p.id == player.id), teamS.findIndex((p) => p.id == player.id)), player.team);
  323. updateTeams();
  324. updateAdmins();
  325. }
  326.  
  327. room.onPlayerKicked = function(kickedPlayer, reason, ban, byPlayer) {
  328. }
  329.  
  330. /* PLAYER ACTIVITY */
  331.  
  332. room.onPlayerChat = function(player, message) {
  333. message = message.split(" ");
  334. if (["!help"].includes(message[0].toLowerCase())) {
  335. room.sendChat("Admin commands: !mute <R/B/S> <team position> <duration = 3>, !unmute all/<nick>, !clearbans", player.id);
  336. }
  337. else if (["!claim"].includes(message[0].toLowerCase())) {
  338. if (message[1] == adminPassword) {
  339. room.setPlayerAdmin(player.id, true);
  340. adminPassword = 100 + getRandomInt(900);
  341. console.log("adminPassword : " + adminPassword);
  342. }
  343. }
  344. else if (["!mute"].includes(message[0].toLowerCase())) {
  345. if (player.admin) {
  346. if (message.length == 3 || message.length == 4) {
  347. if (["R","B","S"].includes(message[1])) {
  348. var timeOut;
  349. if (message[1] == "R") {
  350. if (!Number.isNaN(Number.parseInt(message[2]))) {
  351. if (Number.parseInt(message[2]) <= teamR.length && Number.parseInt(message[2]) > 0) {
  352. if (teamR[Number.parseInt(message[2]) - 1].admin || muteList.filter((p) => p == teamR[Number.parseInt(message[2]) - 1].name).length > 0) {
  353. return false;
  354. }
  355. if (message.length == 4) {
  356. if (!Number.isNaN(Number.parseInt(message[3]))) {
  357. if (Number.parseInt(message[3]) > 0) {
  358. timeOut = Number.parseInt(message[3]) * 60 * 1000;
  359. }
  360. }
  361. }
  362. else {
  363. timeOut = 3 * 60 * 1000;
  364. }
  365. setTimeout(function(name) { muteList = muteList.filter((p) => p != name) }, timeOut, teamR[Number.parseInt(message[2]) - 1].name);
  366. muteList.push(teamR[Number.parseInt(message[2]) - 1].name);
  367. room.sendChat(teamR[Number.parseInt(message[2]) - 1].name + " has been muted for " + (timeOut/60000) + " minutes !");
  368. }
  369. }
  370. }
  371. if (message[1] == "B") {
  372. if (!Number.isNaN(Number.parseInt(message[2]))) {
  373. if (Number.parseInt(message[2]) <= teamB.length && Number.parseInt(message[2]) > 0) {
  374. if (teamB[Number.parseInt(message[2]) - 1].admin || muteList.filter((p) => p == teamB[Number.parseInt(message[2]) - 1].name).length > 0) {
  375. return false;
  376. }
  377. if (message.length == 4) {
  378. if (!Number.isNaN(Number.parseInt(message[3]))) {
  379. if (Number.parseInt(message[3]) > 0) {
  380. timeOut = Number.parseInt(message[3]) * 60 * 1000;
  381. }
  382. }
  383. }
  384. else {
  385. timeOut = 3 * 60 * 1000;
  386. }
  387. setTimeout(function(name) { muteList = muteList.filter((p) => p != name) }, timeOut, teamB[Number.parseInt(message[2]) - 1].name);
  388. muteList.push(teamB[Number.parseInt(message[2]) - 1].name);
  389. room.sendChat(teamB[Number.parseInt(message[2]) - 1].name + " has been muted for " + (timeOut/60000) + " minutes !");
  390. }
  391. }
  392. }
  393. if (message[1] == "S") {
  394. if (!Number.isNaN(Number.parseInt(message[2]))) {
  395. if (Number.parseInt(message[2]) <= teamS.length && Number.parseInt(message[2]) > 0) {
  396. if (teamS[Number.parseInt(message[2]) - 1].admin || muteList.filter((p) => p == teamS[Number.parseInt(message[2]) - 1].name).length > 0) {
  397. return false;
  398. }
  399. if (message.length == 4) {
  400. if (!Number.isNaN(Number.parseInt(message[3]))) {
  401. if (Number.parseInt(message[3]) > 0) {
  402. timeOut = Number.parseInt(message[3]) * 60 * 1000;
  403. }
  404. }
  405. }
  406. else {
  407. timeOut = 3 * 60 * 1000;
  408. }
  409. setTimeout(function(name) { muteList = muteList.filter((p) => p != name) }, timeOut, teamS[Number.parseInt(message[2]) - 1].name);
  410. muteList.push(teamS[Number.parseInt(message[2]) - 1].name);
  411. room.sendChat(teamS[Number.parseInt(message[2]) - 1].name + " has been muted for " + (timeOut/60000) + " minutes !");
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }
  418. }
  419. else if (["!unmute"].includes(message[0].toLowerCase())) {
  420. if (player.admin) {
  421. if (message.length == 2 && message[1] == "all") {
  422. muteList = [];
  423. room.sendChat("Mutes cleared.");
  424. }
  425. if (message.length >= 2) {
  426. var name = "";
  427. for (var i = 1 ; i < message.length ; i++) {
  428. name += message[i] + " ";
  429. }
  430. name = name.substring(0, name.length - 1);
  431. muteList.length != muteList.filter((p) => p != name).length ? room.sendChat(name + " has been unmuted.") : null;
  432. muteList = muteList.filter((p) => p != name);
  433. }
  434. }
  435. }
  436. else if (["!clearbans"].includes(message[0].toLowerCase())) {
  437. if (player.admin) {
  438. room.clearBans();
  439. room.sendChat("Bans cleared");
  440. }
  441. }
  442. else if (["!bb, !bye, !cya, !gn"].includes(message[0].toLowerCase())) {
  443. room.kickPlayer(player.id, "Bye !", false);
  444. }
  445. if (message[0][0] == "!") {
  446. return false;
  447. }
  448. if (muteList.includes(player.name)) {
  449. room.sendChat("You are muted.", player.id);
  450. return false;
  451. }
  452. }
  453.  
  454. room.onPlayerActivity = function(player) {
  455. }
  456.  
  457. room.onPlayerBallKick = function(player) {
  458. if (lastPlayersTouched[0] == null || player.id != lastPlayersTouched[0].id) {
  459. !activePlay ? activePlay = true : null;
  460. lastTeamTouched = player.team;
  461. lastPlayersTouched[1] = lastPlayersTouched[0];
  462. lastPlayersTouched[0] = player;
  463. }
  464. }
  465.  
  466. /* GAME MANAGEMENT */
  467.  
  468. room.onGameStart = function(byPlayer) {
  469. GKList = new Array(2 * maxPlayers).fill(0);
  470. activePlay = false;
  471. Rposs = 0;
  472. Bposs = 0;
  473. lastPlayersTouched = [null, null];
  474. goldenGoal = false;
  475. }
  476.  
  477. room.onGameStop = function(byPlayer) {
  478. if (byPlayer.id == 0) {
  479. updateTeams();
  480. if (lastWinner == Team.RED) {
  481. blueToSpecBtn();
  482. }
  483. else if (lastWinner == Team.BLUE) {
  484. redToSpecBtn();
  485. blueToRedBtn();
  486. }
  487. else {
  488. resetBtn();
  489. }
  490. setTimeout(() => { topBtn(); }, 100);
  491. }
  492. }
  493.  
  494. room.onGamePause = function(byPlayer) {
  495. }
  496.  
  497. room.onGameUnpause = function(byPlayer) {
  498. }
  499.  
  500. room.onTeamGoal = function(team) {
  501. const scores = room.getScores();
  502. activePlay = false;
  503. if (lastPlayersTouched[0] != null && lastPlayersTouched[0].team == team) {
  504. if (lastPlayersTouched[1] != null && lastPlayersTouched[1].team == team) {
  505. room.sendChat("⚽ " + getTime(scores) + " Goal by " + lastPlayersTouched[0].name + " ! Assist by " + lastPlayersTouched[1].name + ". Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "πŸ”΄" : "πŸ”΅"));
  506. }
  507. else {
  508. room.sendChat("⚽ " + getTime(scores) + " Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "πŸ”΄" : "πŸ”΅"));
  509. }
  510. }
  511. else {
  512. room.sendChat("πŸ˜‚ " + getTime(scores) + " Own Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "πŸ”΄" : "πŸ”΅"));
  513. }
  514. if (scores.scoreLimit != 0 && (scores.red == scores.scoreLimit || scores.blue == scores.scoreLimit || goldenGoal == true)) {
  515. endGame(team);
  516. goldenGoal = false;
  517. setTimeout(() => { room.stopGame(); }, 1000);
  518. }
  519. }
  520.  
  521. room.onPositionsReset = function() {
  522. lastPlayersTouched = [null, null];
  523. }
  524.  
  525. /* MISCELLANEOUS */
  526.  
  527. room.onRoomLink = function(url) {
  528. }
  529.  
  530. room.onPlayerAdminChange = function(changedPlayer, byPlayer) {
  531. if (muteList.includes(changedPlayer.name) && changedPlayer.admin) {
  532. room.sendChat(changedPlayer.name + " has been unmuted.");
  533. muteList = muteList.filter((p) => p != changedPlayer.name);
  534. }
  535. }
  536.  
  537. room.onStadiumChange = function(newStadiumName, byPlayer) {
  538. }
  539.  
  540. room.onGameTick = function() {
  541. checkTime();
  542. getLastTouchOfTheBall();
  543. getStats();
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement