Autodarts Caller

Another Autodarts-Caller

// ==UserScript==
// @name         Autodarts Caller
// @namespace    http://tampermonkey.net/
// @version      1.5.1
// @description  Another Autodarts-Caller
// @author       benebelter
// @match        https://play.autodarts.io/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=autodarts.io
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @license      MIT
// @grant        GM.getValue
// @grant        GM.setValue
// @grant        GM_xmlhttpRequest
// @require https://update.greasyfork.org/scripts/445697/1244619/Greasy%20Fork%20API.js
// ==/UserScript==

(function() {
    'use strict';
//////////////// CONFIG ////////////////////

    var triplesound = 0; // 0=off / 1=simple beep on / 2=Löwen-sound (Soft-tip) on => triple_sound if hitting a triple
    var boosound = 0; //  0=off / 1=on => crowd-boo if hitting a miss
    var server = 'http://autodarts.x10.mx/'; //  Server
    var caller = ''; //  caller: "russ_bray", "google" (select language in next line)
    var language = 'en';
    var update = '';

//////////////// CONFIG ////////////////////
    function soundGo(file, audio){
        if (audio === 1){
            console.log('play sound-file : '+ file);
            const audio = new Audio(file);
            audio.play();}
    }

    function getDarts(){
        var runde = $('.css-1tw9fat').text().substring(1).split("/");
        var numItems = $('.ad-ext-turn-throw').length ;
        var dartsgesamt = (runde[0]*3-3)+numItems;
        return  "<span style='font-size:0.5em;'>"+dartsgesamt+" Darts</span>" ;
        console.log('Ausgabe Anzahl Darts: '+dartsgesamt);
    }

    function random_boo() {
        var boos = ["test","values","go","here"],
            boosToUse = boos[Math.floor(Math.random() * boos.length)];
        // do something with the selected value
        alert(boosToUse);
    }


    $(document).ready(function(){

        // Check script for update
        (async () => {
            update = await GM.getValue('update');
            if( (Date.now()  - 86400000) > update ) {
                var myVersion = GM_info.script.version;
                GreasyFork.getScriptHistory('487034').then(data => {
                    var lastVersion = JSON.stringify( data[0]['version']).replace(/^["'](.+(?=["']$))["']$/, '$1');

                    console.log ('Current Version: ', myVersion, myVersion === lastVersion);
                    if(myVersion != lastVersion) {
                        var url = "https://update.greasyfork.org/scripts/487034/Autodarts_Caller.user.js";
                        var tk = "There is a newer version of the script Autodarts-Caller! Get the update from version " +myVersion+ " -> "+lastVersion;
                        if (confirm(tk)) {
                            window.location.replace("https://greasyfork.org/de/scripts/487034-autodarts-caller");
                        }
                        else {
                            // Speichern
                            (async () => {
                                await GM.setValue('update', Date.now() );
                            })();
                        }
                        return false;
                    }

                })
            }
            else{
                console.log('Update wird erneut in '+  (86400000 - (Date.now()-update))   + ' erinnert!');
            }
        })();// asyn-end
        // Update end//



        var t1 = 0; var t2 = 0; var t3 = 0;
        var m1 = 0; var m2 = 0; var m3 = 0;
        var dotmp3 = '.mp3';
        var called =  0 ;
        var gameshot_status = 0;
        var busted = 0;
        var audio = 1;
        var firstgameoncall = 0;
        var score_is_edited = 0;

        $(document).on('change', '#caller', function(){

            caller = $("#caller").find(":selected").val();

            console.log('Neuer Caller gewählt: '+caller);
            // Speichern
            (async () => {
                await GM.setValue('caller', caller);

            })();


            if(caller == 'russ_bray') {
                server = 'http://autodarts.x10.mx/';
                caller = 'russ_bray';
                dotmp3 = '.mp3';
                $("#caller option[value='russ_bray']").prop('selected', true);
                console.log('rb aktiv');
            }
            if(caller == 'georgeno') {
                server = 'http://autodarts.x10.mx/';
                caller = 'georgeno';
                dotmp3 = '.mp3';
                $("#caller option[value='georgeno']").prop('selected', true);
                console.log('georgeno aktiv');
            }
            else if(caller == 'shorty') {
                server = 'http://autodarts.x10.mx/';
                caller = 'shorty';
                dotmp3 = '.mp3';
                $("#caller option[value='shorty']").prop('selected', true);
                console.log('shorty aktiv');
            }

            else if(caller == 'haulpinks') {
                server = 'http://autodarts.x10.mx/';
                caller = 'haulpinks';
                dotmp3 = '.mp3';
                $("#caller option[value='haulpinks']").prop('selected', true);
                console.log('haulpinks aktiv');
            }


            else if(caller == 'bayrisch') {
                server = 'http://autodarts.x10.mx/';
                caller = 'bayrisch';
                dotmp3 = '.mp3';
                $("#caller option[value='bayrisch']").prop('selected', true);
                console.log('bayrisch aktiv');
            }

            else if(caller == '1_male_eng') {
                server = 'http://autodarts.x10.mx/';
                caller = '1_male_eng';
                dotmp3 = '.mp3';
                $("#caller option[value='1_male_eng']").prop('selected', true);
                console.log('1_male_eng aktiv');

            }

            else if(caller == 'google_eng') {
                dotmp3 = '';
                language = 'en';
                //  server = 'http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl='+language+'&q=';
                server = 'https://autodarts.x10.mx/mp3_helper.php?language='+language+'&text=';
                caller = '';
                $("#caller option[value='google_eng']").prop('selected', true);
                console.log('eng aktiv');

            }

            else if(caller == 'google_de') {
                dotmp3 = '';
                language = 'de';
                //server = 'https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl='+language+'&q=';
                server = 'https://autodarts.de.cool/mp3_helper.php?language='+language+'&text=';
                $("#caller option[value='google_de']").prop('selected', true);
                caller = '';
            }

            else  if(caller == 'google_fr') {
                dotmp3 = '';
                language = 'fr';
                server = 'https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl='+language+'&q=';
                server = 'https://autodarts.de.cool/mp3_helper.php?language='+language+'&text=';
                caller = '';
                $("#caller option[value='google_fr']").prop('selected', true);
            }

            // neuen Caller einmal sprechen lassen

            soundGo(server+caller+'/game on'+dotmp3, audio); // Wert in Audio-Funktion übergeben

        })


        setInterval(function() {


            //$('a[href*="/boards"]').attr("target","_blank");

            // If class exist (Header) -> init
            if($(".css-gmuwbf").length ) {

                // // // // // // // // // // read + set last settings // // // // // // // //
                (async () => {

                    triplesound = await GM.getValue('triplesound');
                    caller      = await GM.getValue('caller');
                    boosound    = await GM.getValue('boosound');

                    $("#triplesound option[value='"+triplesound+"']").prop('selected', true);
                    $("#boosound option[value='"+boosound+"']").prop('selected', true);
                    $("#caller option[value='"+caller+"']").prop('selected', true);
                })();

                if (typeof triplesound == 'undefined'){
                    triplesound = '0';
                }

                if (typeof caller == 'undefined'){
                    caller = 'x';
                }

                if (typeof boosound == 'undefined'){
                    boosound = '0';
                }


                // // // // // // // // // // // // // // // // // //

                var x = $(".css-1tq00ko").text();
                //var x0 = $(".css-1tq00ko:eq(0)").text(); // Scrore-Feld
                var x0 = $(".ad-ext-turn-points").text(); // Scrore-Feld neu
//$(".ad-ext-turn-points").css ('border', '1px solid red');
//$(".css-1fzu0fr:eq(2)").css ('border', '2px solid red');

                var x1 = $(".css-1fzu0fr:eq(0)").text(); // 1. Dart
                var x2 = $(".css-1fzu0fr:eq(1)").text(); // 2. Dart
                var x3 = $(".css-1fzu0fr:eq(2)").text(); // 3. Dart

                var gameshot1 = $(".ad-ext-player-score").eq(0).text(); // Spieler 1
                var gameshot2 = $(".ad-ext-player-score").eq(1).text(); // Spieler 2
                var gameshot3 = $(".ad-ext-player-score").eq(2).text(); // Spieler 3
                var gameshot4 = $(".ad-ext-player-score").eq(3).text(); // Spieler 4
                var gameshot5 = $(".ad-ext-player-score").eq(4).text(); // Spieler 5
                var gameshot6 = $(".ad-ext-player-score").eq(5).text(); // Spieler 6


                //// Bust-Ausgabe ////
                if (x0 == 'BUST' && busted == 0) {
                    soundGo(server+caller+"/0"+dotmp3, audio);
                    console.log(server+caller+"/0"+dotmp3);
                    busted = 1;
                }

                //// Gameshot-Ausgabe ////

                // Gameshot aktivieren
                $("button:contains('Undo'),button:contains('Next Leg'),button:contains('Finish'),button:contains('Start')").on( "click", function() {
                    gameshot_status = 0;

                })
                /// Gameshot ///
                if (   gameshot_status != 1
                    && caller != 'x'
                    && (gameshot1 == "0" || gameshot2 == "0" || gameshot3 == "0"|| gameshot4 == "0"|| gameshot5 == "0"|| gameshot6 == "0")   ){
                    gameshot_status = 1;

                    if( $("button:contains('Finish')").length ){
                        soundGo(server+caller+"/gameshot and the match"+dotmp3, audio);
                        soundGo(server+"/chase_the_sun/chase_the_sun.mp3", audio);

                    }
                    else {
                        soundGo(server+caller+"/gameshot"+dotmp3, audio);
                    }

                    if (gameshot1 == "0") {
                        $(".ad-ext-player-score").eq(0).html(getDarts()); }
                    if (gameshot2 == "0") {
                        $(".ad-ext-player-score").eq(1).html(getDarts()); }
                    if (gameshot3 == "0") {
                        $(".ad-ext-player-score").eq(2).html(getDarts()); }
                    if (gameshot4 == "0") {
                        $(".ad-ext-player-score").eq(3).html(getDarts()); }
                    if (gameshot5 == "0") {
                        $(".ad-ext-player-score").eq(4).html(getDarts()); }
                    if (gameshot6 == "0") {
                        $(".ad-ext-player-score").eq(5).html(getDarts()); }
                }
                //// END Gameshot ////




                ///////////////////////////////////////////////// TRIPLE Beep ///////////////////////////
                if(triplesound == 1){
                    // Triple 1st dart
                    if (x1.includes("T") && t1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t1 = 1;
                        soundGo( "https://autodarts.x10.mx/triple/triple_beep.mp3", audio);

                    }
                    // Triple 2st dart
                    else if(x2.includes("T") && t2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t2 = 1;
                        soundGo( "https://autodarts.x10.mx/triple/triple_beep.mp3", audio);
                    }
                    // Triple 3rd dart
                    else if(x3.includes("T") && t3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t3 = 1;
                        soundGo( "https://autodarts.x10.mx/triple/triple_beep.mp3", audio);
                    }
                }
                ///////////////////////////////////////////////// END TRIPLE CALLER ///////////////////////////

                ///////////////////////////////////////////////// TRIPLE CALLER E-Dart Löwen///////////////////////////
                if(triplesound == 2) {
                    // Triple 20 1st dart
                    if ( x1.includes("T20") && t1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t1 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple20_old.wav", audio);
                    }
                    // Triple 19 1st dart
                    else if ( x1.includes("T19") && t1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t1 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple19_old.wav", audio);
                    }
                    // Triple 18 1st dart
                    else if ( x1.includes("T18") && t1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t1 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple18_old.wav", audio);
                    }
                    // Triple 17 1st dart
                    if ( x1.includes("T17") && t1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t1 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple17_old.wav", audio);
                    }
                    // Bullseye 1st dart
                    if ( x1.includes("BULL") && t1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t1 = 1;
                        soundGo("https://autodarts.x10.mx/triple/bullseye.mp3", audio);
                    }

                    /// 2. Dart
                    // Triple 20 1st dart
                    if ( x2.includes("T20") && t2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t2 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple20_old.wav", audio);
                    }
                    // Triple 19 1st dart
                    else if ( x2.includes("T19") && t2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t2 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple19_old.wav", audio);
                    }
                    // Triple 18 1st dart
                    else if ( x2.includes("T18") && t2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t2 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple18_old.wav", audio);
                    }
                    // Triple 17 1st dart
                    if ( x2.includes("T17") && t2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t2 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple17_old.wav", audio);
                    }
                    // Bullseye 2nd dart
                    if ( x2.includes("BULL") && t2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t2 = 1;
                        soundGo("https://autodarts.x10.mx/triple/bullseye.mp3", audio);
                    }


                    /// 3. Dart
                    // Triple 20 1st dart
                    if ( x3.includes("T20") && t3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t3 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple20_old.wav", audio);
                    }
                    // Triple 19 1st dart
                    else if ( x3.includes("T19") && t3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t3 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple19_old.wav", audio);
                    }
                    // Triple 18 1st dart
                    else if ( x3.includes("T18") && t3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t3 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple18_old.wav", audio);
                    }
                    // Triple 17 1st dart
                    else if ( x3.includes("T17") && t3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t3 = 1;
                        soundGo("https://autodarts.x10.mx/triple/SoundHwTriple17_old.wav", audio);
                    }
                    // Bullseye 3rd dart
                    if ( x3.includes("BULL") && t3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0){
                        t3 = 1;
                        soundGo("https://autodarts.x10.mx/triple/bullseye.mp3", audio);
                    }


                }


                ///////////////////////////////////////////////// Miss CALLER ///////////////////////////
                if(boosound == 1){
//console.log( 'Miss 1: '+$(".css-qy5j7n:eq(0)").closest('div').attr('class') );
//console.log( 'x1: '+ x1 );
//console.log('m1: ' +m1 );
//console.log( 'called: '+ called );
                    // miss 1st dart

                if ( x1.includes("M") && m1 === 0 && $(".css-1fzu0fr:eq(0)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0)

                    {   m1 = 1;
                     soundGo("https://autodarts.x10.mx/russ_bray/miss_1st_dart.mp3", audio);
                    }
                    // miss 2st dart
                    else if ( x2.includes("M") && m2 === 0 && $(".css-1fzu0fr:eq(1)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0)
                    {   m2 = 1;
                     soundGo("https://autodarts.x10.mx/russ_bray/miss_2nd_dart.mp3", audio);
                    }

                    // miss 3rd dart
                    else if ( x3.includes("M") && m3 === 0 && $(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr' && called == 0)
                    {    m3 = 1;
                     soundGo("https://autodarts.x10.mx/russ_bray/miss_3rd_dart.mp3", audio);
                    }
                }

                // Caller reset
                if( (x1 == '' || $(".css-1fzu0fr:eq(2)").closest('div').attr('class') != 'ad-ext-turn-throw css-1fzu0fr') && called == 1) {
                    called = 0; busted = 0;
                    console.log('Caller resetet');
                }


                // Korrektur nach Klick auf Ok -> Score auslesen und Caller erneut aktivieren (called=0)
                $("button:contains('Ok')").on( "click", function() {
                    called = 1;
                    setTimeout(function (){
                        var x0 = $(".css-1tq00ko:eq(0)").text(); // read new score with delay(wait for AD-Response)
                        score_is_edited = 1;
                    }, 500);
                    // soundGo(server+caller+'/'+x0+dotmp3, audio); // Wert in Audio-Funktion übergeben


                });

                // Caller if 3rd Dart has a score(class) and called == 0
                if( ($(".css-1fzu0fr:eq(2)").closest('div').attr('class') == 'ad-ext-turn-throw css-1fzu0fr'
                     && called == 0
                     && caller != 'x')
                   || score_is_edited == 1 ) {
                    called = 1;
                    score_is_edited = 0;
                    console.log('Score: '+x0 );
                    soundGo(server+caller+'/'+x0+dotmp3, audio); // Wert in Audio-Funktion übergeben
                    t1 = 0; t2 = 0; t3 = 0; // reset/enable triple-sound
                    m1 = 0; m2 = 0; m3 = 0; // reset/enable miss-sound
                }


                // Sound-Option-Selects einfügen wenn noch nicht existiert
                if($("#caller").length == 0) {

                    $(".css-1tw9fat").after('<select id="caller" name="caller" class="css-1xbroe7" style="padding:5px 5px 5px 5px;"><option selected disabled style="background-color: #353d47">Caller</option><option value="x" style="background-color: #353d47">OFF</option><option value="1_male_eng" style="background-color: #353d47">Male eng</option><option value="google_eng" style="background-color: #353d47">Female eng</option><option value="google_fr" style="background-color: #353d47">Female fr</option><option value="google_de" style="background-color: #353d47">Female de</option><option value="russ_bray" style="background-color: #353d47">Russ B</option><option value="georgeno" style="background-color: #353d47">George N</option><option value="shorty" style="background-color: #353d47">Shorty eng</option><option value="haulpinks" style="background-color: #353d47">Caller Paul eng</option><option value="bayrisch" style="background-color: #353d47">Bavarian male</option></select>');



                    //////  Triple-Select
                    $(".css-1tw9fat").after('<select id="triplesound" name="triplesound" class="css-1xbroe7" style="padding:5px;"><option selected disabled style="background-color: #353d47">Triple-Sound</option><option value="0" style="background-color: #353d47">OFF</option><option value="1" style="background-color: #353d47">Beep on</option><option value="2" style="background-color: #353d47">Löwen (Softtip)</option></select>');
                    $(document).on('change', '#triplesound', function(){
                        triplesound = $("#triplesound").find(":selected").val();

                        // save triple-settings
                        (async () => {
                            await GM.setValue('triplesound', triplesound);
                        })();

                    })

                    // Boo-Select
                    $(".css-1tw9fat").after('<select id="boosound" name="boosound" class="css-1xbroe7" style="padding:5px;"><option selected disabled style="background-color: #353d47">Boo-Sound</option><option value="1" style="background-color: #353d47">ON</option><option value="0" style="background-color: #353d47">OFF</option></select>');

                    $(document).on('change', '#boosound', function(){
                        boosound = $("#boosound").find(":selected").val();
                        // save boo-settings
                        (async () => {
                            await GM.setValue('boosound', boosound);
                        })();
                    })

                } // end add dropdowns

            }
        }, 1500);

    });

})();