TagPro AutoStatsSetting (TP-ASS)

When less than 2 of the 3 last games are won: turn off stats. When the last 3 games are won: turn on stats

  1. // ==UserScript==
  2. // @name TagPro AutoStatsSetting (TP-ASS)
  3. // @version 0.3
  4. // @description When less than 2 of the 3 last games are won: turn off stats. When the last 3 games are won: turn on stats
  5. // @author Ko
  6. // @supportURL https://www.reddit.com/message/compose/?to=Wilcooo
  7. // @website https://www.reddit.com/r/TagPro/comments/6wlsfa/userscript_tagpro_autostatsetting_tpass/
  8. // @include http://tagpro-*.koalabeast.com:*
  9. // @include http://tagpro-*.koalabeast.com/game
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_xmlhttpRequest
  13. // @connect koalabeast.com
  14. // @namespace https://greasyfork.org/users/152992
  15. // ==/UserScript==
  16.  
  17. console.log('START: ' + GM_info.script.name + ' (v' + GM_info.script.version + ' by ' + GM_info.script.author + ')');
  18.  
  19. ////////////////////////////////////////////////////////////////////////////////////////////
  20. // ### --- OPTIONS --- ### //
  21. //////////////////////////////////////////////////////////////////////////////////////// //
  22. // //
  23. // Please type your reserved name between the quotes. // //
  24. // DO NOT FORGET to change this when you change your reserved name in the future, // //
  25. // or it will be set back to what you typed here!! // //
  26. var reservedName = ""; // //
  27. // //
  28. // How many wins in a row should turn stats back on? (Max. 3 at the moment) // //
  29. var wins_in_a_row = 3; // //
  30. // //
  31. // What is the mininum acceptable amount of games to win out of 3? // //
  32. // If you win less than that, stats are turned off. // //
  33. var minimum_wins = 2; // //
  34. // //
  35. // Note: With the default options, winning 2 of the last 3 games keeps the stat // //
  36. // setting unchanged // //
  37. // //
  38. // Start a new session after how many minutes of not playing? // //
  39. var reset_time = 30; // //
  40. // //
  41. // Do you want to be alerted when the stat setting is changed? true/false // //
  42. var show_alert = true; // //
  43. // Currently not working, and I've no idea why. Help appreciated :) // //
  44. // //
  45. // Do you want to be alerted after every game, with the last three results? // //
  46. var show_results = true; // //
  47. // //
  48. // What color should the alerts have (You'll get those in your chat-box) // //
  49. // This tool may come in handy: https://www.w3schools.com/colors/colors_picker.asp // //
  50. var alert_color = "#ffccff"; // //
  51. // //
  52. // Do you want to see some debug-messages in the console // //
  53. // (don't worry if you've no idea what this is) // //
  54. var debug = false; // //
  55. // //
  56. //////////////////////////////////////////////////////////////////////////////////////// //
  57. // ### --- END OF OPTIONS --- ### //
  58. ////////////////////////////////////////////////////////////////////////////////////////////
  59.  
  60. /*
  61.  
  62. UPDATE LOG:
  63.  
  64. 0.2:
  65. Two new options; you can now specify at what number of wins stats should be turned on and off
  66.  
  67.  
  68. Idea for a future update (I don't plan on updating this soon, but if you want you can fork/pull request):
  69.  
  70. Whether or not Stats will be turned off/on won't be based on that simple rule.
  71. Instead the win% of the last session will be compared to your R300 win%.
  72. Only when you are positively influencing your R300 win%, stats will be turned on
  73. I plan on leaving the approach in this version as an option too.
  74.  
  75. */
  76.  
  77.  
  78. //////////////////////////////////////
  79. // SCROLL FURTHER AT YOUR OWN RISK! //
  80. //////////////////////////////////////
  81.  
  82. function chat_alert(message) { // This function shows info in your chat.
  83. tagpro.socket.emit("local:chat", { to:"all", from:"TP-ASS", message:message, c:alert_color});
  84. }
  85.  
  86. tagpro.ready(function () {
  87.  
  88. if (wins_in_a_row < minimum_wins) { // If this is true, stats will be turned on and off simultaneously in some cases
  89. chat_alert("You messed up the options, wins_in_a_row cannot be smaller than minimum_wins!! This script is terminating.");
  90. return;
  91. }
  92.  
  93. tagpro.socket.on('end',function(data) { // This script only runs at the end of a game.
  94.  
  95. if(debug)console.log('TP-ASS: Game has ended, starting the script');
  96.  
  97. if (tagpro.spectator) return; // Spectated game results should NOT affect this script!
  98.  
  99. var player = tagpro.players[tagpro.playerId]; // Your own ball's object
  100. var teamNames = [null,"red","blue"]; // Because tagpro.players[n].team returns a number, and I need to compare it to a string
  101. var team = teamNames [ player.team ] ; // convert number to corresponding teamName string
  102. var winner = data.winner; // Returns "red", "blue", or "tie" (even when custom names are set)
  103. var won = (team == winner); // Determine if this game is won or not
  104.  
  105. if(debug)console.log('TP-ASS: And the winner is: ',winner);
  106. if(debug)console.log('TP-ASS: You are part of that team: ',won);
  107.  
  108. var now = new Date().getTime(); // Get the current time
  109.  
  110. if ( GM_getValue("last_played") < now - reset_time*60000 || !GM_getValue("last_played") ) { // If you haven't played (using this script) during the last x minutes
  111.  
  112. GM_setValue("result1",false); // 2 games earlier is set to Lost
  113. GM_setValue("result2",true); // 1 game earlier is set to Win
  114. GM_setValue("result3",won); // Last games result is stored
  115.  
  116. // Note: The results of those 2 games are chosen this way because it will give the following effect.
  117. // - When loosing the first game of a new session: stats are immediately turned OFF
  118. // - You need to win the first 2 games for stats to be turned ON (if they weren't already)
  119.  
  120. if(debug)console.log('TP-ASS: A new session has started');
  121.  
  122. } else { // Else (if still in the same session)
  123. GM_setValue("result1",GM_getValue("result2")); // Move all results one place back in the 'past'
  124. GM_setValue("result2",GM_getValue("result3"));
  125. GM_setValue("result3",won); // Store the latest result (of the game that just ended)
  126. if(debug)console.log('TP-ASS: This result is stored');
  127. }
  128.  
  129. GM_setValue("last_played",now); // Update the last_played time
  130.  
  131.  
  132. function setStats(to) { // Defines how to turn stats on or off
  133. if ( tagpro.settings.stats == to ) return; // If the current Stat setting is different from what you want it to be:
  134.  
  135. if(debug)console.log('TP-ASS: Trying to change the STAT setting to '+to);
  136.  
  137. // This is where the magic starts!! (changing the Stat Setting)
  138. var settings = tagpro.settings.ui; // We copy your current settings, because we need a fully filled form to send to the server
  139. // What is still missing in tagpro.settings.ui is your displayName and reservedName
  140. settings.displayedName = player.name; // We simply get your display name from your ball
  141.  
  142. // Getting the reserved name isn't that easy:
  143. if(player.auth) settings.reservedName = player.name; // if you have a green checkmark, the script immediately knows your reserved name
  144.  
  145. else if (reservedName !== "") settings.reservedName = reservedName; // if you're playing unauthenticated, the name from the options (top of this script) will be used instead
  146.  
  147. else { // if you haven't set that as well, you will be prompted at the end of the game. (only the first time)
  148. storedName = GM_getValue("storedName");
  149. if (typeof storedName != 'undefined') settings.reservedName = storedName; // If you are prompted before, use that value.
  150. else { // If not, prompt and store the input.
  151. input = "";
  152. while (input === "") input = prompt('What is your reserved name? Be sure to get it correct! The TP-ASS script needs this to work.\n\nIf you change your reserved name in the future, don\'t forget to update it in the TP-ASS script too or it will be set back to the name you type here!!');
  153. // In case you immediately press 'enter' at the end of the game (f.e. to chat 'gg'), the prompt will just show again.
  154.  
  155. settings.reservedName = input;
  156. GM_setValue("storedName",input);
  157. }
  158. }
  159.  
  160. if(debug)console.log('TP-ASS: Using this reserved name: '+settings.reservedName);
  161.  
  162. settings.stats = to; // The only thing we change in the settings is the stats, to whatever this functions argument is
  163.  
  164. if(debug)console.log('TP-ASS: Got the required settings, sending a POST request now...');
  165.  
  166. // Now that we have all settings, we can 'post' it to the server. (This is what happens when you click 'save settings' on the TagPro profile page)
  167. GM_xmlhttpRequest ( {
  168. method: "POST",
  169. url: 'http://'+document.location.hostname+'/profile/update', // Settings should be send to the same server, so you'll be logged in for sure.
  170. data: jQuery.param(settings), // jQuery.param makes it a string: "stats=false&reservedName=Ko&..."
  171. headers: {"Content-Type": "application/x-www-form-urlencoded"}, // No idea why this is needed, but it doesn't work without it.
  172. onload: function(r){
  173. // 'r' is the response that we get back from the TP server, lets do some error handling with it:
  174. if(r.response.error) chat_alert('Your Stat setting could not be saved due to the following error:\n'+e.error); // Alert it when something is wrong
  175. if(r.response.success && show_alert) {
  176. var textify = {true: "on", false: "off" }; // Translation from Boolean to the words on/off
  177. chat_alert("Stats have been turned " + textify[to] + " for next game!"); // Alert when succeeded!
  178. } if(debug)console.log('TP-ASS: Response of the POST request: ', r);
  179. }});
  180.  
  181. // We did the magic!!
  182.  
  183. }
  184.  
  185. var number_of_wins = GM_getValue("result1") + GM_getValue("result2") + GM_getValue("result3"); // Count the number of wins of those last 3 results
  186. if (number_of_wins < minimum_wins) setStats(false); // Set stats off when <2 wins
  187.  
  188. var wins_streak = ( GM_getValue("result1") * GM_getValue("result2") + GM_getValue("result2") ) * GM_getValue("result3") + GM_getValue("result3"); // Do some fancy math to get your current win streak
  189. if (wins_streak >= wins_in_a_row) setStats(true); // Stats on when enough wins
  190.  
  191. // Note: if 2 of the last 3 games are won: the stat setting isn't changed. (With the default options)
  192.  
  193. if (show_results) {
  194. var iconify = {true: "☀ ", false: "· " }; // Translation from Boolean to these icons
  195. var results = iconify[GM_getValue("result1")] + iconify[GM_getValue("result2")] + iconify[GM_getValue("result3")];
  196. chat_alert("Results from the last 3 games: " + results);
  197. // Alerts you when the stat setting was updated.
  198. }
  199.  
  200. if(debug)console.log('TP-ASS: Done! And hopefully succeeded xD');
  201.  
  202. });
  203. });