-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpress.js
More file actions
92 lines (74 loc) · 2.92 KB
/
Copy pathexpress.js
File metadata and controls
92 lines (74 loc) · 2.92 KB
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
var express = require('express');
var bodyParser = require('body-parser');
const { response } = require('express');
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./words.db');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
var port = 9000;
let sql = `select * from "5letterwords" where `;
app.post('/wordle-recommender', function(req, res) {
finalQuery = [];
// add correct letters
correct = req.body.correct;
if (correct.length > 0) {
correctString = [];
correct.forEach(correctElement => {
correctString.push(`letter${correctElement.position}="${correctElement.letter}"`);
});
correctQuery = correctString.join(" and ")
finalQuery.push(correctQuery);
}
// add incorrect letters
incorrect = req.body.incorrect;
if (incorrect.length > 0) {
incorrectString = "";
incorrect.forEach(incorrectElement => {
incorrectString += `letter${incorrectElement.position}!="${incorrectElement.letter}" and `;
});
incorrectString += getNotInQuery(incorrect);
finalQuery.push(incorrectString);
}
// add not available letters
notAvailable = req.body.notIn;
if (notAvailable.length > 0) {
finalQuery.push(`letter1 not in (${JSON.stringify(notAvailable).replace("[", "").replace("]", "")})`);
finalQuery.push(`letter2 not in (${JSON.stringify(notAvailable).replace("[", "").replace("]", "")})`);
finalQuery.push(`letter3 not in (${JSON.stringify(notAvailable).replace("[", "").replace("]", "")})`);
finalQuery.push(`letter4 not in (${JSON.stringify(notAvailable).replace("[", "").replace("]", "")})`);
finalQuery.push(`letter5 not in (${JSON.stringify(notAvailable).replace("[", "").replace("]", "")})`)
}
finalQueryString = sql + finalQuery.join(" and ");
responseValue = [];
db.all(finalQueryString, [], (err, rows) => {
rows.forEach(row => {
responseValue.push(getWord(row).toUpperCase());
})
res.send(responseValue);
})
});
// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);
function getNotInQuery(incorrectPositions) {
s = "("
notInCondition = [];
incorrectPositions.forEach(targetLetter => {
notInCondition.push(GetNotInCondition(targetLetter.letter, targetLetter.position));
});
s += notInCondition.join(" and ")
return s += ")";
}
function GetNotInCondition(letter, position) {
queryParams = [];
[1,2,3,4,5].forEach(current => {
if (current != position) {
queryParams.push(`letter${current}="${letter}"`)
}
})
return "(" + queryParams.join(" or ") + ")";
}
function getWord(assumedWord) {
return assumedWord['letter1'] + assumedWord['letter2'] + assumedWord['letter3'] + assumedWord['letter4'] + assumedWord['letter5'];
}