-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (52 loc) · 1.71 KB
/
server.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
// Requires.
const express = require('express');
const {promises: fs} = require("fs");
const path = require('path');
// App.
const app = express();
const port = process.env.PORT || 8080;
async function main() {
let dict_array = JSON.parse(await fs.readFile(path.resolve(__dirname, "res", "dict.json"), "utf8"));
let dict_set = new Set(dict_array);
// Request handler.
app.get("/", (req, res) => {
if (req.query.id === undefined) {
res.sendFile(path.resolve(__dirname, "res", "index.html"));
return;
}
let wordId = req.query.id % dict_array.length;
let word = dict_array[wordId];
let wordTry = req.query.word;
if (wordTry === undefined) {
console.log(`New connection: ${word}`);
res.json({len: word.length, firstLetter: word[0]});
return;
}
if (wordTry.length !== word.length) {
res.json({error: "Longueur incorrecte"});
return;
} else if (!dict_set.has(wordTry)) {
res.json({error: "Mot invalide"});
return;
}
let okLetters = [];
let misplaced = [];
for (let i = 0; i < word.length; i++) {
if (word[i] === wordTry[i]) {
okLetters.push(i);
} else if (word.includes(wordTry[i])) {
misplaced.push(i);
}
}
if (okLetters.length === word.length) {
res.json({win: "Gagné!"});
return;
}
res.json({okLetters: okLetters, misplaced: misplaced});
});
// Listener.
app.listen(port, () => {
console.log(`Server is up at http://localhost:${port}`)
});
}
main();