64 lines
2.4 KiB
Python
Raw Permalink Normal View History

2023-02-01 14:52:56 -08:00
import json
import random
2023-02-24 12:39:18 -08:00
import subprocess
2023-02-01 14:52:56 -08:00
2023-02-27 20:22:36 -08:00
import flask
2025-08-05 17:57:43 -07:00
from . import core, items, jsonizer
2023-02-01 14:52:56 -08:00
2023-02-24 12:39:18 -08:00
def generate_flavor_text():
2025-07-29 12:50:35 -07:00
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)
2023-02-24 12:39:18 -08:00
return proc_rant.stdout.decode()
2023-02-27 20:22:36 -08:00
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
tick_event_list.append(TickEvent(11, 2, "ENCGULL"))
2023-02-27 20:22:36 -08:00
2023-02-01 14:52:56 -08:00
@core.app.route("/tick")
def tick():
2023-02-27 20:22:36 -08:00
#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"]:
2025-07-29 12:50:35 -07:00
case 0:
pass
2023-02-27 20:22:36 -08:00
case 1: # FLAVOR
result["log"] = generate_flavor_text()
case 10: # ENCHUMAN
2025-07-31 11:28:09 -07:00
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)
2025-07-31 11:28:09 -07:00
}
case 11: # ENCGULL
result["items"] = {
# TODO: read ranges from XML rule files
"food": items.generate_item_list("food", "seagulls", 0, 2),
"shinies": items.generate_item_list("shinies", "seagulls", 0, 2)
}
result["recruit_cost"] = round(random.uniform(0, 10), 2)
2023-02-27 20:22:36 -08:00
case _:
2025-07-29 12:50:35 -07:00
core.log.warning("undefined tick: {0}".format(result["event_type"]))
2023-02-27 20:22:36 -08:00
2025-08-05 17:57:43 -07:00
return flask.Response(json.dumps(result, cls=jsonizer.JSONizer), status=200, content_type="application/json")