|
| 1 | +import { log, statsLog } from "./utils/log.js"; |
| 2 | + |
| 3 | +import { createCanvas } from "canvas"; |
| 4 | +import { decodePartition } from "./utils/decode.js"; |
| 5 | +import { fetchPartition } from "./partition.js"; |
| 6 | +import { getCellColor } from "./utils/color.js"; |
| 7 | + |
| 8 | +export async function renderPartition(totalSize, partitionSize, subreddit, challenge, sequence, name) { |
| 9 | + log("capturing screenshot for %s", name); |
| 10 | + |
| 11 | + const partitionCount = Math.floor(totalSize / partitionSize); |
| 12 | + |
| 13 | + const canvas = createCanvas(totalSize, totalSize); |
| 14 | + const context = canvas.getContext("2d"); |
| 15 | + |
| 16 | + const imageData = context.createImageData(partitionSize, partitionSize); |
| 17 | + |
| 18 | + let total = 0; |
| 19 | + let bans = 0; |
| 20 | + |
| 21 | + for (let partitionX = 0; partitionX < partitionCount; partitionX += 1) { |
| 22 | + for (let partitionY = 0; partitionY < partitionCount; partitionY += 1) { |
| 23 | + const partition = await fetchPartition(partitionX, partitionY, subreddit, challenge, sequence); |
| 24 | + |
| 25 | + if (partition !== null) { |
| 26 | + const cells = decodePartition(partition); |
| 27 | + |
| 28 | + let index = 0; |
| 29 | + |
| 30 | + for (const cell of cells) { |
| 31 | + const color = getCellColor(cell); |
| 32 | + |
| 33 | + imageData.data[index * 4] = (color >> 16) & 0xFF; |
| 34 | + imageData.data[index * 4 + 1] = (color >> 8) & 0xFF; |
| 35 | + imageData.data[index * 4 + 2] = color & 0xFF; |
| 36 | + imageData.data[index * 4 + 3] = 255; |
| 37 | + |
| 38 | + index += 1; |
| 39 | + total += 1; |
| 40 | + |
| 41 | + if (cell?.ban) { |
| 42 | + bans += 1; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + context.putImageData(imageData, partitionX * partitionSize, partitionY * partitionSize); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + statsLog("%s% of claimed cells on %s are bans", (bans / Math.max(total, 1) * 100).toFixed(2), name); |
| 52 | + |
| 53 | + return canvas; |
| 54 | +} |
0 commit comments