Advertisement
Guest User

Anda 7#

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