// Node. import {performance} from 'perf_hooks'; // 3rd party. import immutablediff from 'immutablediff'; // 2nd party. import {StateSynchronizer} from '@avocado/state'; // 1st party. import {WorldTime} from '../common/world-time'; import {createEntityForConnection} from './create-entity-for-connection'; import {createRoom} from './create-server-room'; // Create game. export default function() { setInterval(createMainLoop(), 1000 / 80); return createConnectionListener(); } // Create room. const room = createRoom(); // Time :) const worldTime = new WorldTime(); worldTime.hour = 10; // Entity tracking. const informables = []; const stateSynchronizer = new StateSynchronizer({ room, worldTime, }); // Connection listener. function createConnectionListener() { return (socket) => { // Create and track a new entity for the connection. const entity = createEntityForConnection(socket); // Track informables. informables.push(entity); entity.on('destroyed', () => { const index = informables.indexOf(entity); if (-1 !== index) { informables.splice(index, 1); } }); // Add entity to room. room.addEntityToLayer(entity, 'everything'); // Listen for events. socket.on('message', createMessageListener(socket)); socket.on('disconnect', createDisconnectionListener(socket)); } } // Handle incoming messages. function createMessageListener(socket) { const {entity} = socket; return ({type, payload}) => { switch (type) { case 'input': entity.inputState = payload; break; } }; } // Handle disconnection. function createDisconnectionListener(socket) { const {entity} = socket; return () => { entity.destroy(); }; } // Main loop. let lastTime = performance.now(); function createMainLoop() { return () => { const now = performance.now(); const elapsed = (now - lastTime) / 1000; lastTime = now; // Tick synchronized. stateSynchronizer.tick(elapsed); // Inform entities of the new state. for (const entity of informables) { entity.inform(stateSynchronizer.state); } } }