// ==UserScript==
// @name Autodart - Edit score Numpad
// @namespace http://tampermonkey.net/
// @version 1.1
// @description
// @match https://play.autodarts.io/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=autodarts.io
// @require https://code.jquery.com/jquery-3.7.1.min.js
// @require https://code.jquery.com/ui/1.14.0/jquery-ui.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js
// @grant GM_addStyle
// @license MIT
// @description test
// ==/UserScript==
(function() {
'use strict';
let buttons = '';
let newnumber = 0;
let zahl = 0;
let i= 0;
let dartnr ='';
const corrections = {
"S1": {"x": 0.23864869472714267, "y": 0.7450953567163014},
"S2": {"x": 0.45156927001320907, "y": -0.5932624277493371},
"S3": {"x": 0.006614487481989349, "y": -0.8056185967301086},
"S4": {"x": 0.6363481572289519, "y": 0.4769283984847778},
"S5": {"x": -0.24335398593069038, "y": 0.71623182889537861},
"S6": {"x": 0.7887362326309516, "y": -0.004146439478043747},
"S7": {"x": -0.4574008834816887 , "y":-0.6323923445494517 },
"S8": {"x": -0.7412494631215135, "y": -0.23441905139777536},
"S9": {"x": -0.6534612426143511, "y": 0.48251813111811254},
"S10": {"x": 0.7482240114833393, "y": -0.24319787404082716},
"S11": {"x": -0.7997749434596216 , "y": 0.002609159964620326},
"S12": {"x": -0.44862206143097255, "y": 0.6376106644786924},
"S13": {"x": 0.7657816555847717, "y": 0.2630475650418204},
"S14": {"x": -0.7588071072229459, "y": 0.24548991975571716},
"S15": {"x": 0.6340993248240286, "y": -0.4626684401171191},
"S16": {"x": -0.6446824205636349, "y": -0.47144726276017057 },
"S17": {"x": 0.23612605852489288, "y": -0.7640746841952271},
"S18": {"x": 0.4555966097927985, "y": 0.6463894871217442},
"S19": {"x": -0.22915151016306695, "y": -0.7465170389091238},
"S20": {"x": -0.009680958895161329, "y": 0.7839243751962206},
"D1": {"x": 0.29821686695850486 , "y": 0.921979532538415 },
"D2": {"x": 0.5877363642403922 , "y": -0.7781926904772413 },
"D3": {"x": 0.006683720409154148 , "y": -0.9669106431745664 },
"D4": {"x": 0.7814205788508053 , "y": 0.5626980260563854 },
"D5": {"x": -0.29874138724572774 , "y": 0.9153026218855984 },
"D6": {"x": 0.9602060077219553 , "y": -0.005938962992133956 },
"D7": {"x": -0.5743689234220842 , "y": -0.793091476216504 },
"D8": {"x": -0.9195241263816659, "y": -0.30888093969047203 },
"D9": {"x": -0.7804687928152158, "y": 0.567664287969473 },
"D10": {"x": 0.9254421743303428 , "y": -0.28901589203812184 },
"D11": {"x": -0.9617373526429098, "y": -0.010905224905221356 },
"D12": {"x": -0.5669195305524531, "y": 0.7836966811887797 },
"D13": {"x": 0.9179927814607116, "y": 0.2845873589234851},
"D14": {"x": -0.9269735192512972, "y": 0.2994861446627476 },
"D15": {"x": 0.7814205788508053, "y": -0.5646434282144784 },
"D16": {"x": -0.7879181856848471 , "y": -0.5646434282144784 },
"D17": {"x": 0.2972100423247734, "y": -0.9172480240436915 },
"D18": {"x": 0.5529725308487797, "y": 0.7961123359714984 },
"D19": {"x": -0.31115704202844646, "y": -0.90979863117406 },
"D20": {"x": 0.006683720409154148, "y": 0.9773808957991924 },
"T1": {"x": 0.1854691492803045, "y": 0.5626980260563854 },
"T2": {"x": 0.36673770910799847, "y": -0.4752507137789034 },
"T3": {"x": 0.012218921762980582, "y": -0.590141739888456 },
"T4": {"x": 0.4780778274565276, "y": 0.3544569629003027 },
"T5": {"x": -0.1917285254300467, "y": 0.5691384862613842 },
"T6": {"x": 0.5918590348379009, "y": 0.002379264588128967 },
"T7": {"x": -0.3527396679508579, "y": -0.482800978207915 },
"T8": {"x": -0.5803020827136042, "y": -0.195127736904066 },
"T9": {"x": -0.49013584290195006, "y": 0.3544569629003027 },
"T10": {"x": 0.5725376977354034, "y": -0.20156818260489834},
"T11": {"x": -0.5974766045824907, "y": 0.002379264588128967 },
"T12": {"x": -0.34629922225002546 , "y": 0.48541269215056226 },
"T13": {"x": 0.5618036215673494, "y": 0.18700537467865902 },
"T14": {"x": -0.5803020827136042, "y": 0.18700537467865902 },
"T15": {"x": 0.4952523493254142, "y": -0.3389643575559905 },
"T16": {"x": -0.479401766733896, "y": -0.35184524895765573 },
"T17": {"x": 0.17323006428379178, "y": -0.5665267723187372 },
"T18": {"x": 0.33424120680460284, "y": 0.48541269215056226 },
"T19": {"x": -0.18528807972921424 , "y": -0.5600863266179045 },
"T20": {"x": -0.007102415339516647, "y": 0.605634345232768 },
"25": {"x": -0.0006619696386841871, "y": 0.060343275895620954 },
"BULL": {"x": 0.005778476062148273 , "y": -0.004061181112703493 }
};
let fields = [
[ ],
["S20", "S1", "S18", "D20", "D1", "D18", "T20", "T1", "T18"], // 1
["S17", "S2", "S15", "D17", "D2", "D15", "T17", "T2", "T15"], // 2
["S19", "S3", "S17", "D19", "D3", "D17", "T19", "T3", "T17"], // 3
["S18", "S4", "S13", "D18", "D4", "D13", "T18", "T4", "T13"], // 4
["S12", "S5", "S20", "D12", "D5", "D20", "T12", "T5", "T20"], // 5
["S13", "S6", "S10", "D13", "D6", "D10", "T13", "T6", "T10"], // 6
["S16", "S7", "S19", "D16", "D7", "D19", "T16", "T7", "T19"], // 7
["S11", "S8", "S16", "D11", "D8", "D16", "T11", "T8", "T16"], // 8
["S14", "S9", "S12", "D14", "D9", "D12", "T14", "T9", "T12"], // 9
["S15", "S10", "S6", "D15", "D10", "D6", "T15", "T10", "T6"], // 10
["S8", "S11", "S14", "D8", "D11", "D14", "T8", "T11", "T14"], // 11
["S9", "S12", "S5", "D9", "D12", "D5", "T9", "T12", "T5"], // 12
["S4", "S13", "S6", "D4", "D13", "D6", "T4", "T13", "T6"], // 13
["S11", "S14", "S9", "D11", "D14", "D9", "T11", "T14", "T9"], // 14
["S2", "S15", "S10", "D2", "D15", "D10", "T2", "T15", "T10"], // 15
["S8", "S16", "S7", "D8", "D16", "D7", "T8", "T16", "T7"], // 16
["S3", "S17", "S2", "D3", "D17", "D3", "T3", "T17", "T2"], // 17
["S1", "S18", "S4", "D1", "D18", "D4", "T1", "T18", "T4"], // 18
["S7", "S19", "S3", "D7", "D19", "D3", "T7", "T19", "T3"], // 19
["S5", "S20", "S1", "D5", "D20", "D1", "T5", "T20", "T1"], // 20
];
function generate_buttons(fields, zahl, dartnr ) {
console.log('numpad zahl: '+fields[zahl][2]);
buttons = '';
for (let i = 0; i < 9; i++) {
if ( i<=2) { // color of button
var btn_class = 'single';}
else if ( i <= 5) {
var btn_class = 'double';}
else {
var btn_class = 'triple';}
buttons += '<button class="numpadbtn '+btn_class+'" value="'+fields[zahl][i]+'" >'+fields[zahl][i]+'</button>';
}
return buttons;
}
GM_addStyle("#container { position: relative; } ");
GM_addStyle(".overlay { border-radius: 10px; position: absolute; bottom: 20px; left: 100px; font-size: 18px; padding: .2em .2em; background-color: #525e9c; z-index: 98; width: 290px; height: 260px; border: 1px solid #a9b1cb;}")
GM_addStyle(".numpadbtn{ color: black; z-index: 99; width: 80px; height: 50px; background-color: blue; color: white; padding: 10px 5px 10px 5px; margin-left: 10px; margin-top: 10px; vertical-align: middle; border-radius: 15px; border: 1px solid #a9b1cb;}")
GM_addStyle(".single { background-color: #7aa2ff;} .double { background-color: #3873fc;} .triple{ background-color: #004dff;} .miss{ background-color: grey;} .halfbull{ background-color: green;} .bull { background-color: red;} ");
$(document).ready( function() {
//Append
$('body').append('<div id="container"><div class="overlay"></div></div>');
$('#container').hide();
$(document).on("click",".numpadbtn",function() {
console.log('Numpad clicked:' + $(this).val() );
newnumber = $(this).val();
if (newnumber === 'BOUNCER') {
var body = '{"changes":{"'+dartnr+'":{"type":"bouncer"}}}'; }
else {
var x = corrections[newnumber]['x'];
var y = corrections[newnumber]['y'];
var body = '{"changes":{"'+dartnr+'":{"point":{"x":'+x+',"y":'+y+'},"type":"normal"}}}';
}
fetch('https://api.autodarts.io/gs/v0/matches/'+location.pathname.split("/")[2]+'/throws?1', {
credentials: 'include',
method: 'PATCH',
body: body
}).then((response) => {
console.log('status code', response.status)
if(response.status != 200) { // if Authorization-token has expired
// fetch again
// get new Authorization-token (click a miss)
$("button:contains('Bouncer')").trigger("click", function() { }); // Bouncer-Button click
setTimeout(function(){
$("button:contains('Ok')").trigger("click", function() { }); // OK-Button click
}, 1000);
setTimeout(function(){
// correct darts again with old values and new Authorization-token
fetch('https://api.autodarts.io/gs/v0/matches/'+location.pathname.split("/")[2]+'/throws?2', {
credentials: 'include',
method: 'PATCH',
body: body
})
}, 1500);
// end fetch again
}
})
$('#container').hide('slow');
});
$(document).on("click",".css-1x1xjw8",function() {
$('#container').fadeOut(500);
})
$(document).on("click",".ad-ext-turn-throw",function(e) {
$("#overlay").draggable("destroy");
var offset = $(this).offset(); // throw-div
console.log(offset);
var posY = offset.top +360 ;
var posX = offset.left - 100;
$("#container").css({top: posY, left: posX, position:'absolute'});
$( "#container" ).draggable();
$(".overlay" ).html(); // reset old overlay
dartnr = $(this).index()-1;
zahl = parseInt( $(this).text().replace(/\D/g,'') );
var extra_buttons = '<button class="numpadbtn miss" value="BOUNCER" >Miss</button><button class="numpadbtn halfbull" value="25" >25</button><button class="numpadbtn bull" value="BULL" >Bull</button>';
if(zahl != 25 && Number.isInteger(zahl) && $(this).hasClass('css-1tv7rud') == true ) {
$(".overlay" ).html( generate_buttons(fields, zahl, dartnr ) + extra_buttons );
$('#container').fadeIn(500);
}
else {
$('#container').fadeOut(500);
}
});
});
})();