-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
189 lines (152 loc) · 4.5 KB
/
index.mjs
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
import { chromium } from "playwright";
import { addFakeTimers } from "./add-fake-timers.mjs";
import { suggestWord } from "./suggest-word.mjs";
async function wait(seconds) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}
/**
*
* @param {import('playwright-core').Page} page
*/
async function getPositions(page) {
let forbidden = await page
.locator("#keyboard [data-state='absent']")
.elementHandles();
const absent = await Promise.all(
forbidden.map((elementHandle) => elementHandle.getAttribute("data-key"))
);
const allRows = await page.locator("#board .row").elementHandles();
const correct = new Map();
const present = new Map();
const rows = [];
for (const row of allRows) {
const tiles = await row.$$(".tile");
for (const [tileIndex, tile] of tiles.entries()) {
const letter = await (await tile.innerText()).toLowerCase();
const state = await tile.getAttribute("data-state");
if (state === "empty") {
break;
}
if (state === "correct") {
const toSet = correct.get(letter) || [];
toSet.push(tileIndex);
correct.set(letter, toSet);
}
if (
state === "present" ||
// this happens when the same latter is already correct somewhere else
// if we don't add it here, it would retry the same word over and over
(state === "absent" && !absent.includes(letter))
) {
const toSet = present.get(letter) || [];
toSet.push(tileIndex);
present.set(letter, toSet);
}
}
}
return {
absent,
present,
correct,
};
}
/**
*
* @param {import('playwright-core').Page} page
*/
async function runTries(page, suggestion = "", bannedWords = [], counter = 0) {
const { absent, correct, present } = await getPositions(page);
suggestion = suggestion || suggestWord(absent, correct, present, bannedWords);
await enterWord(page, suggestion);
const worked = await wordWorked(page);
if (!worked) {
await eraseCurrentWord(page);
bannedWords.push(suggestion);
} else {
counter++;
}
const hasWon = await checkHasWon(page);
console.log({ hasWon });
// todo check if there's no more row to try instead of counting
if (counter >= 6 || hasWon) {
return;
}
return runTries(page, "", bannedWords, counter);
}
/**
*
* @param {import('playwright-core').Page} page
*/
async function enterWord(page, word) {
await page.keyboard.type(word);
await page.keyboard.press("Enter");
await wait(3);
}
/**
*
* @param {import('playwright-core').Page} page
*/
async function eraseCurrentWord(page) {
await page.keyboard.press("Backspace");
await page.keyboard.press("Backspace");
await page.keyboard.press("Backspace");
await page.keyboard.press("Backspace");
await page.keyboard.press("Backspace");
}
/**
*
* @param {import('playwright-core').Page} page
*/
async function wordWorked(page) {
const tiles = await page.locator("#board .tile").elementHandles();
for (const tile of tiles) {
const state = await tile.getAttribute("data-state");
if (state === "tbd") {
return false;
}
}
return true;
}
async function checkHasWon(page) {
const allRows = await page.locator("#board .row").elementHandles();
for (const row of allRows) {
let allCorrect = true;
const tiles = await row.$$(".tile");
for (const tile of tiles) {
const letter = await (await tile.innerText()).toLowerCase();
const state = await tile.getAttribute("data-state");
if (state !== "correct") {
allCorrect = false;
break;
}
}
if (allCorrect) {
return true;
}
}
return false;
}
export async function solveWordle() {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext(
process.env.RECORD_VIDEO ? { recordVideo: { dir: "videos/" } } : {}
);
let time = new Date().getTime();
if (process.env.DAYS) {
time += parseInt(process.env.DAYS, 10) * 86400 * 1000;
}
const page = await context.newPage();
const afterLoad = await addFakeTimers(page, time);
await page.goto("https://www.powerlanguage.co.uk/wordle/");
await afterLoad();
await page.locator(".close-icon").click();
// todo plan for the game not being won
await runTries(page, process.env.START_WORD?.toLocaleLowerCase() || "stare");
if (process.env.COPY_STATS) {
await page.locator("#share-button").click({ timeout: 10000 });
}
if (process.env.RECORD_VIDEO) {
await context.close();
await browser.close();
}
}