human theft now reads from xml rules file

This commit is contained in:
Nicole O'Connor 2025-08-07 14:10:20 -07:00
parent 248592dbff
commit 8f7f173108
10 changed files with 94 additions and 25 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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"]))

View File

@ -47,7 +47,7 @@
##
{
a piece of `[pick: <wordlist/nouns/cheese>] |
a piece of `[pick: <wordlist/nouns/cheese>] cheese |
a `{
[if: [maybe]{[desc_food]}] [get_entree] |
[pick: <wordlist/nouns/fruit>]

View File

@ -1,4 +1,3 @@
Flask==3.1.1
gevent==25.5.1
hiredis>=1.0.0
redis==6.2.0
lxml>=6.0.0

View File

@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<ItemRules xmlns="seagull:rules/items">
<Event>ENCHUMAN</Event>
<Food>
<Min>0</Min>
<Min>1</Min>
<Max>10</Max>
</Food>
<Shinies>
<Min>0</Min>
<Min>1</Min>
<Max>20</Max>
</Shinies>
<Food StoryBeat="3">
<Min>0</Min>
<Min>1</Min>
<Max>20</Max>
</Food>
<Shinies StoryBeat="3">

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<ItemRules xmlns="seagull:rules/items">
<Food>
<Min>1</Min>
<Max>5</Max>
</Food>
<Shinies>
<Min>1</Min>
<Max>10</Max>
</Shinies>
<Food StoryBeat="3">
<Min>5</Min>
<Max>20</Max>
</Food>
<Shinies StoryBeat="3">
<Min>5</Min>
<Max>50</Max>
</Shinies>
<Psi StoryBeat="3">
<Min>0</Min>
<Max>15</Max>
</Psi>
</ItemRules>

View File

@ -3,7 +3,6 @@
<xs:element name="ItemRules">
<xs:complexType>
<xs:choice maxOccurs="unbounded" minOccurs="0">
<xs:element type="xs:string" name="Event"/>
<xs:element name="Food">
<xs:complexType>
<xs:sequence>

View File

@ -7,6 +7,7 @@ a = Analysis(
binaries=[],
datas=[
('app/templates', './templates'),
('app/rules', './rules'),
('static', './static'),
('app/rant', './rant'),
('opt', './opt')

View File

@ -88,8 +88,8 @@ function update_ui() {
page_elements["lbl_name"].innerHTML = gamestate["name"];
page_elements["lbl_tick"].innerHTML = gamestate["tick"];
page_elements["lbl_colony"].innerHTML = gamestate["colony"];
page_elements["lbl_shinies"].innerHTML = gamestate["shinies"];
page_elements["lbl_food"].innerHTML = gamestate["food"];
page_elements["lbl_shinies"].innerHTML = gamestate["shinies"].toFixed(2);
page_elements["lbl_food"].innerHTML = gamestate["food"].toFixed(2);
page_elements["lbl_class"].innerHTML = gamestate["class"];
page_elements["lbl_xp"].innerHTML = gamestate["xp"];
page_elements["lbl_xp_next"].innerHTML = gamestate["xp_next"];
@ -118,13 +118,25 @@ function dev_toolbox(open) {
dev_toolbox_open = open;
}
async function steal_resource(resource, amount, items) {
function reward_xp(amount) {
gamestate["xp"] += amount;
if (gamestate["xp"] >= gamestate["xp_next"]) {
old_xp_next = gamestate["xp_next"];
gamestate["xp"] -= old_xp_next;
gamestate["level"] += 1;
gamestate["xp_next"] = (old_xp_next * 1.5) + (gamestate["level"] * 5);
}
}
async function steal_resource(resource, amount, itemstr) {
var items = itemstr.split(",")
var stealdata = await fetch("/act/steal/" + resource, {method: "POST", body: JSON.stringify({gamestate: gamestate})})
.then(res => { return res.json(); })
.catch(e => { throw e; });
if (stealdata["success"] && amount > 0) {
gamestate[resource] += amount;
reward_xp(2);
record_log("Stole " + resource + " from a human: " + items.join(", "));
}
else { record_log("Didn't steal " + resource + " from a human"); }
@ -164,24 +176,25 @@ async function game_tick() {
tickdata.items.food.forEach((item) => {
total_food += item["amount"];
food_descs.push(item["desc"]);
})
});
tickdata.items.shinies.forEach((item) => {
total_shinies += item["amount"];
shinies_descs.push(item["desc"]);
})
});
var logstring = "You have encountered a human. It is carrying these resources:\n\n"
logstring += "<ol>\n"
if (total_food > 0) {
logstring += `<li><b>${total_food} food:</b> ${food_descs.join(", ")}</li>\n`;
logstring += `<li><b>${total_food.toFixed(2)} food:</b> ${food_descs.join(", ")}</li>\n`;
}
if (total_shinies > 0) {
logstring += `<li><b>${total_shinies} shinies:</b> ${shinies_descs.join(", ")}</li>\n`;
logstring += `<li><b>${total_shinies.toFixed(2)} shinies:</b> ${shinies_descs.join(", ")}</li>\n`;
}
logstring += "</ol>\nWhat would you like to do?";
record_log_with_choices(logstring,
"Steal food", `steal_resource('food', ${total_food}, ${JSON.stringify(food_descs)})`,
"Steal shinies", `steal_resource('shinies', ${total_shinies}, ${JSON.stringify(shinies_descs)})`
"Steal food", `steal_resource('food', ${total_food}, "${food_descs.toString()}")`,
"Steal shinies", `steal_resource('shinies', ${total_shinies}, "${shinies_descs.toString()}")`
)
break;
@ -190,14 +203,14 @@ async function game_tick() {
total_food += item["amount"];
food_descs.push(item["desc"]);
})
steal_resource("food", total_food, food_descs);
steal_resource("food", total_food, food_descs.toString());
break;
case "steal-shinies":
tickdata.items.shinies.forEach((item) => {
total_shinies += item["amount"];
shinies_descs.push(item["desc"]);
})
steal_resource("shinies", total_shinies, shinies_descs);
steal_resource("shinies", total_shinies, shinies_descs.toString());
break;
default:
console.error("undefined action " + page_elements["menu_enc_human"]);
@ -249,13 +262,15 @@ target.addEventListener(start_event, function (ev) {
page_elements["menu_enc_human"] = document.querySelector("#menu-enc-human");
page_elements["menu_enc_seagull"] = document.querySelector("#menu-enc-seagull");
prepare_gamestate().then(update_ui());
prepare_gamestate();
record_log("seagull game ver. " + ver_string);
const interval = setInterval(() => {
if (tick_meter_running) { game_tick(); }
}, 1200);
update_ui();
});
function change_seagull_name() {