Visual Ability Buttons

Does Things

As of 2019-05-26. See the latest version.

// ==UserScript==
// @name         Visual Ability Buttons
// @namespace    http://tampermonkey.net/
// @version      0.15
// @description  Does Things
// @author       ceviri
// @match        https://dominion.games/
// @require      http://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

dominion_VAB = {
    currentScale: 1,
    currentCards: [],
    makeButton: function(entry) {
        var scale = dominion_VAB.currentScale;
        var height = MINI_CARD_HEIGHT * scale / 4;
        var width = MINI_CARD_WIDTH * scale;
        var art = publicCardFactory.getNewAnonymousCard(entry.card).miniView.artURL;

        var inner = ''
        if (entry.count > 1) {
                    inner +=
`
<div class="new-card-counter-container ng-scope" style="transform:scale(${scale}); top:0px; left:0px; pointer-events: none;">
    <div class="new-card-counter-text-container" style="top:50%;">
        <div class="new-card-counter-text ng-binding" style="left:-50%;">${entry.count}
        </div>
    </div>
</div>`;
        }
        if (entry.extras.length > 0) {
            inner += entry.extras.map(c => `<div class="mini-info" style="width: ${width / 5}px; height: ${height * 0.8}px; margin: 2px; background-size: cover; background-image: url(${publicCardFactory.getNewAnonymousCard(c.cardName).miniView.artURL}); background-position: center center; border: 2px solid orange;"> </div>`).join('');
        }

        return `<div class="call-button" style="width: ${width}px; height: ${height}px; position:relative; margin-bottom:6px;
                                                background-size: cover; background-image: url(${art}); background-position:center; border: 3px solid white;
                                                display: flex; flex-direction: row-reverse; align-items: center; z-index: 5000;"
        onclick="publicAbilityClicked({'which':1}, ${entry.index});"
        onmouseenter="this.style.border = '3px solid green';"
        onmouseleave="this.style.border = '3px solid white';"> ${inner}
        </div>`;
    },

    cardifyList: function(entries) {
        let inner = entries.map(e => dominion_VAB.makeButton(e)).join("");

        if (entries.length > 0 && activeGame.openQuestions[0].declineButtonId > -1) {
            var scale = dominion_VAB.currentScale;
            var height = MINI_CARD_HEIGHT * scale / 4;
            var width = MINI_CARD_WIDTH * scale;
            inner +=
`<div class="call-button" style="width: ${width}px; height: ${height}px; position:relative; margin-bottom:6px;
                                    background-size: cover; background-color: black; background-position:center; border: 3px solid white;
                                    display: flex; justify-content: center; align-items: center; z-index: 5000;"
onclick="angular.element(document.body).injector().get('answerCollector').sendDecline(7);"
onmouseenter="this.style.border = '3px solid green';"
onmouseleave="this.style.border = '3px solid white';"> <div class="call-text" style="color: white; font-family: TrajanPro-Bold; font-size: 3vh; margin: 0 auto; pointer-events: none;"> Done </div>
</div>`;
        }

        return `<div class="ability-container" style="overflow: hidden;">${inner}</div>`;
    },

    angular_debug_check: function () {
        if (typeof angular.element(document.body).scope() == 'undefined') {
            angular.reloadWithDebugInfo();
            return false;
        } else {
            return true;
        }
    },

    redraw: function(){
        if (angular.element($('.game-area')).length > 0){
            setTimeout(function() {
                var scale = publicPositionProperties.getPileProperties().scale;
                var currentCards = [];
                var reprs = new Object();
                var entries = [];
                for (qn of activeGame.openQuestions) {
                    if (qn.type == 7) {
                        $('.done-buttons-area .done-button').css("display", "none");
                        currentCards = qn.content.elements.map(x => activeGame.getCardNameById(x.association).name);

                        let abilityIndex = 0;
                        for (a of qn.content.elements) {
                            let card = activeGame.getCardNameById(a.association);
                            let nonStacks = new Set(["Archive", "Watchtower"]);

                            let args = [];
                            if (a.logEntryArguments.length > 0) {
                                args = a.logEntryArguments[0].argument;
                            }
                            if (a.name.name == "WATCHTOWER_TRASH_REACTION")
                                args = [{'cardName': CardNames.DONATE}];
                            if (a.name.name == "WATCHTOWER_TOPDECK_REACTION")
                                args = [{'cardName': CardNames.ROYAL_SEAL}];
                            if (card.name == 'Royal Carriage') {
                                if (qn.story.context != -1) {
                                    args = qn.story.context.argument.logEntryArguments[1].argument;
                                }
                            }


                            if (a.name.name == "CHANGELING_MAY_EXCHANGE") {
                                entries.push({'card': CardNames.CHANGELING, 'count': 1, 'index': 0, 'extras': a.logEntryArguments[0].argument});
                            } else if (card in reprs) {
                                reprs[card].count++;
                            } else if (nonStacks.has(card.name)) {
                                entries.push({'card': card, 'count': 1, 'index': abilityIndex, 'extras': args});
                            } else {
                                reprs[card] = {'card': card, 'count': 1, 'index': abilityIndex, 'extras': args};
                                entries.push(reprs[card]);
                            }
                            abilityIndex++;
                        }

                    }
                }

                var changed = false;
                if (scale != dominion_VAB.currentScale)
                    changed = true;
                if (currentCards.length != dominion_VAB.currentCards.length) {
                    changed = true;
                } else {
                    for (let i = 0; i < currentCards.length; i++) {
                        if (currentCards[i] != dominion_VAB.currentCards[i]) {
                            changed = true;x
                            break;
                        }
                    }
                }

                dominion_VAB.currentScale = scale;
                dominion_VAB.currentCards = currentCards;
                inner = dominion_VAB.cardifyList(entries);
                if ($('.done-buttons-area .ability-container').length == 0){
                    $('.done-buttons-area').prepend(inner);
                } else if (changed) {
                    $('.done-buttons-area .ability-container').replaceWith(inner);
                }
            }, 100);
        }
    }
}

dominion_VAB.angular_debug_check();
setInterval(dominion_VAB.redraw, 200);