Advertisement
Guest User

HEADLESS HOST

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