63 lines
1.3 KiB
JavaScript
63 lines
1.3 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');
|
|
};
|
|
|
|
|
|
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();
|
|
});
|
|
});
|
|
}
|