forked from jordanWTheDev/rock-paper-scissors-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (57 loc) · 1.74 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
65
66
67
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PORT = 8000;
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
let playerChoice
const Game = {
//Score count
wins: 0,
losses: 0,
ties: 0,
//Winning Pairs
pairs: {
rock: "scissors",
paper: "rock",
scissors: "paper",
},
//Uses random to pick hand -- can be omitted if we go multiplayer
randomChoice: () => {
let i = Math.floor(Math.random() * 3);
return (i = i === 0 ? "rock" : i === 1 ? "paper" : "scissors");
},
//This should run when the "play game" button is pressed
playGame: (player, bot) => {
console.log(`Player: ${player}`);
console.log(`Bot: ${bot}`);
//Stores a boolean for tie/win. we know if its not one of those then its a loss, so the logic is not needed
let tie = player === bot;
let win = Game.pairs[player] === bot;
//Counts the score
tie ? Game.ties++ : win ? Game.wins++ : Game.losses++;
//Returns a string for the results
return tie ? "TIE!" : win ? "WINNER WINNER CHICKEN DINNER!" : "LOSER";
},
};
//Renders the index.ejs as the home page
app.get("/", (req, res) => {
res.render("index.ejs", {"wins": Game.wins});
});
app.put('/play', (req, res) => {
playerChoice = req.body.id;
console.log("playing game")
Game.playGame(playerChoice, Game.randomChoice());
res.end()
})
app.get('/play', (req,res)=>{
console.log('res.json')
res.json({'wins' : Game.wins, 'losses': Game.losses, 'ties': Game.ties})
})
app.listen(PORT, (req, res) => {
console.log(`Listening on ${PORT}`);
});