woodcutter-grabber

Grab logs from Dominion Online

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// ==UserScript==
// @name         woodcutter-grabber
// @namespace    http://tampermonkey.net/
// @version      0.78
// @description  Grab logs from Dominion Online
// @author       ceviri
// @match        https://dominion.games/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @grant        GM_openInTab
// @require      http://code.jquery.com/jquery-3.3.1.min.js
// @connect      ceviri.me
// ==/UserScript==

var currentlyWaiting;
var oldAlert;
var oldConfirm;
function wait_until_condition(condition, interval, delayedFunction){
    if (condition()){
        setTimeout(delayedFunction, 500);
    }
    else{
        currentlyWaiting = setTimeout(function(){wait_until_condition(condition, interval, delayedFunction);}, interval);
    }
}

function abort(){
    clearTimeout(currentlyWaiting);
}

WC_grabber = {
    shouldDisplay: false,

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

    loopclock: function(){
          WC_grabber.timeout = setTimeout(WC_grabber.loopclock, 500);
          WC_grabber.redraw();
    },

    redraw: function(){
        if (angular.element($('table-starter')).controller('tableStarter')){
            if (angular.element($('table-starter')).controller('tableStarter').replayService.shouldShowReplayWindow()) {
                if ($('table-starter .wc-grab-logs').length == 0){
                    var log_grab_button = $('<button class="lobby-button wc-grab-logs" onclick="WC_grabber.setupTable()">Grab Logs</button>');
                    $('table-starter div[class=""]').append(log_grab_button);
                }
            }
        }
        if (angular.element($('my-table')).controller('myTable')){
            if ($('.table-buttons .wc-grab-logs').length == 0){
                var log_grab_button = $('<button class="lobby-button wc-grab-logs" onclick="WC_grabber.askForMultiLogs()">Grab Logs</button>');
                $('table-buttons div.table-buttons').append(log_grab_button);
            }
        }
    },


    grabLogs: function(){
        log = Object.values(angular.element($(".log-container")).controller().log.getBulletedEntries()).map(x=>x.string).join("~");
        x=activeGame.model.gameState.cards.map(x =>LANGUAGE.getCardName[x.cardName.name].singular);
        supply = Array.from(new Set(x)).map( i => i+"-"+x.filter(j=>j==i).length).join("~");
        if (log.indexOf('unrated') > -1){
            angular.element($('top-controls')).controller('topControls').getControls()[3].execute();
            angular.element($('game-ended-notification')).controller('gameEndedNotification').ok();
        }
        return [log, supply]
    },


    sendLogs: function(logOne, logTwo, supply, players, gameId){
        datastring = "fileone=" + encodeURIComponent(logOne) + "&filetwo=" + encodeURIComponent(logTwo) +
                     "&supply=" + encodeURIComponent(supply) + "&players="+encodeURIComponent(players);

        GM_xmlhttpRequest({
            method: "POST",
            url: "http://ceviri.me/woodcutter/submit",
            data: datastring,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            onload: function(response) {
                if (WC_grabber.shouldDisplay){
                    GM_openInTab("http://ceviri.me/woodcutter/"+gameId+"/display");
                }
            }
        });
    },


    setupTable: function(){
        WC_grabber.shouldDisplay = true;
        angular.element($('table-starter')).controller('tableStarter').submitReplay();
        oldAlert = unsafeWindow.alert;
        oldConfirm = unsafeWindow.confirm;
        unsafeWindow.alert = () => {abort();};
        unsafeWindow.confirm = () => {return true;};

        wait_until_condition(function(){return $('.lobby-page').length > 0;}, 100,function(){
            angular.element($('rules-editor')).controller('rulesEditor').addBot();
            thisLog = angular.element($('rules-editor')).controller('rulesEditor').gameId;

            setTimeout(function(){
                angular.element($('order-players')).controller('orderPlayers').playerOrderService.shouldRandomize = false;
                angular.element($('order-players')).controller('orderPlayers').playerOrderService.done();

                WC_grabber.parseReplays([thisLog]);
            }, 1000);
        });
    },


    parseReplays: function(replayList){
        wait_until_condition(function(){return $('.lobby-page').length > 0;}, 100, function(){
            gameId = replayList.pop();
            console.log("Parsing replay #"+gameId);
            console.log("Remaining Queue:");
            console.log(replayList)

            angular.element($('rules-editor')).controller('rulesEditor').gameId = gameId;
            angular.element($('rules-editor')).controller('rulesEditor').decisionIndex = ""
            angular.element($('rules-editor')).controller('rulesEditor').submitReplay();

            setTimeout(function(){
                playerNames = [];
                for (i=0;i<2;i++){
                    playerNames[i] = angular.element($('order-players')).controller('orderPlayers').getOriginalPlayerName(i);
                }
                playerNameString = playerNames.join('~');

                firstPlayer = angular.element($('order-players')).controller('orderPlayers').playerOrderService.getOrderedPlayers()[0].namedId.name;

                wait_until_condition(function(){return angular.element($('order-players')).controller('orderPlayers').playerOrderService.shouldRandomize == false;}, 100, function(){
                    unsafeWindow.publicTableService.ready();

                    wait_until_condition(function(){return typeof angular.element($(".log-container")).controller() !== "undefined";},100,function(){
                        wait_until_condition(function(){return typeof angular.element($(".log-container")).controller().log.getBulletedEntries() !== "undefined";},100,function(){
                            results = WC_grabber.grabLogs();
                            logOne = results[0];
                            supply = results[1];

                            wait_until_condition(function(){return $('score-table-buttons').length > 0;}, 100, function(){
                                angular.element($('score-table-buttons')).controller('scoreTableButtons').editTable();

                                wait_until_condition(function(){return $('.lobby-page').length > 0;}, 100, function(){
                                    angular.element(document.querySelector('.window.my-table')).injector().get('playerOrderService').moveDown(0);
                                    angular.element(document.querySelector('.window.my-table')).injector().get('playerOrderService').done();

                                    wait_until_condition(function(){return angular.element($('order-players')).controller('orderPlayers').playerOrderService.getOrderedPlayers()[0].namedId.name != firstPlayer;}, 100, function(){
                                        unsafeWindow.publicTableService.ready();

                                        wait_until_condition(function(){return $('.base-hero-bar .opponent-name-counter-pane').length > 0;},100,function(){
                                            results = WC_grabber.grabLogs();
                                            logTwo = results[0];

                                            WC_grabber.sendLogs(logOne, logTwo, supply, playerNameString, gameId);

                                            wait_until_condition(function(){return $('score-table-buttons').length > 0;}, 100,function(){
                                                angular.element($('score-table-buttons')).controller('scoreTableButtons').editTable();
                                                if (replayList.length > 0){
                                                    WC_grabber.parseReplays(replayList);
                                                } else {
                                                    unsafeWindow.confirm = oldConfirm;
                                                    unsafeWindow.alert = oldAlert;
                                                    wait_until_condition(function(){return $('.lobby-page').length > 0;}, 100, function(){
                                                        angular.element($('my-table')).controller('myTable').leave();
                                                    });
                                                }
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            }, 1000);
        });
    },


    setupMultiParse: function(replayList){
        WC_grabber.shouldDisplay = false;
        oldAlert = unsafeWindow.alert;
        oldConfirm = unsafeWindow.confirm;
        unsafeWindow.alert = () => {abort();};
        unsafeWindow.confirm = () => {return true;};
        angular.element($('rules-editor')).controller('rulesEditor').addBot();
        angular.element($('rules-editor')).controller('rulesEditor').replayGame();

        setTimeout(function(){
            angular.element($('order-players')).controller('orderPlayers').playerOrderService.shouldRandomize = false;
            angular.element($('order-players')).controller('orderPlayers').playerOrderService.done();
            WC_grabber.parseReplays(replayList);
        }, 1000);
    },


    askForMultiLogs: function(){
        var rawText = prompt("Enter game IDs, separated with commas. No spaces!");
        WC_grabber.setupMultiParse(rawText.split(","));
    }
}

WC_grabber.angular_debug_check();
WC_grabber.loopclock();