Commit 813f90bc authored by Grant's avatar Grant
Browse files

fix bad math

Assisted-by: deepseek:v4-flash
parent 721f5c93
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
data
frames
frames*
node_modules
out.mp4
.env
+2 −2
Original line number Diff line number Diff line
@@ -57,8 +57,8 @@ client.connect().then(async () => {
  const start = startDate.getTime();
  const end = endDate.getTime() + 1; // +1 so the last event isn't excluded by strict <
  const framesPerWorker = Math.floor((end - start) / (WORKER_COUNT - 1));
  const frameStartIncrement = Math.floor(framesPerWorker / MS_PER_FRAME);
  const framesToGenerate = Math.floor((end - start) / MS_PER_FRAME);
  const frameStartIncrement = Math.ceil(framesPerWorker / MS_PER_FRAME);
  const framesToGenerate = Math.ceil((end - start) / MS_PER_FRAME);
  console.log(framesToGenerate + " frames need to be generated");

  spawnWorkers().then(() => {
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ const makeQuery = (cl: Client) => ({
  },
  existingPixelsBeforeTime: (end: Date) => {
    return cl.query<DBPixel>(
      `SELECT DISTINCT ON (x, y) x, y, color, "createdAt" FROM "Pixel" WHERE "createdAt" < $1 AND "deletedAt" IS NULL ORDER BY x, y, "createdAt" DESC`,
      `SELECT DISTINCT ON (x, y) x, y, color, "createdAt" FROM "Pixel" WHERE "createdAt" < $1 AND ("deletedAt" IS NULL OR "deletedAt" >= $1) ORDER BY x, y, "createdAt" DESC`,
      [end],
    );
  },
+42 −6
Original line number Diff line number Diff line
@@ -184,6 +184,14 @@ const getPixelsAsLog = async (start: number, end: number) => {
      dbpixel.createdAt.getTime() < end
    ) {
      // created within timeframe — handled below
      // Also check if this pixel was deleted within the same timeframe
      if (
        dbpixel.deletedAt &&
        dbpixel.deletedAt.getTime() >= start &&
        dbpixel.deletedAt.getTime() < end
      ) {
        deletedPixels.push(dbpixel);
      }
    } else if (dbpixel.deletedAt) {
      deletedPixels.push(dbpixel);
    } else {
@@ -221,8 +229,12 @@ const getPixelsAsLog = async (start: number, end: number) => {
      list.push(p);
    }

    // For each deleted pixel, walk backwards through the position's history
    // to find the pixel that was active just before this one was placed
    // For each deleted pixel, find the pixel that was active at the deletion time.
    // Walk backwards through the position's history and find the latest pixel
    // that was active at dp.deletedAt (created before deletion, not deleted
    // before deletion). This correctly handles stale deletions — when a newer
    // pixel has already overwritten the deleted pixel, the newer pixel's color
    // is restored instead of the pre-deleted-pixel background.
    for (const dp of deletedPixels) {
      const key = dp.x + "," + dp.y;
      const history = historyByPosition.get(key);
@@ -230,12 +242,21 @@ const getPixelsAsLog = async (start: number, end: number) => {
        coveringColorCache.set(key + ":" + dp.createdAt.getTime(), "ffffff");
        continue;
      }
      // History is ordered createdAt ASC; walk backwards to find
      // the latest pixel created before dp was placed
      // History is ordered createdAt ASC; walk backwards (most recent first)
      // to find the latest pixel that was active at the deletion time.
      const deletionTime = dp.deletedAt!.getTime();
      let covering = "ffffff";
      for (let i = history.length - 1; i >= 0; i--) {
        if (history[i].createdAt.getTime() < dp.createdAt.getTime()) {
          covering = history[i].color;
        const h = history[i];
        // Skip the pixel being deleted itself
        if (h.createdAt.getTime() === dp.createdAt.getTime()) continue;
        // Check if this pixel was active at the deletion time:
        // created before the deletion AND (not deleted, or deleted after the deletion)
        if (
          h.createdAt.getTime() < deletionTime &&
          (!h.deletedAt || h.deletedAt.getTime() >= deletionTime)
        ) {
          covering = h.color;
          break;
        }
      }
@@ -257,6 +278,21 @@ const getPixelsAsLog = async (start: number, end: number) => {
        date: dbpixel.createdAt.getTime(),
        hex: dbpixel.color,
      });
      // Also emit deletion if this pixel was deleted in the same timeframe
      if (
        dbpixel.deletedAt &&
        dbpixel.deletedAt.getTime() >= start &&
        dbpixel.deletedAt.getTime() < end
      ) {
        const cacheKey =
          dbpixel.x + "," + dbpixel.y + ":" + dbpixel.createdAt.getTime();
        pixels.push({
          x: dbpixel.x,
          y: dbpixel.y,
          date: dbpixel.deletedAt.getTime(),
          hex: coveringColorCache.get(cacheKey) || "ffffff",
        });
      }
    } else if (dbpixel.deletedAt) {
      // this is a deleted pixel that was in the timeframe
      const cacheKey =