humus-old/server/index.js

56 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-04-24 18:02:13 -05:00
// 3rd party.
2019-03-20 15:28:18 -05:00
import http from 'http';
2019-04-24 18:02:13 -05:00
// 2nd party.
import {SocketServer} from '@avocado/net/server/socket';
2019-04-20 14:59:14 -05:00
// Import directly for HMR hierarchy.
2019-04-25 00:09:28 -05:00
import 'register-traits';
2019-05-21 03:10:07 -05:00
// Create fixtures.
import './create-fixtures';
2019-03-20 15:28:18 -05:00
// Start game server.
2019-04-20 14:16:06 -05:00
import Game from './game';
2019-04-24 18:02:13 -05:00
const socketServer = new SocketServer();
2019-04-20 14:16:06 -05:00
let game;
2019-04-20 21:36:17 -05:00
let httpServer;
2019-04-20 14:16:06 -05:00
2019-05-03 01:27:09 -05:00
import repl from 'repl';
const instance = repl.start({
prompt: 'humus> ',
input: process.stdin,
output: process.stdout
});
2019-04-20 14:16:06 -05:00
function createGame() {
game = new Game();
2019-05-03 01:27:09 -05:00
instance.context.game = game;
2019-04-20 19:46:45 -05:00
// Listen for connections.
2019-04-20 21:36:17 -05:00
httpServer = http.createServer();
2019-04-20 19:46:45 -05:00
httpServer.listen(8420, '0.0.0.0');
2019-04-22 14:17:45 -05:00
socketServer.on('connect', game.acceptConnection, game);
socketServer.open(httpServer);
2019-04-20 14:16:06 -05:00
}
2019-04-20 19:46:45 -05:00
function destroyGame(fn) {
// Stop listening.
2019-04-22 14:17:45 -05:00
socketServer.off('connect', game.acceptConnection);
socketServer.close(() => {
2019-04-20 19:46:45 -05:00
httpServer.close(() => {
game.destroy(fn);
})
});
2019-04-20 14:16:06 -05:00
}
createGame();
if (module.hot) {
2019-04-20 14:59:14 -05:00
module.hot.accept([
'./game',
2019-04-25 00:09:28 -05:00
'register-traits'
2019-04-20 14:59:14 -05:00
], () => {
2019-04-20 19:46:45 -05:00
destroyGame(() => {
createGame();
});
2019-04-20 14:16:06 -05:00
});
}