state sync; the basic early game works
This commit is contained in:
19
app/pylocal/actions.py
Normal file
19
app/pylocal/actions.py
Normal 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")
|
@@ -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
11
app/pylocal/jsonizer.py
Normal 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)
|
@@ -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")
|
Reference in New Issue
Block a user