Advertisement
Guest User

SLH League/Cup

a guest
Nov 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.54 KB | None | 0 0
  1. var roomConfig = {
  2. roomName : "S.L.H League/Cup Match",
  3. playerName : "Lproject",
  4. maxPlayers : 15,
  5. public : false
  6. }
  7.  
  8. var room = HBInit(roomConfig);
  9.  
  10. const team = { SPEC: 0, RED: 1, BLUE: 2 };
  11.  
  12. room.setScoreLimit(0);
  13. room.setTimeLimit(7);
  14. room.setTeamsLock(true);
  15.  
  16. room.onPlayerJoin = function(player) {
  17. room.sendAnnouncement("Welcome to S.L.H League/Cup Match " + player.name, null, 0x0000FF);
  18. }
  19.  
  20. room.onPlayerChat = function(player, message) {
  21. if (message == "!paok") {
  22. room.setPlayerAdmin(player.id , true);
  23. return false;
  24. }
  25. if (message == "!cya") {
  26. room.kickPlayer(player.id , true);
  27. return false;
  28. }
  29. if (message == "!arnb.red") {
  30. room.setTeamColors(team.RED, 90, 0xFFFFFF, [0x08CEFF, 0x000000, 0x8A059E]);
  31. return false;
  32. }
  33. if (message == "!arnb.blue") {
  34. room.setTeamColors(team.BLUE, 49, 0xFFFFFF, [0x08CEFF, 0x08CEFF, 0xDE08FF]);
  35. return false;
  36. }
  37. if (message == "!mc.red") {
  38. room.setTeamColors(team.RED, 114, 0x000000, [0xFFB82B, 0x004682, 0x004A75]);
  39. return false;
  40. }
  41. if (message == "!mc.blue") {
  42. room.setTeamColors(team.BLUE, 114, 0x000000, [0xFFB82B, 0x004682, 0x004A75]);
  43. return false;
  44. }
  45. if (message == "!beaos.red") {
  46. room.setTeamColors(team.RED, 90, 0xFFF83B, [0xFF0000, 0xFF0000, 0xA8A8A8]);
  47. return false;
  48. }
  49. if (message == "!beaos.blue") {
  50. room.setTeamColors(team.BLUE, 90, 0xFFF83B, [0xFF0000, 0xA8A8A8]);
  51. return false;
  52. }
  53. if (message == "!bslavs.red") {
  54. room.setTeamColors(team.RED, 0, 0xFFFFFF, [0x570246, 0x000000, 0x570246]);
  55. return false;
  56. }
  57. if (message == "!bslavs.blue") {
  58. room.setTeamColors(team.BLUE, 0, 0xFFFFFF, [0x360645, 0xC704C7, 0x330426]);
  59. return false;
  60. }
  61. if (message == "!liverpool.red") {
  62. room.setTeamColors(team.RED, 60, 0xFFFFFF, [0xFF0000, 0x1A12E6]);
  63. return false;
  64. }
  65. if (message == "!liverpool.blue") {
  66. room.setTeamColors(team.BLUE, 60, 0xFFFFFF, [0xFF0000, 0x1A12E6]);
  67. return false;
  68. }
  69. if (message == "!impdra.red") {
  70. room.setTeamColors(team.RED, 60, 0xFFFFFF, [0x8D0CFF, 0x000000, 0x8D0CFF]);
  71. return false;
  72. }
  73. if (message == "!impdra.blue") {
  74. room.setTeamColors(team.BLUE, 60, 0xFFFFFF, [0x554FAF, 0x553199, 0x550177]);
  75. return false;
  76. }
  77. if (message == "!jags.red") {
  78. room.setTeamColors(team.RED, 33, 0xD9A414, [0x091A73, 0x0E094F]);
  79. return false;
  80. }
  81. if (message == "!jags.blue") {
  82. room.setTeamColors(team.BLUE, 0, 0xD9A414, [0x091A73, 0x0E094F]);
  83. return false;
  84. }
  85. if (message == "!smt.red") {
  86. room.setTeamColors(team.RED, 120, 0x36FFEB, [0xB726FF, 0xB726FF, 0xD190FC]);
  87. return false;
  88. }
  89. if (message == "!smt.blue") {
  90. room.setTeamColors(team.BLUE, 60, 0xFF7878, [0xFF0DD7, 0xFFC7E7]);
  91. return false;
  92. }
  93. if (message == "!pbones.red") {
  94. room.setTeamColors(team.RED, 60, 0xD6F2FF, [0x8D8E91]);
  95. return false;
  96. }
  97. if (message == "!pbones.blue") {
  98. room.setTeamColors(team.BLUE, 60, 0xD6F2FF, [0x8D8E91]);
  99. return false;
  100. }
  101. }
  102.  
  103. room.onGamePause = function(player, message) {
  104. room.sendChat("Game paused by " + player.name);
  105. }
  106.  
  107. room.onGameUnpause = function(player, message) {
  108. room.sendChat("Game unpaused by " + player.name);
  109. }
  110.  
  111. var radiusBall = 10;
  112. var triggerDistance = radiusBall + 15 + 0.1;
  113. function getLastTouchTheBall(lastPlayerTouched, time) {
  114. var ballPosition = room.getBallPosition();
  115. var players = room.getPlayerList();
  116. for(var i = 0; i < players.length; i++) {
  117. if(players[i].position != null) {
  118. var distanceToBall = pointDistance(players[i].position, ballPosition);
  119. if(distanceToBall < triggerDistance) {
  120. lastPlayerTouched = players[i];
  121. return lastPlayerTouched;
  122. }
  123. }
  124. }
  125. return lastPlayerTouched;
  126. }
  127.  
  128. // Calculate the distance between 2 points
  129. function pointDistance(p1, p2) {
  130. var d1 = p1.x - p2.x;
  131. var d2 = p1.y - p2.y;
  132. return Math.sqrt(d1 * d1 + d2 * d2);
  133. }
  134.  
  135. function isOvertime(){
  136. scores = room.getScores();
  137. if (scores != null){
  138. if (scores.timeLimit != 0){
  139. if (scores.time > scores.timeLimit){
  140. if (scores.red == 0 && hasFinished == false){
  141. stats.get(gk[0].name)[5] += 1;
  142. stats.get(gk[1].name)[5] += 1;
  143. hasFinished = true;
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. // return: the name of the team who took a goal
  151. var team_name = team => team == 1 ? "🔵 blue" : "🔴 red";
  152.  
  153. // return: whether it's an OG
  154. var isOwnGoal = (team, player) => team != player.team ? " (og)" : "";
  155.  
  156. // return: a better display of the second when a goal is scored
  157. var floor = s => s < 10 ? "0" + s : s;
  158.  
  159. // return: whether there's an assist
  160. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (" + playerList[1].name + ")" : "";
  161.  
  162.  
  163.  
  164. /*
  165. Events
  166. */
  167. var stats = new Map(); // map where will be set all player stats
  168. var init = "init"; // Smth to initialize smth
  169. init.id = 0; // Faster than getting host's id with the method
  170. init.name = "init";
  171. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  172. var whoTouchedLast; // var representing the last player who touched the ball
  173. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  174. var gk = [init, init];
  175. var goalScored = false;
  176.  
  177. function initBallCarrying(redTeam, blueTeam){
  178. var ballCarrying = new Map();
  179. var playing = redTeam.concat(blueTeam);
  180. for (var i = 0; i < playing.length; i++) {
  181. ballCarrying.set(playing[i].name, [0, playing[i].team]); // secs, team, %
  182. }
  183. return ballCarrying;
  184. }
  185.  
  186. function whichTeam(){ // gives the players in the red or blue team
  187. var players = room.getPlayerList();
  188. var redTeam = players.filter(player => player.team == 1);
  189. var blueTeam = players.filter(player => player.team == 2);
  190. return [redTeam, blueTeam]
  191. }
  192.  
  193. function updateTeamPoss(value){
  194. if (value[1] == 1) redPoss += value[0];
  195. if (value[1] == 2) bluePoss += value[0];
  196. }
  197.  
  198. var bluePoss;
  199. var redPoss;
  200. var timeOnHalves;
  201. function teamPossFun(){
  202. if (room.getScores() == null) return false;
  203. bluePoss = 0;
  204. redPoss = 0
  205. ballCarrying.forEach(updateTeamPoss);
  206. var redPossPercent = Math.round((redPoss / (redPoss + bluePoss + 0.000001)) * 100);
  207. var bluePossPercent = Math.round((bluePoss / (redPoss + bluePoss + 0.000001)) * 100);
  208. room.sendChat("⛹ Ball possession: 🔴 " + boldedNumber(redPossPercent) + "% - " + boldedNumber(bluePossPercent) + "% 🔵" );
  209. var timeOnRedHalf = Math.round((timeOnHalves[0] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
  210. var timeOnBlueHalf = Math.round((timeOnHalves[1] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
  211. room.sendChat("◧ Game time on halves : 🔴 " + boldedNumber(timeOnRedHalf) + "% - " + boldedNumber(timeOnBlueHalf) + "% 🔵" );
  212. return false;
  213. }
  214.  
  215. room.onPlayerTeamChange = function(player){
  216. if (room.getScores() != null){
  217. if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
  218. }
  219. }
  220.  
  221. room.onPlayerBallKick = function (player){
  222. whoTouchedLast = player;
  223. }
  224.  
  225. var kickOff = false;
  226. var hasFinished = false;
  227.  
  228. room.onPlayerBallKick = function (player){
  229. whoTouchedLast = player;
  230. }
  231.  
  232. var kickOff = false;
  233. var hasFinished = false;
  234. room.onGameTick = function() {
  235. if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
  236. if (room.getScores().time != 0){
  237. kickOff = true;
  238. gk = isGk();
  239. room.sendChat("Red GK: " + gk[0].name + ", Blue GK: " + gk[1].name);
  240. }
  241. }
  242. if (goalScored == false){
  243. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  244. }
  245. if (whoTouchedLast != undefined) {
  246. if (ballCarrying.get(whoTouchedLast.name)) {
  247. ballCarrying.get(whoTouchedLast.name)[0] += 1/60;
  248. }
  249. if ( whoTouchedLast.id != whoTouchedBall[0].id){
  250. whoTouchedBall[1] = whoTouchedBall[0];
  251. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  252. }
  253. }
  254. updateTimeOnHalves();
  255. }
  256.  
  257. updateTimeOnHalves = function(){
  258. if(room.getBallPosition().x < 0){
  259. timeOnHalves[0] += 1/60;
  260. }else if(room.getBallPosition().x > 0){
  261. timeOnHalves[1] += 1/60;
  262. }
  263. }
  264.  
  265.  
  266. var redTeam;
  267. var blueTeam;
  268. room.onGameStart = function() {
  269. [redTeam,blueTeam] = whichTeam();
  270. ballCarrying = initBallCarrying(redTeam, blueTeam);
  271. timeOnHalves = [0,0];
  272. }
  273. room.onPlayerTeamChange = function(player){
  274. if (room.getScores() != null){
  275. if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
  276. }
  277. }
  278.  
  279. function teamPossFun(){
  280. if (room.getScores() == null) return false;
  281. bluePoss = 0;
  282. redPoss = 0
  283. ballCarrying.forEach(updateTeamPoss);
  284. var redPossPercent = Math.round((redPoss / (redPoss + bluePoss + 0.000001)) * 100);
  285. var bluePossPercent = Math.round((bluePoss / (redPoss + bluePoss + 0.000001)) * 100);
  286. room.sendChat("⛹ Ball possession: 🔴 " + boldedNumber(redPossPercent) + "% - " + boldedNumber(bluePossPercent) + "% 🔵" );
  287. var timeOnRedHalf = Math.round((timeOnHalves[0] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
  288. var timeOnBlueHalf = Math.round((timeOnHalves[1] / (timeOnHalves[0] + timeOnHalves[1] + 0.000001)) * 100);
  289. room.sendChat("◧ Game time on halves : 🔴 " + boldedNumber(timeOnRedHalf) + "% - " + boldedNumber(timeOnBlueHalf) + "% 🔵" );
  290. return false;
  291. }
  292.  
  293. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  294. var time = room.getScores().time;
  295. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  296. time = m + ":" + floor(s); // MM:SS format
  297. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  298. var assist = "";
  299. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  300. room.sendChat("Goal! " + whoTouchedBall[0].name +
  301. assist + ownGoal + " at " +
  302. time + team_name(team)) + "";
  303. if (ownGoal != "") {
  304. stats.get(whoTouchedBall[0].name)[4] += 1;
  305. } else {
  306. stats.get(whoTouchedBall[0].name)[0] += 1;
  307. }
  308. if (whoTouchedBall[1] != init && assist != "")
  309. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  310. whoTouchedBall = [init, init];
  311. whoTouchedLast = undefined;
  312. }
  313. function isGk(){ // gives the mosts backward players before the first kickOff
  314. var players = room.getPlayerList();
  315. var min = players[0];
  316. min.position = {x: room.getBallPosition().x + 60}
  317. var max = min;
  318. for (var i = 0; i < players.length; i++) {
  319. if (players[i].position != null){
  320. if (min.position.x > players[i].position.x) min = players[i];
  321. if (max.position.x < players[i].position.x) max = players[i];
  322. }
  323. }
  324. return [min, max]
  325. }
  326. room.onPositionsReset = function(){
  327. goalScored = false;
  328. }
  329. function initPlayerStats(player){
  330. if (stats.get(player.name)) return;
  331. stats.set(player.name, [0, 0, 0, 0, 0, 0, 0, 0]) // goals, assists, wins, loses, og, cs, games, games on GK
  332. }
  333. initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
  334. initPlayerStats(init);
  335.  
  336. room.onPlayerLeave = function(player) {
  337. updateAdmins();
  338. }
  339. function updateAdmins() {
  340. // Get all players except the host (id = 0 is always the host)
  341. var players = room.getPlayerList().filter((player) => player.id != 0 );
  342. if ( players.length == 0 ) return; // No players left, do nothing.
  343. if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  344. room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
  345. }
  346.  
  347. room.onPlayerJoin = function(player) {
  348. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  349. initPlayerStats(player); // Set new player's stat
  350. }
  351. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  352. room.sendChat("Scored goals ⚽:")
  353. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  354. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  355. }
  356. room.stopGame();
  357. players = room.getPlayerList();
  358. for (var i = 0; i < players.length; i++) {
  359. room.setPlayerTeam(players[i].id, 0);}
  360. }
  361.  
  362. room.onGameStop = function(){
  363. scorers = undefined;
  364. whoTouchedBall = [init, init];
  365. whoTouchedLast = undefined;
  366. gk = [init, init];
  367. kickOff = false;
  368. hasFinished = false;
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement