-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
86 lines (66 loc) · 1.93 KB
/
app.ts
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
var express = require('express');
var bodyParser = require('body-parser');
import GameOfPinochle from './server/GameOfPinochle';
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.game = new GameOfPinochle();
app.get('/api/start', function(req, res, next) {
// start a new game, deal cards
console.log('Starting game!');
app.game.newGame();
res.send(app.game.getState());
});
app.get('/api/play-card', function(req, res, next) {
// human player plays a card
res.send(app.game.getState());
});
app.get('/api/discard', function(req, res, next) {
console.log('discard called');
app.game.cpuDiscardCards();
res.send(app.game.getState());
});
app.get('/api/play-round', function(req, res, next) {
if (app.game.playNextRound()) {
res.send(app.game.getState());
} else {
res.send(app.game.getState());
}
});
app.get('/api/play-rounds', function(req, res, next) {
for(let n=0;n<10000;n++) {
app.game.newGame();
if (app.game.state !== "cannotBid") {
app.game.playRounds();
}
}
app.game.getStats();
res.send(app.game.getState());
});
app.get('/api/get-state', function(req, res, next) {
// get full game status from server: all cards, points, etc
res.send(app.game.getState());
});
app.use(express.static('public'));
app.use(express.static('bower_components'));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
console.log('page not found: ' + JSON.stringify(req));
// err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = err;
// render the error page
res.status(err.status || 500);
next();
});
app.listen(3000, function() {
app.emit('started');
console.log('Example app listening on port 3000!')
});
module.exports = app;