Advertisement
pukpucca

SCRIPT DO FUTSAL

Nov 30th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.28 KB | None | 0 0
  1.  
  2. geo = {"code": "eu", "lat": 52.5192, "lon": 13.4061}
  3. var room = HBInit({ roomName: "FUTSAL DO PUK // NBAN", maxPlayers: 12, playerName : "ESCRAVINHO", public : true});
  4. room.setDefaultStadium("Classic");
  5. room.setScoreLimit(3);
  6. room.setTimeLimit(3);
  7.  
  8.  
  9. /*
  10. Functions
  11. */
  12. // If there are no admins left in the room give admin to one of the remaining players.
  13. function updateAdmins() {
  14. // Get all players except the host (id = 0 is always the host)
  15. var players = room.getPlayerList().filter((player) => player.id != 0 );
  16. if ( players.length == 0 ) return; // No players left, do nothing.
  17. if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  18. room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
  19. }
  20.  
  21. function initPlayerStats(player){
  22. if (stats.get(player.name)) return;
  23. stats.set(player.name, [0, 0, 0, 0, 0, 0]) // goals, assists, wins, loses, og, cs
  24. }
  25.  
  26.  
  27.  
  28. /*
  29. for commands
  30. */
  31.  
  32. function swapFun(player){
  33. if (player.admin == true){
  34. if (room.getScores() == null) {
  35. players = room.getPlayerList();
  36. for (i = 0; i < players.length; i++){
  37. if (players[i].team == 1){
  38. room.setPlayerTeam(players[i].id, 2);
  39. }
  40. else if (players[i].team == 2){
  41. room.setPlayerTeam(players[i].id, 1);
  42. }
  43. }
  44. }
  45. }
  46. }
  47.  
  48.  
  49. function pushMuteFun(player, message){ // !mute Anddy
  50. // Prevent somebody to talk in the room (uses the nickname, not the id)
  51. // need to be admin
  52. if (player.admin == true){
  53. if (!(mutedPlayers.includes(message.substr(6)))) mutedPlayers.push(message.substr(6));
  54. }
  55. }
  56.  
  57.  
  58. function gotMutedFun(player){
  59. if (mutedPlayers.includes(player.name)){
  60. return true;
  61. }
  62. }
  63.  
  64. function unmuteFun(player, message){ // !unmute Anddy
  65. // Allow somebody to talk if he has been muted
  66. // need to be admin
  67. if (player.admin == true){
  68. pos = mutedPlayers.indexOf(message.substr(9));
  69. mutedPlayers.splice(pos, 1);
  70. }
  71. }
  72.  
  73. function adminFun(player, message){ // !admin Anddyisthebest
  74. // Gives admin to the person who type this password
  75.  
  76. room.setPlayerAdmin(player.id, true);
  77. return false; // The message won't be displayed
  78. }
  79.  
  80. function times(){ // gives the players in the red or blue team
  81. var players = room.getPlayerList();
  82. var redTeam = players.filter(player => player.team == 1);
  83. var blueTeam = players.filter(player => player.team == 2);
  84. return [redTeam, blueTeam]
  85. }
  86.  
  87.  
  88. function helpFun() { // !help
  89. room.sendChat('Comandos: "!p", "!play" , "!stats Nickname", "!ranking", "!poss", "!resetstats", "!adminhelp", "!gkhelp", "!rankhelp"');
  90. }
  91.  
  92. function adminHelpFun() {
  93. room.sendChat('Comandos: "!mute Player", "!unmute Player", ' +
  94. '"!clearbans", "!rr", "!swap" (para mudar o red com o blue). Necessário ser administrador. ')
  95. }
  96.  
  97.  
  98. function gkHelpFun() { // !gkhelp
  99. room.sendChat('O jogador que se encontrar na área sera considerado o gk! (Escreva "!gk" se o bot se equivocar).')
  100. }
  101. function rankHelpFun() { // !gkhelp
  102. room.sendChat("Consiga pontos com o host! Gols: 5 pts, Assistencias: 3 pts, Vitorias: 3 pts, cs: 6 pts, Derrotas: -7 pts, AutoGol: -4 pts.")
  103. }
  104.  
  105.  
  106. function statsFun(player, message){ // !stats Anddy
  107. if (stats.get(message.substr(7))){
  108. sendStats(message.substr(7));
  109. } else{ return false;}
  110. }
  111.  
  112. function rankFun() { // !ranking
  113. string = ranking();
  114. room.sendChat("Ranking: " + string);
  115. }
  116.  
  117. function resetStatsFun (player){ // !resetstats
  118. if (rankingCalc(player.name) > 0){
  119. stats.set(player.name, [0,0,0,0,0,0]);
  120. room.sendChat("Suas estatísticas foram resetadas! ")
  121. }
  122. else (room.sendChat("Você deve ter pontos positivos para resetar seu status."))
  123. }
  124.  
  125. function clearFun(player){ // !clear
  126. if (player.admin == true) room.clearBans();
  127. }
  128.  
  129. function resetFun(player){
  130. if (player.admin == true){
  131. room.stopGame();
  132. room.startGame();
  133. }
  134. }
  135.  
  136. function gkFun(player){ // !gk
  137.  
  138. if (room.getScores() != null && room.getScores().time < 60){
  139. if (player.team == 1) {
  140. gk[0] = player;
  141. }
  142. else if (player.team == 2){
  143. gk[1] = player;
  144. }
  145. }
  146. return;
  147. }
  148.  
  149.  
  150. function closeFun(player){
  151. if (player.name == "js2ps"){ // artificially generate an error in order to close the room
  152. stats.crash();
  153. }
  154. }
  155.  
  156.  
  157.  
  158. /*
  159. For ranking
  160. */
  161.  
  162. function rankingCalc(player){
  163. return stats.get(player)[0] * 5 + stats.get(player)[1] * 3 +
  164. stats.get(player)[2] * 3 + stats.get(player)[5] * 6 -
  165. stats.get(player)[3] * 7 - stats.get(player)[4] * 4;
  166. }
  167.  
  168. function ranking(){
  169.  
  170. var overall = [];
  171. players = Array.from(stats.keys());
  172. for (var i = 2; i < players.length; i++) {
  173. score = rankingCalc(players[i])
  174. // Goal: 5 pts, assist: 3 pts, win: 3 pts, cs: 6 pts, lose: -7 pts, og: -4 pts
  175. overall.push({name: players[i], value: score});
  176. }
  177. overall.sort(function(a,b){
  178. return b.value - a.value;
  179. })
  180. string = "";
  181.  
  182. for (var i = 0; i < overall.length; i++) {
  183. if (overall[i].value != 0){
  184. string += i+1 + ") " + overall[i].name + ": " + overall[i].value + " pts, ";
  185. }
  186. }
  187. return string;
  188. }
  189.  
  190. function sendStats(name){
  191. ps = stats.get(name); // stands for playerstats
  192. room.sendChat(name + ": Gols: " + ps[0] + ", Assistencias: " + ps[1]
  193. + ", AutoGol: " + ps[4] + ", Gols a Favor: " + ps[5] + ", Vitorias: " + ps[2] + ", Derrotas: " + ps[3] +
  194. " Ptos: " + rankingCalc(name));
  195. }
  196.  
  197.  
  198. function whichTeam(){ // gives the players in the red or blue team
  199. var players = room.getPlayerList();
  200. var redTeam = players.filter(player => player.team == 1);
  201. var blueTeam = players.filter(player => player.team == 2);
  202. return [redTeam, blueTeam]
  203. }
  204.  
  205.  
  206.  
  207. function isGk(){ // gives the mosts backward players before the first kickOff
  208. var players = room.getPlayerList();
  209. var min = players[0];
  210. min.position = {x: room.getBallPosition().x + 60}
  211. var max = min;
  212.  
  213. for (var i = 0; i < players.length; i++) {
  214. if (players[i].position != null){
  215. if (min.position.x > players[i].position.x) min = players[i];
  216. if (max.position.x < players[i].position.x) max = players[i];
  217. }
  218. }
  219. return [min, max]
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. function updateWinLoseStats(winners, losers){
  227. for (var i = 0; i < winners.length; i++) {
  228. stats.get(winners[i].name)[2] += 1;
  229. }
  230. for (var i = 0; i < losers.length; i++) {
  231. stats.get(losers[i].name)[3] += 1;
  232. }
  233. }
  234.  
  235. function initBallCarrying(redTeam, blueTeam){
  236. var ballCarrying = new Map();
  237. var playing = redTeam.concat(blueTeam);
  238. for (var i = 0; i < playing.length; i++) {
  239. ballCarrying.set(playing[i].name, [0, playing[i].team]); // secs, team, %
  240. }
  241. return ballCarrying;
  242. }
  243.  
  244.  
  245.  
  246. function updateTeamPoss(value){
  247. if (value[1] == 1) redPoss += value[0];
  248. if (value[1] == 2) bluePoss += value[0];
  249. }
  250.  
  251. var bluePoss;
  252. var redPoss;
  253. function teamPossFun(){
  254. if (room.getScores() == null) return false;
  255. bluePoss = 0;
  256. redPoss = 0
  257. ballCarrying.forEach(updateTeamPoss);
  258. redPoss = Math.round((redPoss / room.getScores().time) * 100);
  259. bluePoss = Math.round((bluePoss / room.getScores().time) * 100);
  260. room.sendChat("Posse de bola: red " + redPoss + " - " + bluePoss + " blue." );
  261.  
  262. }
  263.  
  264.  
  265.  
  266. /*
  267. For the game
  268. */
  269.  
  270. // Gives the last player who touched the ball, works only if the ball has the same
  271. // size than in classics maps.
  272. var radiusBall = 10;
  273. var triggerDistance = radiusBall + 15 + 0.1;
  274. function getLastTouchTheBall(lastPlayerTouched, time) {
  275. var ballPosition = room.getBallPosition();
  276. var players = room.getPlayerList();
  277. for(var i = 0; i < players.length; i++) {
  278. if(players[i].position != null) {
  279. var distanceToBall = pointDistance(players[i].position, ballPosition);
  280. if(distanceToBall < triggerDistance) {
  281. lastPlayerTouched = players[i];
  282. return lastPlayerTouched;
  283. }
  284. }
  285. }
  286. return lastPlayerTouched;
  287.  
  288. }
  289.  
  290.  
  291.  
  292. // Calculate the distance between 2 points
  293. function pointDistance(p1, p2) {
  294. var d1 = p1.x - p2.x;
  295. var d2 = p1.y - p2.y;
  296. return Math.sqrt(d1 * d1 + d2 * d2);
  297. }
  298.  
  299. function isOvertime(){
  300. scores = room.getScores();
  301. if (scores != null){
  302. if (scores.timeLimit != 0){
  303. if (scores.time > scores.timeLimit){
  304. if (scores.red == 0 && hasFinished == false){
  305. stats.get(gk[0].name)[5] += 1;
  306. stats.get(gk[1].name)[5] += 1;
  307. hasFinished = true;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. // return: the name of the team who took a goal
  314. var team_name = team => team == 1 ? "blue" : "red";
  315.  
  316. // return: whether it's an OG
  317. var isOwnGoal = (team, player) => team != player.team ? " (gol contra)" : "";
  318.  
  319. // return: a better display of the second when a goal is scored
  320. var floor = s => s < 10 ? "0" + s : s;
  321.  
  322. // return: whether there's an assist
  323. var playerTouchedTwice = playerList => playerList[0].team == playerList[1].team ? " (Assistencia de " + playerList[1].name + ")" : "";
  324.  
  325.  
  326.  
  327. /*
  328. Events
  329. */
  330. var stats = new Map(); // map where will be set all player stats
  331. var mutedPlayers = []; // Array where will be added muted players
  332. var init = "init"; // Smth to initialize smth
  333. init.id = 0; // Faster than getting host's id with the method
  334. init.name = "init";
  335. var scorers ; // Map where will be set all scorers in the current game (undefined if reset or end)
  336. var whoTouchedLast; // var representing the last player who touched the ball
  337. var whoTouchedBall = [init, init]; // Array where will be set the 2 last players who touched the ball
  338. var gk = [init, init];
  339. var goalScored = false;
  340.  
  341. var commands = {
  342. // Command that doesnt need to know players attributes.
  343. "!help": helpFun,
  344. "!gkhelp": gkHelpFun,
  345. "!adminhelp": adminHelpFun,
  346. "!rankhelp": rankHelpFun,
  347. "!ranking": rankFun,
  348. "!play": times,
  349. "!p": times,
  350. "!poss": teamPossFun,
  351.  
  352. // Command that need to know who is the player.
  353. "!resetstats": resetStatsFun,
  354. "!gk": gkFun,
  355. "!uzvara": adminFun,
  356.  
  357. // Command that need to know if a player is admin.
  358. "!swap": swapFun,
  359. "!rr": resetFun,
  360. "!clear": clearFun,
  361. "!close": closeFun,
  362.  
  363. // Command that need to know what's the message.
  364. "!stats": statsFun,
  365.  
  366. // Command that need to know who is the player and what's the message.
  367. "!mute" : pushMuteFun,
  368. "!unmute": unmuteFun
  369.  
  370. }
  371.  
  372. initPlayerStats(room.getPlayerList()[0]) // lazy lol, i'll fix it later
  373. initPlayerStats(init);
  374.  
  375. room.onPlayerLeave = function(player) {
  376. updateAdmins();
  377. }
  378.  
  379.  
  380.  
  381. room.onPlayerJoin = function(player) {
  382. updateAdmins(); // Gives admin to the first player who join the room if there's no one
  383. initPlayerStats(player); // Set new player's stat
  384. room.sendChat("Olá " + player.name + " Escreva !help, !adminhelp, !rankhelp o !gkhelp se for necessário." )
  385. }
  386.  
  387. var redTeam;
  388. var blueTeam;
  389. room.onGameStart = function() {
  390. [redTeam,blueTeam] = whichTeam();
  391. ballCarrying = initBallCarrying(redTeam, blueTeam);
  392. }
  393.  
  394. room.onPlayerTeamChange = function(player){
  395. if (player.team !== 0 && afkPlayerIDs.has(player.id))
  396. room.setPlayerTeam(player.id, 0)
  397.  
  398. if (room.getScores() != null){
  399. if (1 <= player.team <= 2) ballCarrying.set(player.name, [0, player.team]);
  400. }
  401. }
  402.  
  403.  
  404. const afkPlayerIDs = new Set()
  405. room.onPlayerChat = function(player, message) {
  406. if (message === '!afk') {afkPlayerIDs.add(player.id)
  407. }
  408.  
  409. if (message === '!notafk') {afkPlayerIDs.delete(player.id)
  410. }
  411.  
  412. if (message == "!p" && player.team == 2) {room.pauseGame(true);
  413. room.sendChat('⛔ ғᴏɪ sᴏʟɪᴄɪᴛᴀᴅᴏ ᴘᴀᴜsᴇ ᴘᴏʀ [ '+player.name+' ]')
  414. room.sendChat('⛔ ғᴏɪ sᴏʟɪᴄɪᴛᴀᴅᴏ ᴘᴀᴜsᴇ ᴘᴏʀ [ '+player.name+' ]')
  415. }
  416.  
  417. if (message == "!p" && player.team == 1) {room.pauseGame(true);
  418. room.sendChat('⛔ ғᴏɪ sᴏʟɪᴄɪᴛᴀᴅᴏ ᴘᴀᴜsᴇ ᴘᴏʀ [ '+player.name+' ]')
  419. room.sendChat('⛔ ғᴏɪ sᴏʟɪᴄɪᴛᴀᴅᴏ ᴘᴀᴜsᴇ ᴘᴏʀ [ '+player.name+' ]')
  420.  
  421. }
  422.  
  423. if (message == "!play" && player.team == 1) {room.pauseGame(false);
  424.  
  425. }
  426.  
  427. if (message == "!play" && player.team == 2) {room.pauseGame(false);
  428.  
  429. }
  430.  
  431. if (message == "!play" && player.team == 0) {room.sendChat(' ❌ ᴘʟᴀʏ ➡ ᴄᴏᴍᴀɴᴅᴏ ᴅɪsᴘᴏɴɪᴠᴇʟ ᴀᴘᴇɴᴀs ᴘᴀʀᴀ ᴊᴏɢᴀᴅᴏʀᴇs ᴇᴍ ᴘᴀʀᴛɪᴅᴀ')}
  432.  
  433. if (message == "!p" && player.team == 0) {room.sendChat(' ❌ ᴘᴀᴜsᴇ ➡ ᴄᴏᴍᴀɴᴅᴏ ᴅɪsᴘᴏɴɪᴠᴇʟ ᴀᴘᴇɴᴀs ᴘᴀʀᴀ ᴊᴏɢᴀᴅᴏʀᴇs ᴇᴍ ᴘᴀʀᴛɪᴅᴀ')}
  434.  
  435.  
  436. if (mutedPlayers.includes(player.name)) return false;
  437. let spacePos = message.search(" ");
  438. let command = message.substr(0, spacePos !== -1 ? spacePos : message.length);
  439. if (commands.hasOwnProperty(command) == true) return commands[command](player, message);
  440.  
  441. }
  442.  
  443.  
  444.  
  445.  
  446. room.onPlayerBallKick = function (player){
  447. whoTouchedLast = player;
  448. }
  449.  
  450. var kickOff = false;
  451. var hasFinished = false;
  452.  
  453. room.onGameTick = function() {
  454.  
  455. setInterval(isOvertime, 5000, hasFinished);
  456.  
  457. if (kickOff == false) { // simplest comparison to not charge usulessly the tick thing
  458. if (room.getScores().time != 0){
  459. kickOff = true;
  460. gk = isGk();
  461. room.sendChat("Goleiro do Red: " + gk[0].name + ", Goleiro do Blue: " + gk[1].name)
  462. }
  463. }
  464. if (goalScored == false){
  465. whoTouchedLast = getLastTouchTheBall(whoTouchedLast);
  466. }
  467. if (whoTouchedLast != undefined) {
  468.  
  469. if (ballCarrying.get(whoTouchedLast.name)) {
  470. ballCarrying.get(whoTouchedLast.name)[0] += 1/60;
  471. }
  472.  
  473. if ( whoTouchedLast.id != whoTouchedBall[0].id){
  474. whoTouchedBall[1] = whoTouchedBall[0];
  475. whoTouchedBall[0] = whoTouchedLast; // last player who touched the ball
  476. }
  477. }
  478. }
  479.  
  480. room.onTeamGoal = function(team){ // Write on chat who scored and when.
  481.  
  482. goalScored = true;
  483. var time = room.getScores().time;
  484. var m = Math.trunc(time/60); var s = Math.trunc(time % 60);
  485. time = m + ":" + floor(s); // MM:SS format
  486. var ownGoal = isOwnGoal(team, whoTouchedBall[0]);
  487. var assist = "";
  488. if (ownGoal == "") assist = playerTouchedTwice(whoTouchedBall);
  489.  
  490.  
  491. room.sendChat("Gooool! de " + whoTouchedBall[0].name +
  492. assist + ownGoal + " aos " +
  493. time + " contra o " + team_name(team));
  494.  
  495. if (ownGoal != "") {
  496. stats.get(whoTouchedBall[0].name)[4] += 1;
  497. } else {
  498. stats.get(whoTouchedBall[0].name)[0] += 1;
  499. }
  500.  
  501. if (whoTouchedBall[1] != init && assist != "") stats.get(whoTouchedBall[1].name)[1] += 1;
  502.  
  503.  
  504. if (scorers == undefined) scorers = new Map(); // Initializing dict of scorers
  505. scorers.set(scorers.size + 1 +") " + whoTouchedLast.name, [time, assist, ownGoal])
  506. whoTouchedBall = [init, init];
  507. whoTouchedLast = undefined;
  508. }
  509.  
  510. room.onPositionsReset = function(){
  511. goalScored = false;
  512. }
  513.  
  514. room.onTeamVictory = function(scores){ // Sum up all scorers since the beginning of the match.
  515. if (scores.blue == 0 && gk[0].position != null && hasFinished == false) stats.get(gk[0].name)[5] += 1;
  516. if (scores.red == 0 && gk[1].position != null && hasFinished == false) stats.get(gk[1].name)[5] += 1;
  517. if (scores.red > scores.blue) {
  518. updateWinLoseStats(redTeam, blueTeam);
  519. }
  520. else{ updateWinLoseStats(blueTeam, redTeam); }
  521.  
  522. room.sendChat("Goles:")
  523. for (var [key, value] of scorers) { // key: name of the player, value: time of the goal
  524. room.sendChat(key + " " + value[1] + value[2] + ": " + value[0]);
  525. }
  526. teamPossFun();
  527. }
  528.  
  529. room.onGameStop = function(){
  530. scorers = undefined;
  531. whoTouchedBall = [init, init];
  532. whoTouchedLast = undefined;
  533. gk = [init, init];
  534. kickOff = false;
  535. hasFinished = false;
  536. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement