state sync; the basic early game works

This commit is contained in:
Nicole O'Connor 2025-08-05 17:57:43 -07:00
parent 9f514db33b
commit 1b72e899af
16 changed files with 209 additions and 23 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
scratch.ipynb
.docker-login-credentials
seagull
build/**

4
.rsync-include Normal file
View File

@ -0,0 +1,4 @@
app/**
ext/imsky/wordlists/**
static/**
seagull.spec

View File

@ -9,13 +9,13 @@ import webview
import flask
from pylocal import core, desktop, dev, items, tick
from pylocal import core, actions, desktop, dev, items, tick
core.desktop_mode = True
sig_exit = threading.Event()
argp = argparse.ArgumentParser("seagull")
argp.add_argument("-d", "--debug", action="store_true")
argp.add_argument("-d", "--debug", action="store_true", help="Launches the game in \"debug mode\".")
argo = argp.parse_args()
@core.app.route("/")

View File

@ -10,7 +10,7 @@ import threading
import flask
from gevent.pywsgi import WSGIServer
from pylocal import core, dev, items, tick
from pylocal import core, actions, dev, items, tick
sig_exit = threading.Event()

19
app/pylocal/actions.py Normal file
View File

@ -0,0 +1,19 @@
import json
import random
import flask
from . import core
def dice_roll(min=0, max=100, modifiers=[]):
result = random.randint(min, max)
for _, mod in modifiers:
result += mod
return result
@core.app.route("/act/steal/<resource>", methods=["POST"])
def steal_resource(resource):
return flask.Response(json.dumps({
"success": (dice_roll() >= 50)
}), status=200, content_type="application/json")

View File

@ -1,3 +1,4 @@
import os
import subprocess
import xml.etree.ElementTree as xmltree
@ -7,13 +8,18 @@ valid_resources = [
"food", "shinies", "psi" # early game
]
rant_env = os.environ.copy()
rant_env["RANT_MODULES_PATH"] = (core.path_appdir / "rant").as_posix()
def generate_item(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()], capture_output=True)
return proc_rant.stdout.decode()
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()
class TickItem(object):
def __init__(self, resource, amount, target):

11
app/pylocal/jsonizer.py Normal file
View File

@ -0,0 +1,11 @@
import json
from . import items
class JSONizer(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, items.TickItem):
return {"resource": obj.resource, "amount": obj.amount, "desc": obj.desc}
else:
# if no encoding here, fall back to stdlib
return super().default(obj)

View File

@ -4,7 +4,7 @@ import subprocess
import flask
from . import core, items
from . import core, items, jsonizer
def generate_flavor_text():
if core.desktop_mode:
@ -47,10 +47,10 @@ def tick():
case 10: # ENCHUMAN
result["items"] = {
# TODO: read ranges from XML rule files
"food": [items.TickItem("food", random.uniform(0.0, 20.0), "humans")],
"shinies": [items.TickItem("food", random.uniform(0.0, 20.0), "humans")]
"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))]
}
case _:
core.log.warning("undefined tick: {0}".format(result["event_type"]))
return flask.Response(json.dumps(result), status=200, content_type="application/json")
return flask.Response(json.dumps(result, cls=jsonizer.JSONizer), status=200, content_type="application/json")

View File

@ -1,4 +1,4 @@
@require "../wordlist"
@require "wordlist"
[$desc_food] @text {
{
[pick: <wordlist/adjectives/food>] @weight 1.25 |
@ -31,6 +31,7 @@
}
}
##
[$mod_order] @text {
{
add | no | sub | extra |
@ -43,11 +44,12 @@
[pick: <wordlist/nouns/plants>]
}
}
##
{
a piece of `[pick: <wordlist/nouns/cheese>] |
a `{
[if: [%maybe]]{[desc_food]} [get_entree] |
[if: [maybe]{[desc_food]}] [get_entree] |
[pick: <wordlist/nouns/fruit>]
}
}

View File

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

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<uri name="seagull:rules/items" uri="items.xsd" />
</catalog>

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="seagull:rules/items">
<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>
<xs:element type="xs:int" name="Min"/>
<xs:element type="xs:int" name="Max"/>
</xs:sequence>
<xs:attribute type="xs:int" name="StoryBeat" use="optional" default="0"/>
</xs:complexType>
</xs:element>
<xs:element name="Shinies">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="Min"/>
<xs:element type="xs:int" name="Max"/>
</xs:sequence>
<xs:attribute type="xs:int" name="StoryBeat" use="optional" default="0"/>
</xs:complexType>
</xs:element>
<xs:element name="Psi">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="Min"/>
<xs:element type="xs:int" name="Max"/>
</xs:sequence>
<xs:attribute type="xs:int" name="StoryBeat" default="0"/>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@ -1,6 +1,8 @@
#!/bin/bash
BUILD_DIR=${BUILD_DIR:-./build}
srcdir=$(pwd)
BUILD_DIR=${BUILD_DIR:-$srcdir/build}
echo "$srcdir => $BUILD_DIR"
die () {
echo "$@" >&2
@ -15,14 +17,13 @@ findcmd cargo
findcmd python
findcmd rsync
srcdir=$(pwd)
mkdir -p $BUILD_DIR && cd $BUILD_DIR
rsync -rv $srcdir/ $BUILD_DIR/
rsync -rv --include-from=$srcdir/.rsync-include $srcdir/ $BUILD_DIR/
# rant
mkdir -p opt/rant
cargo install rant --version 4.0.0-alpha.33 --root $BUILD_DIR/opt/rant --features cli
python $srcdir/ext/imsky/wordlists/render.py -i ext/imsky/wordlists -o rant $BUILD_DIR/app/rant/wordlist.rant
python $srcdir/render-wordlists.py -i $srcdir/ext/imsky/wordlists -o rant $BUILD_DIR/app/rant/wordlist.rant
# python venv
python -m venv pyvenv

View File

@ -1 +0,0 @@
{"statever":"1","tick":211,"name":"Jeff","level":1,"shinies":0,"colony":1,"food":0,"autosave":35,"story_beat":0,"xp":0,"xp_next":50}

View File

@ -21,6 +21,10 @@ async function prepare_gamestate() {
}
tick_meter_running = true;
if (window.pywebview.api.debug_mode) {
dev_toolbox(true);
}
}
function save_game() {

View File

@ -45,6 +45,44 @@ function record_log(text) {
page_elements["div_log"].append(div_logrow);
}
function record_log_with_choices() {
const div_logrow = document.createElement("div");
if (bool_log_alt) { div_logrow.className = "log-line"; }
else { div_logrow.className = "log-line-alt"; }
bool_log_alt = !bool_log_alt;
const div_logtick = document.createElement("div");
div_logtick.className = "log-tick"
div_logtick.innerHTML = "Day " + gamestate["tick"];
div_logrow.append(div_logtick);
const div_logdata = document.createElement("div");
const div_logmsg = document.createElement("div");
div_logmsg.innerHTML = arguments[0];
div_logmsg.className = "log-msg";
div_logdata.append(div_logmsg);
const div_logactions = document.createElement("div");
div_logactions.className = "log-button-row";
for (var i = 1; i < arguments.length; i += 2) {
console.log(i)
var label = arguments[i];
var callback = arguments[i+1];
var btn_action = document.createElement("button");
btn_action.innerHTML = label;
btn_action.setAttribute("onclick", callback + "; tick_meter_running = true;");
div_logactions.append(btn_action);
}
div_logdata.append(div_logactions);
div_logrow.append(div_logdata);
page_elements["div_log"].append(div_logrow);
tick_meter_running = false;
}
function update_ui() {
page_elements["lbl_name"].innerHTML = gamestate["name"];
page_elements["lbl_tick"].innerHTML = gamestate["tick"];
@ -78,6 +116,18 @@ function dev_toolbox(open) {
dev_toolbox_open = open;
}
async function steal_resource(resource, amount, items) {
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"]) {
gamestate[resource] += amount;
record_log("Stole " + resource + " from a human: " + items.join(", "));
}
else { record_log("Didn't steal " + resource + " from a human"); }
}
async function game_tick() {
gamestate["tick"] += 1;
ticks_since_last_save += 1;
@ -102,7 +152,55 @@ async function game_tick() {
// Flavor event - no gameplay effect, but occasionally says something fun.
record_log(tickdata["log"]);
} else if (tickdata["event_type"] == 10) { // ENCHUMAN
var total_food = 0;
var food_descs = [];
var total_shinies = 0;
var shinies_descs = [];
switch (page_elements["menu_enc_human"].value) {
case "pause":
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";
}
if (total_shinies > 0) {
logstring += "<li><b>" + total_shinies + " 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)})`
)
break;
case "steal-food":
tickdata.items.food.forEach((item) => {
total_food += item["amount"];
food_descs.push(item["desc"]);
})
steal_resource("food", total_food, food_descs);
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);
break;
default:
console.error("undefined action " + page_elements["menu_enc_human"]);
break;
}
}
// sanity check