-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
109 lines (93 loc) · 2.9 KB
/
app.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
'use strict';
const Player = require('./player');
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const path = require('path');
const os = require('os');
const mongo = require('./lib/service/mongo');
const app = express();
const ObjectID = require('mongodb').ObjectID;
const matchmaker = require('./matchmaker');
const port = process.env.NODE_PORT || 3000;
let client, pirateDB, player; // initialized via service
// configure app
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true, limit: '2mb' }));
app.use(fileUpload());
// defines routes (should be outsourced if long)
app.get('/', (req, res) => {
res.render('pages/index', {name: 'World'});
});
app.get('/upload', (req, res) => {
res.render('pages/upload');
});
app.post('/upload', (req, res) => {
if (!req.files) {
return res.status(400).send('No files were uploaded');
}
if (!req.files.file || !req.body.player_name) {
return res.status(400).send('Missing one or more parameters');
}
const file = path.resolve(os.tmpdir(), req.files.file.name);
req.files.file.mv(file, (err) => {
if (!err) {
player.createNew(file, req.body.player_name)
.then(_ => res.redirect("/upload?success"));
} else {
console.log(err);
res.redirect("/upload?error");
}
});
});
app.get('/matches', async (req, res) => {
const matches = await pirateDB.collection('events').find({played: true}).toArray();
res.locals = {
matches: matches,
games: null
};
res.render('pages/matches');
});
app.get('/matches/:id', async (req, res)=>{
const selectedMatch = await pirateDB.collection('events').findOne({_id:new ObjectID(req.params.id)});
const games = await pirateDB.collection('games').find({'player1.name':selectedMatch.player1, 'player2.name':selectedMatch.player2}).toArray();
res.locals = {
matches: null,
match: selectedMatch,
games: games
};
res.render('pages/matches');
});
app.get('/game/:id', async (req, res) => {
const selectedGame = await pirateDB.collection('games').findOne({_id:new ObjectID(req.params.id)});
res.locals = {
game:selectedGame
};
res.render('pages/game');
});
app.get('/players', async (req, res) => {
const players = await pirateDB.collection('players').find({}).toArray();
res.locals = {
players: players
};
res.render('pages/players');
});
app.get('/players/:id', async (req, res) => {
const selectedPlayer = await pirateDB.collection('players').findOne({_id:new ObjectID(req.params.id)});
res.locals = {
player:selectedPlayer
};
res.render('pages/player');
});
// initialize service
(async () => {
client = await mongo.connect();
pirateDB = client.db('nodepirates');
player = await Player.initFacade(pirateDB);
app.listen(port, () => {
console.log(`listening on ${port}`);
});
})();
// @TODO: on process sigint -> close app/client
matchmaker.startInterval();