From 4067bce6b04c170cc2ffb1001edc968ad4990906 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sun, 4 Oct 2020 17:28:26 -0700 Subject: [PATCH] explorer: implement chunk syncing with all clients (#22) --- client/src/api/GameManager.ts | 17 ++++++++++++++++- index.mjs | 11 ++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/client/src/api/GameManager.ts b/client/src/api/GameManager.ts index a435bbf..09d4e59 100644 --- a/client/src/api/GameManager.ts +++ b/client/src/api/GameManager.ts @@ -383,13 +383,28 @@ class GameManager extends EventEmitter implements AbstractGameManager { this.explorerIPs.forEach((ip) => { const explorer = new window.Primus(ip); + const syncChunk = (chunk) => { + this.explorers.forEach((otherExplorer, otherIp) => { + if (ip !== otherIp) { + otherExplorer.emit('sync-chunk', chunk); + } + }); + }; + explorer.on('sync-map', (chunks) => { - chunks.forEach((chunk) => this.addNewChunk(chunk)); + chunks.forEach((chunk) => { + this.addNewChunk(chunk); + syncChunk(chunk); + }); }); explorer.on('new-chunk', (chunk) => { this.lastChunkPerExplorer.set(ip, chunk.chunkFootprint); this.addNewChunk(chunk); this.emit(GameManagerEvent.DiscoveredNewChunk, chunk); + syncChunk(chunk); + }); + explorer.on('new-sync-chunk', (chunk) => { + this.addNewChunk(chunk); }); explorer.on('hash-rate', (hashRate) => { this.hashRatePerExplorer.set(ip, hashRate); diff --git a/index.mjs b/index.mjs index c6365ae..d7bd32e 100644 --- a/index.mjs +++ b/index.mjs @@ -43,7 +43,7 @@ const answers = await inquirer.prompt([ { type: 'input', name: 'initCoords', - message: `Which x,y coords do you want to start mining at? (comma-separated)`, + message: `Which x,y coords do you want to start exploring at? (comma-separated)`, default: '0,0', filter: (coords) => { const [x = 0, y = 0] = coords.split(',').map((i) => i.trim()); @@ -176,6 +176,15 @@ if (isWebsocketServer) { spark.on('set-radius', (radius) => { minerManager.setRadius(radius); }); + + spark.on('sync-chunk', (chunk) => { + localStorageManager.updateChunk(chunk, false); + primus.forEach((otherSpark, otherId) => { + if (otherId !== spark.id) { + otherSpark.emit('new-sync-chunk', chunk); + } + }); + }); }); }