This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
127 lines (104 loc) · 3.25 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
// Load environment variables
require('dotenv').config({ path: 'config/.env' });
// Dependencies
var express = require('express');
var http = require('http');
var nunjucks = require('nunjucks');
var asyncSeries = require('async/series');
var config = require('./config/config.json');
var assert = require('./app/lib/assert');
var log = require('./app/lib/log');
var DB = require('./app/lib/db');
var Tournament = require('./app/tournament').Tournament;
// Create logger
var logger = log.createLogger('app', "App");
/*
* Initialise Express and the web server
*/
var app = express();
var server = http.Server(app);
// Configure Nunjucks
nunjucks.configure(['templates', 'templates/partials'], {
autoescape: true,
express: app
});
// Set the view engine
app.set('view engine', 'njk');
// Serve static files from the `public` folder
app.use(express.static(__dirname + '/public'));
// Pass server-side configuration to client
app.locals.baseUrl = process.env.BASE_URL;
app.locals.port = process.env.PORT;
// Assert relevant configuration options
assert.ok(config.maxScore >= 3 && config.maxScore <= 5, "maximum score must be 3, 4 or 5 (current: " + config.maxScore + ")");
// Corner Judge route
app.get('/', function (req, res) {
var identity = 'corner-judge';
res.render(identity, {
identity: identity,
title: "Corner Judge",
metaViewport: 'width=device-width, initial-scale=1, user-scalable=no',
maxScore: config.maxScore
});
});
// Jury President route
app.get('/jury', function (req, res) {
var identity = 'jury-president';
res.render(identity, {
identity: identity,
title: "Jury President",
metaViewport: 'width=device-width, initial-scale=1'
});
});
/*
* Initialise tournament
*/
var tournament;
if (process.argv.indexOf('--force') !== -1) {
// If `--force` argument is present, create a new tournament
createTournament();
} else {
// Get timestamp for the start of today
var now = new Date();
var startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
// Look for an open tournament in the database
DB.findOpenTournament(startOfToday, function (doc) {
if (doc) {
// Initialise log module using the existing tournament's ID
log.init(doc._id);
// If a tournament was found, restore it
tournament = new Tournament(doc._id);
// Restore its users and rings
asyncSeries([
tournament.restoreUsers.bind(tournament),
tournament.restoreRings.bind(tournament)
], function () {
// The tournament has been restored and is ready to receive Web Socket connections
tournament.ready(server);
logger.info('tournamentRestored', { tournament: doc });
});
} else {
// No open tournament found; create a new tournament
createTournament();
}
});
}
/**
* Create a new tournament.
*/
function createTournament() {
DB.insertTournament(function (newDoc) {
if (newDoc) {
// Initialise log module using the new tournament's ID
log.init(newDoc._id);
// Initialise the new tournament
tournament = new Tournament(newDoc._id);
tournament.createRings(config.ringCount, function () {
// The tournament has been initialised and is ready to receive Web Socket connections
tournament.ready(server);
logger.info('tournamentStarted', { tournament: newDoc });
});
}
});
}