human theft now reads from xml rules file
This commit is contained in:
		@@ -21,6 +21,10 @@ desktop_mode = False
 | 
			
		||||
app = flask.Flask("seagull-game", root_path=path_appdir)
 | 
			
		||||
orig_url_for = app.url_for
 | 
			
		||||
 | 
			
		||||
xml_namespaces = {
 | 
			
		||||
    "items": "seagull:rules/items"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#REDIS_HOST="stub-implementation.example.net"
 | 
			
		||||
#REDIS_PORT=6379
 | 
			
		||||
#REDIS_USER="seagull"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,14 @@
 | 
			
		||||
import os
 | 
			
		||||
import random
 | 
			
		||||
import subprocess
 | 
			
		||||
import xml.etree.ElementTree as xmltree
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
]
 | 
			
		||||
@@ -11,7 +16,11 @@ valid_resources = [
 | 
			
		||||
rant_env = os.environ.copy()
 | 
			
		||||
rant_env["RANT_MODULES_PATH"] = (core.path_appdir / "rant").as_posix()
 | 
			
		||||
 | 
			
		||||
def generate_item(resource, target):
 | 
			
		||||
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:
 | 
			
		||||
@@ -21,6 +30,26 @@ def generate_item(resource, target):
 | 
			
		||||
        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:
 | 
			
		||||
@@ -29,4 +58,4 @@ class TickItem(object):
 | 
			
		||||
        self.resource = resource
 | 
			
		||||
        self.amount = amount
 | 
			
		||||
        self.target = target
 | 
			
		||||
        self.desc = generate_item(resource, target)
 | 
			
		||||
        self.desc = generate_item_description(resource, target)
 | 
			
		||||
@@ -47,8 +47,8 @@ def tick():
 | 
			
		||||
        case 10: # ENCHUMAN
 | 
			
		||||
            result["items"] = {
 | 
			
		||||
                # TODO: read ranges from XML rule files
 | 
			
		||||
                "food": [items.TickItem("food", round(random.uniform(0.0, 20.0), 2), "humans") for i in range(random.randint(0, 3))],
 | 
			
		||||
                "shinies": [items.TickItem("shinies", round(random.uniform(0.0, 20.0), 2), "humans") for i in range(random.randint(0, 3))]
 | 
			
		||||
                "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"]))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user