Spaces:
Sleeping
Sleeping
Add connection to firebase and method to get user attempts from firestore
Browse filescreate play method to make a model play rs version alone
Play word method still not done
- .gitignore +3 -0
- play_rs_wordle.py +4 -0
- rs_wordle_player/__init__.py +0 -0
- rs_wordle_player/firebase_connector.py +60 -0
- rs_wordle_player/rs_wordle_player.py +53 -0
.gitignore
CHANGED
@@ -122,3 +122,6 @@ GitHub.sublime-settings
|
|
122 |
*.png
|
123 |
*.jpg
|
124 |
*.jpeg
|
|
|
|
|
|
|
|
122 |
*.png
|
123 |
*.jpg
|
124 |
*.jpeg
|
125 |
+
|
126 |
+
# Json files (used for firebase certs and others)
|
127 |
+
*.json
|
play_rs_wordle.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from rs_wordle_player.rs_wordle_player import play
|
2 |
+
|
3 |
+
if __name__ == '__main__':
|
4 |
+
print(play())
|
rs_wordle_player/__init__.py
ADDED
File without changes
|
rs_wordle_player/firebase_connector.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import firebase_admin
|
3 |
+
from firebase_admin import credentials
|
4 |
+
from firebase_admin import firestore
|
5 |
+
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')
|
rs_wordle_player/rs_wordle_player.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|