OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

a timer within a timer

  • Thread starter Thread starter DoesNotMatter
  • Start date Start date
D

DoesNotMatter

Guest
hello and thank you in advance, i'm currently coding The Cookie Cutter game from 100 Days of Code by Angela Yu, the goal is to get as many clicks as possible within the span of 5 mins , while checking every 5 secs for the money (i called it "score") i've accumulated so i can purchase the highest available upgrade.

this is Angela's solution on Github: i found it to be long and unnecessarily complicated.

my code is a bit messy as it's still a work in progress.

what i've been meaning to do with my code is to iterate through a directory that contains the #id and price of upgrade, and as soon as it recognizes a match for a possible purchase it clicks on it.

Code:
import threading
import time
from collections import defaultdict

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from threading import Thread
from time import sleep
from datetime import datetime, timedelta

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://orteil.dashnet.org/experiments/cookie/")
driver.maximize_window()

#######################ELEMENTS & CONVERTING TO LISTS\DICT#######################
cookie = driver.find_element(By.CSS_SELECTOR, '#cookie')
score = driver.find_element(By.CSS_SELECTOR, '#money')
all_prices = driver.find_elements(By.CSS_SELECTOR, '#store b')
all_ids = driver.find_elements(By.CSS_SELECTOR, '#store div')
new_all_prices = []
new_all_ids = []
for new_id in all_ids:
    new_all_ids.append(new_id.get_attribute("id"))
for price in all_prices:
    a, b, c = price.text.partition('-')
    new_price = c.replace(" ", "")
    if c == "":
        pass
    else:
        new_all_prices.append(c)

tuples = [(key, value)
          for i, (key, value) in enumerate(zip(new_all_prices, new_all_ids))]

myDict = dict(tuples)

#######################FUNCTIONS#######################

print(myDict)


def five_seconds_check():
    for key, value in myDict.items():
        if score >= key:
            my_clickable_purchase = driver.find_element(By.CSS_SELECTOR, f'{myDict[key]}')
            my_clickable_purchase.click()


timeout = time.time() + 5
five_min = time.time() + 60 * 5  # 1 minutes

while True:
    cookie.click()
    if time.time() > timeout:
        five_seconds_check()
    elif time.time() > five_min:
        break

the part i'm struggling with is how to write a timer within a timer - meaning, how do i get the overall while loop to run for 5 mins, and simultaneously check the money\purchase upgrade every 5 secs.
<p>hello and thank you in advance,
i'm currently coding The Cookie Cutter game from 100 Days of Code by Angela Yu,
the goal is to get as many clicks as possible within the span of 5 mins , while checking every 5 secs for the money (i called it "score") i've accumulated so i can purchase the highest available upgrade.</p>
<p><a href="https://gist.github.com/angelabauer/affb3ce61bc79ada90dea26052c27c2b" rel="nofollow noreferrer">this is Angela's solution on Github:</a>
i found it to be long and unnecessarily complicated.</p>
<p>my code is a bit messy as it's still a work in progress.</p>
<p>what i've been meaning to do with my code is to iterate through a directory that contains the #id and price of upgrade, and as soon as it recognizes a match for a possible purchase it clicks on it.</p>
<pre><code>import threading
import time
from collections import defaultdict

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from threading import Thread
from time import sleep
from datetime import datetime, timedelta

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://orteil.dashnet.org/experiments/cookie/")
driver.maximize_window()

#######################ELEMENTS & CONVERTING TO LISTS\DICT#######################
cookie = driver.find_element(By.CSS_SELECTOR, '#cookie')
score = driver.find_element(By.CSS_SELECTOR, '#money')
all_prices = driver.find_elements(By.CSS_SELECTOR, '#store b')
all_ids = driver.find_elements(By.CSS_SELECTOR, '#store div')
new_all_prices = []
new_all_ids = []
for new_id in all_ids:
new_all_ids.append(new_id.get_attribute("id"))
for price in all_prices:
a, b, c = price.text.partition('-')
new_price = c.replace(" ", "")
if c == "":
pass
else:
new_all_prices.append(c)

tuples = [(key, value)
for i, (key, value) in enumerate(zip(new_all_prices, new_all_ids))]

myDict = dict(tuples)

#######################FUNCTIONS#######################

print(myDict)


def five_seconds_check():
for key, value in myDict.items():
if score >= key:
my_clickable_purchase = driver.find_element(By.CSS_SELECTOR, f'{myDict[key]}')
my_clickable_purchase.click()


timeout = time.time() + 5
five_min = time.time() + 60 * 5 # 1 minutes

while True:
cookie.click()
if time.time() > timeout:
five_seconds_check()
elif time.time() > five_min:
break
</code></pre>
<p>the part i'm struggling with is how to write a timer within a timer - meaning, how do i get the overall while loop to run for 5 mins, and simultaneously check the money\purchase upgrade every 5 secs.</p>
 

Latest posts

A
Replies
0
Views
1
Adrian-Mihai Enache
A
Top