CocoaBotter

Tagpro Fair Teams Userscript

Aug 13th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          Tagpro Fair Teams Userscript
  3. // @namespace     http://www.reddit.com/user/happytagpro/
  4. // @include       http://tagpro-*.koalabeast.com:*
  5. // @include       http://tangent.jukejuice.com:*
  6. // @include       http://*.newcompte.fr:*
  7. // @license       WTFPL
  8. // @author        CocoaBotter (Original version by happy)
  9. // @version       0.2
  10. // ==/UserScript==
  11.  
  12. //set preferences HERE:
  13. automaticallySwitchTeams = true //whenever the regular conditions are satisfied AND you don't have the flag, switch teams
  14. useUnfairTeamsWarning = true //tint the screen slightly when the other team has fewer players then your's does, but the team switching conditions are not met.
  15. requestSwitch = true //if conditions are met, request that someone on the other team to switch to yours
  16.  
  17. // Wait until the tagpro object exists, and add the function to tagpro.ready
  18. function addToTagproReady(fn) {
  19.     // Make sure the tagpro object exists.
  20.     if (typeof tagpro !== "undefined") {
  21.         tagpro.ready(fn);
  22.     } else {
  23.         // If not ready, try again after a short delay.
  24.         setTimeout(function() {
  25.             addToTagproReady(fn);
  26.         }, 0);
  27.     }
  28. }
  29. addToTagproReady(function() {
  30.     //requests that someone switch teams in chat is limited to once per game.
  31.     alreadyRequested = false
  32.     //ensure that this runs at least once when joining a game
  33.     setTimeout(checkConditions, 500)
  34.     //hide switch team button because you don't need it any more
  35.     if (automaticallySwitchTeams) {
  36.         $("#switchButton").css("display", "none")
  37.     }
  38.     //check team switching requirements if someone leaves/joins the game.
  39.     tagpro.socket.on('chat', function(m) {
  40.             if (m.from == null) {
  41.                 setTimeout(checkConditions, 200);
  42.             }
  43.         })
  44.         //check team switching requirements if someone scores or drops the flag.
  45.     tagpro.socket.on("sound", function(message) {
  46.         if (["friendlydrop"].indexOf(message.s) > -1) {
  47.             setTimeout(checkConditions, 3000)
  48.         } else if (["cheering"].indexOf(message.s) > -1) {
  49.             setTimeout(checkConditions, 200)
  50.         }
  51.     });
  52.     function checkConditions() {
  53.         if (tagpro.state != 2) {
  54.             //count number of players on each team
  55.             rCount = 0
  56.             bCount = 0
  57.             for (id in tagpro.players) {
  58.                 rCount += (tagpro.players[id].team == 1)
  59.                 bCount += (tagpro.players[id].team == 2)
  60.             }
  61.             playerCount = {
  62.                     r: rCount,
  63.                     b: bCount
  64.                 }
  65.                 //get team colors
  66.             myTeam = tagpro.players[tagpro.playerId].team == 1 ? 'r' : 'b';
  67.             opponentTeam = myTeam == 'r' ? 'b' : 'r';
  68.             //check conditions for switching teams, tinting, switchteam request
  69.             if (((playerCount[myTeam] - playerCount[opponentTeam] > 1) | (playerCount[myTeam] > playerCount[opponentTeam] & tagpro.score[myTeam] > tagpro.score[opponentTeam])) & !tagpro.players[tagpro.playerId].flag & automaticallySwitchTeams) {
  70.                 tagpro.socket.emit("switch")
  71.                 switchingTeamWarning()
  72.             } else if (playerCount[myTeam] > playerCount[opponentTeam]) {
  73.                 if (useUnfairTeamsWarning) {
  74.                     unevenTeamsWarning("ON")
  75.                 }
  76.             } else if ((playerCount[opponentTeam] - playerCount[myTeam] > 1) | (playerCount[opponentTeam] > playerCount[myTeam] & tagpro.score[opponentTeam] > tagpro.score[myTeam])) {
  77.                 if (requestSwitch) {
  78.                     if (!alreadyRequested) {
  79.                         tagpro.socket.emit("chat", {
  80.                             message: "can someone switch teams?",
  81.                             toAll: true
  82.                         })
  83.                         alreadyRequested = true
  84.                     }
  85.                 }
  86.             } else if (playerCount[myTeam] <= playerCount[opponentTeam]) {
  87.                 unevenTeamsWarning("OFF")
  88.             }
  89.         }
  90.     }
  91.     //unfair team warning tinted overlay
  92.     var unevenTeamsOverlayCSS = {
  93.         height: "100%",
  94.         width: "100%",
  95.         position: "fixed",
  96.         left: 0,
  97.         top: 0,
  98.         background: "rgba(200,200,200,.35)",
  99.         display: "none"
  100.     }
  101.     var unevenTeamsOverlay = '<div class="unevenTeamsOverlay"></div>'
  102.     $('body').find('#sound').after(unevenTeamsOverlay);
  103.     $(".unevenTeamsOverlay").css(unevenTeamsOverlayCSS)
  104.     function unevenTeamsWarning(m) {
  105.         if (m == "ON") {
  106.             $(".unevenTeamsOverlay").css("display", "")
  107.         } else {
  108.             $(".unevenTeamsOverlay").css("display", "none")
  109.         }
  110.     }
  111.     //message/overlay when switching teams
  112.     var switchingTeamOverlayCSS = {
  113.         height: "100%",
  114.         width: "100%",
  115.         position: "fixed",
  116.         left: 0,
  117.         top: 0,
  118.         background: "rgba(10,10,10,.7)",
  119.         display: "none",
  120.         "text-align": "center",
  121.         "line-height": "300px",
  122.         "font-size": "x-large"
  123.     }
  124.     var switchingTeamOverlay = '<div class="switchingTeamOverlay"><h3>Switching Teams...</h3></div>'
  125.     $('body').find('#sound').after(switchingTeamOverlay);
  126.     $(".switchingTeamOverlay").css(switchingTeamOverlayCSS)
  127.     function switchingTeamWarning() {
  128.         $(".switchingTeamOverlay").css("display", "")
  129.         setTimeout(function() {
  130.             $(".switchingTeamOverlay").css("display", "none")
  131.         }, 2000);
  132.     }
  133.     //disable overlays at end of game
  134.     tagpro.socket.on("end", function() {
  135.         $(".unevenTeamsOverlay").remove()
  136.         $(".switchingTeamOverlay").remove()
  137.     })
  138. });
Add Comment
Please, Sign In to add comment