DF Backpack Transaction Handler

Extends the original Dead Frontier inventory transactions to include Backpack <-> Storage transactions

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greatest.deepsurf.us/scripts/579722/1834455/DF%20Backpack%20Transaction%20Handler.js

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

function getActualSlotId(slotNumber, inventoryType)
{
    if (inventoryType === "storage")
    {
        slotNumber += 40;
    }
    else if (inventoryType === "implants")
    {
        slotNumber += 1000;
    }
    else if (inventoryType === "backpackdisplay")
    {
        slotNumber += 1050;
    }

    return slotNumber;
}

function reloadStorage(data)
{
    unsafeWindow.reloadStorageData(data);
    unsafeWindow.populateBackpack();
}

const internalTransactionHandler = {
    handleBackpackToStorageOneWay(requestParams, itemSlots)
    {
        /*
            1. Swap target item with first slot in inventory
            2. Swap target item with target slot in storage
            3. (If needed) Swap items that were from original backpack slot and first slot in inventory
        */

        unsafeWindow.playSound("swap");

        const sourceItemType = itemSlots[0][1];
        const sourceInvType = itemSlots[0][2];
        const sourceSlotId = getActualSlotId(itemSlots[0][0], sourceInvType);

        const destItemType = itemSlots[1][1];
        const destInvType = itemSlots[1][2];
        const destSlotId = getActualSlotId(itemSlots[1][0], destInvType);

        const invStepSlotElement = document.querySelector("#inventory > tr > td.validSlot");
        const invStepSlotId = parseInt(invStepSlotElement.dataset.slot);
        const invStepItemType = invStepSlotElement.children.length > 0 ? invStepSlotElement.firstElementChild.dataset.type : "";

        // Backpack <-> Inventory
        requestParams.action = "backpack";
        requestParams.expected_itemtype = sourceItemType;
        requestParams.itemnum = sourceSlotId;
        requestParams.expected_itemtype2 = invStepItemType;
        requestParams.itemnum2 = invStepSlotId;

        makeRequest("hotrods/backpack", requestParams, function (data)
        {
            unsafeWindow.updateIntoArr(unsafeWindow.flshToArr(data, "DFSTATS_"), unsafeWindow.userVars);

            // Now 'sourceItemType' is at 'invStepSlotId' and 'invStepItemType' is at 'sourceSlotId'
            // Inventory <-> Storage
            requestParams.action = "store";
            requestParams.expected_itemtype = sourceItemType;
            requestParams.itemnum = invStepSlotId;
            requestParams.expected_itemtype2 = destItemType;
            requestParams.itemnum2 = destSlotId;

            makeRequest("inventory_new", requestParams, function (data)
            {
                // Now 'sourceItemType' is at 'destSlotId' and 'destItemType' is at 'invStepSlotId'
                // If the inventory swap or destination swap don't have any items, we don't need to make another request
                if (!invStepItemType && !destItemType)
                {
                    reloadStorage(data)
                    return;
                }

                unsafeWindow.updateIntoArr(unsafeWindow.flshToArr(data, "DFSTATS_"), unsafeWindow.userVars);

                // Inventory <-> Backpack
                requestParams.action = "backpack";
                if (!invStepItemType && destItemType)
                {
                    requestParams.expected_itemtype = destItemType;
                    requestParams.itemnum = invStepSlotId;
                    requestParams.expected_itemtype2 = invStepItemType;
                    requestParams.itemnum2 = sourceSlotId;
                }
                else
                {
                    requestParams.expected_itemtype = invStepItemType;
                    requestParams.itemnum = sourceSlotId;
                    requestParams.expected_itemtype2 = destItemType;
                    requestParams.itemnum2 = invStepSlotId;
                }

                makeRequest("hotrods/backpack", requestParams, reloadStorage);
            });
        });

        return true;
    },
    handleStorageToBackpackOneWay(requestParams, itemSlots)
    {
        unsafeWindow.playSound("swap");

        const sourceItemType = itemSlots[0][1];
        const sourceInvType = itemSlots[0][2];
        const sourceSlotId = getActualSlotId(itemSlots[0][0], sourceInvType);

        const destItemType = itemSlots[1][1];
        const destInvType = itemSlots[1][2];
        const destSlotId = getActualSlotId(itemSlots[1][0], destInvType);

        const invStepSlotElement = document.querySelector("#inventory > tr > td.validSlot");
        const invStepSlotId = parseInt(invStepSlotElement.dataset.slot);
        const invStepItemType = invStepSlotElement.children.length > 0 ? invStepSlotElement.firstElementChild.dataset.type : "";

        // Storage <-> Inventory
        requestParams.action = "take";
        requestParams.expected_itemtype = sourceItemType;
        requestParams.itemnum = sourceSlotId;
        requestParams.expected_itemtype2 = invStepItemType;
        requestParams.itemnum2 = invStepSlotId;

        makeRequest("inventory_new", requestParams, function (data)
        {
            unsafeWindow.updateIntoArr(unsafeWindow.flshToArr(data, "DFSTATS_"), unsafeWindow.userVars);

            // Now 'sourceItemType' is at 'invStepSlotId' and 'invStepItemType' is at 'sourceSlotId'
            // Inventory <-> Backpack
            requestParams.action = "backpack";
            requestParams.expected_itemtype = sourceItemType;
            requestParams.itemnum = invStepSlotId;
            requestParams.expected_itemtype2 = destItemType;
            requestParams.itemnum2 = destSlotId;

            makeRequest("hotrods/backpack", requestParams, function (data)
            {
                // Now 'sourceItemType' is at 'destSlotId' and 'destItemType' is at 'invStepSlotId'
                // If the inventory swap or destination swap don't have any items, we don't need to make another request
                if (!invStepItemType && !destItemType)
                {
                    reloadStorage(data);
                    return;
                }

                unsafeWindow.updateIntoArr(unsafeWindow.flshToArr(data, "DFSTATS_"), unsafeWindow.userVars);

                // Inventory <-> Storage
                if (!invStepItemType && destItemType)
                {
                    requestParams.action = "store";
                    requestParams.expected_itemtype = destItemType;
                    requestParams.itemnum = invStepSlotId;
                    requestParams.expected_itemtype2 = invStepItemType;
                    requestParams.itemnum2 = sourceSlotId;
                }
                else
                {
                    requestParams.action = "take";
                    requestParams.expected_itemtype = invStepItemType;
                    requestParams.itemnum = sourceSlotId;
                    requestParams.expected_itemtype2 = destItemType;
                    requestParams.itemnum2 = invStepSlotId;
                }

                makeRequest("inventory_new", requestParams, reloadStorage);
            });
        });

        return true;
    },
    handleBackpackToInventoryInStorage(requestParams, itemSlots)
    {
        unsafeWindow.playSound("swap");

        const sourceItemType = itemSlots[0][1];
        const sourceInvType = itemSlots[0][2];
        const sourceSlotId = getActualSlotId(itemSlots[0][0], sourceInvType);

        const destItemType = itemSlots[1][1];
        const destInvType = itemSlots[1][2];
        const destSlotId = getActualSlotId(itemSlots[1][0], destInvType);

        requestParams.action = "backpack";
        requestParams.expected_itemtype = sourceItemType;
        requestParams.itemnum = sourceSlotId;
        requestParams.expected_itemtype2 = destItemType;
        requestParams.itemnum2 = destSlotId;

        makeRequest("hotrods/backpack", requestParams, reloadStorage);

        return true;
    }
};

const transactionRules = [
    {
        name: "Backpack -> Storage (One-Way)",
        condition: (itemSlots) => {
            const sourceInvType = itemSlots[0][2];
            const destInvType = itemSlots[1][2];
            return sourceInvType === "backpackdisplay" && destInvType === "storage";
        },
        onConditionSuccess: internalTransactionHandler.handleBackpackToStorageOneWay
    },
    {
        name: "Storage -> Backpack (One-Way)",
        condition: (itemSlots) => {
            const sourceInvType = itemSlots[0][2];
            const destInvType = itemSlots[1][2];
            return sourceInvType === "storage" && destInvType === "backpackdisplay";
        },
        onConditionSuccess: internalTransactionHandler.handleStorageToBackpackOneWay
    },
    {
        name: "Backpack <-> Inventory (Storage page)",
        condition: (itemSlots) => {
            const sourceInvType = itemSlots[0][2];
            const destInvType = itemSlots[1][2];
            return document.getElementById("storage") && (sourceInvType === "backpackdisplay" || destInvType === "backpackdisplay");
        },
        onConditionSuccess: internalTransactionHandler.handleBackpackToInventoryInStorage
    }
];

function executeInventoryTransaction(requestParams, itemSlots)
{
    const matchedRule = transactionRules.find(rule => rule.condition(itemSlots));
    let success = matchedRule?.onConditionSuccess(requestParams, itemSlots) ?? false;
    return success;
}