61 lines
2.4 KiB
Python
61 lines
2.4 KiB
Python
import os
|
|
import random
|
|
import subprocess
|
|
|
|
import lxml.etree as xmltree
|
|
|
|
from . import core
|
|
|
|
# NOTE: due to how XML libraries handle namespaces, you have to prepend "{seagull:rules/items}" to every tag value
|
|
# this is merely the price you pay for editor autocompletions
|
|
|
|
valid_resources = [
|
|
"food", "shinies", "psi" # early game
|
|
]
|
|
|
|
rant_env = os.environ.copy()
|
|
rant_env["RANT_MODULES_PATH"] = (core.path_appdir / "rant").as_posix()
|
|
|
|
fd_item_schema = xmltree.parse(core.path_appdir / "rules/schemas/items.xsd")
|
|
item_schema = xmltree.XMLSchema(fd_item_schema)
|
|
item_schema_parser = xmltree.XMLParser(schema=item_schema)
|
|
|
|
def generate_item_description(resource, target):
|
|
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 / f"rant/{resource}/{target}.rant").as_posix()], env=rant_env, capture_output=True)
|
|
if proc_rant.stderr:
|
|
core.log.warning("rant is throwing up:\n" + proc_rant.stderr.decode())
|
|
return proc_rant.stdout.decode().strip()
|
|
|
|
def generate_item_list(resource, target, min, max, storybeat=0):
|
|
count = random.randint(min, max)
|
|
result = []
|
|
rulefile = xmltree.parse(core.path_appdir / f"rules/items/{target}.xml", item_schema_parser)
|
|
ruleset = rulefile.getroot()
|
|
resource_rules = []
|
|
for res_rule in ruleset.iter(f"{{seagull:rules/items}}{resource.title()}"):
|
|
if int(res_rule.get("StoryBeat", "0")) > storybeat:
|
|
continue
|
|
|
|
mindata = res_rule.xpath("./items:Min", namespaces=core.xml_namespaces)[0]
|
|
maxdata = res_rule.xpath("./items:Max", namespaces=core.xml_namespaces)[0]
|
|
resource_rules.append((res_rule, int(mindata.text), int(maxdata.text)))
|
|
for i in range(0, count):
|
|
core.log.warning("TODO: we don't know which rule this parses yet")
|
|
core.log.warning(f"{resource} vs humans: {resource_rules[0]}")
|
|
result.append(TickItem(resource, round(random.uniform(resource_rules[0][1], resource_rules[0][2]), 2), target))
|
|
|
|
return result
|
|
|
|
class TickItem(object):
|
|
def __init__(self, resource, amount, target):
|
|
if resource not in valid_resources:
|
|
raise TypeError
|
|
|
|
self.resource = resource
|
|
self.amount = amount
|
|
self.target = target
|
|
self.desc = generate_item_description(resource, target) |