Autodarts - Rematch-Button for local matches

Rematch-Button on mainpage with the last 2-6 players

  1. // ==UserScript==
  2. // @name Autodarts - Rematch-Button for local matches
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.71
  5. // @description Rematch-Button on mainpage with the last 2-6 players
  6. // @author benebelter
  7. // @match https://play.autodarts.io/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=autodarts.io
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
  10. // @license MIT
  11. // @grant GM.getValue
  12. // @grant GM.setValue
  13. // @grant GM_xmlhttpRequest
  14. // ==/UserScript==
  15.  
  16.  
  17. (function() {
  18.  
  19. 'use strict';
  20. var player1 = '';
  21. var player2 = '';
  22. var player3 = '';
  23. var player4 = '';
  24. var player5 = '';
  25. var player6 = '';
  26. var player1Id = '';
  27. var player2Id = '';
  28. var player3Id = '';
  29. var player4Id = '';
  30. var player5Id = '';
  31. var player6Id = '';
  32. var rematch = '';
  33. var player_clicked = 0;
  34. var gamemode = '';
  35. var startx01 = '';
  36. var variante = '';
  37. var firstto = '';
  38. var tosets = '';
  39. var gameconditions = '';
  40. var account;
  41. const players = [];
  42. var statedata = 0;
  43. var playerstart = 0;
  44. var data = '';
  45. const avcpuPPR = [0, 20, 30, 40, 50, 60 ,70 , 80 ,90 ,100, 110 ,120];
  46.  
  47. function startgame(){
  48. $.ajax({
  49. url: 'https://api.autodarts.io/gs/v0/lobbies/'+location.pathname.split("/")[2]+'/start',
  50. data: data,
  51. type: 'POST',
  52. async: false,
  53. xhrFields: {
  54. withCredentials: true
  55. },
  56. success: function(start){
  57. (async () => {
  58. await GM.deleteValue("player1");
  59. await GM.deleteValue("player2");
  60. await GM.deleteValue("player3");
  61. await GM.deleteValue("player4");
  62. await GM.deleteValue("player5");
  63. await GM.deleteValue("player6");
  64.  
  65. for (let index = 0; index < start['players'].length; ++index) {
  66. if(start['players'][index]['name'] != ''){
  67.  
  68. await GM.setValue("player"+(index+1), start['players'][index]['name'].toUpperCase() );
  69. await GM.setValue("player"+(index+1)+"Id", start['players'][index]['userId'] );
  70. }
  71. }
  72. player_clicked = 0;
  73. })();
  74. }
  75. })
  76. }
  77.  
  78.  
  79. async function removeplayer(playerID) { //remove default player from lobby
  80. fetch('https://api.autodarts.io/gs/v0/lobbies/'+location.pathname.split("/")[2]+'/players/by-index/'+playerID, {
  81. method: 'DELETE',
  82. credentials: 'include'
  83. })
  84. }
  85.  
  86. async function addplayer(playername, boardID, avcpuPPR, userId) {
  87. if(typeof playername !== "undefined" ) {
  88.  
  89. if(playername.substring(0,9) === 'BOT LEVEL') { // bot-level
  90. var level = playername.substring(10);
  91. data = '{"name":"'+playername+'","cpuPPR":'+avcpuPPR[level]+'}'; }
  92. else if (userId != null ) { // account-player
  93. data ='{"name":"'+playername+'","boardId":"'+boardID+'" , "userId":"'+userId+'"}';
  94. }
  95. else { // local player
  96. data ='{"name":"'+playername+'","boardId":"'+boardID+'"}';
  97. }
  98.  
  99.  
  100. $.ajax({
  101. url: 'https://api.autodarts.io/gs/v0/lobbies/'+location.pathname.split("/")[2]+'/players',
  102. data: data,
  103. type: 'POST',
  104. contentType: "text/xml",
  105. dataType: "text",
  106. async: false,
  107. xhrFields: {
  108. withCredentials: true
  109. },
  110. success: function(data) {},
  111. error: function(e){}
  112. });
  113. }
  114. }
  115.  
  116.  
  117.  
  118.  
  119. // Load last players
  120. (async () => {
  121. player1 = await GM.getValue('player1');
  122. player2 = await GM.getValue('player2');
  123. player3 = await GM.getValue('player3');
  124. player4 = await GM.getValue('player4');
  125. player5 = await GM.getValue('player5');
  126. player6 = await GM.getValue('player6');
  127.  
  128. player1Id = await GM.getValue('player1Id');
  129. player2Id = await GM.getValue('player2Id');
  130. player3Id = await GM.getValue('player3Id');
  131. player4Id = await GM.getValue('player4Id');
  132. player5Id = await GM.getValue('player5Id');
  133. player6Id = await GM.getValue('player6Id');
  134. gamemode = await GM.getValue('gamemode');
  135. startx01 = await GM.getValue('startx01');
  136. variante = await GM.getValue('variante');
  137. firstto = await GM.getValue('firstto');
  138. tosets = await GM.getValue('tosets');
  139.  
  140.  
  141. setInterval(function() {
  142. // Lobby autostart
  143. if( ($('button:contains("Open Lobby")').length != 0 || $('button:contains("Lobby öffnen")').length != 0) && playerstart > 0) {
  144. $('button:contains("Off")').eq(0).click();
  145. $('button:contains("Private")').click();
  146. $('button:contains("Privat")').click();
  147.  
  148. setTimeout(() => { $('button:contains("Open Lobby")').click(); $('button:contains("Lobby öffnen")').click(); }, 2000)
  149.  
  150. }
  151.  
  152.  
  153. // add players and start game
  154.  
  155. if( ($('button:contains("Start game")').length != 0 || $('button:contains("Spiel starten")').length != 0 )
  156. && player_clicked == 0 && playerstart > 0) {
  157. const boardID = $('.css-1h3944a option:selected').val();
  158.  
  159. setTimeout(function(){
  160.  
  161. $('button:contains("Join")').addClass("account css-1e89954");
  162. $('.account').css('color', 'yellow') ;
  163. $('.account').text(account);
  164.  
  165. }, 200);
  166.  
  167. const delay = 800;
  168. setTimeout(function(){ startgame(); }, (delay*8));
  169. // Player 1 starts
  170. if(playerstart == 1){
  171. setTimeout(function(){removeplayer(0);}, (delay*1));
  172. setTimeout(function(){addplayer(player1, boardID,avcpuPPR, player1Id); }, (delay*2));
  173. setTimeout(function(){addplayer(player2, boardID,avcpuPPR, player2Id); }, (delay*3));
  174. setTimeout(function(){addplayer(player3, boardID,avcpuPPR, player3Id); }, (delay*4));
  175. setTimeout(function(){addplayer(player4, boardID,avcpuPPR, player4Id); }, (delay*5));
  176. setTimeout(function(){addplayer(player5, boardID,avcpuPPR, player5Id); }, (delay*6));
  177. setTimeout(function(){addplayer(player6, boardID,avcpuPPR, player6Id); }, (delay*7));
  178. }
  179. if(playerstart == 2){ // Player 2 starts
  180. setTimeout(function(){removeplayer(0);}, (delay*1));
  181. setTimeout(function(){addplayer(player2, boardID,avcpuPPR, player2Id); }, (delay*2));
  182. setTimeout(function(){addplayer(player3, boardID,avcpuPPR, player3Id); }, (delay*3));
  183. setTimeout(function(){addplayer(player4, boardID,avcpuPPR, player4Id); }, (delay*4));
  184. setTimeout(function(){addplayer(player5, boardID,avcpuPPR, player5Id); }, (delay*5));
  185. setTimeout(function(){addplayer(player6, boardID,avcpuPPR, player6Id); }, (delay*6));
  186. setTimeout(function(){addplayer(player1, boardID,avcpuPPR, player1Id); }, (delay*7));
  187. }
  188. if(playerstart == 3){ // Player 3 starts
  189.  
  190. setTimeout(function(){removeplayer(0);}, (delay*1));
  191. setTimeout(function(){addplayer(player3, boardID,avcpuPPR, player3Id); }, (delay*2));
  192. setTimeout(function(){addplayer(player4, boardID,avcpuPPR, player4Id); }, (delay*3));
  193. setTimeout(function(){addplayer(player5, boardID,avcpuPPR, player5Id); }, (delay*4));
  194. setTimeout(function(){addplayer(player6, boardID,avcpuPPR, player6Id); }, (delay*5));
  195. setTimeout(function(){addplayer(player1, boardID,avcpuPPR, player1Id); }, (delay*6));
  196. setTimeout(function(){addplayer(player2, boardID,avcpuPPR, player2Id); }, (delay*7));
  197. }
  198. if(playerstart == 4){ // Player 4 starts
  199. setTimeout(function(){removeplayer(0);}, (delay*1));
  200. setTimeout(function(){addplayer(player4, boardID,avcpuPPR, player4Id); }, (delay*2));
  201. setTimeout(function(){addplayer(player5, boardID,avcpuPPR, player5Id); }, (delay*3));
  202. setTimeout(function(){addplayer(player6, boardID,avcpuPPR, player6Id); }, (delay*4));
  203. setTimeout(function(){addplayer(player1, boardID,avcpuPPR, player1Id); }, (delay*5));
  204. setTimeout(function(){addplayer(player2, boardID,avcpuPPR, player2Id); }, (delay*6));
  205. setTimeout(function(){addplayer(player3, boardID,avcpuPPR, player3Id); }, (delay*7));
  206. }
  207. if(playerstart == 5){ // Player 5 starts
  208. setTimeout(function(){removeplayer(0);}, (delay*1));
  209. setTimeout(function(){addplayer(player5, boardID,avcpuPPR, player5Id); }, (delay*2));
  210. setTimeout(function(){addplayer(player6, boardID,avcpuPPR, player6Id); }, (delay*3));
  211. setTimeout(function(){addplayer(player1, boardID,avcpuPPR, player1Id); }, (delay*4));
  212. setTimeout(function(){addplayer(player2, boardID,avcpuPPR, player2Id); }, (delay*5));
  213. setTimeout(function(){addplayer(player3, boardID,avcpuPPR, player3Id); }, (delay*6));
  214. setTimeout(function(){addplayer(player4, boardID,avcpuPPR, player4Id); }, (delay*7));
  215. }
  216. if(playerstart == 6){ // Player 6 starts
  217. setTimeout(function(){removeplayer(0);}, (delay*1));
  218. setTimeout(function(){addplayer(player6, boardID,avcpuPPR, player6Id); }, (delay*2));
  219. setTimeout(function(){addplayer(player1, boardID,avcpuPPR, player1Id); }, (delay*3));
  220. setTimeout(function(){addplayer(player2, boardID,avcpuPPR, player2Id); }, (delay*4));
  221. setTimeout(function(){addplayer(player3, boardID,avcpuPPR, player3Id); }, (delay*5));
  222. setTimeout(function(){addplayer(player4, boardID,avcpuPPR, player4Id); }, (delay*6));
  223. setTimeout(function(){addplayer(player5, boardID,avcpuPPR, player5Id); }, (delay*7));
  224. }
  225.  
  226. player_clicked = 1;
  227. }
  228.  
  229. // add Rematch-button to mainpage
  230. if( player1 !=''
  231. && player2 !=''
  232. && $('.rematch_button').length == 0
  233. && typeof(player1) !== "undefined"
  234. && player1 !== null
  235. && typeof(player2) !== "undefined"
  236. && player2 !== null
  237. && gamemode !== undefined)
  238. {
  239. if(gamemode.toUpperCase() == 'X01') {
  240. var gameconditions = startx01 + ' | ' +variante ;
  241. if (typeof firstto !== "undefined" ) { gameconditions = gameconditions + ' | ' + 'First to '+firstto+ ' Legs'}
  242. if (typeof tosets !== "undefined" ) { gameconditions = gameconditions + ' / '+tosets+ ' Sets' ; }
  243. }
  244. else if(gamemode == 'Count Up') {
  245. gameconditions = 'Count-Up'; gamemode='count-up';
  246. }
  247. else {
  248.  
  249. if( variante == undefined ) { var variantetext = '';}
  250. else { var variantetext = ' | '+ variante;}
  251.  
  252. var gameconditions = gamemode + variantetext ;
  253. }
  254.  
  255. // Load last players
  256. (async () => {
  257. player1 = await GM.getValue('player1');
  258. player2 = await GM.getValue('player2');
  259. player3 = await GM.getValue('player3');
  260. player4 = await GM.getValue('player4');
  261. player5 = await GM.getValue('player5');
  262. player6 = await GM.getValue('player6');
  263.  
  264. var mainmenu = '<div><div class="chakra-stack css-1aucrbz"><span style="font-size: 2.5em;">REMATCH</span><div style="font-size: 1em; min-height: 30px; " >'+gameconditions+'</div><a class="css-z1xk1r rematch_button" href="#" style="font-size: 1em; min-height: 50px; width: 500px;" data-id="1"><span style="font-size: 1.5em;" >'+player1+'</span>&nbsp;&nbsp;to throw first</a><a class="css-z1xk1r rematch_button" href="#" style="font-size: 1em; min-height: 50px; width: 500px; " data-id="2"><span style="font-size: 1.5em;">'+player2+'</span>&nbsp;&nbsp;to throw first</a>';
  265.  
  266. // 3. Spieler?
  267. if( typeof(player3) !== "undefined" && player3 !== ''){
  268. mainmenu += '<a class="chakra-button css-z1xk1r rematch_button" href="#" style="font-size: 1em; min-height: 50px; width: 500px;" data-id="3"> <span style="font-size: 1.5em;" data-id="3">'+player3+'</span>&nbsp;&nbsp;to throw first</a>';
  269. }
  270. // 4. Spieler?
  271. if( typeof(player4) !== "undefined" && player4 !== null){
  272. mainmenu += '<a class="chakra-button css-z1xk1r rematch_button" href="#" style="font-size: 1em; min-height: 50px; width: 500px;" data-id="4"> <span style="font-size: 1.5em;" >'+player4+'</span>&nbsp;&nbsp;to throw first</a>';
  273. }
  274. // 5. Spieler?
  275. if( typeof(player5) !== "undefined" && player5 !== null){
  276. mainmenu += '<a class="chakra-button css-z1xk1r rematch_button" href="#" style="font-size: 1em; min-height: 50px; width: 500px;" data-id="5"> <span style="font-size: 1.5em;" >'+player5+'</span>&nbsp;&nbsp;to throw first</a>';
  277. }
  278. // 6. Spieler?
  279. if( typeof(player6) !== "undefined" && player6 !== null){
  280. mainmenu += '<a class="chakra-button css-z1xk1r rematch_button" href="#" style="font-size: 1em; min-height: 50px; width: 500px;" data-id="6"> <span style="font-size: 1.5em;" >'+player6+'</span>&nbsp;&nbsp;to throw first</a>';
  281. }
  282.  
  283. $('.css-1aucrbz').first().prepend(mainmenu+'</div>');
  284.  
  285. })();
  286.  
  287. }
  288.  
  289.  
  290.  
  291. // only on own match-page::
  292. if( ($('button:contains("Abort")').length != 0 || $('button:contains("Abbrechen")').length != 0 )
  293. && statedata != 1
  294. && location.pathname.split("/")[1] === 'matches'
  295. && $('#ad-ext-game-variant').text() != "Bull-off") {
  296. fetch('https://api.autodarts.io/gs/v0/matches/'+location.pathname.split("/")[2]+'/state', {credentials: 'include'})
  297. .then(function(response) { return response.json(); })
  298. .then(function(json) {
  299. if( typeof json['error'] !== "undefined" ){
  300. console.log('ERROR');
  301. return; //stop the execution of function
  302. }
  303. (async() => {
  304. let storedValues = await GM.listValues();
  305. for (let key of storedValues) {
  306. await GM.deleteValue(key);
  307. }
  308.  
  309. statedata = 1;
  310. player_clicked = 0;
  311. playerstart = 0;
  312. for (let i = 0; i < json.players.length; i++) {
  313. if(i == 0){ player1 = json['players'][0]['name'];
  314. player1Id = json['players'][0]['userId'];}
  315. if(i == 1){ player2 = json['players'][1]['name'];}
  316. if(i == 2){ player3 = json['players'][2]['name'];}
  317. if(i == 3){ player4 = json['players'][3]['name'];}
  318. if(i == 4){ player5 = json['players'][4]['name'];}
  319. if(i == 5){ player6 = json['players'][5]['name'];}
  320. }
  321.  
  322. gamemode = json['variant'];
  323. startx01 = json['settings']['baseScore'];
  324. if( json['settings']['inMode'] == undefined) {
  325. variante = json['settings']['Mode'];
  326. }
  327. else { variante = json['settings']['inMode'][0] +'I/' + json['settings']['outMode'][0]+ 'O';
  328. }
  329. firstto = json['legs'];
  330. tosets = json['sets'];
  331. (async () => {
  332.  
  333. await GM.setValue("gamemode" , gamemode ); // x01
  334. await GM.setValue("startx01" , startx01 ); // 501
  335. await GM.setValue("variante" , variante ); // SI/DO
  336. await GM.setValue("firstto" , firstto ); // int
  337. await GM.setValue("tosets" , tosets ); // int
  338. })();
  339.  
  340. for (let index = 0; index < json['players'].length; ++index) {
  341. if(json['players'][index]['name'] != ''){
  342. (async () => {
  343. await GM.setValue("player"+(index+1), json['players'][index]['name'] ); // .toUpperCase()?
  344. await GM.setValue("player"+(index+1)+"Id", json['players'][index]['userId']);
  345.  
  346.  
  347. })();
  348. }
  349. }
  350. })(); // end async
  351. });
  352. }
  353.  
  354. },100); // end-interval
  355. })();
  356.  
  357. $(document).on('click', '.css-u7txwc', function(){ // $('button:contains("Start game"), $('button:contains("Spiel starten")
  358. statedata = 0;
  359. })
  360.  
  361. $(document).on ('click', '.rematch_button',function() {
  362. playerstart = $(this).data("id");
  363. setTimeout(function(){
  364. if(gamemode == "ATC"){ gamemode = 'Around the Clock';}
  365. if(gamemode == "RTW"){ gamemode = 'Round the World';}
  366. if(gamemode == "CountUp"){ gamemode = 'Count Up';}
  367. $('.chakra-button:contains("'+gamemode+'")')[0].click();
  368.  
  369. }, 100);
  370. });
  371.  
  372. })();