-
Notifications
You must be signed in to change notification settings - Fork 0
/
Highscore.py
203 lines (164 loc) · 6.86 KB
/
Highscore.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import pathlib
import customtkinter as ctk
import threading
import time
CHROMEDRIVER_PATH = r"C:\\Users\\anmel\\OneDrive\\Desktop\\chromedriver-win32\\chromedriver.exe"
WEBSITE_PATH = "https://www.whatbeatsrock.com"
DIR_PATH = pathlib.Path().resolve()
choices = ["paper", "scissor", "rock"]
threads = []
app = ctk.CTk()
app.geometry("600x400")
app.title("Terminal")
output_textbox = ctk.CTkTextbox(app, width=600, height=350)
output_textbox.pack(pady=0, padx=5)
input_entry = ctk.CTkEntry(app, width=600, height=40, placeholder_text="Console: ")
input_entry.pack(pady=0, padx=5)
input_entry.bind("<Return>", lambda event: on_enter_press())
output_textbox.tag_config("info", foreground="black")
output_textbox.tag_config("success", foreground="green")
output_textbox.tag_config("correct", foreground="#00FF00")
output_textbox.tag_config("warning", foreground="orange")
output_textbox.tag_config("error", foreground="red")
output_textbox.tag_config("note", foreground="#2b55e2")
output_textbox.tag_config("note2", foreground="#b82be2")
def remove_duplicates(list):
clean_list = []
for i in list:
if i not in clean_list:
clean_list.append(i)
return clean_list
user_input_comit = threading.Event()
def on_enter_press():
user_input_comit.set()
def log_message(msg, id, tag="info", end="\n"):
output_textbox.insert("end", "Instance "+ str(id) + ": " + msg + end, tag)
output_textbox.see("end")
def main(id):
id += 1
with open(DIR_PATH.joinpath("Names.txt"), "r") as f:
content = [i.strip("\n") for i in f.readlines()]
content = [i.strip(" ") for i in content]
names = []
for i in content:
names.append(f"A Person named {i} plays Paper")
names.append(f"A Person named {i} plays Scissor")
names.append(f"A Person named {i} plays Rock")
log_message("Word List loaded", id, tag="success")
for i in names:
print(i)
service = Service(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service)
log_message("Chrome Driver initialized", id, tag="success")
driver.get(WEBSITE_PATH)
log_message("Website loaded", id, tag="success")
log_message("Starting to insert words", id, tag="warning")
prev_word = "Rock"
score = 0
for index, name in enumerate(names):
pause = False
if user_input_comit.is_set():
pause = True
while pause:
user_input_comit.clear()
user_input = input_entry.get()
input_entry.delete(0, ctk.END)
log_message(f"Captured user Input: '{user_input}'", id, tag="note")
if user_input == "pause":
log_message(f"Pausing Script ...", id, tag="warning")
log_message(f"Type 'continue' to continue", id)
while not user_input_comit.is_set():
time.sleep(0.5)
user_input = input_entry.get()
user_input_comit.clear()
log_message(f"Captured user Input: '{user_input}'", id, tag="note")
input_entry.delete(0, ctk.END)
if user_input == "continue":
log_message("Continuingt the script ...", id, tag="warning")
pause = False
break
elif user_input == "quit":
log_message("Quitting programm and all drivers ...", id, tag="error")
time.sleep(1)
driver.quit()
app.quit()
else:
pause = False
break
input_field = driver.find_element(By.CSS_SELECTOR, ".pl-4.py-4.text-lg.border.border-1-black")
input_field.clear()
input_field.send_keys(name)
input_field.send_keys(Keys.RETURN)
alert = None
try:
time.sleep(0.5)
alert = driver.switch_to.alert
alert_text = alert.text
alert.accept()
except NoAlertPresentException:
pass
while alert:
user_input_comit.clear()
log_message(f"Alert: {alert_text}", id, tag="error")
log_message(f"Score: {score}", id)
log_message(f"To try to continue the programm type 'continue'", id)
log_message(f"To quit type anything", id)
while not user_input_comit.is_set():
time.sleep(0.5)
user_input = input_entry.get()
log_message(f"Captured user Input: '{user_input}'", id, tag="note")
input_entry.delete(0, ctk.END)
if user_input == "continue":
log_message("Continuing the programm", id, tag="warning")
input_field.send_keys(Keys.RETURN)
try:
time.sleep(0.5)
alert = driver.switch_to.alert
alert_text = alert.text
alert.accept()
except NoAlertPresentException:
alert = None
else:
log_message("Quitting programm and all drivers ...", id, tag="error")
time.sleep(1)
driver.quit()
app.quit()
correct = None
while True:
try:
button_field = driver.find_element(By.CSS_SELECTOR, ".py-4.px-8.border.border-1-black.text-lg")
correct = True
break
except selenium.common.exceptions.NoSuchElementException:
pass
try:
button_field = driver.find_element(By.CSS_SELECTOR, ".p-2.px-4.border.border-1-black.text-lg")
correct = False
break
except selenium.common.exceptions.NoSuchElementException:
pass
time.sleep(0.5)
if correct:
prev_word = name
score += 1
button_field.send_keys(Keys.RETURN)
else:
log_message(f"{name} does not beat {prev_word}", id, tag="error")
score = driver.find_element(By.CSS_SELECTOR, "p.text-gray-500").text.split(": ")[1]
log_message(f"Score: {score}", id)
input()
driver.quit()
log_message("Ran out of Words", id, tag="warning")
log_message(f"Score: {score}", id)
for i in range(1):
thread = threading.Thread(target=main, args=(i,), daemon=True)
threads.append(thread)
thread.start()
app.mainloop()