Known answers for the bot: the guide's encounter answers and the bot's own results, by choice text (#35)
CI / test (pull_request) Successful in 2m3s
CI / test (pull_request) Successful in 2m3s
analysis/known/build_known.py writes data/public/known_answers.json from pdf_encounters.json and tree.json;
solver.js knownAnswer(known, offered, { target, sceneColour }) matches a screen's choices against it (the guide's
paraphrases by their words, the bot's texts exactly) and returns a pick when one choice is clearly on the guide's
recommended path to the target, plus the evidence for every choice to hand to Clef. Rebuilt daily, checked in CI,
and sent to the bot with the route model.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fefc163e09
commit
e10312ab02
@@ -0,0 +1,131 @@
|
||||
"""Known answers: what the Atlas knows about every answer the bot can be offered, for screens its other sources don't
|
||||
cover (issue #35). The bot looks the screen's choices up here first (solver.js `knownAnswer`), and passes what it finds
|
||||
to Clef as evidence when nothing is certain.
|
||||
|
||||
python analysis/known/build_known.py [--check] -> data/public/known_answers.json
|
||||
|
||||
Two sources, keyed by the answer text normalised exactly as the bot does (collection/etl/transform/bot.py normalize):
|
||||
|
||||
guide every answer in the community route PDF's encounter trees (data/public/pdf_encounters.json): the
|
||||
encounter, how it ends there (or "continues"), the guide's rate, whether the guide marks it unconfirmed,
|
||||
and `on_path`: the colours whose recommended path in the guide goes through it
|
||||
bot every answer in the bot's screen tree (data/public/tree.json): times played, times it died straight
|
||||
after, and the bonds the runs that took it ended in
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
from collection.etl.transform.bot import normalize # noqa: E402
|
||||
|
||||
GUIDE = ROOT / "data/public/pdf_encounters.json"
|
||||
TREE = ROOT / "data/public/tree.json"
|
||||
OUT = ROOT / "data/public/known_answers.json"
|
||||
# the colours each of the guide's encounters can end in a bond with (the two Greens can also end with an Orange or a
|
||||
# Brown swooping in), and the encounter a colour's route continues to from there (the shadow leads on to the Black)
|
||||
ENC_COLOURS = {"valley-Red": ["Red"], "tree-Red": ["Red"], "tree-Greens": ["Green", "Brown", "Orange"],
|
||||
"meadow-Orange": ["Orange"], "meadow-Brown": ["Brown"], "meadow-Blue": ["Blue"],
|
||||
"shadow": ["Black", "Blue"], "black": ["Black"]}
|
||||
ONWARD = {("shadow", "Black"): "Black"}
|
||||
|
||||
|
||||
def is_goal(end, colour, onward):
|
||||
end = end or ""
|
||||
if onward:
|
||||
return end.startswith(f"On to the {onward}")
|
||||
if not (end.startswith("Bonded") or "Bonded (" in end):
|
||||
return False
|
||||
return "(" not in end or colour in end
|
||||
|
||||
|
||||
def leads_to(o, colour, onward):
|
||||
if "end" in o:
|
||||
return is_goal(o["end"], colour, onward)
|
||||
return any(not c.get("roll") and leads_to(c, colour, onward) for c in o.get("then", []))
|
||||
|
||||
|
||||
def guide_path(options, colour, onward):
|
||||
"""The guide's recommended answers toward `colour` (the same walk as the page's encounter panel)."""
|
||||
out, opts = [], options
|
||||
while opts:
|
||||
live = [o for o in opts if not o.get("roll")]
|
||||
pick = next((o for o in live if o.get("best") and leads_to(o, colour, onward)), None) \
|
||||
or next((o for o in live if leads_to(o, colour, onward)), None)
|
||||
if not pick:
|
||||
break
|
||||
out.append(id(pick))
|
||||
if "end" in pick:
|
||||
break
|
||||
opts = pick.get("then", [])
|
||||
return out
|
||||
|
||||
|
||||
def walk(options, enc, on_path, out):
|
||||
"""Every answer of an encounter, with its colours (where it happens) and on_path (whose recommended path)."""
|
||||
for o in options:
|
||||
if o.get("roll"):
|
||||
continue
|
||||
entry = {"encounter": enc, "colours": ENC_COLOURS.get(enc, []), "end": o.get("end", "continues")}
|
||||
if o.get("pct"):
|
||||
entry["pct"] = o["pct"]
|
||||
if o.get("best"):
|
||||
entry["best"] = True
|
||||
if o.get("unconfirmed"):
|
||||
entry["unconfirmed"] = True
|
||||
cols = sorted(c for c, ids in on_path.items() if id(o) in ids)
|
||||
if cols:
|
||||
entry["on_path"] = cols
|
||||
for text in [s.strip() for s in o["say"].split(", or ")]: # "Jump on its back, or jump to land…"
|
||||
out.setdefault(normalize(text), {"text": text, "guide": []})["guide"].append(entry)
|
||||
walk(o.get("then", []), enc, on_path, out)
|
||||
|
||||
|
||||
def build():
|
||||
g = json.loads(GUIDE.read_text(encoding="utf-8"))
|
||||
out = {}
|
||||
for enc, E in g["encounters"].items():
|
||||
on_path = {c: set(guide_path(E["options"], c, ONWARD.get((enc, c)))) for c in ENC_COLOURS.get(enc, [])}
|
||||
walk(E["options"], enc, on_path, out)
|
||||
t = json.loads(TREE.read_text(encoding="utf-8")) if TREE.exists() else {"screens": {}}
|
||||
for sc in t["screens"].values():
|
||||
for o in sc["options"].values():
|
||||
if not o.get("n"):
|
||||
continue
|
||||
k = normalize(o["text"])
|
||||
e = out.setdefault(k, {"text": o["text"], "guide": []})
|
||||
b = e.setdefault("bot", {"n": 0, "died": 0, "bonded": {}})
|
||||
b["n"] += o["n"]
|
||||
for dest, d in o.get("to", {}).items():
|
||||
if dest == "DIED":
|
||||
b["died"] += d["n"]
|
||||
elif dest.startswith("BONDED:"):
|
||||
c = dest.split(":", 1)[1]
|
||||
b["bonded"][c] = b["bonded"].get(c, 0) + d["n"]
|
||||
for e in out.values():
|
||||
if not e["guide"]:
|
||||
del e["guide"]
|
||||
src = g["source"]
|
||||
return {"schema": 1, "guide": {"title": src["title"], "version": src["version"], "link": src["link"]},
|
||||
"bot": {"runs": (t.get("builtFrom") or {}).get("runs")}, "answers": dict(sorted(out.items()))}
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("--check", action="store_true")
|
||||
a = ap.parse_args(argv)
|
||||
text = json.dumps(build(), ensure_ascii=False, indent=1) + "\n"
|
||||
if a.check:
|
||||
if not OUT.exists() or OUT.read_text(encoding="utf-8") != text:
|
||||
print(f"{OUT.relative_to(ROOT)} is out of date (run python analysis/known/build_known.py)", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
OUT.write_text(text, encoding="utf-8")
|
||||
print(f"wrote {OUT.relative_to(ROOT)}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user