pxl_oboard/bot.py

80 lines
1.7 KiB
Python
Raw Normal View History

2023-11-24 19:04:35 +00:00
import requests
from time import sleep
# Progress-bar
2023-11-24 19:04:35 +00:00
from tqdm import tqdm
# Work with list-like objects
from listwork import *
# Easy debug
from icecream import ic
2023-11-25 10:14:22 +00:00
ic.disable() # Disable debug
2023-11-24 19:04:35 +00:00
global server
#server = 'http://127.0.0.1:3333'
server = 'http://pb.gulyaipole.fun'
2023-11-24 19:53:05 +00:00
# fill(0,0, 10,10, [0,0,0])
2023-11-24 19:04:35 +00:00
def fill(x1,y1, x2,y2, color):
pxls = []
r = color[0] ; g = color[1] ; b = color[2]
for x in range(x1, x2+1):
for y in range(y1, y2+1):
pxls.append([x, y, r, g, b])
return pxls
# draw( fill(...) )
def draw(pxls):
global server
ic(pxls)
2023-11-24 19:53:05 +00:00
push = [] # Push %limit% items
limit = 300
while len(pxls) > limit:
packs = [] # Merge elements to %limit% size list
for i in range(limit):
# Take first element
2023-11-24 19:04:35 +00:00
packs.append(pxls[0])
pxls.pop(0)
2023-11-24 19:53:05 +00:00
push.append({"main": pack(packs)})
push.append({"main": pack(pxls)}) # Pack last
2023-11-24 19:04:35 +00:00
ic(push)
2023-11-24 19:04:35 +00:00
for i in tqdm(push):
2023-11-24 19:53:05 +00:00
response = requests.post(server, i)
while not response.status_code == 200:
2023-11-24 19:04:35 +00:00
print("Error, retrying...")
2023-11-24 19:53:05 +00:00
response = requests.post(server, i)
sleep(0.1)
2023-11-24 19:04:35 +00:00
2023-11-25 10:14:22 +00:00
# cfill(0,0, 10,10) // Limit - 34x34
2023-11-24 19:04:35 +00:00
def cfill(x1,y1, x2,y2):
pxls = []
for x in range(x1, x2+1):
for y in range(y1, y2+1):
pxls.append([x, y])
packed = pack(pxls)
return packed
# ccheck( packed([[0,0]]) ) or ccheck(cfill(...))
def ccheck(packed):
global server
response = requests.get(f'{server}/get_color={packed}')
2023-11-25 10:14:22 +00:00
ic(response.text)
2023-11-24 19:04:35 +00:00
out = unpack(response.text)
return out
2023-11-25 10:14:22 +00:00
### EXAMPLE
# Draw random square 100x100 on 0,0
2023-11-24 19:04:35 +00:00
from random import randint as ri
2023-11-25 10:14:22 +00:00
draw( fill(0,0, 100,100, [ri(0,255),ri(0,255),ri(0,255)]) )
2023-11-24 19:04:35 +00:00
2023-11-25 10:14:22 +00:00
# Check square 34x34 on 0,0
print(ccheck(cfill(0,0, 34,34)))
2023-11-24 19:04:35 +00:00
2023-11-25 10:14:22 +00:00
# Use extras, draw 1/2 random square 100x100 on 0,0
from bot_extras import *
draw(pas2( rand(0,0, 100,100) ))