santit96 commited on
Commit
8430de7
·
1 Parent(s): 9b38408

Add selenium class to simulate the user in the rs wordle

Browse files

Change to class firebase conection
Refactor rs_wordle_player

play_rs_wordle.py DELETED
@@ -1,4 +0,0 @@
1
- from rs_wordle_player.rs_wordle_player import play
2
-
3
- if __name__ == '__main__':
4
- print(play())
 
 
 
 
 
requirements.txt CHANGED
@@ -1,3 +1,6 @@
 
 
1
  gym
2
  matplotlib
 
3
  torch
 
1
+ dotenv
2
+ firebase-admin
3
  gym
4
  matplotlib
5
+ selenium
6
  torch
rs_wordle_player/firebase_connector.py CHANGED
@@ -6,55 +6,54 @@ from datetime import datetime
6
  from dotenv import load_dotenv
7
 
8
 
9
- def init_fb_app():
10
- cert_path = get_credentials_path()
11
- cred = credentials.Certificate(cert_path)
12
- firebase_admin.initialize_app(cred)
13
-
14
-
15
- def connect_to_db():
16
- db = firestore.client()
17
- return db
18
-
19
-
20
- def get_credentials_path():
21
- load_dotenv()
22
- credentials_path = os.getenv('FIREBASE_CREDENTIALS_PATH')
23
- return credentials_path
24
-
25
-
26
- def get_user():
27
- load_dotenv()
28
- user = os.getenv('RS_WORDLE_USER')
29
- return user
30
-
31
-
32
- def today():
33
- return datetime.today().strftime('%Y%m%d')
34
-
35
-
36
- def today_user_results():
37
- db = connect_to_db()
38
- daily_results_col = 'dailyResults'
39
- currentUser = get_user()
40
- # Execute the query and get the first result
41
- docs = db.collection(daily_results_col).where(
42
- 'user.email', '==', currentUser).where(
43
- 'date', '==', today()).limit(1).get()
44
- return docs
45
-
46
-
47
- def today_user_attempts():
48
- docs = today_user_results()
49
- attempted_words = []
50
- if len(docs) > 0:
51
- doc = docs[0]
52
- attempted_words = doc.to_dict().get('attemptedWords')
53
- return attempted_words
54
-
55
-
56
- def today_word():
57
- words_col = 'words'
58
- db = connect_to_db()
59
- doc = db.collection(words_col).document(today())
60
- return doc.get().get('word')
 
6
  from dotenv import load_dotenv
7
 
8
 
9
+ class FirebaseConnector():
10
+
11
+ def __init__(self):
12
+ load_dotenv()
13
+ cert_path = self.get_credentials_path()
14
+ cred = credentials.Certificate(cert_path)
15
+ firebase_admin.initialize_app(cred)
16
+ self.db = self.get_db()
17
+
18
+ def get_db(self):
19
+ db = firestore.client()
20
+ return db
21
+
22
+ def get_credentials_path(self):
23
+ credentials_path = os.getenv('FIREBASE_CREDENTIALS_PATH')
24
+ return credentials_path
25
+
26
+ def get_user(self):
27
+ user = os.getenv('RS_WORDLE_USER')
28
+ return user
29
+
30
+ def get_state_from_fb_result(self, firebase_result):
31
+ result_number_map = {'incorrect': '0',
32
+ 'misplaced': '1',
33
+ 'correct': '2'}
34
+ return ''.join(map(lambda char_res: result_number_map[char_res], firebase_result))
35
+
36
+ def today(self):
37
+ return datetime.today().strftime('%Y%m%d')
38
+
39
+ def today_user_results(self):
40
+ daily_results_col = 'dailyResults'
41
+ currentUser = self.get_user()
42
+ # Execute the query and get the first result
43
+ docs = self.db.collection(daily_results_col).where(
44
+ 'user.email', '==', currentUser).where(
45
+ 'date', '==', self.today()).limit(1).get()
46
+ return docs
47
+
48
+ def today_user_attempts(self):
49
+ docs = self.today_user_results()
50
+ attempted_words = []
51
+ if len(docs) > 0:
52
+ doc = docs[0]
53
+ attempted_words = doc.to_dict().get('attemptedWords')
54
+ return attempted_words
55
+
56
+ def today_word(self):
57
+ words_col = 'words'
58
+ doc = self.db.collection(words_col).document(self.today())
59
+ return doc.get().get('word')
 
rs_wordle_player/rs_wordle_player.py CHANGED
@@ -1,53 +1,62 @@
1
  import gym
2
  import os
3
  from a3c.play import suggest
4
- from .firebase_connector import init_fb_app, today_user_attempts, today_word
 
 
5
 
6
 
7
- def get_state_from_fb_result(firebase_result):
8
- result_number_map = {'incorrect': '0',
9
- 'misplaced': '1',
10
- 'correct': '2'}
11
- return ''.join(map(lambda char_res: result_number_map[char_res], firebase_result))
 
12
 
 
 
 
13
 
14
- def get_attempts():
15
- attempts = today_user_attempts()
 
16
  words = []
17
  results = []
18
  for attempt in attempts:
19
  word, result = attempt.values()
20
  words.append(word)
21
- results.append(get_state_from_fb_result(result))
22
  return words, results
23
 
24
 
25
- def play_word(word):
26
- # Makes an attemp to rs wordle
27
- # TO DO
28
- pass
 
 
 
 
 
 
 
 
29
 
30
 
31
  def play():
32
- init_fb_app()
33
- pretrained_model_name = 'model_95%_one_hot.pth'
34
- env_id = 'WordleEnvFull-v0'
35
- env = gym.make(env_id)
36
- model_checkpoint_dir = os.path.join('checkpoints', 'best_models')
37
- pretrained_model_path = os.path.join(model_checkpoint_dir, pretrained_model_name)
38
- goal_word = today_word()
39
  if goal_word and len(goal_word) == 5:
40
- words = []
41
- states = []
42
- finished = False
43
- attempts = 0
44
- while not finished or attempts == 6:
45
- attempts += 1
46
- new_attempt = suggest(env, words, states, pretrained_model_path)
47
- play_word(new_attempt)
48
- words, states = get_attempts()
49
- finished = states[-1] == '22222' or len(states) == 6
50
  else:
51
  print("Can't play, today's word is not 5 characters long")
52
- new_attempt = None
53
- return new_attempt
 
 
 
 
 
 
1
  import gym
2
  import os
3
  from a3c.play import suggest
4
+ from dotenv import load_dotenv
5
+ from .firebase_connector import FirebaseConnector
6
+ from .selenium_player import SeleniumPlayer
7
 
8
 
9
+ def get_model_path():
10
+ load_dotenv()
11
+ model_name = os.getenv('RS_WORDLE_MODEL_NAME')
12
+ model_checkpoint_dir = os.path.join('checkpoints', 'best_models')
13
+ return os.path.join(model_checkpoint_dir, model_name)
14
+
15
 
16
+ def get_env():
17
+ env_id = 'WordleEnvFull-v0'
18
+ return gym.make(env_id)
19
 
20
+
21
+ def get_attempts(fb_connector):
22
+ attempts = fb_connector.today_user_attempts()
23
  words = []
24
  results = []
25
  for attempt in attempts:
26
  word, result = attempt.values()
27
  words.append(word)
28
+ results.append(fb_connector.get_state_from_fb_result(result))
29
  return words, results
30
 
31
 
32
+ def play_game(player, fb_connector, env, model_path):
33
+ words = []
34
+ states = []
35
+ finished = False
36
+ attempts = 0
37
+ while not finished or attempts == 6:
38
+ attempts += 1
39
+ new_attempt = suggest(env, words, states, model_path)
40
+ player.play_word(new_attempt)
41
+ words, states = get_attempts(fb_connector)
42
+ finished = states[-1] == '22222'
43
+ return states, finished
44
 
45
 
46
  def play():
47
+ fb = FirebaseConnector()
48
+ player = SeleniumPlayer()
49
+ model_path = get_model_path()
50
+ env = get_env()
51
+ goal_word = fb.today_word()
 
 
52
  if goal_word and len(goal_word) == 5:
53
+ states, won = play_game(player, fb, env, model_path)
 
 
 
 
 
 
 
 
 
54
  else:
55
  print("Can't play, today's word is not 5 characters long")
56
+ states, won = None, None
57
+ player.finish()
58
+ return states, won
59
+
60
+
61
+ if __name__ == '__main__':
62
+ print(play())
rs_wordle_player/selenium_player.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from selenium import webdriver
3
+ from dotenv import load_dotenv
4
+ from selenium.webdriver.common.keys import Keys
5
+
6
+
7
+ class SeleniumPlayer():
8
+
9
+ def __init__(self):
10
+ self.driver = self.get_driver()
11
+
12
+ def get_driver(self):
13
+ driver = webdriver.Chrome()
14
+ driver.get(self.get_wordle_url())
15
+ return driver
16
+
17
+ def get_wordle_url(self):
18
+ load_dotenv()
19
+ return os.getenv('RS_WORDLE_URL')
20
+
21
+ def play_word(self, word):
22
+ element = self.driver.find_element_by_name('html')
23
+ # simulate typing the letters in the word into the input field
24
+ element.send_keys(word)
25
+ # simulate pressing the Enter key
26
+ element.send_keys(Keys.ENTER)
27
+
28
+ def finish(self):
29
+ self.driver.quit()