Commit 21b60c6c authored by Ategon's avatar Ategon Committed by Grant
Browse files

chore: improve syncronization of pixel cooldowns

parent 150361d1
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -165,7 +165,7 @@ export const AudioSettings = () => {
    const canvas = CanvasCore.get(false);
    if (!canvas?.lastPlace) return;

    const cooldown = CanvasLib.getPixelCooldown(pixelsAvailable + 1, config);
    const cooldown = CanvasLib.getPixelCooldown(config);
    const availableAt = canvas.lastPlace + cooldown * 1000;
    const soundAt = availableAt + pixelAvailableOffset * 1000;
    const delay = soundAt - Date.now();
@@ -214,7 +214,7 @@ export const AudioSettings = () => {
        pixel <= pixelBucketThreshold;
        pixel += 1
      ) {
        cooldownSeconds += CanvasLib.getPixelCooldown(pixel, config);
        cooldownSeconds += CanvasLib.getPixelCooldown(config);
      }

      soundAt =
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ const getTimeLeft = (available: number, config: ClientConfig) => {

  if (!lastPlace) return undefined;

  const cooldown = CanvasLib.getPixelCooldown(available + 1, config);
  const cooldown = CanvasLib.getPixelCooldown(config);
  const pixelAvailableAt = lastPlace + cooldown * 1000;

  const pixelCooldown = (Date.now() - pixelAvailableAt) / 1000;
+3 −2
Original line number Diff line number Diff line
@@ -155,8 +155,9 @@ class Network extends EventEmitter<INetworkEvents> {
      this.acceptState("pixels", { available: count });
    });

    this.socket.on("pixelLastPlaced", (time) => {
      this.acceptState("pixelLastPlaced", time);
    this.socket.on("pixelLastPlaced", (time, serverTime) => {
      const elapsedAtServer = Math.max(0, serverTime - time);
      this.acceptState("pixelLastPlaced", Date.now() - elapsedAtServer);
    });

    this.socket.on("online", ({ count }) => {
+2 −3
Original line number Diff line number Diff line
@@ -4,11 +4,10 @@ export const CanvasLib = new (class {
  /**
   * Get pixel cooldown
   *
   * @param pixelNumber What pixel is this
   * @param config
   * @returns Seconds to take to give the pixel
   */
  getPixelCooldown(pixelNumber: number, config: ClientConfig) {
    return config.pallete.pixel_cooldown / 1000;
  getPixelCooldown(config: ClientConfig) {
    return config.canvas.pixel.cooldown;
  }
})();
+1 −7
Original line number Diff line number Diff line
@@ -233,7 +233,7 @@ export interface ServerToClientEvents {
  pixel: (pixel: Pixel) => void;
  online: (count: { count: number }) => void;
  availablePixels: (count: number, undo: boolean) => void;
  pixelLastPlaced: (time: number) => void;
  pixelLastPlaced: (time: number, serverTime: number) => void;
  undo: (
    data: { available: false } | { available: true; expireAt: number },
  ) => void;
@@ -434,7 +434,6 @@ export type CanvasConfig = {
  pixel: {
    maxStack: number;
    cooldown: number;
    multiplier: number;
  };
  undo: {
    /**
@@ -451,11 +450,6 @@ export type ClientConfig = {
  version: string;
  pallete: {
    colors: PaletteColor[];
    /**
     * ms for cooldown
     * @see CanvasLib#getPixelCooldown
     */
    pixel_cooldown: number;
  };
  canvas: CanvasConfig;
  cursors: {
Loading