tunic-tracker-redux/main.py

62 lines
2.1 KiB
Python
Executable File

#!/usr/bin/python
import logging
from time import sleep
from lib import entrances as e
from lib import saves as s
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
# Set up a default logger, later this should be changed to default only errors.
logging.basicConfig(level=logging.DEBUG)
entrances_map = e.import_entrances()
visited_entrances = None
save_filename = ''
save = ''
# Make this an optional command based on CLI flags
# e.log_entrances(entrances_map)
@app.get("/")
def homepage():
return {"Hello": "World"}
@app.get("/get/entrances")
async def get_entrances():
visited_entrances = None
save_filename = ''
save = ''
current_save_filename, current_save = s.get_current_save_filename()
if (current_save != save):
save = current_save
logging.debug(f'Updated current save to: {current_save}')
visited_entrances = None
logging.debug(f'Re-initializing visited entrances.')
if (current_save_filename != save_filename):
save_filename = current_save_filename
logging.debug(f'Updated latest save filename to: {save_filename}')
try:
updated_visited_entrances = s.get_visited_entrances(save_filename)
except:
logging.error(
f'Could not get visited entrances from save file at {save_filename}')
if (visited_entrances != updated_visited_entrances):
if (visited_entrances != None):
new_entrances = [
x for x in updated_visited_entrances if x not in visited_entrances]
logging.debug(new_entrances)
if (e.check_mapped_entrances(new_entrances, entrances_map)):
visited_entrances = updated_visited_entrances
logging.debug(
f'Updated visited entrances to: {visited_entrances}')
else:
visited_entrances = updated_visited_entrances
logging.debug(f'Updated visited entrances to: {visited_entrances}')
return zip([y for x, y in enumerate(visited_entrances) if x % 2 == 0], [y for x, y in enumerate(visited_entrances) if x % 2 == 1])