forked from ArkMowers/arknights-mower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·315 lines (255 loc) · 7.15 KB
/
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
from arknights_mower.solvers import record
from arknights_mower.utils.conf import load_conf, save_conf, load_plan, write_plan
from arknights_mower.__main__ import main
from arknights_mower.utils.asst import Asst
from flask import Flask, send_from_directory, request, abort
from flask_cors import CORS
from flask_sock import Sock
from simple_websocket import ConnectionClosed
import webview
import os
import multiprocessing
import subprocess
from threading import Thread
import json
import time
import sys
import mimetypes
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from functools import wraps
mimetypes.add_type("text/html", ".html")
mimetypes.add_type("text/css", ".css")
mimetypes.add_type("application/javascript", ".js")
app = Flask(__name__, static_folder="dist", static_url_path="")
sock = Sock(app)
CORS(app)
conf = {}
plan = {}
mower_process = None
read = None
operators = {}
log_lines = []
ws_connections = []
def require_token(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if hasattr(app, "token") and request.headers.get("token", "") != app.token:
abort(403)
return f(*args, **kwargs)
return decorated_function
@app.route("/")
def serve_index():
return send_from_directory("dist", "index.html")
@app.route("/conf", methods=["GET", "POST"])
@require_token
def load_config():
global conf
if request.method == "GET":
conf = load_conf()
return conf
else:
conf.update(request.json)
save_conf(conf)
return f"New config saved!"
@app.route("/plan", methods=["GET", "POST"])
@require_token
def load_plan_from_json():
global plan
if request.method == "GET":
global conf
plan = load_plan(conf["planFile"])
return plan
else:
plan = request.json
write_plan(plan, conf["planFile"])
return f"New plan saved at {conf['planFile']}"
@app.route("/operator")
def operator_list():
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
with open(
os.path.join(
sys._MEIPASS,
"arknights_mower",
"__init__",
"data",
"agent.json",
),
"r",
encoding="utf8",
) as f:
return json.load(f)
else:
with open(
os.path.join(
os.getcwd(),
"arknights_mower",
"data",
"agent.json",
),
"r",
encoding="utf8",
) as f:
return json.load(f)
@app.route("/shop")
def shop_list():
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
with open(
os.path.join(
sys._MEIPASS,
"arknights_mower",
"__init__",
"data",
"shop.json",
),
"r",
encoding="utf8",
) as f:
return json.load(f)
else:
with open(
os.path.join(
os.getcwd(),
"arknights_mower",
"data",
"shop.json",
),
"r",
encoding="utf8",
) as f:
return json.load(f)
def read_log(conn):
global operators
global mower_process
global log_lines
global ws_connections
try:
while True:
msg = conn.recv()
if msg["type"] == "log":
new_line = time.strftime("%m-%d %H:%M:%S ") + msg["data"]
log_lines.append(new_line)
log_lines = log_lines[-500:]
for ws in ws_connections:
ws.send(new_line)
elif msg["type"] == "operators":
operators = msg["data"]
elif msg["type"] == "update_conf":
global conf
conn.send(conf)
except EOFError:
conn.close()
@app.route("/running")
def running():
global mower_process
return "false" if mower_process is None else "true"
@app.route("/start")
@require_token
def start():
global conf
global plan
global mower_process
global operators
global log_lines
if mower_process is not None:
return "Mower is already running."
read, write = multiprocessing.Pipe()
mower_process = multiprocessing.Process(
target=main,
args=(
conf,
plan,
operators,
write,
),
daemon=True,
)
mower_process.start()
Thread(target=read_log, args=(read,)).start()
log_lines = []
return "Mower started."
@app.route("/stop")
@require_token
def stop():
global mower_process
if mower_process is None:
return "Mower is not running."
mower_process.terminate()
mower_process = None
return "Mower stopped."
@sock.route("/log")
def log(ws):
global ws_connections
global log_lines
ws.send("\n".join(log_lines))
ws_connections.append(ws)
try:
while True:
ws.receive()
except ConnectionClosed:
ws_connections.remove(ws)
@app.route("/dialog/file")
@require_token
def open_file_dialog():
window = webview.active_window()
file_path = window.create_file_dialog(dialog_type=webview.OPEN_DIALOG)
if file_path:
return file_path[0]
else:
return ""
@app.route("/dialog/folder")
@require_token
def open_folder_dialog():
window = webview.active_window()
folder_path = window.create_file_dialog(dialog_type=webview.FOLDER_DIALOG)
if folder_path:
return folder_path[0]
else:
return ""
@app.route("/check-maa")
@require_token
def get_maa_adb_version():
try:
Asst.load(conf["maa_path"])
asst = Asst()
version = asst.get_version()
asst.set_instance_option(2, conf["maa_touch_option"])
if asst.connect(conf["maa_adb_path"], conf["adb"]):
maa_msg = f"Maa {version} 加载成功"
else:
maa_msg = "连接失败,请检查Maa日志!"
except Exception as e:
maa_msg = "Maa加载失败:" + str(e)
return maa_msg
@app.route("/maa-conn-preset")
@require_token
def get_maa_conn_presets():
try:
with open(
os.path.join(conf["maa_path"], "resource", "config.json"),
"r",
encoding="utf-8",
) as f:
presets = [i["configName"] for i in json.load(f)["connection"]]
except:
presets = []
return presets
@app.route("/record/getMoodRatios")
def get_mood_ratios():
return record.get_mood_ratios()
@app.route("/test-email")
@require_token
def test_email():
msg = MIMEMultipart()
msg.attach(MIMEText("arknights-mower测试邮件", "plain"))
msg["Subject"] = conf["mail_subject"] + "测试邮件"
msg["From"] = conf["account"]
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465, timeout=10.0)
s.login(conf["account"], conf["pass_code"])
s.sendmail(conf["account"], conf["account"], msg.as_string())
except Exception as e:
return "邮件发送失败!\n" + str(e)
return "邮件发送成功!"