Advertisement
Guest User

host

a guest
Jun 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. /* VARIABLES */
  2.  
  3. /* ROOM */
  4.  
  5. const roomName = "πŸ’ͺ POWER BIG 3v3 [24/7]";
  6. const botName = "PowerBot";
  7. const maxPlayers = 12;
  8. const roomPublic = true;
  9. const geo = [{"code": "HT", "lat": 51.9, "lon": 19.1}];
  10.  
  11. const room = HBInit({ roomName: roomName, maxPlayers: maxPlayers, public: roomPublic, playerName: botName, geo: geo[0] });
  12.  
  13. const scoreLimit = 3;
  14. const timeLimit = 3;
  15. room.setScoreLimit(scoreLimit);
  16. room.setTimeLimit(timeLimit);
  17. room.setTeamsLock(true);
  18.  
  19. var adminPassword = 100 + getRandomInt(900);
  20. console.log("adminPassword : " + adminPassword);
  21.  
  22. /* OPTIONS */
  23.  
  24. var drawTimeLimit = Infinity;
  25.  
  26. /* PLAYERS */
  27.  
  28. const Team = { SPECTATORS: 0, RED: 1, BLUE: 2 };
  29. var players;
  30. var teamR;
  31. var teamB;
  32. var teamS;
  33.  
  34. /* GAME */
  35.  
  36. var lastPlayersTouched;
  37. var point = [{"x": 0, "y": 0}, {"x": 0, "y": 0}];
  38. var ballSpeed;
  39. var goldenGoal = false;
  40.  
  41. /* AUXILIARY */
  42.  
  43. var checkTimeVariable = false;
  44.  
  45. /* FUNCTIONS */
  46.  
  47. /* AUXILIARY FUNCTIONS */
  48.  
  49. function getRandomInt(max) { // return random number from 0 to max-1
  50. return Math.floor(Math.random() * Math.floor(max));
  51. }
  52.  
  53. function arrayMin(arr) {
  54. var len = arr.length;
  55. var min = Infinity;
  56. while (len--) {
  57. if (arr[len] < min) {
  58. min = arr[len];
  59. }
  60. }
  61. return min;
  62. }
  63.  
  64. function getTime(scores) {
  65. 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() + "]"
  66. }
  67.  
  68. function pointDistance(p1, p2) {
  69. var d1 = p1.x - p2.x;
  70. var d2 = p1.y - p2.y;
  71. return Math.sqrt(d1 * d1 + d2 * d2);
  72. }
  73.  
  74. /* GAME FUNCTIONS */
  75.  
  76. function checkTime() {
  77. const scores = room.getScores();
  78. if (Math.abs(scores.time - scores.timeLimit) <= 0.01 && scores.timeLimit != 0) {
  79. if (scores.red != scores.blue) {
  80. if (checkTimeVariable == false) {
  81. checkTimeVariable = true;
  82. setTimeout(() => { checkTimeVariable = false; }, 3000);
  83. scores.red > scores.blue ? endGame(Team.RED) : endGame(Team.BLUE);
  84. setTimeout(() => { room.stopGame(); }, 2000);
  85. }
  86. return;
  87. }
  88. goldenGoal = true;
  89. room.sendChat("⚽ First goal wins! ⚽");
  90. }
  91. if (Math.abs(drawTimeLimit * 60 - scores.time - 60) <= 0.01 && players.length > 2) {
  92. if (checkTimeVariable == false) {
  93. checkTimeVariable = true;
  94. setTimeout(() => { checkTimeVariable = false; }, 10);
  95. room.sendChat("βŒ› 60 seconds left until draw! βŒ›");
  96. }
  97. }
  98. if (Math.abs(scores.time - drawTimeLimit * 60) <= 0.01 && players.length > 2) {
  99. if (checkTimeVariable == false) {
  100. checkTimeVariable = true;
  101. setTimeout(() => { checkTimeVariable = false; }, 10);
  102. endGame(Team.SPECTATORS);
  103. room.stopGame();
  104. goldenGoal = false;
  105. }
  106. }
  107. }
  108.  
  109. function endGame(winner) { // no stopGame() function in it
  110. const scores = room.getScores();
  111. if (winner == Team.RED) {
  112. room.sendChat("πŸ”΄ Red Team won " + scores.red + "-" + scores.blue + "!");
  113. }
  114. else if (winner == Team.BLUE) {
  115. room.sendChat("πŸ”΅ Blue Team won " + scores.blue + "-" + scores.red + "!");
  116. }
  117. else {
  118. room.sendChat("πŸ’€ Draw limit reached! πŸ’€");
  119. }
  120. }
  121.  
  122. /* PLAYER FUNCTIONS */
  123.  
  124. function updateTeams() {
  125. players = room.getPlayerList().filter((player) => player.id != 0);
  126. teamR = players.filter(p => p.team === Team.RED);
  127. teamB = players.filter(p => p.team === Team.BLUE);
  128. teamS = players.filter(p => p.team === Team.SPECTATORS);
  129. }
  130.  
  131. function updateAdmins() {
  132. if (players.length == 0 || players.find((player) => player.admin) != null) {
  133. return;
  134. }
  135. var copie = [];
  136. players.forEach(function(element) { copie.push(element.id); });
  137. room.setPlayerAdmin(arrayMin(copie), true); // Give admin to the player who's played the longest on the room
  138. }
  139.  
  140. /* STATS FUNCTIONS */
  141.  
  142. function getStats() {
  143. const ballPosition = room.getBallPosition();
  144. point[1] = point[0];
  145. point[0] = ballPosition;
  146. ballSpeed = (pointDistance(point[0], point[1]) * 60 * 60 * 60)/15000;
  147. }
  148.  
  149. /* EVENTS */
  150.  
  151. /* PLAYER MOVEMENT */
  152.  
  153. room.onPlayerJoin = function(player) {
  154. room.sendChat("[PM] πŸ‘‹ Welcome " + player.name + " ! This 24/7 host is in development, but you can play !", player.id);
  155. updateTeams();
  156. updateAdmins();
  157. }
  158.  
  159. room.onPlayerTeamChange = function(changedPlayer, byPlayer) {
  160. if (changedPlayer.id == 0) {
  161. room.setPlayerTeam(0, Team.SPECTATORS);
  162. return;
  163. }
  164. updateTeams();
  165. }
  166.  
  167. room.onPlayerLeave = function(player) {
  168. updateTeams();
  169. updateAdmins();
  170. }
  171.  
  172. room.onPlayerKicked = function(kickedPlayer, reason, ban, byPlayer) {
  173. }
  174.  
  175. /* PLAYER ACTIVITY */
  176.  
  177. room.onPlayerChat = function(player, message) {
  178. message = message.split(" ");
  179. if (["!claim"].includes(message[0].toLowerCase())) {
  180. if (message[1] == adminPassword) {
  181. room.setPlayerAdmin(player.id, true);
  182. adminPassword = 100 + getRandomInt(900);
  183. console.log("adminPassword : " + adminPassword);
  184. }
  185. }
  186. if (message[0][0] == "!") {
  187. return false;
  188. }
  189. }
  190.  
  191. room.onPlayerActivity = function(player) {
  192. }
  193.  
  194. room.onPlayerBallKick = function(player) {
  195. if (lastPlayersTouched[0] == null || player.id != lastPlayersTouched[0].id) {
  196. lastPlayersTouched[1] = lastPlayersTouched[0];
  197. lastPlayersTouched[0] = player;
  198. }
  199. }
  200.  
  201. /* GAME MANAGEMENT */
  202.  
  203. room.onGameStart = function(byPlayer) {
  204. goldenGoal = false;
  205. lastPlayersTouched = [null, null];
  206. }
  207.  
  208. room.onGameStop = function(byPlayer) {
  209. }
  210.  
  211. room.onGamePause = function(byPlayer) {
  212. }
  213.  
  214. room.onGameUnpause = function(byPlayer) {
  215. }
  216.  
  217. room.onTeamGoal = function(team) {
  218. const scores = room.getScores();
  219. if (lastPlayersTouched[0] != null && lastPlayersTouched[0].team == team) {
  220. if (lastPlayersTouched[1] != null && lastPlayersTouched[1].team == team) {
  221. room.sendChat("⚽ " + getTime(scores) + " Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "πŸ”΄" : "πŸ”΅"));
  222. }
  223. else {
  224. room.sendChat("⚽ " + getTime(scores) + " Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "πŸ”΄" : "πŸ”΅"));
  225. }
  226. }
  227. else {
  228. room.sendChat("πŸ˜‚ " + getTime(scores) + " Own Goal by " + lastPlayersTouched[0].name + " ! Goal speed : " + ballSpeed.toPrecision(4).toString() + "km/h " + (team == Team.RED ? "πŸ”΄" : "πŸ”΅"));
  229. }
  230. if (scores.red == scores.scoreLimit || scores.blue == scores.scoreLimit || goldenGoal == true) {
  231. endGame(team);
  232. goldenGoal = false;
  233. setTimeout(() => { room.stopGame(); }, 1000);
  234. }
  235. }
  236.  
  237. room.onPositionsReset = function() {
  238. lastPlayersTouched = [null, null];
  239. }
  240.  
  241. /* MISCELLANEOUS */
  242.  
  243. room.onRoomLink = function(url) {
  244. }
  245.  
  246. room.onPlayerAdminChange = function(changedPlayer, byPlayer) {
  247. }
  248.  
  249. room.onStadiumChange = function(newStadiumName, byPlayer) {
  250. }
  251.  
  252. room.onGameTick = function() {
  253. checkTime();
  254. getStats();
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement