Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.45 KB | None | 0 0
  1. geo = {"code": "pl", "lat": 51.66, "lon": 17.81}
  2. var room = HBInit({
  3. roomName: "3vs3 VPS",
  4. maxPlayers: 10,
  5. playerName : "",
  6. public: true,
  7. geo
  8. });
  9. room.setDefaultStadium("Classic");
  10. room.setScoreLimit(3);
  11. room.setTimeLimit(3);
  12.  
  13. // If there are no admins left in the room give admin to one of the remaining players.
  14. function updateAdmins() {
  15. // Get all players except the host (id = 0 is always the host)
  16. var players = room.getPlayerList().filter((player) => player.id != 0 );
  17. if ( players.length == 0 ) return; // No players left, do nothing.
  18. if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  19. room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
  20. }
  21.  
  22. room.onPlayerJoin = function(player) {
  23. updateAdmins();
  24. }
  25.  
  26. room.onPlayerLeave = function(player) {
  27. updateAdmins();
  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. room.onPlayerChat = function(player, message) {
  83.  
  84. if (message == "!adminadaj"){
  85. // Gives admin to the person why type this password
  86. room.setPlayerAdmin(player.id, true);
  87. return false; // The message won't be displayed
  88. }
  89. }
  90.  
  91. function putPauseFun() { // p
  92. room.pauseGame(true);
  93. }
  94.  
  95. function unPauseFun() { // !p
  96. room.pauseGame(false);
  97. }
  98.  
  99. function helpFun() { // !help
  100. room.sendChat('Dostępne komendy: "p", "!p" , "!stats Nickname", "!ranking", "!poss", "!resetstats", "!adminhelp", "!gkhelp", "!rankhelp"');
  101. }
  102.  
  103. function adminHelpFun() {
  104. room.sendChat('Dostępne komendy: "!mute Player", "!unmute Player", ' +
  105. '"!clearbans", "!swap". Musisz być adminem.')
  106. }
  107.  
  108.  
  109. function gkHelpFun() { // !gkhelp
  110. room.sendChat('Najbardziej wycofany zawodnik przy rozpoczęciu zostanie zarejestrowany jako gk')
  111. }
  112. function rankHelpFun() { // !gkhelp
  113. room.sendChat("Zdobywaj punkty za poszczególne czynności! Gol: 5 pkt, asysta: 3 pkt, zwycięstwo: 3 pkt, czyste konto: 6 pkt, przegrana: -7 pkt, samobój: -4 pkt.")
  114. }
  115.  
  116.  
  117. function statsFun(player, message){ // !stats Anddy
  118. if (stats.get(message.substr(7))){
  119. sendStats(message.substr(7));
  120. } else{ return false;}
  121. }
  122.  
  123. function rankFun() { // !ranking
  124. string = ranking();
  125. room.sendChat("Ranking: " + string);
  126. }
  127.  
  128. function resetStatsFun (player){ // !resetstats
  129. if (rankingCalc(player.name) > 0){
  130. stats.set(player.name, [0,0,0,0,0,0]);
  131. room.sendChat("Your stats have been reseted ! ")
  132. }
  133. else (room.sendChat("Musisz posiadać dodatni bilans punktów by módz je zresetować."))
  134. }
  135.  
  136. function clearFun(player){ // !clear
  137. if (player.admin == true) room.clearBans();
  138. }
  139.  
  140. function resetFun(player){
  141. if (player.admin == true){
  142. room.stopGame();
  143. room.startGame();
  144. }
  145. }
  146.  
  147. function gkFun(player){ // !gk
  148. if (room.getScores().time < 60){
  149. if (player.team == 1) {
  150. gk[0] = player;
  151. }
  152. else if (player.team == 2){
  153. gk[1] = player;
  154. }
  155. }
  156. return;
  157. }
  158.  
  159.  
  160. /*
  161. For ranking
  162. */
  163.  
  164. function rankingCalc(player){
  165. return stats.get(player)[0] * 5 + stats.get(player)[1] * 3 +
  166. stats.get(player)[2] * 3 + stats.get(player)[5] * 6 -
  167. stats.get(player)[3] * 7 - stats.get(player)[4] * 4;
  168. }
  169.  
  170. function ranking(){
  171.  
  172. var overall = [];
  173. players = Array.from(stats.keys());
  174. for (var i = 2; i < players.length; i++) {
  175. score = rankingCalc(players[i])
  176. // Goal: 5 pts, assist: 3 pts, win: 3 pts, cs: 6 pts, lose: -7 pts, og: -4 pts
  177. overall.push({name: players[i], value: score});
  178. }
  179. overall.sort(function(a,b){
  180. return b.value - a.value;
  181. })
  182. string = "";
  183.  
  184. for (var i = 0; i < overall.length; i++) {
  185. if (overall[i].value != 0){
  186. string += i+1 + ") " + overall[i].name + ": " + overall[i].value + " pts, ";
  187. }
  188. }
  189. return string;
  190. }
  191.  
  192. function sendStats(name){
  193. ps = stats.get(name); // stands for playerstats
  194. room.sendChat(name + ": goals: " + ps[0] + ", assists: " + ps[1]
  195. + ", og: " + ps[4] + ", cs: " + ps[5] + ", wins: " + ps[2] + ", loses: " + ps[3] +
  196. " points: " + rankingCalc(name));
  197. }
  198.  
  199.  
  200. function whichTeam(){ // gives the players in the red or blue team
  201. var players = room.getPlayerList();
  202. var redTeam = players.filter(player => player.team == 1);
  203. var blueTeam = players.filter(player => player.team == 2);
  204. return [redTeam, blueTeam]
  205. }
  206.  
  207.  
  208.  
  209. function isGk(){ // gives the mosts backward players before the first kickOff
  210. var players = room.getPlayerList();
  211. var min = players[0];
  212. min.position = {x: room.getBallPosition().x + 60}
  213. var max = min;
  214.  
  215. for (var i = 0; i < players.length; i++) {
  216. if (players[i].position != null){
  217. if (min.position.x > players[i].position.x) min = players[i];
  218. if (max.position.x < players[i].position.x) max = players[i];
  219. }
  220. }
  221. return [min, max]
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. function updateWinLoseStats(winners, losers){
  229. for (var i = 0; i < winners.length; i++) {
  230. stats.get(winners[i].name)[2] += 1;
  231. }
  232. for (var i = 0; i < losers.length; i++) {
  233. stats.get(losers[i].name)[3] += 1;
  234. }
  235. }
  236.  
  237. function initBallCarrying(redTeam, blueTeam){
  238. var ballCarrying = new Map();
  239. var playing = redTeam.concat(blueTeam);
  240. for (var i = 0; i < playing.length; i++) {
  241. ballCarrying.set(playing[i].name, [0, playing[i].team]); // secs, team, %
  242. }
  243. return ballCarrying;
  244. }
  245.  
  246.  
  247.  
  248. function updateTeamPoss(value){
  249. if (value[1] == 1) redPoss += value[0];
  250. if (value[1] == 2) bluePoss += value[0];
  251. }
  252.  
  253. var bluePoss;
  254. var redPoss;
  255. function teamPossFun(){
  256. if (room.getScores() == null) return false;
  257. bluePoss = 0;
  258. redPoss = 0
  259. ballCarrying.forEach(updateTeamPoss);
  260. redPoss = Math.round((redPoss / room.getScores().time) * 100);
  261. bluePoss = Math.round((bluePoss / room.getScores().time) * 100);
  262. room.sendChat("Posiadanie piłki: red " + redPoss + " - " + bluePoss + " blue." );
  263.  
  264. }
  265.  
  266.  
  267.  
  268. /*
  269. For the game
  270. */
  271.  
  272. // Gives the last player who touched the ball, works only if the ball has the same
  273. // size than in classics maps.
  274. var radiusBall = 10;
  275. var triggerDistance = radiusBall + 15 + 0.1;
  276. function getLastTouchTheBall(lastPlayerTouched, time) {
  277. var ballPosition = room.getBallPosition();
  278. var players = room.getPlayerList();
  279. for(var i = 0; i < players.length; i++) {
  280. if(players[i].position != null) {
  281. var distanceToBall = pointDistance(players[i].position, ballPosition);
  282. if(distanceToBall < triggerDistance) {
  283. lastPlayerTouched = players[i];
  284. return lastPlayerTouched;
  285. }
  286. }
  287. }
  288. return lastPlayerTouched;
  289.  
  290. }
  291.  
  292.  
  293.  
  294. // Calculate the distance between 2 points
  295. function pointDistance(p1, p2) {
  296. var d1 = p1.x - p2.x;
  297. var d2 = p1.y - p2.y;
  298. return Math.sqrt(d1 * d1 + d2 * d2);
  299. }
  300.  
  301. function isOvertime(){
  302. scores = room.getScores();
  303. if (scores != null){
  304. if (scores.timeLimit != 0){
  305. if (scores.time > scores.timeLimit){
  306. if (scores.red == 0 && hasFinished == false){
  307. stats.get(gk[0].name)[5] += 1;
  308. stats.get(gk[1].name)[5] += 1;
  309. hasFinished = true;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. // return: the name of the team who took a goal
  316. var team_name = team => team == 1 ? "blue" : "red";
  317.  
  318. // return: whether it's an OG
  319. var isOwnGoal = (team, player) => team != player.team ? " (samobój)" : "";
  320.  
  321. // return: a better display of the second when a goal is scored
  322. var floor = s => s < 10 ? "0" + s : s;
  323.  
  324. // return: whether there's an assist
  325. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " ("+ + playerList[1].name + ")" : "";
  326.  
  327.  
  328.  
  329. /*
  330. Events
  331. */
  332. var stats = new Map(); // map where will be set all player stats
  333. var mutedPlayers = []; // Array where will be added muted players
  334. var init = "init"; // Smth to initialize smth
  335. init.id = 0; // Faster than getting host's id with the method
  336. init.name = "init";
  337. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  338. var whoTouchedLast; // var representing the last player who touched the ball
  339. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  340. var gk = [init, init];
  341.  
  342.  
  343. var commands = {
  344. // Command that doesnt need to know players attributes.
  345. "!help": helpFun,
  346. "!gkhelp": gkHelpFun,
  347. "!adminhelp": adminHelpFun,
  348. "!rankhelp": rankHelpFun,
  349. "!ranking": rankFun,
  350. "p": putPauseFun,
  351. "!p": unPauseFun,
  352. "!poss": teamPossFun,
  353.  
  354. // Command that need to know who is the player.
  355. "!resetstats": resetStatsFun,
  356. "!gk": gkFun,
  357.  
  358.  
  359. // Command that need to know if a player is admin.
  360. "!swap": swapFun,
  361. "!rr": resetFun,
  362. "!clear": clearFun,
  363.  
  364. // Command that need to know what's the message.
  365. "!stats": statsFun,
  366.  
  367. // Command that need to know who is the player and what's the message.
  368. "!mute" : pushMuteFun,
  369. "!unmute": unmuteFun
  370.  
  371. }
  372.  
  373. initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
  374. initPlayerStats(init);
  375.  
  376. room.onPlayerLeave = function(player) {
  377. updateAdmins();
  378. }
  379.  
  380.  
  381.  
  382. room.onPlayerJoin = function(player) {
  383. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  384. initPlayerStats(player); // Set new player's stat
  385. room.sendChat("Cześć " + player.name + " ! Komendy !help, !adminhelp, !rankhelp, !gkhelp." )
  386. }
  387.  
  388. var redTeam;
  389. var blueTeam;
  390. room.onGameStart = function() {
  391. [redTeam,blueTeam] = whichTeam();
  392. ballCarrying = initBallCarrying(redTeam, blueTeam);
  393. }
  394.  
  395. room.onPlayerTeamChange = function(player){
  396. if (room.getScores() != null){
  397. if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
  398. }
  399. }
  400.  
  401.  
  402.  
  403. room.onPlayerChat = function(player, message) {
  404. if (mutedPlayers.includes(player.name)) return false;
  405. let spacePos = message.search(" ");
  406. let command = message.substr(0, spacePos !== -1 ? spacePos : message.length);
  407. if (commands.hasOwnProperty(command) == true) return commands[command](player, message);
  408.  
  409. }
  410.  
  411.  
  412.  
  413.  
  414. room.onPlayerBallKick = function (player){
  415. whoTouchedLast = player;
  416. }
  417.  
  418. var kickOff = false;
  419. var hasFinished = false;
  420.  
  421. room.onGameTick = function() {
  422.  
  423. setInterval(isOvertime, 5000, hasFinished);
  424.  
  425. if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
  426. if (room.getScores().time != 0){
  427. kickOff = true;
  428. gk = isGk();
  429. room.sendChat("Red GK: " + gk[0].name + ", Blue GK: " + gk[1].name)
  430. }
  431. }
  432. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  433.  
  434. if (whoTouchedLast != undefined) {
  435.  
  436. if (ballCarrying.get(whoTouchedLast.name)) {
  437. ballCarrying.get(whoTouchedLast.name)[0] += 1/60;
  438. }
  439.  
  440. if ( whoTouchedLast.id != whoTouchedBall[0].id){
  441. whoTouchedBall[1] = whoTouchedBall[0];
  442. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  443. }
  444. }
  445. }
  446.  
  447. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  448.  
  449.  
  450. var time = room.getScores().time;
  451. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  452. time = m + ":" + floor(s); // MM:SS format
  453. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  454. var assist = "";
  455. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  456.  
  457.  
  458. room.sendChat("Gola zdobył " + whoTouchedBall[0].name +
  459. assist + ownGoal + " w " +
  460. time);
  461.  
  462. if (ownGoal != "") {
  463. stats.get(whoTouchedBall[0].name)[4] += 1;
  464. } else {
  465. stats.get(whoTouchedBall[0].name)[0] += 1;
  466. }
  467.  
  468. if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].name)[1] += 1;
  469.  
  470.  
  471. if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  472. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  473. whoTouchedBall = [init, init];
  474. whoTouchedLast = undefined;
  475. }
  476.  
  477.  
  478.  
  479. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  480. if (scores.blue == 0 && gk[0].position != null && hasFinished == false) stats.get(gk[0].name)[5] += 1;
  481. if (scores.red == 0 && gk[1].position != null && hasFinished == false) stats.get(gk[1].name)[5] += 1;
  482. if (scores.red > scores.blue) {
  483. updateWinLoseStats(redTeam, blueTeam);
  484. }
  485. else{ updateWinLoseStats(blueTeam, redTeam); }
  486.  
  487. room.sendChat("Gole:")
  488. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  489. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  490. }
  491. }
  492.  
  493. room.onGameStop = function(){
  494. scorers = undefined;
  495. whoTouchedBall = [init, init];
  496. whoTouchedLast = undefined;
  497. gk = [init, init];
  498. kickOff = false;
  499. hasFinished = false;
  500. }
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509. // Made by Anddy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement