Greasy Fork

Greasy Fork is available in English.

joust

joust test

当前为 2025-05-24 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/537047/1594660/joust.js

// joust.js (Improved Joust Tab with Auto Event)

Tabs.Joust = {
    tabOrder: 2120,
    tabLabel: 'Joust',
    tabColor: 'brown',
    validJoust: false, // Use camelCase for consistency
    isBusy: false,
    myDiv: null,
    timer: null,
    numJousts: 0,  // Use camelCase
    numWins: 0,   // Use camelCase
    autoJoustEnabled: false, // New option for auto jousting
    joustDelay: 9, // Default delay in seconds

    init(div) {
        this.myDiv = div;
        this.loadOptions();
        this.checkEvent(); // Check initially
    },

    loadOptions() {
        // Load options from storage (GM_setValue or similar)
        this.autoJoustEnabled = Options.JoustOptions ? Options.JoustOptions.autoJoustEnabled : false;
        this.joustDelay = Options.JoustOptions ? Options.JoustOptions.joustDelay : 9; // Load delay
    },

    saveOptions() {
        Options.JoustOptions = Options.JoustOptions || {};
        Options.JoustOptions.autoJoustEnabled = this.autoJoustEnabled;
        Options.JoustOptions.joustDelay = this.joustDelay;
        saveOptions();
    },

    checkEvent() {
        this.validJoust = uW.cm.JoustingModel.getTimeLeft() > 0; // || true;  Remove the || true (for testing only)
        // ... (Update tab style based on validJoust) ...

        if (this.autoJoustEnabled && this.validJoust) {
            this.start();
        }
    },

    async doJoust() {  // Use async/await
        if (!this.isBusy) return;

        const div = $("#pbjoust_info"); // Use jQuery consistently

        try {
            const params = { ...uW.g_ajaxparams }; // Use spread syntax
            params.ctrl = 'jousting\\JoustingController';
            params.action = 'opponents';

            const result = await api.apiRequest("_dispatch53.php", params); // Use api.js
            if (!result.ok) {
                throw new Error(`Server Error: ${result.error.message}`); // Throw error for catch block
            }

            result.opponents.forEach((opponent, index) => {
                setTimeout(() => this.doFight(opponent.id, opponent.serverid), index * this.joustDelay * 1000);
            });

            // Add random delay before next joust (adjust range as needed)
            const randomDelay = Math.floor(Math.random() * 4);
            setTimeout(() => this.nextJoust(), (this.joustDelay * 3 + randomDelay) * 1000);

        } catch (error) { // Catch errors and update the UI
            div.prepend(`<span style="color:#800;">${error.message}</span><br>`); // Prepend error message
            this.handleError();
        }
    },


    async doFight(opponent, opponentServerId) { // Use async/await
        // ... (Similar structure as doJoust, using try...catch and api.apiRequest) ...
        try {
            // ... (Your existing doFight logic, using api.apiRequest) ...
        } catch (error) {
            // ... (Handle error, update UI) ...
            div.prepend(`<span style="color:#800;">${error.message}</span><br>`); // Prepend error message
            this.handleError(); // Handle the error appropriately
        }
    },



    show() {
        // ... (Similar to previous example, but add auto-joust toggle) ...
        // ...

        if (this.validJoust) {
            m += `<br><label for="autoJoustEnable"><input type="checkbox" id="autoJoustEnable" ${this.autoJoustEnabled ? 'checked' : ''}> Enable Auto Joust</label><br>`;
            // ... other elements ...
        }

        // ...

        if (this.validJoust) {
            // ... event listeners ...
            $("#autoJoustEnable").change(() => {
                this.autoJoustEnabled = $("#autoJoustEnable").is(":checked");
                this.saveOptions();
                if (this.autoJoustEnabled) {
                    this.start();
                }
            });
        }
    },

    handleError() {
        const t = Tabs.Joust;
        ById('pbJoustCancel').firstChild.innerHTML = uW.g_js_strings.commonstr.close;
        Options.JoustOptions.JoustRunning = false;
        t.isBusy = false;
        t.saveOptions();
    },

    // ... (other functions - start, nextfight, setPopup, setCurtain, e_Cancel) ...
};

// In Options tab (or where you initialize options):
if (!Options.JoustOptions) {
    Options.JoustOptions = {
        JoustRunning: false,
        JoustDelay: 9,
        autoJoustEnabled: false // Add autoJoustEnabled option
    };
}