2024-03-02 10:43:49 -06:00
|
|
|
from django.shortcuts import render
|
2024-03-08 19:11:50 -06:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2024-03-02 10:43:49 -06:00
|
|
|
from django.template import loader
|
2024-03-08 19:11:50 -06:00
|
|
|
from json import loads, dumps
|
2024-03-02 10:43:49 -06:00
|
|
|
from math import floor
|
|
|
|
import requests
|
2024-04-06 19:23:44 -05:00
|
|
|
from .forms import ServerAddressForm
|
2024-03-09 17:17:02 -06:00
|
|
|
import logging
|
2024-03-02 10:43:49 -06:00
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
|
2024-03-09 23:20:35 -06:00
|
|
|
# Debug and defaults.
|
|
|
|
defaults = {
|
2024-04-05 12:06:07 -05:00
|
|
|
"listen_address": "http://localhost:51111/",
|
2024-03-09 23:20:35 -06:00
|
|
|
}
|
2024-04-05 12:06:07 -05:00
|
|
|
logging.basicConfig(encoding="utf-8", level=logging.DEBUG)
|
|
|
|
|
|
|
|
# API endpoints
|
|
|
|
# /overview - {screne, seed, items (int), entrances (int), hints (int), codes (dict of codes in an area and how care they are, can be empty)}
|
|
|
|
# /items - {[scene names] ({name (name of item that was received), owner (in multiworld, who received that item)})}
|
|
|
|
# /doors - {[scene names] ({scene (name of scene the mapped entrance is in), door (name of entrance it leads to)})}
|
|
|
|
# /hints - {[where hint was received] (raw trunic string of hint)}
|
2024-03-09 17:17:02 -06:00
|
|
|
|
2024-03-02 10:43:49 -06:00
|
|
|
|
2024-03-09 23:20:35 -06:00
|
|
|
def session_key(session, key):
|
|
|
|
if key not in session.keys():
|
|
|
|
session[key] = defaults[key]
|
|
|
|
|
|
|
|
# TODO: consider serializing user sessions for debugging in the future.
|
2024-04-05 12:06:07 -05:00
|
|
|
logging.info(f"Session {key} set to: {session[key]}")
|
2024-03-09 23:20:35 -06:00
|
|
|
return session[key]
|
|
|
|
|
|
|
|
|
2024-03-02 10:43:49 -06:00
|
|
|
def index(request):
|
2024-04-05 12:06:07 -05:00
|
|
|
listen_address = session_key(request.session, "listen_address")
|
2024-03-09 23:20:35 -06:00
|
|
|
|
2024-03-03 20:29:51 -06:00
|
|
|
try:
|
2024-04-05 12:06:07 -05:00
|
|
|
request_overview_data = requests.get(
|
|
|
|
f"{listen_address}overview", timeout=5, verify=True
|
|
|
|
).text
|
|
|
|
request_items_data = requests.get(
|
|
|
|
f"{listen_address}items", timeout=5, verify=True
|
|
|
|
).text
|
|
|
|
request_doors_data = requests.get(
|
|
|
|
f"{listen_address}doors", timeout=5, verify=True
|
|
|
|
).text
|
|
|
|
request_hints_data = requests.get(
|
|
|
|
f"{listen_address}hints", timeout=5, verify=True
|
|
|
|
).text
|
|
|
|
tracker_overview_data = loads(request_overview_data)
|
2024-04-07 20:13:07 -05:00
|
|
|
if "error" in tracker_overview_data.keys():
|
|
|
|
raise ValueError
|
2024-04-05 12:06:07 -05:00
|
|
|
tracker_items_data = loads(request_items_data)
|
|
|
|
tracker_doors_data = loads(request_doors_data)
|
|
|
|
tracker_hints_data = loads(request_hints_data)
|
2024-03-05 14:03:35 -06:00
|
|
|
except:
|
2024-04-07 20:13:07 -05:00
|
|
|
with open("empty_overview.json", "r") as t:
|
2024-04-06 19:23:44 -05:00
|
|
|
tracker_overview_data = loads(t.read())
|
2024-04-07 20:13:07 -05:00
|
|
|
with open("empty_items.json", "r") as t:
|
2024-04-06 19:23:44 -05:00
|
|
|
tracker_items_data = loads(t.read())
|
2024-04-07 20:13:07 -05:00
|
|
|
with open("empty_doors.json", "r") as t:
|
2024-04-06 19:23:44 -05:00
|
|
|
tracker_doors_data = loads(t.read())
|
2024-04-07 20:13:07 -05:00
|
|
|
with open("empty_hints.json", "r") as t:
|
2024-04-06 19:23:44 -05:00
|
|
|
tracker_hints_data = loads(t.read())
|
2024-04-05 12:06:07 -05:00
|
|
|
with open("tracker/static/tracker/data/holy_cross_codes.json", "r") as t:
|
2024-03-06 12:01:57 -06:00
|
|
|
try:
|
2024-03-10 01:36:19 -06:00
|
|
|
temp_codes = loads(t.read())
|
2024-04-05 12:06:07 -05:00
|
|
|
cross_codes = {
|
|
|
|
x: {
|
|
|
|
k: l.replace("U", "⬆️")
|
|
|
|
.replace("D", "⬇️")
|
|
|
|
.replace("L", "⬅️")
|
|
|
|
.replace("R", "➡️")
|
|
|
|
.strip()
|
|
|
|
for k, l in temp_codes[x].items()
|
|
|
|
}
|
|
|
|
for x in temp_codes.keys()
|
|
|
|
}
|
2024-03-06 12:01:57 -06:00
|
|
|
except:
|
|
|
|
return
|
2024-03-13 14:33:01 -05:00
|
|
|
|
2024-04-05 12:06:07 -05:00
|
|
|
# Data from the /overview API call
|
|
|
|
tracker_current_scene = tracker_overview_data["scene"]
|
|
|
|
tracker_seed = tracker_overview_data["seed"]
|
|
|
|
tracker_hints_found_total = tracker_overview_data["hints"]
|
|
|
|
tracker_current_scene_codes = tracker_overview_data["codes"]
|
|
|
|
|
|
|
|
# Data from the /doors API call
|
|
|
|
tracker_entrances_total = tracker_doors_data["total"]
|
2024-04-06 19:23:44 -05:00
|
|
|
tracker_entrances_remaining = tracker_doors_data["remaining"]
|
2024-04-05 12:06:07 -05:00
|
|
|
tracker_entrances_mapped_total = tracker_doors_data["found"]
|
|
|
|
|
|
|
|
# Data from the /items API call
|
|
|
|
tracker_checks_total = tracker_items_data["total"]
|
2024-04-06 19:23:44 -05:00
|
|
|
tracker_checks_remaining = tracker_items_data["remaining"]
|
2024-04-05 12:06:07 -05:00
|
|
|
tracker_checks_cleared_total = tracker_items_data["collected"]
|
|
|
|
|
|
|
|
active_codes = {}
|
2024-04-06 22:48:16 -05:00
|
|
|
for name, value in tracker_current_scene_codes.items():
|
|
|
|
logging.debug(value)
|
|
|
|
this_scene = tracker_current_scene
|
|
|
|
if value["Global"]:
|
|
|
|
this_scene = "Global"
|
2024-04-05 12:06:07 -05:00
|
|
|
active_codes[name] = {
|
2024-04-06 22:48:16 -05:00
|
|
|
"distance": value["Distance"],
|
|
|
|
"global": value["Global"],
|
|
|
|
"in_range": value["InRange"],
|
|
|
|
"code": cross_codes[this_scene][name],
|
2024-04-05 12:06:07 -05:00
|
|
|
}
|
|
|
|
logging.debug(active_codes)
|
|
|
|
|
|
|
|
scene_data = {}
|
|
|
|
for scene in tracker_doors_data["scenes"].keys():
|
|
|
|
scene_data[scene] = {
|
|
|
|
"entrances": tracker_doors_data["scenes"][scene],
|
|
|
|
"checks": tracker_items_data["scenes"][scene],
|
|
|
|
}
|
|
|
|
|
|
|
|
template = loader.get_template("tracker/index.html")
|
2024-03-09 23:20:35 -06:00
|
|
|
server_address_form = ServerAddressForm()
|
2024-04-05 12:06:07 -05:00
|
|
|
server_address_form.fields["server_address_form"].initial = listen_address
|
|
|
|
logging.debug(format_overview_output(tracker_overview_data))
|
2024-03-02 10:43:49 -06:00
|
|
|
context = {
|
2024-04-05 12:06:07 -05:00
|
|
|
"server_address": listen_address,
|
|
|
|
"debug": {},
|
|
|
|
"default_codes": cross_codes["Default"],
|
|
|
|
"totals": {
|
|
|
|
"Checks": {
|
|
|
|
"Undiscovered": tracker_checks_cleared_total,
|
2024-04-06 19:23:44 -05:00
|
|
|
"Remaining": tracker_checks_remaining,
|
2024-04-05 12:06:07 -05:00
|
|
|
"Total": tracker_checks_total,
|
|
|
|
},
|
|
|
|
"Entrances": {
|
|
|
|
"Undiscovered": tracker_entrances_mapped_total,
|
2024-04-06 19:23:44 -05:00
|
|
|
"Remaining": tracker_entrances_remaining,
|
2024-04-05 12:06:07 -05:00
|
|
|
"Total": tracker_entrances_total,
|
|
|
|
},
|
2024-03-08 19:11:50 -06:00
|
|
|
},
|
2024-04-05 12:06:07 -05:00
|
|
|
"scenes": scene_data,
|
|
|
|
"current_scene": tracker_current_scene,
|
|
|
|
"codes": active_codes,
|
|
|
|
"server_address_form": server_address_form,
|
2024-04-06 19:23:44 -05:00
|
|
|
"hints": tracker_hints_data,
|
2024-03-02 10:43:49 -06:00
|
|
|
}
|
|
|
|
return HttpResponse(template.render(context, request))
|
2024-03-08 19:11:50 -06:00
|
|
|
|
|
|
|
|
2024-03-09 16:48:00 -06:00
|
|
|
def get_address(request):
|
2024-04-05 12:06:07 -05:00
|
|
|
if request.method == "GET":
|
|
|
|
return HttpResponse(
|
|
|
|
dumps({"listen_address": request.session["listen_address"]}),
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2024-03-09 16:48:00 -06:00
|
|
|
else:
|
2024-04-05 12:06:07 -05:00
|
|
|
return render(request, "tracker/index.html")
|
2024-03-09 16:48:00 -06:00
|
|
|
|
|
|
|
|
2024-03-08 19:11:50 -06:00
|
|
|
def set_address(request):
|
2024-04-05 12:06:07 -05:00
|
|
|
if request.method == "POST":
|
2024-03-08 19:11:50 -06:00
|
|
|
form = ServerAddressForm(request.POST)
|
|
|
|
if form.is_valid():
|
2024-04-05 12:06:07 -05:00
|
|
|
request.session["listen_address"] = form.cleaned_data["server_address_form"]
|
|
|
|
return HttpResponseRedirect("/")
|
2024-03-08 19:11:50 -06:00
|
|
|
else:
|
|
|
|
form = ServerAddressForm()
|
2024-04-05 12:06:07 -05:00
|
|
|
return render(request, "tracker/index.html", {"server_address_form": form})
|
2024-03-09 23:20:35 -06:00
|
|
|
|
|
|
|
|
2024-04-05 12:06:07 -05:00
|
|
|
def format_overview_output(overview):
|
|
|
|
overview_string = "\nReceived Overview Data:\n"
|
|
|
|
for key, value in overview.items():
|
|
|
|
overview_string += f"\t{key}: "
|
|
|
|
if key == "codes":
|
|
|
|
overview_string += "\n"
|
2024-04-06 22:48:16 -05:00
|
|
|
for code, something in value.items():
|
|
|
|
overview_string += f"\t\t{code}: {something['Distance']:.2f}\n"
|
2024-04-05 12:06:07 -05:00
|
|
|
else:
|
|
|
|
overview_string += f"{value}\n"
|
|
|
|
return overview_string
|
|
|
|
|
|
|
|
|
|
|
|
def format_items_output(items):
|
|
|
|
items_string = "\nReceived Item Data:\n"
|
|
|
|
for key, value in items.items():
|
|
|
|
items_string += f"\t{key}:\n"
|
|
|
|
for check_key, check_value in value.items():
|
|
|
|
items_string += f"\t\t{check_key}: {check_value}\n"
|
|
|
|
return items_string
|
|
|
|
|
|
|
|
|
|
|
|
def format_doors_output(doors):
|
|
|
|
doors_string = "\nReceived Door Data:\n"
|
|
|
|
for key, value in doors.items():
|
|
|
|
doors_string += f"\t{key}: "
|
|
|
|
for entrance_key, entrance_value in value.items():
|
|
|
|
doors_string += f"\t\t{entrance_key}: {entrance_value}\n"
|
|
|
|
return doors_string
|
|
|
|
|
|
|
|
|
|
|
|
def format_hints_output(hints):
|
|
|
|
hints_string = "\nReceived Hint Data:\n"
|
|
|
|
for key, value in hints.items():
|
|
|
|
hints_string += f"\t{key}: "
|
|
|
|
return hints_string
|