REEEEEEEEEEEE
This commit is contained in:
parent
522b9b06f8
commit
d5ac5e8583
48
async-bot.py
48
async-bot.py
|
@ -47,7 +47,7 @@ class AsyncBotManager:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
|
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
|
||||||
left, top, right, bottom = font.getbbox(text)
|
left, top, right, bottom = font.getbbox(text)
|
||||||
with Image.new("LA", (int(right - left) + 4, int(bottom - top) + 8), 0) as im:
|
with Image.new("LA", (int(right - left) + 4, int(bottom - top) + 16), 0) as im:
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
draw.rectangle((0, 0, im.width, im.height), (0, 0))
|
draw.rectangle((0, 0, im.width, im.height), (0, 0))
|
||||||
draw.text((left + 2, top + 2), text, font=font, fill=(255, 0),
|
draw.text((left + 2, top + 2), text, font=font, fill=(255, 0),
|
||||||
|
@ -192,7 +192,7 @@ class AsyncBotManager:
|
||||||
else:
|
else:
|
||||||
print("unknown event", event, data)
|
print("unknown event", event, data)
|
||||||
now = time_now()
|
now = time_now()
|
||||||
if (now - self._last_printout) > 10:
|
if (now - self._last_printout) > 5:
|
||||||
outgoing = self._written_boxes / (now - self._last_printout)
|
outgoing = self._written_boxes / (now - self._last_printout)
|
||||||
incoming = self._read_boxes / (now - self._last_printout)
|
incoming = self._read_boxes / (now - self._last_printout)
|
||||||
print()
|
print()
|
||||||
|
@ -239,26 +239,30 @@ class AsyncBotManager:
|
||||||
):
|
):
|
||||||
proxy = ProxyConnector.from_url(proxy_url) if proxy_url else None
|
proxy = ProxyConnector.from_url(proxy_url) if proxy_url else None
|
||||||
await self.ready_event.wait()
|
await self.ready_event.wait()
|
||||||
async with ClientSession(connector=proxy, timeout=TIMEOUT) as http:
|
try:
|
||||||
async with AsyncSimpleClient(http_session=http) as sio:
|
async with ClientSession(connector=proxy, timeout=TIMEOUT) as http:
|
||||||
await sio.connect(f"{self.base}/socket.io")
|
async with AsyncSimpleClient(http_session=http) as sio:
|
||||||
print(f"Writer {bot_index} connected")
|
await sio.connect(f"{self.base}/socket.io")
|
||||||
offset = 0
|
print(f"Writer {bot_index} connected")
|
||||||
while not self._shutdown:
|
offset = 0
|
||||||
diff = list(self.difference.items())
|
while not self._shutdown:
|
||||||
diff = sorted(diff, key=lambda kv: kv[0])
|
diff = list(self.difference.items())
|
||||||
for _ in range(100):
|
diff = sorted(diff, key=lambda kv: kv[0])
|
||||||
index, expected = diff[offset % len(diff)]
|
for _ in range(100):
|
||||||
offset += randint(0, 1000)
|
index, expected = diff[offset % len(diff)]
|
||||||
y, x = divmod(index, 1000)
|
offset += randint(0, 1000)
|
||||||
current = self.canvas.getpixel((x, y)) > 0 # type: ignore
|
y, x = divmod(index, 1000)
|
||||||
if current != expected:
|
current = self.canvas.getpixel((x, y)) > 0 # type: ignore
|
||||||
self._written_boxes += 1
|
if current != expected:
|
||||||
await sio.emit("toggle_bit", {"index": index})
|
self._written_boxes += 1
|
||||||
await asyncio.sleep(delay)
|
await sio.emit("toggle_bit", {"index": index})
|
||||||
self._active.add(bot_index)
|
await asyncio.sleep(delay)
|
||||||
with suppress():
|
self._active.add(bot_index)
|
||||||
await sio.receive(0.1)
|
with suppress(BaseException):
|
||||||
|
await sio.receive(0.1)
|
||||||
|
print(f"Worker {bot_index} stopped: shutdown")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Worker {bot_index} died: {e!r}")
|
||||||
|
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
self._listener_task = asyncio.create_task(self.listener())
|
self._listener_task = asyncio.create_task(self.listener())
|
||||||
|
|
Loading…
Reference in New Issue