-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
118 lines (88 loc) · 2.61 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
const express = require('express');
const mustache = require('mustache-express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const htmlspecialchars = require('htmlspecialchars');
const SERVER_PORT = 3000;
var game = require('./game.js');
game.createGame();
var app = express();
app.engine('html', mustache());
app.set('view engine', 'html');
app.set('views', './public_html');
app.use(cookieParser());
app.use(bodyParser());
app.get('/', (req, res) => {
res.redirect('/connect');
});
app.get('/connect', (req, res) => {
let data = {error: ""};
if (req.query.error == 1)
data.error = '<label class="alert alert-danger mt-5">Eh oh! Your username is required!</label>';
else if (req.query.error == 2)
data.error = '<label class="alert alert-danger mt-5">Oops! Can\'t add you to the party.</label>';
else if (req.query.error == 3)
data.error = '<label class="alert alert-warning mt-5">Wait a minute! You must be connected first.</label>';
else if (req.query.error == 4)
data.error = '<label class="alert alert-danger mt-5">Your username is too long! 20 characters MAX</label>';
res.render('index', data);
});
app.get('/play', (req, res) => {
let data = {
'username': req.cookies.username
};
res.render('game', data);
});
app.get('/start', (req, res) => {
game.startRound();
res.end();
});
app.get('/update', (req, res) => {
try {
let username = req.cookies.username;
res.json(game.getGameInfo(username));
} catch (err) {
console.error(err);
res.writeHead(400);
res.send();
}
});
app.get('/check', (req, res) => {
let username = htmlspecialchars(req.body.username);
game.bet(game.getPlayer(username), 0);
res.end();
});
app.get('/raise/:amount', (req, res) => {
let username = htmlspecialchars(req.body.username);
game.bet(game.getPlayer(username), req.params.amount);
res.end();
});
app.get('/fold', (req, res) => {
let username = htmlspecialchars(req.body.username);
game.fold(username);
res.end();
});
app.get('/quit', (req, res) => {
let username = htmlspecialchars(req.body.username);
game.quitGame(username);
res.end();
});
app.post('/connect', (req, res) => {
let username = htmlspecialchars(req.body.username);
if (!username) {
res.redirect('/connect?error=1');
} else if (username.length > 20) {
res.redirect('/connect?error=4');
} else if (!game.addPlayer(username)) {
res.redirect('/connect?error=2');
} else {
res.cookie("username", username);
res.redirect('/play');
}
});
app.use(express.static('./public_html'));
app.use((req, res) => {
res.writeHead(404);
res.end('404 ERROR - Not Found');
});
app.listen(SERVER_PORT);