0% found this document useful (0 votes)
10 views1 page

Infinitecrafter

The document contains a JavaScript code that simulates drag-and-drop functionality for processing item pairs in a web application. It tracks processed pairs using localStorage, and continuously processes combinations of items in rows through an infinite loop. The code includes functions for saving processed pairs, simulating mouse events, and handling the drag-and-drop logic for items on the screen.

Uploaded by

dakssh.bhambre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Infinitecrafter

The document contains a JavaScript code that simulates drag-and-drop functionality for processing item pairs in a web application. It tracks processed pairs using localStorage, and continuously processes combinations of items in rows through an infinite loop. The code includes functions for saving processed pairs, simulating mouse events, and handling the drag-and-drop logic for items on the screen.

Uploaded by

dakssh.bhambre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Initialize processedPairs globally to track processed item pairsconst

processedPairs = new Set( JSON.parse(localStorage.getItem("processedPairs") ||


"[]"));// Function to save processed pairs to localStoragefunction
saveProcessedPairs() { localStorage.setItem("processedPairs",
JSON.stringify(Array.from(processedPairs)));}// Function to simulate drag-and-drop
async function simulateDragAndDrop(element, startX, startY, targetX, targetY, steps
= 10) { // Helper function to trigger mouse events function
triggerMouseEvent(target, eventType, clientX, clientY) { const event = new
MouseEvent(eventType, { bubbles: true, cancelable: true,
clientX, clientY, view: window, });
target.dispatchEvent(event); } console.log(`Start: (${startX}, ${startY}),
Target: (${targetX}, ${targetY})`); // Start dragging
triggerMouseEvent(element, "mousedown", startX, startY); // Gradual movement
let currentX = startX; let currentY = startY; const deltaX = (targetX -
startX) / steps; const deltaY = (targetY - startY) / steps; 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)
) { // Drop the item at the target
triggerMouseEvent(document, "mouseup", targetX, targetY);
console.log("Drag-and-drop completed."); // Force final alignment
element.style.position = "absolute"; element.style.left = `$
{targetX}px`; element.style.top = `${targetY}px`;
resolve(); } else { requestAnimationFrame(moveMouse);
} } moveMouse(); });}// Process combination of two itemsasync
function processCombination(firstItem, secondItem, targetX, targetY) { 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; await simulateDragAndDrop(firstItem,
firstStartX, firstStartY, targetX, targetY); await
simulateDragAndDrop(secondItem, secondStartX, secondStartY, targetX, targetY);
await clickClearButton();}// Process all items in a rowasync function
processItems(itemsRow) { const items = itemsRow.getElementsByClassName("item");
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(); await processCombination(items[i], items[j],
500, 100); // Fixed target position } } }}// Infinite loop to
process all rows continuouslyasync function startInfiniteLoop() { while (true) {
try { const itemsRows = document.getElementsByClassName("items-row");
for (const row of itemsRows) { await processItems(row); }
console.log("Cycle completed. Waiting for new items..."); } catch (error) {
console.error("Error in infinite loop:", error); } // Wait for 1
second before the next cycle await new Promise((resolve) =>
setTimeout(resolve, 200)); }}// Start the infinite loopstartInfiniteLoop();

You might also like