From 93ea65f27d83729e385e43ad9df0fae702770d40 Mon Sep 17 00:00:00 2001 From: HF Date: Sun, 29 Mar 2020 23:16:44 +0200 Subject: [PATCH] make rollback function by coords --- utils/redisCopy.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/utils/redisCopy.js b/utils/redisCopy.js index f7f6f46..c378fba 100644 --- a/utils/redisCopy.js +++ b/utils/redisCopy.js @@ -1,6 +1,5 @@ /* @flow */ -//this script just copies chunks from one redis to another with a different -//key as needed +//this script just copies chunks from one redis to another import redis from 'redis'; import bluebird from 'bluebird'; @@ -37,4 +36,27 @@ async function copyChunks() { } } -copyChunks(); +function chunkOfCord(cor) { + return Math.floor((cor + CANVAS_SIZE / 2) / OUR_TILE_SIZE); +} + +async function copyChunksByCoords(xMin, xMax, yMin, yMax) { + const chunkXMin = chunkOfCord(xMin); + const chunkXMax = chunkOfCord(xMax); + const chunkYMin = chunkOfCord(yMin); + const chunkYMax = chunkOfCord(yMax); + for (let x = chunkXMin; x <= chunkXMax; x++) { + for (let y = chunkYMin; y < chunkYMax; y++) { + const oldkey = `ch:2:${x}:${y}`; + const newkey = `ch:2:${x}:${y}`; + const chunk = await oldredis.getAsync(oldkey); + if (chunk) { + const setNXArgs = [newkey, chunk]; + await newredis.sendCommandAsync('SET', setNXArgs); + console.log("Created Chunk ", newkey); + } + } + } +} + +copyChunksByCoords(-160, 60, -60, 160);