-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (65 loc) · 1.84 KB
/
Copy pathserver.js
File metadata and controls
74 lines (65 loc) · 1.84 KB
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
const express = require('express');
const app = express();
const fs = require('fs');
require('dotenv').config();
const port = process.env.PORT || 8080;
const apiKey = process.env.APIKEY || "no_api_key";
const levels = [
"easymobile",
"inception",
"hohum",
"the original",
"inexorable",
"ratchet up",
"the crucible",
"4 square",
"the devil's lair",
"labyrinth"
]
// Serve static files
app.use(express.static(__dirname + '/public'));
// use handlebars
var exphbs = require("express-handlebars");
app.engine(".hbs", exphbs({
defaultLayout: "main",
extname: ".hbs",
helpers: {
// capitalize words on request
'Capitalize': function(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
}));
app.set("view engine", ".hbs");
// Serve app
console.log('Listening on: http://localhost:' + port);
app.get ("/", (req, res) => {
res.render("index");
})
app.get('/game/:levelID', (req, res) => {
console.log(levels[req.params.levelID])
let level = req.params.levelID;
// fail gracefully:
// if level does not exist, try the next in the list
if (level == parseInt(level)) -- level;
let filename = `public/levels/${levels[level]}.lvl`;
while (!fs.existsSync(filename) && parseInt(level) < 99) {
console.log("file not found:", filename);
level = parseInt(level) + 1;
filename = `public/levels/${levels[level]}.lvl`;
}
console.log(filename);
if (level === 99) {
res.render("win");
}
else {
const levelData = fs.readFileSync(filename)
.toString();
// see what we've got
console.log(levelData);
// render this level
res.render("game", {levelData: levelData, levelNumber: req.params.levelID, levelName: levels[level]});
}
})
app.listen(port);