2020-09-07 21:48:16 +00:00
|
|
|
import importlib
|
|
|
|
import threading
|
|
|
|
import time
|
2020-09-09 01:52:50 +00:00
|
|
|
import traceback
|
2020-09-21 01:08:23 +00:00
|
|
|
import json
|
2020-09-07 21:48:16 +00:00
|
|
|
|
2021-04-26 03:07:06 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('werkzeug')
|
|
|
|
log.setLevel(logging.ERROR)
|
|
|
|
|
2020-09-06 21:39:21 +00:00
|
|
|
from flask import Flask
|
2021-04-26 02:37:43 +00:00
|
|
|
from flask_cors import CORS
|
2021-04-26 03:07:06 +00:00
|
|
|
build_folder = 'web_interface/build'
|
|
|
|
app = Flask(__name__, template_folder=build_folder, static_folder=build_folder, static_url_path='')
|
2021-04-26 02:37:43 +00:00
|
|
|
CORS(app)
|
2020-09-06 21:39:21 +00:00
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
from munch import Munch
|
2020-09-07 21:48:16 +00:00
|
|
|
from watchdog.observers import Observer
|
|
|
|
from watchdog.events import PatternMatchingEventHandler
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2021-04-22 00:46:54 +00:00
|
|
|
from mosfet import bot
|
2020-09-08 21:33:42 +00:00
|
|
|
|
2020-10-14 02:26:50 +00:00
|
|
|
global_state = Munch()
|
2020-09-07 21:48:16 +00:00
|
|
|
g = global_state
|
2020-09-08 01:28:56 +00:00
|
|
|
g.connection = False
|
2020-12-13 05:31:09 +00:00
|
|
|
g.name = None
|
2020-09-09 01:52:50 +00:00
|
|
|
g.mcdata = False
|
2020-09-08 21:33:42 +00:00
|
|
|
g.pos = False
|
2020-12-13 05:31:09 +00:00
|
|
|
g.dimension = None
|
2021-04-24 22:34:25 +00:00
|
|
|
g.item_lock = False
|
2020-09-09 21:58:19 +00:00
|
|
|
g.inv = {}
|
2020-10-14 02:26:50 +00:00
|
|
|
g.objects = {}
|
2020-12-03 03:30:54 +00:00
|
|
|
g.mobs = {}
|
2020-12-05 10:56:56 +00:00
|
|
|
g.players = {}
|
2020-12-13 11:38:30 +00:00
|
|
|
g.player_names = {}
|
2020-09-25 06:03:22 +00:00
|
|
|
g.window = None
|
|
|
|
g.job = None
|
2020-10-19 21:49:14 +00:00
|
|
|
g.correction_count = 0
|
2020-12-02 11:12:51 +00:00
|
|
|
g.holding = 0
|
2020-12-03 03:30:54 +00:00
|
|
|
g.afk = False
|
2021-01-05 05:57:06 +00:00
|
|
|
g.health = 20
|
|
|
|
g.food = 20
|
2021-04-24 22:34:25 +00:00
|
|
|
g.sand_origin = None
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2021-04-26 02:37:43 +00:00
|
|
|
@app.route('/api/global')
|
|
|
|
def api_global():
|
2020-09-21 01:08:23 +00:00
|
|
|
data = json.dumps(g, default=lambda o: str(o), indent=4)
|
|
|
|
|
|
|
|
response = app.response_class(
|
|
|
|
response=data,
|
|
|
|
status=200,
|
|
|
|
mimetype='application/json'
|
|
|
|
)
|
|
|
|
return response
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2021-04-26 03:07:06 +00:00
|
|
|
@app.route('/')
|
|
|
|
def root():
|
|
|
|
return app.send_static_file('index.html')
|
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
reload_timeout = time.time()
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
def main():
|
|
|
|
def reload_bot(event):
|
|
|
|
global reload_timeout
|
2020-09-08 01:28:56 +00:00
|
|
|
if time.time() - reload_timeout > 2.0:
|
2020-09-07 21:48:16 +00:00
|
|
|
reload_timeout = time.time()
|
|
|
|
print('Reloading...')
|
2020-09-08 01:28:56 +00:00
|
|
|
g.running = False
|
2020-09-06 22:43:20 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py'])
|
|
|
|
event_handler.on_any_event = reload_bot
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
observer = Observer()
|
|
|
|
observer.schedule(event_handler, '.', recursive=True)
|
|
|
|
observer.start()
|
2020-09-06 22:10:00 +00:00
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
try:
|
|
|
|
while True:
|
2020-09-09 01:52:50 +00:00
|
|
|
try:
|
|
|
|
g.running = True
|
|
|
|
bot.bot(global_state)
|
|
|
|
importlib.reload(bot)
|
|
|
|
except BaseException as e:
|
|
|
|
g.running = True
|
|
|
|
traceback.print_exc()
|
|
|
|
print('Locking...')
|
|
|
|
while g.running:
|
|
|
|
time.sleep(1)
|
2020-09-09 21:58:19 +00:00
|
|
|
importlib.reload(bot)
|
2020-09-07 21:48:16 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
observer.stop()
|
|
|
|
observer.join()
|
2020-09-06 22:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-04-26 03:25:42 +00:00
|
|
|
host = '0.0.0.0'
|
|
|
|
port = 3300
|
|
|
|
|
|
|
|
threading.Thread(target=app.run, kwargs={'host': host, 'port': port}).start()
|
|
|
|
print('Web interface listening on port:', port)
|
|
|
|
print('Try going to http://localhost:' + str(port))
|
2020-09-08 04:31:55 +00:00
|
|
|
time.sleep(1)
|
2021-04-26 03:25:42 +00:00
|
|
|
|
2020-09-06 22:10:00 +00:00
|
|
|
main()
|