humus-old/server/index.js
2019-05-06 12:14:01 -05:00

65 lines
1.4 KiB
JavaScript

// 3rd party.
import http from 'http';
// 2nd party.
import {SocketServer} from '@avocado/net/server/socket';
// Import directly for HMR hierarchy.
import 'register-traits';
// Start game server.
import Game from './game';
const socketServer = new SocketServer();
let game;
let httpServer;
import repl from 'repl';
import {Entity} from '@avocado/entity';
import {flowerBarrelJSON} from './create-server-room';
const instance = repl.start({
prompt: 'humus> ',
input: process.stdin,
output: process.stdout
});
instance.context.createBarrel = () => {
return (new Entity()).fromJSON(flowerBarrelJSON([400, 400]));
};
instance.context.addEntityToRoom = (entity) => {
game.room.addEntityToLayer(entity, 'everything');
};
instance.context.ab = () => {
instance.context.addEntityToRoom(instance.context.createBarrel());
}
function createGame() {
game = new Game();
instance.context.game = game;
// Listen for connections.
httpServer = http.createServer();
httpServer.listen(8420, '0.0.0.0');
socketServer.on('connect', game.acceptConnection, game);
socketServer.open(httpServer);
}
function destroyGame(fn) {
// Stop listening.
socketServer.off('connect', game.acceptConnection);
socketServer.close(() => {
httpServer.close(() => {
game.destroy(fn);
})
});
}
createGame();
if (module.hot) {
module.hot.accept([
'./game',
'register-traits'
], () => {
destroyGame(() => {
createGame();
});
});
}