pixelplanet/src/core/setPixel.js
HF e95b6ae8d3 Set pixels in redis in batches
finish node-redis update by making sure that everything that needs redis
runs after redis connected
2022-04-07 07:54:36 +02:00

55 lines
1.1 KiB
JavaScript

/*
* Set pixels on canvas.
* Pixels get collected in a cache for 5ms and sent to players at once.
* */
import RedisCanvas from '../data/models/RedisCanvas';
import {
getChunkOfPixel,
getOffsetOfPixel,
} from './utils';
import pixelCache from './PixelCache';
// eslint-disable-next-line import/no-unresolved
import canvases from './canvases.json';
/**
*
* By Offset is prefered on server side
* @param canvasId
* @param i Chunk coordinates
* @param j
* @param offset Offset of pixel withing chunk
*/
export function setPixelByOffset(
canvasId,
color,
i,
j,
offset,
) {
RedisCanvas.enqueuePixel(canvasId, color, i, j, offset);
pixelCache.append(canvasId, color, i, j, offset);
}
/**
*
* @param canvasId
* @param canvasId
* @param color
* @param x
* @param y
* @param z optional, if given its 3d canvas
*/
export function setPixelByCoords(
canvasId,
color,
x,
y,
z = null,
) {
const canvasSize = canvases[canvasId].size;
const [i, j] = getChunkOfPixel(canvasSize, x, y, z);
const offset = getOffsetOfPixel(canvasSize, x, y, z);
setPixelByOffset(canvasId, color, i, j, offset);
}