Ortenskung Script / Hack 2.0

Automates crimes

当前为 2025-09-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         Ortenskung Script / Hack 2.0
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Automates crimes 
// @match        https://www.ortenskung.com/en/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // ---------------- KEEP-ALIVE ----------------
    try {
        setInterval(() => { /* heartbeat */ }, 30000);
        const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        const osc = audioCtx.createOscillator();
        osc.frequency.value = 0.0001;
        osc.connect(audioCtx.destination);
        osc.start();
    } catch (e) { /* ignore */ }

    // ---------------- State ----------------
    let running = false;
    let loopTimer = null;

    // remove previous panel if it exists
    const existing = document.getElementById('__ortens_crime_bot_panel');
    if (existing) existing.remove();

    // ---------------- Create GUI ----------------
    const panel = document.createElement('div');
    panel.id = '__ortens_crime_bot_panel';
    Object.assign(panel.style, {
        position: 'fixed',
        top: '5px',
        right: '5px',
        width: '260px',
        background: '#111',
        color: '#eee',
        borderRadius: '8px',
        border: '1px solid rgba(255,255,255,0.05)',
        boxShadow: '0 4px 16px rgba(0,0,0,0.5)',
        fontFamily: 'Inter, Roboto, sans-serif',
        fontSize: '13px',
        zIndex: 999999,
        userSelect: 'none',
        overflow: 'hidden'
    });

    panel.innerHTML = `
        <div id="__header" style="display:flex;align-items:center;justify-content:space-between;padding:6px 10px;background:#0c0c0c;border-bottom:1px solid rgba(255,255,255,0.05);cursor:move;font-weight:600;font-size:13px">
            <div>Crime Bot</div>
            <div style="display:flex;gap:6px;align-items:center">
                <button id="__minBtn" title="Minimize" style="background:transparent;border:none;color:#aaa;cursor:pointer;font-size:14px">—</button>
                <button id="__closeBtn" title="Close" style="background:transparent;border:none;color:#aaa;cursor:pointer;font-size:14px">✕</button>
            </div>
        </div>
        <div id="__body" style="padding:8px;display:block">
            <div style="display:flex;gap:6px;margin-bottom:6px">
                <button class="__tab" data-tab="main" style="flex:1;padding:4px;border-radius:4px;border:0;background:#1b1b1b;color:#fff;cursor:pointer">Main</button>
                <button class="__tab" data-tab="settings" style="flex:1;padding:4px;border-radius:4px;border:0;background:#121212;color:#888;cursor:pointer">Settings</button>
            </div>

            <div id="__tab_main" class="__tabpanel">
                <label style="font-size:12px">Crime Type</label>
                <select id="__crimeType" style="width:100%;padding:4px;margin:4px 0 8px;border-radius:4px;border:1px solid rgba(255,255,255,0.06);background:#0b0b0b;color:#fff;font-size:12px">
                    <option value="neighborhood">Neighborhood</option>
                    <option value="car">Car Robbery</option>
                </select>

                <label style="font-size:12px">Crime ID</label>
                <select id="__crimeId" style="width:100%;padding:4px;margin:4px 0 8px;border-radius:4px;border:1px solid rgba(255,255,255,0.06);background:#0b0b0b;color:#fff;font-size:12px">
                    ${Array.from({length:30}, (_,i)=>i+1).map(n=>`<option value="${n}">${n}</option>`).join('')}
                </select>

                <div style="display:flex;align-items:center;gap:6px;margin-bottom:8px">
                    <label style="font-size:12px;display:flex;align-items:center;gap:4px">
                        <input type="checkbox" id="__autoClose" checked /> <span>Close dialogs</span>
                    </label>
                </div>

                <button id="__startBtn" style="width:100%;padding:6px;border-radius:6px;border:0;background:linear-gradient(180deg,#2fa84f,#208f3a);color:#fff;font-weight:600;cursor:pointer;font-size:13px">Start</button>
            </div>

            <div id="__tab_settings" class="__tabpanel" style="display:none">
                <p style="font-size:12px;color:#bbb;margin:0 0 6px">Settings</p>
                <p style="font-size:11px;color:#999;margin:0 0 6px">Loop delay (ms)</p>
                <input id="__loopDelay" type="number" value="4000" style="width:100%;padding:4px;border-radius:4px;border:1px solid rgba(255,255,255,0.06);background:#0b0b0b;color:#fff;font-size:12px" />
                <p style="font-size:11px;color:#999;margin:6px 0 0">If something breaks, stop and inspect selectors.</p>
            </div>
        </div>
        <div id="__statsBar" style="display:flex;justify-content:space-around;align-items:center;padding:6px 8px;background:#181818;border-top:1px solid rgba(255,255,255,0.05);border-bottom:1px solid rgba(255,255,255,0.05);font-size:12px;color:#eaeaea">
          <span id="__diamonds">💎 0</span>
          <span id="__cash">💵 0</span>
          <span id="__bullets">🔫 0</span>
        </div>
        <div id="__footer" style="font-size:11px;padding:5px 8px;background:#0f0f0f;border-top:1px solid rgba(255,255,255,0.03);color:#9a9a9a">
            Status: <span id="__status">Idle</span>
        </div>
    `;
    document.body.appendChild(panel);

    // ---------------- UI refs ----------------
    const header = panel.querySelector('#__header');
    const body = panel.querySelector('#__body');
    const footer = panel.querySelector('#__footer');
    const closeBtn = panel.querySelector('#__closeBtn');
    const minBtn = panel.querySelector('#__minBtn');
    const tabs = panel.querySelectorAll('.__tab');
    const tabMain = panel.querySelector('#__tab_main');
    const tabSettings = panel.querySelector('#__tab_settings');
    const startBtn = panel.querySelector('#__startBtn');
    const crimeIdEl = panel.querySelector('#__crimeId');
    const crimeTypeEl = panel.querySelector('#__crimeType');
    const autoCloseEl = panel.querySelector('#__autoClose');
    const statusEl = panel.querySelector('#__status');
    const loopDelayEl = panel.querySelector('#__loopDelay');

    // ---------------- Draggable ----------------
    (function makeDraggable(el) {
        let dragging = false, sx = 0, sy = 0, ox = 0, oy = 0;
        header.addEventListener('pointerdown', (ev) => {
            dragging = true;
            sx = ev.clientX; sy = ev.clientY;
            const rect = el.getBoundingClientRect();
            ox = rect.left; oy = rect.top;
            header.setPointerCapture(ev.pointerId);
        });
        document.addEventListener('pointermove', (ev) => {
            if (!dragging) return;
            const dx = ev.clientX - sx;
            const dy = ev.clientY - sy;
            el.style.left = (ox + dx) + 'px';
            el.style.top = (oy + dy) + 'px';
            el.style.right = 'auto';
            el.style.position = 'fixed';
        });
        document.addEventListener('pointerup', () => { dragging = false; });
    })(panel);

    // ---------------- Tabs ----------------
    tabs.forEach(t => t.addEventListener('click', () => {
        tabs.forEach(x => x.style.background = '#121212');
        t.style.background = '#1b1b1b';
        const tab = t.dataset.tab;
        if (tab === 'main') {
            tabMain.style.display = 'block';
            tabSettings.style.display = 'none';
        } else {
            tabMain.style.display = 'none';
            tabSettings.style.display = 'block';
        }
    }));

    // ---------------- Minimize / Close ----------------
    minBtn.addEventListener('click', () => {
        const isMinimized = panel.getAttribute('data-minimized') === 'true';
        if (!isMinimized) {
            body.style.display = 'none';
            footer.style.display = 'none';
            document.getElementById('__statsBar').style.display = 'none';
            panel.style.width = '140px';
            panel.setAttribute('data-minimized', 'true');
            minBtn.textContent = '▢';
            minBtn.title = 'Restore';
        } else {
            body.style.display = 'block';
            footer.style.display = 'block';
            document.getElementById('__statsBar').style.display = 'flex';
            panel.style.width = '260px';
            panel.setAttribute('data-minimized', 'false');
            minBtn.textContent = '—';
            minBtn.title = 'Minimize';
        }
    });
    closeBtn.addEventListener('click', () => panel.remove());

    // ---------------- Stats Auto Update ----------------
    function updateStats() {
        const diamondEl = document.querySelector('.val[data-tip*="diamond"]');
        const cashEl = document.querySelector('.val[data-tip*="cash"]');
        const bulletEl = document.querySelector('.val[data-tip*="ammunition"]');
        if (diamondEl) {
            document.getElementById('__diamonds').textContent = '💎 ' + diamondEl.textContent.trim().replace(/\s+/g, ' ');
        }
        if (cashEl) {
            document.getElementById('__cash').textContent = '💵 ' + cashEl.textContent.trim().replace(/\s+/g, ' ');
        }
        if (bulletEl) {
            document.getElementById('__bullets').textContent = '🔫 ' + bulletEl.textContent.trim().replace(/\s+/g, ' ');
        }
    }
    updateStats();
    setInterval(updateStats, 2000);


    // ---------------- Core automation ----------------

    function setStatus(text) {
        statusEl.textContent = text;
    }

    function stopLoop() {
        running = false;
        if (loopTimer) {
            clearTimeout(loopTimer);
            loopTimer = null;
        }
        startBtn.textContent = 'Start';
        setStatus('Stopped');
    }

    async function startLoop() {
        if (running) return;
        running = true;
        startBtn.textContent = 'Stop';
        setStatus('Running');
        // sync UI values
        selectedCrimeId = parseInt(crimeIdEl.value, 10);
        autoClose = autoCloseEl.checked;
        await performCrime(); // start immediately
    }

    startBtn.addEventListener('click', () => {
        if (!running) startLoop();
        else stopLoop();
    });

    // Utility: safe query with timeout (optional)
    function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }

    // Attempt to robustly find and click elements. The exact selectors depend on the game's DOM;
    // this function uses plausible selectors and falls back gracefully.
    async function performCrime() {
        if (!running) return;

        try {
            const delay = Math.max(50, parseInt(loopDelayEl.value, 10) || 4000);

            // 0) Check timers area if available (best-effort)
            const timerEl = document.querySelector('#my_timers > div.val') || document.querySelector('.timers .val');
            if (timerEl) {
                const txt = timerEl.textContent.trim();
                const m = txt.match(/(\d+)\s*\/\s*(\d+)/);
                if (m) {
                    const used = parseInt(m[1], 10), total = parseInt(m[2], 10);
                    const avail = total - used;
                    if (avail <= 0) {
                        setStatus('No timers left — waiting');
                        loopTimer = setTimeout(performCrime, 5000);
                        return;
                    }
                }
            }

            const mode = (crimeTypeEl.value || 'neighborhood');

            // 1) Ensure relevant dialog is open
            if (mode === 'neighborhood') {
                // Try to open crimes dialog if not present
                if (!document.querySelector('#go_crimes_dialog') && document.querySelector('#go_crimes')) {
                    document.querySelector('#go_crimes').click();
                    await sleep(300);
                }
                // Switch to Neighborhood tab/button if needed
                const nbBtn = Array.from(document.querySelectorAll('.crimes_button, .crimes_button_small, .crimes-tab, .crimes_button'))
                    .find(el => /neighborhood/i.test(el.textContent || el.innerText || ''));
                if (nbBtn && !document.querySelector('#neighborhood_crimes')) {
                    try { nbBtn.click(); } catch (e) {}
                    await sleep(250);
                }
            } else if (mode === 'car') {
                // Try to open cars dialog
                if (!document.querySelector('#dialog_cars') && document.querySelector('#go_cars')) {
                    document.querySelector('#go_cars').click();
                    await sleep(300);
                } else {
                    // sometimes the button text is 'Cars' or 'Car Robbery' or '#go_rob_car'
                    const carOpener = Array.from(document.querySelectorAll('a,button'))
                        .find(el => /(car|cars|vehicle|rob)/i.test(el.textContent || el.getAttribute('title') || ''));
                    if (carOpener && !document.querySelector('#dialog_cars')) {
                        try { carOpener.click(); } catch (e) {}
                        await sleep(300);
                    }
                }
            }

            // 2) Find commit/click button for the selected crime
            let commitBtn = null;

            if (mode === 'neighborhood') {
                // Buttons often have onclick including "'id':'N'" or "id: N"
                const btns = Array.from(document.querySelectorAll('.crime_button_small, .crime_button, a, button'))
                    .filter(el => el.getAttribute && (el.getAttribute('onclick') || '').toString().length > 0);
                commitBtn = btns.find(el => {
                    const on = (el.getAttribute('onclick') || '');
                    return new RegExp(`['"]?id['"]?\\s*[:=]\\s*['"]?${selectedCrimeId}['"]?`).test(on);
                }) || btns.find(el => (el.getAttribute('onclick') || '').includes(`'id':'${selectedCrimeId}'`));
                // fallback: match by visible label containing the id (rare)
                if (!commitBtn) {
                    const byText = Array.from(document.querySelectorAll('.crime_button_small, .crime_button'))
                        .find(el => (el.textContent || '').includes(String(selectedCrimeId)));
                    if (byText) commitBtn = byText;
                }
                // final fallback: first available small crime button
                if (!commitBtn) {
                    commitBtn = document.querySelector('.crime_button_small, .crime_button');
                }
            } else if (mode === 'car') {
                // Car robbery buttons may live under #dialog_cars or have onclick with 'robbing:perform' or 'robbing'
                commitBtn = Array.from(document.querySelectorAll('#dialog_cars .crime_button_small, #dialog_cars a, #dialog_cars button, .car_list a, .car_list button'))
                    .find(el => {
                        const on = el.getAttribute('onclick') || '';
                        return /robbing:perform|robbing|car|vehicle/i.test(on + ' ' + (el.textContent || ''));
                    });
                if (!commitBtn) {
                    // broader search
                    commitBtn = Array.from(document.querySelectorAll('a,button'))
                        .find(el => {
                            const on = (el.getAttribute('onclick') || '') + ' ' + (el.textContent || '');
                            return /robbing:perform|rob car|rob car|car robbery|rob car/i.test(on);
                        });
                }
                // last fallback: any clickable inside dialog_cars
                if (!commitBtn) {
                    commitBtn = document.querySelector('#dialog_cars .crime_button_small') || document.querySelector('#dialog_cars a') || document.querySelector('#dialog_cars button');
                }
            }

            if (!commitBtn) {
                setStatus('No crime button found — retrying');
                loopTimer = setTimeout(performCrime, 5000);
                return;
            }

            // Click commit
            try {
                commitBtn.scrollIntoView({block:'center', behavior:'auto'});
            } catch (e) {}
            try {
                commitBtn.click();
                setStatus(`Clicked ${mode} button`);
            } catch (e) {
                try { commitBtn.dispatchEvent(new MouseEvent('click', {bubbles:true,cancelable:true})); setStatus('Dispatched click'); }
                catch (err) { console.error('click failed', err); setStatus('Click failed'); }
            }

            // 3) After clicking, attempt to close dialogs / notifications
            setTimeout(() => {
                try {
                    if (typeof close_dialog === 'function') {
                        // best-effort closes
                        try { close_dialog('social'); } catch {}
                        try { close_dialog('levelup'); } catch {}
                        if (autoClose) try { close_dialog('crimes'); } catch {}
                        if (autoClose) try { close_dialog('cars'); } catch {}
                    }
                } catch (err) { /* ignore */ }

                // click primary notification button if present
                const notif = document.querySelector('.button.first, .ui-button.primary, .ui-dialog .button');
                if (notif) {
                    try { notif.click(); } catch (e) {}
                }
            }, 800);

            // 4) Schedule next run
            loopTimer = setTimeout(performCrime, Math.max(500, delay));
        } catch (err) {
            console.error('performCrime error', err);
            setStatus('Error — retrying');
            loopTimer = setTimeout(performCrime, 5000);
        }
    }

    // Expose stop for debugging from console
    window.__ortensCrimeBot = {
        stop: stopLoop,
        start: startLoop,
        status: () => ({ running, selectedCrimeId, autoClose })
    };

    // set initial selectedCrimeId
    selectedCrimeId = parseInt(crimeIdEl.value, 10);

})();