Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@300&display=swap" />

<script type="application/javascript" src="/snarkjs.min.js"></script>
<script type="application/javascript" src="/primus/primus.js"></script>
<title>Sophon - The Dark Forest Toolbox</title>
</head>

Expand Down
61 changes: 10 additions & 51 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import os from 'os';
import fs from 'fs';
import http from 'http';
import level from 'level';
import Primus from 'primus';
import primusEmit from 'primus-emit';
import { createServer } from 'vite';
import inquirer from 'inquirer';
import dotenv from 'dotenv';
import updateDotenv from 'update-dotenv';
import multileveldown from 'multileveldown';
import LevelRangeEmitter from 'level-range-emitter';
import WebSocketServer from 'simple-websocket/server.js';

import { MIN_CHUNK_SIZE, MAX_CHUNK_SIZE } from './lib/constants.mjs';
import { MinerManager, MinerManagerEvent } from './lib/MinerManager.mjs';
Expand Down Expand Up @@ -271,6 +272,7 @@ await updateDotenv({
console.log('Config written to `.env` file - edit/delete this file to change settings');

const db = level(path.join(__dirname, `known_board_perlin`), { valueEncoding: 'json' });
const lre = LevelRangeEmitter.server(db);

const planetRarity = 16384;

Expand Down Expand Up @@ -321,49 +323,13 @@ if (isClientServer) {
server = http.createServer();
}

let primus;
let wss;
if (isWebsocketServer) {
primus = new Primus(server, {
iknowhttpsisbetter: true,
plugin: {
emit: primusEmit,
}
});

primus.on('connection', (spark) => {
spark.on('set-pattern', (worldCoords) => {
const newPattern = new ExplorePattern(worldCoords, chunkSize);
minerManager.setMiningPattern(newPattern);
const coords = `${worldCoords.x},${worldCoords.y}`;
updateDotenv({ INIT_COORDS: toEnv(worldCoords) })
.then(() => console.log(`Updated INIT_COORDS to ${coords} in .env`))
.catch(() => console.log(`Failed to update INIT_COORDS to ${coords} in .env`));
});

spark.on('set-radius', (radius) => {
if (!radiusUpdates) {
return;
}

minerManager.setRadius(radius);
updateDotenv({ WORLD_RADIUS: toEnv(radius) })
.then(() => console.log(`Updated WORLD_RADIUS to ${radius} in .env`))
.catch(() => console.log(`Failed to update WORLD_RADIUS to ${radius} in .env`));
});
wss = new WebSocketServer({ server });

spark.on('startup-sync', () => {
// Only send sync chunks with 200 objects in them
let chunks = [];
for (let chunk of localStorageManager.allChunks()) {
if (chunks.length < 200) {
chunks.push(chunk);
} else {
spark.emit('sync-chunks', chunks);
chunks = [];
}
}
chunks = null;
});
wss.on('connection', (websocketStream) => {
const dbStream = multileveldown.server(db, { readonly: true });
lre.session(dbStream, websocketStream);
});
}

Expand All @@ -373,14 +339,7 @@ if (server && port) {
});
}

minerManager.on(MinerManagerEvent.DiscoveredNewChunk, (chunk, miningTimeMillis) => {
const hashRate = chunk.chunkFootprint.sideLength ** 2 / (miningTimeMillis / 1000);
if (isWebsocketServer) {
primus.forEach((spark) => {
spark.emit('new-chunk', chunk);
spark.emit('hash-rate', hashRate);
});
}
minerManager.on(MinerManagerEvent.DiscoveredNewChunk, (chunk, _miningTimeMillis) => {
localStorageManager.updateChunk(chunk, false);
});

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"inquirer": "^7.3.3",
"level": "^6.0.1",
"level-concat-iterator": "^2.0.0",
"level-range-emitter": "^3.0.0",
"lodash": "^4.17.15",
"primus": "^7.3.5",
"primus-emit": "^1.0.0",
"multileveldown": "^3.0.0",
"simple-websocket": "^9.0.0",
"update-dotenv": "^1.1.1",
"vite": "1.0.0-rc.4",
"vite-plugin-react": "^3.0.2",
"ws": "^7.3.1"
"vite-plugin-react": "^3.0.2"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.4.0",
Expand Down
Loading