From 3855e2db5584e9289905facc4fa4d118f6ab9e7d Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Fri, 29 Nov 2024 20:43:33 +0530 Subject: [PATCH] Added a cookie clicker project --- .../Day-48/cookie_clicker.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Visual Studio Code Projects/Day-48/cookie_clicker.py diff --git a/Visual Studio Code Projects/Day-48/cookie_clicker.py b/Visual Studio Code Projects/Day-48/cookie_clicker.py new file mode 100644 index 0000000..75a8b4b --- /dev/null +++ b/Visual Studio Code Projects/Day-48/cookie_clicker.py @@ -0,0 +1,72 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + + +# Optional - Keep the browser open (helps diagnose issues if the script crashes) +chrome_options = webdriver.ChromeOptions() +chrome_options.add_experimental_option("detach", True) + +driver = webdriver.Chrome(options=chrome_options) + +driver.get("http://orteil.dashnet.org/experiments/cookie/") + +# Get cookie to click on. +cookie = driver.find_element(by=By.ID, value="cookie") + +# Get upgrade item ids. +items = driver.find_elements(by=By.CSS_SELECTOR, value="#store div") +item_ids = [item.get_attribute("id") for item in items] + +timeout = time.time() + 5 +five_min = time.time() + 60*5 # 5 minutes + +while True: + cookie.click() + + # Every 5 seconds: + if time.time() > timeout: + + # Get all upgrade tags + all_prices = driver.find_elements(by=By.CSS_SELECTOR, value="#store b") + item_prices = [] + + # Convert text into an integer price. + for price in all_prices: + element_text = price.text + if element_text != "": + cost = int(element_text.split("-")[1].strip().replace(",", "")) + item_prices.append(cost) + + # Create dictionary of store items and prices + cookie_upgrades = {} + for n in range(len(item_prices)): + cookie_upgrades[item_prices[n]] = item_ids[n] + + # Get current cookie count + money_element = driver.find_element(by=By.ID, value="money").text + if "," in money_element: + money_element = money_element.replace(",", "") + cookie_count = int(money_element) + + # Find upgrades that we can currently afford + affordable_upgrades = {} + for cost, id in cookie_upgrades.items(): + if cookie_count > cost: + affordable_upgrades[cost] = id + + # Purchase the most expensive affordable upgrade + highest_price_affordable_upgrade = max(affordable_upgrades) + print(highest_price_affordable_upgrade) + to_purchase_id = affordable_upgrades[highest_price_affordable_upgrade] + + driver.find_element(by=By.ID, value=to_purchase_id).click() + + # Add another 5 seconds until the next check + timeout = time.time() + 5 + + # After 5 minutes stop the bot and check the cookies per second count. + if time.time() > five_min: + cookie_per_s = driver.find_element(by=By.ID, value="cps").text + print(cookie_per_s) + break \ No newline at end of file