import json import random import subprocess import flask from . import core, items, jsonizer def generate_flavor_text(): if core.desktop_mode: rant_path = core.path_appdir / "opt/rant/bin/rant" else: rant_path = "rant" # rely on OS PATH proc_rant = subprocess.run([rant_path, (core.path_appdir / "rant/flavor.rant").as_posix()], capture_output=True) return proc_rant.stdout.decode() class TickEvent(object): def __init__(self, tickno, tickweight, tickcode): self.tickno = tickno self.tickweight = tickweight self.tickcode = tickcode tick_event_list = [] tick_event_list.append(TickEvent(0, 16, "XYZZY")) # nothing happens tick_event_list.append(TickEvent(1, 1, "FLAVOR")) # procedurally generated event of no consequence tick_event_list.append(TickEvent(10, 2, "ENCHUMAN")) # encounter: human @core.app.route("/tick") def tick(): #return random.choices([json.dumps({"code": 200, "event_type": 0}), json.dumps({"code": 200, "event_type": 1, "log": generate_flavor_text()})], weights=[16, 1])[0] ticktypes = [] tickweights = [] for event in tick_event_list: ticktypes.append(event.tickno) # unce unce unce tickweights.append(event.tickweight) result = {} result["code"] = 200 result["event_type"] = random.choices(ticktypes, weights=tickweights)[0] match result["event_type"]: case 0: pass case 1: # FLAVOR result["log"] = generate_flavor_text() case 10: # ENCHUMAN result["items"] = { # TODO: read ranges from XML rule files "food": items.generate_item_list("food", "humans", 0, 2), "shinies": items.generate_item_list("shinies", "humans", 0, 2) } case _: core.log.warning("undefined tick: {0}".format(result["event_type"])) return flask.Response(json.dumps(result, cls=jsonizer.JSONizer), status=200, content_type="application/json")