forked from rocketmo/feh-damage-calc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
101 lines (80 loc) · 2.64 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
// This application uses express as its web server
// for more info, see: https://expressjs.com
const express = require('express');
//Filesystem manipulation
const fs = require('fs');
const path = require('path');
var hbs = require('hbs');
//Lodash is awesome
const _ = require('lodash');
//Load damage calc config from file
var config = require('./config/main.json');
config.filters = require('./config/matchup-filters.json');
var hbsHelpers = require('handlebars-helpers')();
// create a new express server
var app = express();
for (var h in hbsHelpers) {
hbs.registerHelper(h, hbsHelpers[h]);
}
hbs.registerPartials(`${__dirname}/views/partials`);
hbs.localsAsTemplateData(app);
app.set('view engine', 'hbs');
app.set('views', `${__dirname}/views`);
app.locals.layout = 'layouts/test_layout';
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public', { redirect : false }));
// start server on the specified port and binding host
app.listen(3000, '0.0.0.0', function() {
// print a message when the server starts listening
console.log('KageroCalc Starting on port 3000. Test at https://localhost:3000');
});
app.get('/', function(req, res, next) {
//Register configuration variables from config file
_.forIn(config, function(config, key) {
res.locals[key] = config;
});
res.locals.ads = false;
res.locals.data = JSON.stringify(loadJson('public/data'));
res.locals.pageCSS = [
{ stylesheet: 'sorter' },
{ stylesheet: 'calc-style' },
{ stylesheet: 'hero-portraits' }
];
res.locals.js = [
'lib/material-components-web.min.js'
];
res.locals.bottomJS = [
'lib/clipboard.min.js',
'lib/jquery.min.js',
'tsorter.js',
'util.js',
'character-tabs.js',
'growthvectors.js',
'combat-functions.js',
'stat-functions.js',
'display/update-handlers.js',
'reddit-markdown.js',
'main.js',
'init/setup-heroes.js',
'init/event-listeners.js',
'init/init.js'
];
res.render('damage_calc', res.locals);
});
function loadJson(dir) {
var filenames = fs.readdirSync(dir);
var data = {};
filenames.forEach(function(filename) {
var file = `${dir}/${filename}`;
//console.log(file); //Useful to identify which file has issues
var parts = filename.split('.');
if (parts.length === 1) {
data[filename] = loadJson(file);
}
else {
var contents = fs.readFileSync(`${dir}/${filename}`, 'utf-8');
data[parts[0]] = JSON.parse(contents);
}
});
return data;
}