forked from kevin-rph-lee/Tourney-Watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
122 lines (109 loc) · 4.23 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
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
119
120
121
122
require('dotenv').config();
const port = process.env.PORT || 8080;
const env = process.env.ENV || 'development';
const express = require('express');
const bodyParser = require('body-parser');
const knexConfig = require('./knexfile');
const knex = require('knex')(knexConfig[env]);
const morgan = require('morgan');
const knexLogger = require('knex-logger');
const cookieSession = require('cookie-session');
const owjs = require('overwatch-js');
const bcrypt = require('bcrypt');
const _ = require('lodash');
const mailGun = require('mailgun-js')({ apiKey: process.env.MAILGUN_API, domain: process.env.MAILGUN_DOMAIN });
const moment = require('moment');
const app = express();
// // Seperated Routes for each Resource
const usersRoutes = require('./routes/users');
const enrollmentsRoutes = require('./routes/enrollments');
const tournamentsRoutes = require('./routes/tournaments');
const highlightsRoutes = require('./routes/highlights');
// const gamesRoutes = require('./routes/games');
// const teamsRoutes = require('./routes/teams');
app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2'],
// Cookie Options
maxAge: 24 * 60 * 60 * 1000,
}));
app.set('view engine', 'ejs');
app.use(morgan('dev'));
app.use(knexLogger(knex));
app.use(bodyParser.urlencoded({ extended: true }));
// Mount all resource routes
app.use('/users', usersRoutes(knex, bcrypt, cookieSession, owjs, _));
app.use('/enrollments', enrollmentsRoutes(knex, owjs, _, moment));
app.use('/tournaments', tournamentsRoutes(knex, _, env, mailGun, owjs));
app.use('/highlights', highlightsRoutes(knex));
function playersEnrolled(tournamentID) {
return knex
.select('users.battlenet_id', 'level', 'games_won', 'medal_gold', 'medal_silver', 'medal_bronze')
.from('enrollments')
.innerJoin('users', 'users.id', 'enrollments.user_id')
.where({ tournament_id: tournamentID })
.then((result) => {
return result;
});
}
// Home page, passes along whis logged in as the 'login' variable
app.get('/', async (req, res) => {
const email = req.session.email;
if (!email) {
res.render('index', { email: email });
} else {
const asPlayerList = [];
const asOwnerList = [];
knex
.select('tournament_id', 'tournaments.name', 'tournaments.is_started')
.from('enrollments')
.innerJoin('tournaments', 'tournaments.id', 'enrollments.tournament_id')
.innerJoin('users', 'users.id', 'enrollments.user_id')
.where({ email: email })
.then(async (asPlayer) => {
for (let t = 0; t < asPlayer.length; t++) {
const playerRosterCount = await playersEnrolled(asPlayer[t].tournament_id);
asPlayer[t].enrolledPlayers = playerRosterCount.length;
asPlayerList.push(asPlayer[t]);
}
knex
.select('tournaments.id', 'name', 'is_started', 'no_of_teams')
.from('tournaments')
.innerJoin('users', 'users.id', 'tournaments.creator_user_id')
.where({ email: email })
.then(async (asOwner) => {
for (let t = 0; t < asOwner.length; t++) {
const ownerRosterCount = await playersEnrolled(asOwner[t].id);
asOwner[t].enrolledPlayers = ownerRosterCount.length;
const isReady = (asOwner[t].enrolledPlayers === (asOwner[t].no_of_teams * 6));
if (!isReady) {
asOwner[t].status = 'Waiting';
} else if (isReady && asOwner[t].is_started) {
asOwner[t].status = 'In Progress';
} else {
asOwner[t].status = 'Ready';
}
asOwnerList.push(asOwner[t]);
}
res.render('index', {
email: req.session.email,
userID: req.session.userID,
asPlayerList: asPlayerList,
asOwnerList: asOwnerList,
});
});
});
}
});
// shortened link to redirect to tournaments pages
app.get('/t/:id', (req, res) => {
const tournamentID = req.params.id;
res.redirect(`/tournaments/${tournamentID}`);
});
app.get('/faq', (req, res) => {
res.render('faq', { email: req.session.email, userID: req.session.email });
});
app.use(express.static(`${__dirname}/public`));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});