This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.py
106 lines (98 loc) · 4.51 KB
/
web_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import flask
import socket
import json
from flask import render_template, request, send_file, Response
from threading import Thread
from flask_cors import CORS, cross_origin
import cv2
class WebServer():
def __init__(self, bot):
self.app = flask.Flask(__name__)
self.cors = CORS(self.app)
self.bot = bot
self.local_ip = socket.gethostbyname(socket.gethostname())
def stream():
while True:
im = self.bot.stream_live(True)
if im is False:
im = cv2.imread("error_screen.png")
ret, im = cv2.imencode(".jpg", im)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + im.tobytes() + b'\r\n\r\n')
@self.app.route("/live")
def live():
return Response(stream(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@cross_origin()
@self.app.route("/", methods=["GET", "POST"])
def page():
action = request.form.get("action")
if action:
if action == "bobber_roi":
bot.bobber_roi()
ret = {"message": "Bobber ROI - OK", "color": "green"}
return json.dumps(ret)
elif action == "start":
bot.start_init()
ret = {"message": "Start - OK", "color": "green"}
return json.dumps(ret)
elif action == "stop":
bot.stop_init()
ret = {"message": "Stop - OK", "color": "green"}
return json.dumps(ret)
elif action == "bobber_template":
bot.select_bobber_template()
ret = {"message": "Bobber template - OK", "color": "green"}
return json.dumps(ret)
elif action == "debug":
bot.debug()
ret = {"message": "Debugging", "color": "green"}
return json.dumps(ret)
elif action == "change_config":
if all([name in request.form for name in ["config[margin_c_b]", "config[bobber_threshold]"]]):
bot.change_config(request.form)
ret = {"message": "Changed", "color": "green"}
return json.dumps(ret)
ret = {"message": "Error", "color": "red"}
return json.dumps(ret)
elif action == "alt_tab":
bot.alt_tab()
ret = {"message": "Alt Tab - OK", "color": "green"}
return json.dumps(ret)
elif action == "hit_enter":
bot.hit_enter()
ret = {"message": "Enter - OK", "color": "green"}
return json.dumps(ret)
elif action == "hand_cursor_im":
bot.create_hand_cursor()
ret = {"message": "Hand cursor - OK", "color": "green"}
return json.dumps(ret)
elif action == "fishing_cursor_im":
bot.create_fishing_cursor()
ret = {"message": "Fishing cursor - OK", "color": "green"}
return json.dumps(ret)
elif action == "get_item_list":
items = bot.item_list
items["time"] = bot.running_minutes()
return json.dumps(items)
elif action == "save":
bot.save()
ret = {"message": "Save Settings - OK", "color": "green"}
return json.dumps(ret)
elif action == "load":
bot.load()
ret = {"message": "Load Settings - OK", "color": "green"}
return json.dumps(ret)
elif action == "limit":
bot.set_limit(request.form["item"], request.form["count"], request.form["time"])
ret = {"message": "Limit - OK", "color": "green"}
return json.dumps(ret)
elif action == "shutdown":
bot.shutdown()
ret = {"message": "Shutdown - OK", "color": "green"}
return json.dumps(ret)
return render_template("template.html", ip=self.local_ip, **self.bot.config_properties)
def run(self):
flask_t = Thread(target=self.app.run, kwargs={"host": "0.0.0.0"})
flask_t.daemon = True
flask_t.start()