forked from kyleaedwards/dungeon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (75 loc) · 2.11 KB
/
index.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
/**
* Imports
*/
const express = require('express');
const http = require('http');
const uuid = require('uuid/v4');
const socketIO = require('socket.io');
const Parser = require('./lib/parser');
const Context = require('./lib/context');
const State = require('./lib/state');
const handlers = require('./lib/handlers');
/**
* Constants
*/
const app = express();
const server = http.createServer(app);
const serverName = uuid();
const io = socketIO(server);
const PORT = process.env.PORT || 5000;
const parser = new Parser();
const IS_DEV = process.env.NODE_ENV === 'development';
/**
* LOCAL GAME STATE
*/
const state = new State();
/**
* Use webpack for hot reloading while developing.
*/
if (IS_DEV) {
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const compiler = webpack(webpackConfig);
app.use(require("webpack-dev-middleware")(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
}));
app.use(require("webpack-hot-middleware")(compiler));
}
/**
* Webserver functionality for the client.
*/
app.set('views', __dirname + '/public');
// app.engine('vi', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.use(express.static('public', { index: false }));
const appView = IS_DEV ? 'index.dev.ejs' : 'index.ejs';
app.get('/', (req, res) => {
res.render(appView, {
socketURL: '/',
});
});
io.on('connection', (client) => {
/**
* Each connection has a context with dependencies injected. All actions on
* context should pertain mainly to that user's connection OR pass along
* responsibility to the game state, which will funnel responses out to
* relevant user contexts.
*/
const context = new Context(client, state, serverName);
// Set up handlers to respond to socket events.
for (let label in handlers) {
try {
const handler = handlers[label](context, parser);
client.on(label, handler);
} catch (e) {
console.error('Invalid handler interface: ', e);
}
}
});
/**
* Start the server.
*/
server.listen(PORT, function () {
console.log(`Example app listening on port ${PORT}!`);
});