Rename to mosfet.py, delete linux bash scripts
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
import importlib
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
|
||||
import logging
|
||||
log = logging.getLogger('werkzeug')
|
||||
log.setLevel(logging.ERROR)
|
||||
|
||||
from flask import Flask
|
||||
from flask_cors import CORS
|
||||
build_folder = 'web_interface/build'
|
||||
app = Flask(__name__, template_folder=build_folder, static_folder=build_folder, static_url_path='')
|
||||
CORS(app)
|
||||
|
||||
from munch import Munch
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
from mosfet import connect
|
||||
|
||||
global_state = Munch()
|
||||
g = global_state
|
||||
g.connection = False
|
||||
g.connected = False
|
||||
g.connection_status = 'disconnected'
|
||||
g.player_position = None
|
||||
g.last_chat = None
|
||||
g.name = None
|
||||
g.mcdata = False
|
||||
g.pos = False
|
||||
g.dimension = None
|
||||
g.item_lock = False
|
||||
g.inv = {}
|
||||
g.objects = {}
|
||||
g.mobs = {}
|
||||
g.players = {}
|
||||
g.player_names = {}
|
||||
g.window = None
|
||||
g.job = None
|
||||
g.correction_count = 0
|
||||
g.holding = 0
|
||||
g.afk = False
|
||||
g.health = 20
|
||||
g.food = 20
|
||||
g.sand_origin = None
|
||||
|
||||
@app.route('/api/global')
|
||||
def api_global():
|
||||
data = json.dumps(g, default=lambda o: str(o), indent=4)
|
||||
|
||||
response = app.response_class(
|
||||
response=data,
|
||||
status=200,
|
||||
mimetype='application/json'
|
||||
)
|
||||
return response
|
||||
|
||||
@app.route('/')
|
||||
def root():
|
||||
return app.send_static_file('index.html')
|
||||
|
||||
def main():
|
||||
"""Connect using the current offline/AuthMe protocol bootstrap.
|
||||
|
||||
The legacy automation loop is deliberately not started yet: its packet
|
||||
handlers still target Minecraft 1.16 and will be migrated incrementally.
|
||||
"""
|
||||
reload_timeout = time.time()
|
||||
reload_requested = False
|
||||
|
||||
def reload_bot(event):
|
||||
nonlocal reload_timeout, reload_requested
|
||||
if time.time() - reload_timeout > 2.0:
|
||||
reload_timeout = time.time()
|
||||
reload_requested = True
|
||||
print('Reloading...')
|
||||
g.running = False
|
||||
|
||||
observer = Observer()
|
||||
event_handler = PatternMatchingEventHandler(
|
||||
patterns=['*.py'], ignore_patterns=['./mosfet.py'])
|
||||
event_handler.on_any_event = reload_bot
|
||||
observer.schedule(event_handler, 'mosfet', recursive=True)
|
||||
observer.start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
g.running = True
|
||||
connect.main(global_state)
|
||||
if not reload_requested:
|
||||
break
|
||||
reload_requested = False
|
||||
importlib.reload(connect)
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
except BaseException:
|
||||
g.running = True
|
||||
traceback.print_exc()
|
||||
print('Locking...')
|
||||
while g.running:
|
||||
time.sleep(1)
|
||||
reload_requested = False
|
||||
importlib.reload(connect)
|
||||
finally:
|
||||
g.running = False
|
||||
observer.stop()
|
||||
observer.join()
|
||||
|
||||
def run_api():
|
||||
host = '0.0.0.0'
|
||||
port = 3300
|
||||
|
||||
while True:
|
||||
print('Trying to run web interface on port:', port)
|
||||
print('If it works, go to http://localhost:' + str(port))
|
||||
try:
|
||||
app.run(host=host, port=port)
|
||||
except OSError:
|
||||
print()
|
||||
print('Error: Port already taken.')
|
||||
port += 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
threading.Thread(target=run_api, daemon=True).start()
|
||||
main()
|
||||
Reference in New Issue
Block a user