-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
48 lines (39 loc) · 1.14 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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PIERRE = "p";
const FEUILLE = "f";
const CISEAU = "c";
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use("/public", express.static(__dirname + "/public"));
app.use("/dist", express.static(__dirname + "/dist"));
app.get('/', (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post('/api/jouer', (req, res) => {
let coup = req.body.coup;
let coupOrdinateur;
let hasard = Math.floor(Math.random() * 3);
switch (hasard) {
case 0:
coupOrdinateur = PIERRE;
break;
case 1:
coupOrdinateur = FEUILLE;
break;
case 2:
coupOrdinateur = CISEAU;
break;
}
if ((coup === FEUILLE && coupOrdinateur === PIERRE) ||
(coup === CISEAU && coupOrdinateur === FEUILLE) ||
(coup === PIERRE && coupOrdinateur === CISEAU)) {
return res.send("GAGNE");
} else if (coup === coupOrdinateur) {
return res.send("NUL");
} else {
return res.send("PERDU");
}
});
app.listen(3000);