DF Backpack Transaction Handler

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

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greatest.deepsurf.us/scripts/579722/1834455/DF%20Backpack%20Transaction%20Handler.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

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;
}