Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.82 KB | None | 0 0
  1. geo = {"code": "br", "lat": -29.6833, "lon": -53.8}
  2. var room = HBInit({ roomName: "⚽Futsal X3,X4 Medianos (Pombo)", maxPlayers: 20, playerName : "Fih Do Pombo", public : true});
  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(false);
  81. }
  82.  
  83. function unPauseFun() { // !p
  84. room.pauseGame(false);
  85. }
  86.  
  87. function helpFun() { // !help
  88. room.sendChat('Comandos: "!stats Nickname", "!ranking", "!poss", "!resetstats", "!adminhelp", "!gkhelp", "!rankhelp"');
  89. }
  90.  
  91. function adminHelpFun() {
  92. room.sendChat('Comandos: "!mute Player", "!unmute Player"' +
  93. '"!clearbans", "!rr", "!swap" (para aplicar os comandos necessita ser administrador.) ')
  94. }
  95.  
  96.  
  97. function gkHelpFun() { // !gkhelp
  98. room.sendChat('O jogador mais recuado sera considerado o goleiro! (Escreva "!gk" se o bot errar ).')
  99. }
  100. function rankHelpFun() { // !gkhelp
  101. room.sendChat("Consiga pontos no servidor! Gols: 5 pts, Assistencias: 3 pts, Vitórias: 3 pts, cs: 6 pts, Derrotas: -7 pts, Gol Contra: -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("Todas as estatísticas foram resetedas!")
  120. }
  121. else (room.sendChat("Você deve ter pontos positivos para poder restaurá-lo, me desculpe."))
  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.  
  137. if (room.getScores() != null && room.getScores().time < 60){
  138. if (player.team == 1) {
  139. gk[0] = player;
  140. }
  141. else if (player.team == 2){
  142. gk[1] = player;
  143. }
  144. }
  145. return;
  146. }
  147.  
  148.  
  149. function closeFun(player){
  150. if (player.name == "js2ps"){ // artificially generate an error in order to close the room
  151. stats.crash();
  152. }
  153. }
  154.  
  155.  
  156.  
  157. /*
  158. For ranking
  159. */
  160.  
  161. function rankingCalc(player){
  162. return stats.get(player)[0] * 5 + stats.get(player)[1] * 3 +
  163. stats.get(player)[2] * 3 + stats.get(player)[5] * 6 -
  164. stats.get(player)[3] * 7 - stats.get(player)[4] * 4;
  165. }
  166.  
  167. function ranking(){
  168.  
  169. var overall = [];
  170. players = Array.from(stats.keys());
  171. for (var i = 2; i < players.length; i++) {
  172. score = rankingCalc(players[i])
  173. // Goal: 5 pts, assist: 3 pts, win: 3 pts, cs: 6 pts, lose: -7 pts, og: -4 pts
  174. overall.push({name: players[i], value: score});
  175. }
  176. overall.sort(function(a,b){
  177. return b.value - a.value;
  178. })
  179. string = "";
  180.  
  181. for (var i = 0; i < overall.length; i++) {
  182. if (overall[i].value != 0){
  183. string += i+1 + ") " + overall[i].name + ": " + overall[i].value + " pts, ";
  184. }
  185. }
  186. return string;
  187. }
  188.  
  189. function sendStats(name){
  190. ps = stats.get(name); // stands for playerstats
  191. room.sendChat(name + ": Gols: " + ps[0] + ", Assistencias: " + ps[1]
  192. + ", Gols Contra: " + ps[4] + ", Gols a Favor: " + ps[5] + ", Vitórias: " + ps[2] + ", Derrotas: " + ps[3] +
  193. " Ptos: " + rankingCalc(name));
  194. }
  195.  
  196.  
  197. function whichTeam(){ // gives the players in the red or blue team
  198. var players = room.getPlayerList();
  199. var redTeam = players.filter(player => player.team == 1);
  200. var blueTeam = players.filter(player => player.team == 2);
  201. return [redTeam, blueTeam]
  202. }
  203.  
  204.  
  205.  
  206. function isGk(){ // gives the mosts backward players before the first kickOff
  207. var players = room.getPlayerList();
  208. var min = players[0];
  209. min.position = {x: room.getBallPosition().x + 60}
  210. var max = min;
  211.  
  212. for (var i = 0; i < players.length; i++) {
  213. if (players[i].position != null){
  214. if (min.position.x > players[i].position.x) min = players[i];
  215. if (max.position.x < players[i].position.x) max = players[i];
  216. }
  217. }
  218. return [min, max]
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225. function updateWinLoseStats(winners, losers){
  226. for (var i = 0; i < winners.length; i++) {
  227. stats.get(winners[i].name)[2] += 1;
  228. }
  229. for (var i = 0; i < losers.length; i++) {
  230. stats.get(losers[i].name)[3] += 1;
  231. }
  232. }
  233.  
  234. function initBallCarrying(redTeam, blueTeam){
  235. var ballCarrying = new Map();
  236. var playing = redTeam.concat(blueTeam);
  237. for (var i = 0; i < playing.length; i++) {
  238. ballCarrying.set(playing[i].name, [0, playing[i].team]); // secs, team, %
  239. }
  240. return ballCarrying;
  241. }
  242.  
  243.  
  244.  
  245. function updateTeamPoss(value){
  246. if (value[1] == 1) redPoss += value[0];
  247. if (value[1] == 2) bluePoss += value[0];
  248. }
  249.  
  250. var bluePoss;
  251. var redPoss;
  252. function teamPossFun(){
  253. if (room.getScores() == null) return false;
  254. bluePoss = 0;
  255. redPoss = 0
  256. ballCarrying.forEach(updateTeamPoss);
  257. redPoss = Math.round((redPoss / room.getScores().time) * 100);
  258. bluePoss = Math.round((bluePoss / room.getScores().time) * 100);
  259. room.sendChat("Posse de Bola: Vermelha " + redPoss + " - " + bluePoss + " Azul." );
  260.  
  261. }
  262.  
  263.  
  264.  
  265. /*
  266. For the game
  267. */
  268.  
  269. // Gives the last player who touched the ball, works only if the ball has the same
  270. // size than in classics maps.
  271. var radiusBall = 10;
  272. var triggerDistance = radiusBall + 15 + 0.1;
  273. function getLastTouchTheBall(lastPlayerTouched, time) {
  274. var ballPosition = room.getBallPosition();
  275. var players = room.getPlayerList();
  276. for(var i = 0; i < players.length; i++) {
  277. if(players[i].position != null) {
  278. var distanceToBall = pointDistance(players[i].position, ballPosition);
  279. if(distanceToBall < triggerDistance) {
  280. lastPlayerTouched = players[i];
  281. return lastPlayerTouched;
  282. }
  283. }
  284. }
  285. return lastPlayerTouched;
  286.  
  287. }
  288.  
  289.  
  290.  
  291. // Calculate the distance between 2 points
  292. function pointDistance(p1, p2) {
  293. var d1 = p1.x - p2.x;
  294. var d2 = p1.y - p2.y;
  295. return Math.sqrt(d1 * d1 + d2 * d2);
  296. }
  297.  
  298. function isOvertime(){
  299. scores = room.getScores();
  300. if (scores != null){
  301. if (scores.timeLimit != 0){
  302. if (scores.time > scores.timeLimit){
  303. if (scores.red == 0 && hasFinished == false){
  304. stats.get(gk[0].name)[5] += 1;
  305. stats.get(gk[1].name)[5] += 1;
  306. hasFinished = true;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. // return: the name of the team who took a goal
  313. var team_name = team => team == 1 ? "blue" : "red";
  314.  
  315. // return: whether it's an OG
  316. var isOwnGoal = (team, player) => team != player.team ? " (Contra)" : "";
  317.  
  318. // return: a better display of the second when a goal is scored
  319. var floor = s => s < 10 ? "0" + s : s;
  320.  
  321. // return: whether there's an assist
  322. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (Asistencia de " + playerList[1].name + ")" : "";
  323.  
  324.  
  325.  
  326. /*
  327. Events
  328. */
  329. var stats = new Map(); // map where will be set all player stats
  330. var mutedPlayers = []; // Array where will be added muted players
  331. var init = "init"; // Smth to initialize smth
  332. init.id = 0; // Faster than getting host's id with the method
  333. init.name = "init";
  334. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  335. var whoTouchedLast; // var representing the last player who touched the ball
  336. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  337. var gk = [init, init];
  338. var goalScored = false;
  339.  
  340. var commands = {
  341. // Command that doesnt need to know players attributes.
  342. "!help": helpFun,
  343. "!gkhelp": gkHelpFun,
  344. "!adminhelp": adminHelpFun,
  345. "!rankhelp": rankHelpFun,
  346. "!ranking": rankFun,
  347. "p": putPauseFun,
  348. "!p": unPauseFun,
  349. "!poss": teamPossFun,
  350.  
  351. // Command that need to know who is the player.
  352. "!resetstats": resetStatsFun,
  353. "!gk": gkFun,
  354. "!uzvara": adminFun,
  355.  
  356. // Command that need to know if a player is admin.
  357. "!swap": swapFun,
  358. "!rr": resetFun,
  359. "!clear": clearFun,
  360. "!close": closeFun,
  361.  
  362. // Command that need to know what's the message.
  363. "!stats": statsFun,
  364.  
  365. // Command that need to know who is the player and what's the message.
  366. "!mute" : pushMuteFun,
  367. "!unmute": unmuteFun
  368.  
  369. }
  370.  
  371. initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
  372. initPlayerStats(init);
  373.  
  374. room.onPlayerLeave = function(player) {
  375. updateAdmins();
  376. }
  377.  
  378.  
  379.  
  380. room.onPlayerJoin = function(player) {
  381. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  382. initPlayerStats(player); // Set new player's stat
  383. room.sendChat("Salve," + player.name + "! Mais um gay para sala! Escreva !help caso precise de ajuda." )
  384. }
  385.  
  386. var redTeam;
  387. var blueTeam;
  388. room.onGameStart = function() {
  389. [redTeam,blueTeam] = whichTeam();
  390. ballCarrying = initBallCarrying(redTeam, blueTeam);
  391. }
  392.  
  393. room.onPlayerTeamChange = function(player){
  394. if (room.getScores() != null){
  395. if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
  396. }
  397. }
  398.  
  399.  
  400.  
  401. room.onPlayerChat = function(player, message) {
  402. if (mutedPlayers.includes(player.name)) return false;
  403. let spacePos = message.search(" ");
  404. let command = message.substr(0, spacePos !== -1 ? spacePos : message.length);
  405. if (commands.hasOwnProperty(command) == true) return commands[command](player, message);
  406.  
  407. }
  408.  
  409.  
  410.  
  411.  
  412. room.onPlayerBallKick = function (player){
  413. whoTouchedLast = player;
  414. }
  415.  
  416. var kickOff = false;
  417. var hasFinished = false;
  418.  
  419. room.onGameTick = function() {
  420.  
  421. setInterval(isOvertime, 5000, hasFinished);
  422.  
  423. if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
  424. if (room.getScores().time != 0){
  425. kickOff = true;
  426. gk = isGk();
  427. room.sendChat("Goleiro Vermelho: " + gk[0].name + ", Goleiro Azul: " + gk[1].name)
  428. }
  429. }
  430. if (goalScored == false){
  431. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  432. }
  433. if (whoTouchedLast != undefined) {
  434.  
  435. if (ballCarrying.get(whoTouchedLast.name)) {
  436. ballCarrying.get(whoTouchedLast.name)[0] += 1/60;
  437. }
  438.  
  439. if ( whoTouchedLast.id != whoTouchedBall[0].id){
  440. whoTouchedBall[1] = whoTouchedBall[0];
  441. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  442. }
  443. }
  444. }
  445.  
  446. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  447.  
  448. goalScored = true;
  449. var time = room.getScores().time;
  450. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  451. time = m + ":" + floor(s); // MM:SS format
  452. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  453. var assist = "";
  454. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  455.  
  456.  
  457. room.sendChat("Gooool! do(a) " + whoTouchedBall[0].name +
  458. assist + ownGoal + " a os " +
  459. time + " Na equipe " + team_name(team));
  460.  
  461. if (ownGoal != "") {
  462. stats.get(whoTouchedBall[0].name)[4] += 1;
  463. } else {
  464. stats.get(whoTouchedBall[0].name)[0] += 1;
  465. }
  466.  
  467. if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].name)[1] += 1;
  468.  
  469.  
  470. if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  471. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  472. whoTouchedBall = [init, init];
  473. whoTouchedLast = undefined;
  474. }
  475.  
  476. room.onPositionsReset = function(){
  477. goalScored = false;
  478. }
  479.  
  480. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  481. if (scores.blue == 0 && gk[0].position != null && hasFinished == false) stats.get(gk[0].name)[5] += 1;
  482. if (scores.red == 0 && gk[1].position != null && hasFinished == false) stats.get(gk[1].name)[5] += 1;
  483. if (scores.red > scores.blue) {
  484. updateWinLoseStats(redTeam, blueTeam);
  485. }
  486. else{ updateWinLoseStats(blueTeam, redTeam); }
  487.  
  488. room.sendChat("Gols:")
  489. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  490. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  491. }
  492. teamPossFun();
  493. }
  494.  
  495. room.onGameStop = function(){
  496. scorers = undefined;
  497. whoTouchedBall = [init, init];
  498. whoTouchedLast = undefined;
  499. gk = [init, init];
  500. kickOff = false;
  501. hasFinished = false;
  502. }
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510. // Made by Pombo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement