BGA Ark Nova Helper

可以自动进行BGA上方舟动物园匹配也可以随机休息或者卡牌的摆烂操作,期间会自动进行弃牌收获等操作(暂不支持需要进行行动的收获操作),当游戏进度超过50后自动投降

当前为 2024-06-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         BGA Ark Nova Helper
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  可以自动进行BGA上方舟动物园匹配也可以随机休息或者卡牌的摆烂操作,期间会自动进行弃牌收获等操作(暂不支持需要进行行动的收获操作),当游戏进度超过50后自动投降
// @author       HQ163
// @match        *://boardgamearena.com/*
// @grant        none
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';

    // 创建悬浮窗
    let floatingWindow = document.createElement('div');
   floatingWindow.style.position = 'fixed';
    floatingWindow.style.bottom = '10px';
    floatingWindow.style.right = '10px';
    floatingWindow.style.zIndex = '1000';
    floatingWindow.style.backgroundColor = 'white';
    floatingWindow.style.border = '1px solid black';
    floatingWindow.style.padding = '10px';
    floatingWindow.style.boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.5)';
    floatingWindow.style.width = '300px';
    floatingWindow.style.maxHeight = '250px';
    floatingWindow.style.overflowY = 'auto';
    floatingWindow.style.cursor = 'move';
    floatingWindow.innerHTML = `
        <div id="floatingHeader" style="cursor: move; background-color: #f0f0f0; padding: 5px; text-align: center;">
            Ark Nova Helper
        </div>
        <button id="startButton">开始摆烂</button>
        <button id="matchButton">开始匹配</button>
        <div id="logArea" style="margin-top: 10px; max-height: 150px; overflow-y: auto; border: 1px solid black; padding: 5px; background-color: #f0f0f0;"></div>
    `;
    document.body.appendChild(floatingWindow);
        // 实现拖动功能
    dragElement(floatingWindow);

    function dragElement(element) {
        let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
        const header = document.getElementById('floatingHeader');
        if (header) {
            header.onmousedown = dragMouseDown;
        } else {
            element.onmousedown = dragMouseDown;
        }

        function dragMouseDown(e) {
            e = e || window.event;
            e.preventDefault();
            pos3 = e.clientX;
            pos4 = e.clientY;
            document.onmouseup = closeDragElement;
            document.onmousemove = elementDrag;
        }

        function elementDrag(e) {
            e = e || window.event;
            e.preventDefault();
            pos1 = pos3 - e.clientX;
            pos2 = pos4 - e.clientY;
            pos3 = e.clientX;
            pos4 = e.clientY;
            element.style.top = (element.offsetTop - pos2) + "px";
            element.style.left = (element.offsetLeft - pos1) + "px";
        }

        function closeDragElement() {
            document.onmouseup = null;
            document.onmousemove = null;
        }
    }
    let isRunning = false;
    let isMatching = false;
    let intervalId = null;
    let matchIntervalId = null;


    const originalConsoleLog = console.log;
    console.log = function(message) {
        originalConsoleLog.apply(console, arguments);
        const logArea = document.getElementById('logArea');
        logArea.innerHTML += `<div>${message}</div>`;
        logArea.scrollTop = logArea.scrollHeight; // 自动滚动到最新的消息
    };

    document.getElementById('startButton').addEventListener('click', function() {
        isRunning = !isRunning;
        if (isRunning) {
            this.textContent = '停止摆烂';
            startMonitoring();
        } else {
            this.textContent = '开始摆烂';
            stopMonitoring();
        }
    });


        document.getElementById('matchButton').addEventListener('click', function() {
        isMatching = !isMatching;
        if (isMatching) {
            this.textContent = '停止匹配';
            startMatching();
        } else {
            this.textContent = '开始匹配';
            stopMatching();
        }
    });

    function startMonitoring() {
        console.log("开始检测");
        intervalId = setInterval(function() {
            console.log("3s检测");
            // 检测是否轮到我的回合
            if (isMyTurn()) {
                // 执行休息行动
                performRestAction();
            }
        }, 3000); // 每3秒检测一次
    }

    function stopMonitoring() {
        clearInterval(intervalId);
    }


        function startMatching() {
            console.log("开始匹配");
        matchIntervalId = setInterval(function() {
            let startButton = document.getElementsByClassName('bgabutton bgabutton_blue bga-button-inner flex-1 truncate svelte-eirkpo bgabutton_huge'); // 假设“开始”按钮的ID为 startGameButton
            if (startButton.length) {
                simulateClick(startButton[0]);
                console.log('已点击开始匹配');
            }
            let querenButton = document.getElementsByClassName('bgabutton bgabutton_always_big bgabutton_blue'); // 假设“开始”按钮的ID为 startGameButton
            if (querenButton.length) {
                simulateClick(querenButton[0]);
                console.log('已确认游戏开始');
            }

        }, 3000); // 每3秒检测一次
    }

    function stopMatching() {
        let stpobutton=document.getElementsByClassName('bgabutton bgabutton_red bga-button-inner flex-1 truncate svelte-eirkpo bgabutton_huge');
        if(stpobutton.length){
         simulateClick(stpobutton[0]);
         console.log('已点击取消匹配');
        }
        clearInterval(matchIntervalId);
    }


    function extractNumberFromString(str) {
    // 使用正则表达式匹配字符串中的数字部分
    let result = str.match(/\d+/);
    if (result) {
        // 将匹配到的字符串转换为数字
        return parseInt(result[0], 10);
    } else {
        // 如果没有匹配到数字,则返回 NaN
        return NaN;
    }
}
    function isMyTurn() {
        // 这里假设游戏界面上存在一个特定元素或标识符来表示轮到玩家的回合
        // 你需要根据实际情况修改选择器
        return 1;
    }

    function performRestAction() {
        // 选择赞助商卡牌
        let gamepercent=Number(document.getElementById('pr_gameprogression').innerText);
        let dianle=document.getElementById('ingame_menu_concede');
        let sponsorCard = document.getElementById('btnChoose5');
        let drawcaard=document.getElementById('btnChoose2');
        let restButton = document.getElementById('btnPickMoney');
        let confirmButton = document.getElementById('btnConfirmTurn');
        let getallCard=document.getElementById('btnAnytimeAction0');
        let btchoice=document.getElementById('btnChoice0');
        let conformchoice=document.getElementById('btnConfirmChoice');
        let drawallcard=document.getElementById('btnDrawAllFromDeck');
        let allbluebutton=document.getElementsByClassName('bgabutton bgabutton_blue');
        let drawonfirm=false;
        for(let k=0;k<allbluebutton.length;k++){
        if(allbluebutton[k].innerText.indexOf("确认") !== -1){
            drawonfirm=allbluebutton[k];
        }
        }
        //let finalcards=document.getElementsByClassName('ark-card zoo-card scoring-card tooltipable selectable');
        let gametext=document.getElementById('pagemaintitletext');
        let carddom=document.getElementById('floating-hand').childNodes;
        let cards=carddom[0].childNodes;
        let finalcards=carddom[1].childNodes
        if(gametext.innerText.indexOf("游戏结束") !== -1&&gametext.innerText.indexOf("触发游戏结束") == -1){
           let endbutton=document.getElementById('startButton');
           simulateClick(endbutton);
           console.log('游戏结束!停止摆烂!')
            return;
        }
        if(gamepercent>50){
           simulateClick(dianle);
           console.log('游戏进度超过50%,点了!')
        }
        if((gametext.innerText.indexOf("你必须弃除") !== -1||gametext.innerText.indexOf("你必须选择你想保留的") !== -1)&&!conformchoice&&gametext.innerText.indexOf("终局计分卡") == -1){
            let num=extractNumberFromString(gametext.innerText);
            console.log('需要弃除或保留'+num+'张牌');
            for(let i=0;i<num;i++){
            simulateClick(cards[i]);
            }
            console.log('已经选择'+num+'张牌');
            simulateClick(conformchoice);
            console.log('已经确认弃除或保留'+num+'张牌');
        }
        if((gametext.innerText.indexOf("你必须弃除") !== -1&&gametext.innerText.indexOf("终局计分卡") !== -1)&&!conformchoice){
            console.log('需要弃除一张终局计分卡');
        let cardarea=document.getElementById('floating-hand-wrapper');
        cardarea.setAttribute('data-open','scoringHand');
        simulateClick(finalcards[0]);
            console.log('已经选择一张终局计分卡');
            simulateClick(conformchoice);
            console.log('已经确认弃除一张终局计分卡');
        }
        if(drawcaard&&sponsorCard){
            let mathrandom=Math.random();
            console.log(mathrandom);
            if(mathrandom>0.5){
                console.log("找到抽牌卡牌");
                simulateClick(drawcaard);
        }else{
            console.log("找到赞助商卡牌");
            let plus=document.getElementById('xtoken-modifier-plus');
            for(let j=0;j<5;j++){
            simulateClick(plus);
            }
            simulateClick(sponsorCard);
        }
        }
        if(drawcaard&&!sponsorCard){
            console.log("找到抽牌卡牌");
            simulateClick(drawcaard);
        }
        if (sponsorCard&&!drawcaard) {
            console.log("找到赞助商卡牌");
            let plus=document.getElementById('xtoken-modifier-plus');
            for(let j=0;j<5;j++){
            simulateClick(plus);
            }
            simulateClick(sponsorCard);
        }
        else if( restButton ){
            console.log("找到休息卡牌");
            simulateClick(restButton);
        }
        else if( confirmButton ){
            console.log("找到确定卡牌");
            simulateClick(confirmButton);
        }
        else if( getallCard ){
            console.log("找到收获所有卡牌");
            simulateClick(confirmButton);
        }
        else if( btchoice ){
            console.log("找到收获第一个选择卡牌");
            simulateClick(btchoice);
        }
        else if( conformchoice ){
            console.log("找到选择确认卡牌");
            simulateClick(conformchoice);
        }
        else if( drawonfirm ){
            console.log("找到确认抽取卡牌");
            simulateClick(drawonfirm);
        }
        else if( drawallcard &&!drawonfirm){
            console.log("找到抽取所有卡牌");
            simulateClick(drawallcard);
        }

    }

    function simulateClick(element) {
        let event = new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window
        });
        element.dispatchEvent(event);
    }
})();