silphius/app/net/server/worker.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-06-15 20:59:11 -05:00
import {get, set} from 'idb-keyval';
2024-06-13 12:24:32 -05:00
import {encode} from '@/packets/index.js';
2024-06-10 22:42:30 -05:00
2024-06-15 20:59:11 -05:00
import Engine from '../../engine/engine.js';
2024-06-10 22:42:30 -05:00
import Server from './server.js';
class WorkerServer extends Server {
2024-06-15 20:59:11 -05:00
constructor() {
super();
this.fs = {};
}
static qualify(path) {
return ['UNIVERSE', path].join('/');
}
async readAsset(path) {
return fetch(path);
}
2024-06-15 20:59:11 -05:00
async readData(path) {
const data = await get(this.constructor.qualify(path));
if ('undefined' !== typeof data) {
return data;
}
const error = new Error();
error.code = 'ENOENT';
throw error;
}
async writeData(path, view) {
await set(this.constructor.qualify(path), view);
}
2024-06-10 22:42:30 -05:00
transmit(connection, packed) { postMessage(packed); }
}
const engine = new Engine(WorkerServer);
2024-06-15 20:59:11 -05:00
onmessage = async (event) => {
if (0 === event.data) {
2024-06-21 18:16:41 -05:00
engine.stop();
2024-06-15 20:59:11 -05:00
await engine.disconnectPlayer(0, 0);
2024-06-21 18:16:41 -05:00
await engine.saveEcses();
2024-06-15 20:59:11 -05:00
postMessage(0);
return;
}
engine.server.accept(0, event.data);
2024-06-10 22:42:30 -05:00
};
2024-06-12 23:17:24 -05:00
(async () => {
await engine.load();
engine.start();
2024-06-15 20:59:11 -05:00
await engine.connectPlayer(0, 0);
2024-06-13 12:24:32 -05:00
postMessage(encode({type: 'ConnectionStatus', payload: 'connected'}));
2024-06-12 23:17:24 -05:00
})();
2024-06-13 12:24:32 -05:00
2024-06-15 20:59:11 -05:00
import.meta.hot.accept('../../engine/engine.js', async () => {
await engine.disconnectPlayer(0, 0);
2024-06-13 12:24:32 -05:00
postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'}));
close();
});