parent
10bc736709
commit
437c78973c
@ -0,0 +1,44 @@
|
||||
import requests
|
||||
from time import sleep
|
||||
|
||||
def draw(cords):
|
||||
for i in range(len(cords)):
|
||||
sleep(0.6)
|
||||
|
||||
payload = {'x': cords[i][1], 'y': cords[i][0], 'color': 'b'}
|
||||
response = requests.post('http://pb.dmcraft.online', data=payload)
|
||||
print(response)
|
||||
|
||||
def linex(y, x1, x2):
|
||||
res = []
|
||||
for i in range(x1, x2+1):
|
||||
res.append( [i,y] )
|
||||
return res
|
||||
|
||||
def liney(x, y1, y2):
|
||||
res = []
|
||||
for i in range(y1, y2+1):
|
||||
res.append( [x,i] )
|
||||
return res
|
||||
|
||||
|
||||
#draw( liney(300, 300, 500) )
|
||||
draw(linex(500, 300, 500))
|
||||
|
||||
'''
|
||||
xs = 180
|
||||
ys = 180
|
||||
#S
|
||||
cords = [ [3+xs,0+ys],[2+xs,0+ys],[1+xs,0+ys],[1+xs,-1+ys],[1+xs,-2+ys],[2+xs,-2+ys],[3+xs,-2+ys],[3+xs,-3+ys],[3+xs,-4+ys],[2+xs,-4+ys],[1+xs,-4+ys] ]
|
||||
draw(cords)
|
||||
#A
|
||||
cords = [ [5+xs,-4+ys],[5+xs,-3+ys],[5+xs,-2+ys],[5+xs,-1+ys],[5+xs,0+ys],[6+xs,0+ys],[7+xs,0+ys],[8+xs,0+ys],[8+xs,-4+ys],[8+xs,-3+ys],[8+xs,-2+ys],[8+xs,-1+ys],[8+xs,0+ys],[5+xs,-2+ys],[6+xs,-2+ys],[7+xs,-2+ys],[8+xs,-2+ys], ]
|
||||
draw(cords)
|
||||
#N
|
||||
cords = [ [10+xs,-4+ys],[10+xs,-3+ys],[10+xs,-2+ys],[10+xs,-1+ys],[10+xs,0+ys],[11+xs,-1+ys],[12+xs,-2+ys],[13+xs,-3+ys],[14+xs,-4+ys],[15+xs,-4+ys],[15+xs,-3+ys],[15+xs,-2+ys],[15+xs,-1+ys],[15+xs,0+ys], ]
|
||||
draw(cords)
|
||||
#S
|
||||
xs = xs + 16
|
||||
cords = [ [3+xs,0+ys],[2+xs,0+ys],[1+xs,0+ys],[1+xs,-1+ys],[1+xs,-2+ys],[2+xs,-2+ys],[3+xs,-2+ys],[3+xs,-3+ys],[3+xs,-4+ys],[2+xs,-4+ys],[1+xs,-4+ys] ]
|
||||
draw(cords)
|
||||
'''
|
@ -0,0 +1,90 @@
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from urllib.parse import parse_qs
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import time
|
||||
|
||||
global LTIME
|
||||
LTIME = cur_time = time.monotonic()
|
||||
|
||||
class RequestHandler(BaseHTTPRequestHandler):
|
||||
MATRIX_SIZE = (800, 1024)
|
||||
COLORS = {
|
||||
'w': (255, 255, 255),
|
||||
'b': (0, 0, 0),
|
||||
'r': (255, 0, 0),
|
||||
'g': (0, 255, 0),
|
||||
}
|
||||
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'image/png')
|
||||
self.end_headers()
|
||||
|
||||
matrix = self.get_matrix()
|
||||
matrix = np.flip(matrix, axis=0)
|
||||
self.send_image(matrix)
|
||||
|
||||
def do_POST(self):
|
||||
global LTIME
|
||||
cur_time = time.monotonic()
|
||||
address = self.client_address[0]
|
||||
print("IP: ", address)
|
||||
|
||||
if cur_time - LTIME <= 0.01:
|
||||
self.send_error(429, 'Too Many Requests')
|
||||
self.send_response(429)
|
||||
return 0
|
||||
else:
|
||||
LTIME = time.monotonic()
|
||||
|
||||
content_length = int(self.headers['Content-Length'])
|
||||
body = self.rfile.read(content_length)
|
||||
params = parse_qs(body.decode('utf-8'))
|
||||
|
||||
y = int(params['y'][0])
|
||||
x = int(params['x'][0])
|
||||
color = params['color'][0]
|
||||
|
||||
matrix = self.get_matrix()
|
||||
matrix[x][y] = self.COLORS[color]
|
||||
|
||||
self.save_matrix(matrix)
|
||||
self.send_response(302)
|
||||
self.send_header('Location', '/')
|
||||
self.end_headers()
|
||||
|
||||
def get_matrix(self):
|
||||
try:
|
||||
with open('matrix.npy', 'rb') as f:
|
||||
matrix = np.load(f)
|
||||
except FileNotFoundError:
|
||||
matrix = np.full(shape=(*self.MATRIX_SIZE, 3), fill_value=255, dtype=np.uint8)
|
||||
self.save_matrix(matrix)
|
||||
|
||||
return matrix
|
||||
|
||||
def save_matrix(self, matrix):
|
||||
with open('matrix.npy', 'wb') as f:
|
||||
np.save(f, matrix)
|
||||
|
||||
def send_image(self, matrix):
|
||||
image = Image.fromarray(matrix)
|
||||
buffer = BytesIO()
|
||||
image.save(buffer, format='PNG')
|
||||
self.wfile.write(buffer.getvalue())
|
||||
|
||||
|
||||
def run():
|
||||
server = HTTPServer(('127.0.0.1', 3333), RequestHandler)
|
||||
server.serve_forever()
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue