-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfinite craft auto crafter.js
156 lines (127 loc) · 5.31 KB
/
infinite craft auto crafter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
let lastClearButtonClick = 0;
const DRAG_STEPS = 10;
const CLEAR_BUTTON_DELAY = 5500;
const THREAD_COUNT = 8;
const TARGET_COORDINATES = [
{ x: 300, y: 300 },
];
const OFFSET_VALUES = [
{ x: 50, y: 50 },
{ x: 100, y: 100 },
{ x: 150, y: 150 },
{ x: 200, y: 200 },
];
function simulateDragAndDrop(element, startX, startY, targetX, targetY, steps = DRAG_STEPS) {
function triggerMouseEvent(target, eventType, clientX, clientY) {
const event = new MouseEvent(eventType, {
bubbles: true,
cancelable: true,
clientX,
clientY,
view: window,
});
target.dispatchEvent(event);
}
console.log(`Dragging from (${startX}, ${startY}) to (${targetX}, ${targetY})`);
triggerMouseEvent(element, "mousedown", startX, startY);
const deltaX = (targetX - startX) / steps;
const deltaY = (targetY - startY) / steps;
let currentX = startX;
let currentY = startY;
return new Promise((resolve) => {
function moveMouse() {
currentX += deltaX;
currentY += deltaY;
triggerMouseEvent(document, "mousemove", currentX, currentY);
if (Math.abs(currentX - targetX) < Math.abs(deltaX) && Math.abs(currentY - targetY) < Math.abs(deltaY)) {
triggerMouseEvent(document, "mouseup", targetX, targetY);
console.log("Drag-and-drop completed.");
element.style.position = "absolute";
element.style.left = `${targetX}px`;
element.style.top = `${targetY}px`;
resolve();
} else {
requestAnimationFrame(moveMouse);
}
}
requestAnimationFrame(moveMouse);
});
}
function saveProcessedPairs(processedPairs) {
localStorage.setItem("processedPairs", JSON.stringify([...processedPairs]));
}
async function clickClearButtonNonBlocking() {
const clearBtn = document.querySelector(".clear");
if (clearBtn) {
const now = Date.now();
if (now - lastClearButtonClick < CLEAR_BUTTON_DELAY) {
console.log("Clear button click throttled.");
return;
}
console.log("Waiting before clicking the clear button...");
lastClearButtonClick = now;
setTimeout(() => {
clearBtn.click();
console.log("Clear button clicked.");
}, CLEAR_BUTTON_DELAY);
} else {
console.warn("Clear button not found.");
}
}
async function processCombination(firstItem, secondItem, target1X, target1Y, target2X, target2Y, offsetIndex) {
const firstRect = firstItem.getBoundingClientRect();
const secondRect = secondItem.getBoundingClientRect();
const firstStartX = firstRect.x + firstRect.width / 2;
const firstStartY = firstRect.y + firstRect.height / 2;
const secondStartX = secondRect.x + secondRect.width / 2;
const secondStartY = secondRect.y + secondRect.height / 2;
const offset = OFFSET_VALUES[offsetIndex];
await simulateDragAndDrop(firstItem, firstStartX, firstStartY, target1X + offset.x, target1Y + offset.y);
await simulateDragAndDrop(secondItem, secondStartX, secondStartY, target2X + offset.x, target2Y + offset.y);
await clickClearButtonNonBlocking();
}
async function processItems(itemsRow, processedPairs, threadIndex) {
const items = [...itemsRow.getElementsByClassName("item")];
console.log(`Found ${items.length} items in row.`);
const target = TARGET_COORDINATES[threadIndex];
let offsetIndex = 0;
for (let i = 0; i < items.length; i++) {
for (let j = i + 1; j < items.length; j++) {
const pairKey = `${i}-${j}`;
if (!processedPairs.has(pairKey)) {
processedPairs.add(pairKey);
saveProcessedPairs(processedPairs);
console.log(`Processing combination: ${pairKey}`);
await processCombination(
items[i],
items[j],
target.x,
target.y,
target.x,
target.y,
offsetIndex
);
offsetIndex = (offsetIndex + 1) % OFFSET_VALUES.length;
}
}
}
}
async function processThread(threadIndex, itemsRows, processedPairs) {
const rowSubset = itemsRows.filter((_, index) => index % THREAD_COUNT === threadIndex);
for (const row of rowSubset) {
console.log(`Thread ${threadIndex}: Processing row:`, row);
await processItems(row, processedPairs, threadIndex);
}
console.log(`Thread ${threadIndex}: Processing completed.`);
}
async function Run() {
const processedPairs = new Set(JSON.parse(localStorage.getItem("processedPairs") || "[]"));
const itemsRows = [...document.querySelectorAll(".items-inner")];
console.log(`Found ${itemsRows.length} item rows.`);
const threadPromises = Array.from({ length: THREAD_COUNT }, (_, index) =>
processThread(index, itemsRows, processedPairs)
);
await Promise.all(threadPromises);
console.log("All threads completed.");
}
Run().catch((error) => console.error("An error occurred during the Run:", error));