async def on_chunk(data): self.alive = time() x = data.get("cx") y = data.get("cy") comp = data.get("comp") async with self.chunk_lock: image = ChunkPz.load(comp) self.chunks[f"{x}_{y}"] = CachedPZChunk(x, y, image)
async def fetch_pixelzone(x, y, dx, dy): chunks, shape = ChunkPz.get_intersecting(x, y, dx, dy) fetched = Image.new('RGB', tuple([512 * x for x in shape]), colors.pixelzone[2]) await http.fetch_chunks(chunks) for i, ch in enumerate(chunks): if ch.is_in_bounds(): fetched.paste(ch.image, ((i % shape[0]) * 512, (i // shape[0]) * 512)) return fetched.crop((x % 512, y % 512, (x % 512) + dx, (y % 512) + dy))
async def on_message(pixels): self.alive = time() pixels = pixels.replace(" ", "").replace("[", "").replace("]", "") pixels = [int(pixel) for pixel in pixels.split(",")] for pixel in pixels: x = (pixel >> 17) - canvases.PZ_MAP_LENGTH_HALVED y = ((pixel >> 4) & 0b1111111111111) - canvases.PZ_MAP_LENGTH_HALVED color = pixel & 0b1111 cx, cy = ChunkPz.chunk_from_coords(x, y) async with self.chunk_lock: cached = self.chunks.get(f"{cx}_{cy}", None) if cached: cached.add_pixel(x, y, color) async with self.listener_lock: for _, listener in self.listeners.items(): self.bot.loop.create_task(listener(x, y, color, "pixelzone"))
async def fetch_pixelzone(x, y, dx, dy): """Fetches the current state of a given section of pixelzone. Arguments: x - The x coordinate, integer. y - The y coordinate, integer. dx - The width, integer. dy - The height, integer. Returns: A PIL Image object of the area requested. """ chunks, shape = ChunkPz.get_intersecting(x, y, dx, dy) fetched = Image.new('RGB', tuple([512 * x for x in shape]), colors.pixelzone[2]) await http.fetch_chunks(chunks) for i, ch in enumerate(chunks): if ch.is_in_bounds(): fetched.paste(ch.image, ((i % shape[0]) * 512, (i // shape[0]) * 512)) return fetched.crop((x % 512, y % 512, (x % 512) + dx, (y % 512) + dy))