-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (48 loc) · 1.47 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
var model = require('./model');
var express = require('express');
var webserver = express();
var webserverPort = process.env.PORT || 9400;
webserver.configure(function() {
webserver.use(express.static(__dirname + '/public'));
webserver.use(express.bodyParser());
webserver.use(express.logger('dev'));
});
webserver.get("/matches.json", function(req, res) {
res.send([
{name: "Rockman", lat: 37.7619029, lng: -122.4151263},
{name: "Papercutter", lat: 37.7602744, lng: -122.4101267}
])
});
webserver.get('/api/v1/login', function(req, res) {
model.findOrCreatePlayer(req.query.email, function(player) {
res.send(player);
});
});
webserver.post('/api/v1/position', function(req, res) {
if (!req.body.email) {
res.send(400);
} else {
model.updatePlayerPosition(req.body.email, req.body.position);
res.send(200);
}
});
webserver.post('/api/v1/match_requests', function(req, res) {
model.requestGame(req.query.email);
res.send(200);
});
webserver.get('/api/v1/active_games',function(req,res){
if (!req.query.email) {
res.send(400, "Parameter required: email");
} else {
model.getOpponent(req.query.email, function(opponent){
res.send(opponent)
});
}
})
webserver.post('/api/v1/completed_games',function(req,res){
console.log("Storing results");
model.reportMatchResults(req.query.email,req.query.iWon);
res.send(200);
})
webserver.listen(webserverPort);
console.log("RPSing on port http://localhost:" + webserverPort);