This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
208 lines (188 loc) · 6.34 KB
/
main.js
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
204
205
206
207
208
const puppeteer = require("puppeteer");
const cluster = require("cluster");
const sqlite3 = require("sqlite3");
if (cluster.isMaster) {
let db = new sqlite3.Database('words_en.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to SQLite Database.');
});
let que = [];
function addQue(content) {
for (let i = 0; i < content.length; i++) {
que.push(content[i]);
}
}
async function processQue() {
if (que[0] !== "" && que[0] !== undefined) {
writeWord(que[0]);
que.splice(0, 1);
}
setTimeout(function () {
processQue();
}, 250);
}
processQue();
function writeWord(word) {
db.all("SELECT * FROM words", [], function () {
db.run("INSERT INTO words VALUES (?)", [word], function (err) {
if (err) {
//console.log("Couldnt add " + word);
} else {
console.log("Added Word: " + word);
}
});
});
}
function messageHandler(worker, message) {
if (message.cmd === "setWord") {
addQue(message.content);
}
}
cluster.fork();
cluster.fork();
cluster.on("message", messageHandler);
cluster.on('exit', function () {
cluster.fork();
});
} else if (cluster.isWorker) {
const USERNAME_SELECTOR = "#inputName";
const LANGUAGE_SELECTOR = "#loginLanguage";
const LANGUAGE = "English";
const START_ROOM = "buttonLoginCreatePrivate";
const ROUND_CNT = "#lobbySetRounds";
const INVITE_LINK = "invite";
const START_GAME = "formLogin";
const CHAT = "#inputChat";
const BROWSER_CONF = {headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']};
let slaveReady = false;
let masterPage;
let masterBrowser;
let slavePage;
let slaveBrowser;
let inviteLink;
let swap = false;
let id = 0;
let oldWord;
let firstrun = true;
let stopGameVar = false;
async function startSlave(result) {
inviteLink = result;
slaveBrowser = await puppeteer.launch(BROWSER_CONF);
slavePage = await slaveBrowser.pages();
slavePage = slavePage[0];
await slavePage.goto(result);
await slavePage.click(USERNAME_SELECTOR);
await slavePage.keyboard.type("Bot_Slave");
await slavePage.select(LANGUAGE_SELECTOR, LANGUAGE);
let waitTill = new Date(new Date().getTime() + 1.5 * 1000);
while (waitTill > new Date()) {
}
await slavePage.evaluate(function (START_GAME) {
let form = document.getElementById(START_GAME);
let btn = form.children[form.children.length - 1];
btn.click();
}, START_GAME);
await slavePage.waitFor(1000);
}
async function main() {
masterBrowser = await puppeteer.launch(BROWSER_CONF);
masterPage = await masterBrowser.pages();
masterPage = masterPage[0];
await masterPage.goto("https://skribbl.io/");
await masterPage.click(USERNAME_SELECTOR);
await masterPage.keyboard.type("Bot_Master");
await masterPage.select(LANGUAGE_SELECTOR, LANGUAGE);
let waitTill = new Date(new Date().getTime() + 1.5 * 1000);
while (waitTill > new Date()) {
}
await masterPage.evaluate(function (START_ROOM) {
document.getElementById(START_ROOM).click();
}, START_ROOM);
await masterPage.waitFor(1000);
await masterPage.evaluate(function (INVITE_LINK) {
return document.getElementById(INVITE_LINK).value;
}, INVITE_LINK).then(function (result) {
startSlave(result).then(function () {
slaveReady = true;
})
});
await masterPage.select(ROUND_CNT, "10");
await startGame();
}
async function handleWord(result) {
saveWords([result]);
let page;
if (swap) {
page = slavePage;
swap = false;
} else {
page = masterPage;
swap = true;
}
await page.waitFor(1200);
await page.click(CHAT);
await page.keyboard.type(result);
await page.keyboard.press("Enter");
await page.waitFor(4000);
}
function saveWords(words) {
process.send({cmd: "setWord", content: words});
}
async function wordSelector() {
id++;
console.log("JOB ID: " + id);
let page;
let word = "";
if (swap) {
page = masterPage;
} else {
page = slavePage;
}
await page.waitFor(2000);
await page.evaluate(function () {
return document.getElementById("screenGame").style.display === "none";
}).then(function (result) {
stopGameVar = result
});
if (stopGameVar)
return;
if (!firstrun)
await page.waitForFunction("document.getElementsByClassName('word').length === 3 && document.getElementsByClassName('word')[1] === \"" + oldWord + "\"");
else
await page.waitForFunction("document.getElementsByClassName('word').length === 3");
await page.evaluate(function () {
let wordsArr = [];
wordsArr[0] = document.getElementsByClassName("word")[0].innerText;
wordsArr[1] = document.getElementsByClassName("word")[1].innerText;
wordsArr[2] = document.getElementsByClassName("word")[2].innerText;
document.getElementsByClassName("word")[1].click();
return wordsArr;
}).then(function (result) {
saveWords(result);
word = result[1];
});
await handleWord(word);
//console.log("Handled Word: " + word);
oldWord = word;
await wordSelector();
}
async function startGame() {
if (slaveReady === false) {
setTimeout(startGame, 1000);
return;
}
await masterPage.evaluate(function () {
document.getElementById("buttonLobbyPlay").click();
});
console.log("Started Game with Invite Link: " + inviteLink);
await wordSelector();
id = 0;
await startGame();
}
process.on('unhandledRejection', function () {
process.exit();
});
main();
}