diff --git a/app.py b/app.py index 1fec8b1..032fb35 100644 --- a/app.py +++ b/app.py @@ -238,7 +238,7 @@ def quanah_add_item(): from quanah import store body = request.get_json(silent=True) or {} - store.add_item(body.get("slot"), body.get("lines")) + store.add_item(body.get("slot"), body.get("lines"), item_class=body.get("item_class")) return jsonify(store.payload()) @@ -247,7 +247,7 @@ def quanah_update_item(identifier): from quanah import store body = request.get_json(silent=True) or {} - store.update_item(identifier, body.get("slot"), body.get("lines")) + store.update_item(identifier, body.get("slot"), body.get("lines"), body.get("item_class")) return jsonify(store.payload()) @@ -290,7 +290,7 @@ def quanah_set_mission(identifier): from quanah import missions body = request.get_json(silent=True) or {} - return jsonify(missions.set_done(identifier, body.get("done"))) + return jsonify(missions.set_done(identifier, body.get("done"), body.get("cascade", True))) @app.delete("/api/quanah/missions/round") diff --git a/src/quanah/data/missions.json b/src/quanah/data/missions.json index 3a56174..53028a3 100644 --- a/src/quanah/data/missions.json +++ b/src/quanah/data/missions.json @@ -606,6 +606,7 @@ "key": "starforce", "name": "Star Force", "type": "permanent", + "optional_cascade": true, "chains": [ { "key": "starforce", diff --git a/src/quanah/missions.py b/src/quanah/missions.py index 741ef9a..1c55e42 100644 --- a/src/quanah/missions.py +++ b/src/quanah/missions.py @@ -3,8 +3,9 @@ One global list, like the Quanah pieces. Missions sit in ordered chains (inspector levels, Battlesim floors, each boss's difficulties, star force levels): completing a mission completes every one before it in its chain, and undoing one undoes every -one after it, so a chain never has gaps. Round missions reset when the character -dies; permanent missions are kept. +one after it, so a chain never has gaps. Star force is the exception: an item can be +tapped straight from 23 to 24, so its group is marked optional_cascade and the caller chooses. +Round missions reset when the character dies; permanent missions are kept. """ import json @@ -33,13 +34,13 @@ def catalog(): @lru_cache(maxsize=1) def _index(): - """Each mission id -> (group type, its chain's ids in order, its place in the chain).""" + """Each mission id -> (group type, its chain's ids in order, its place in it, cascade optional).""" index = {} for group in catalog()["groups"]: for chain in group["chains"]: ids = [m["id"] for m in chain["missions"]] for place, identifier in enumerate(ids): - index[identifier] = (group["type"], ids, place) + index[identifier] = (group["type"], ids, place, bool(group.get("optional_cascade"))) return index @@ -80,19 +81,23 @@ def _write(done): os.unlink(name) -def cascade(done, identifier, complete): - """DONE (a set of ids) after marking IDENTIFIER complete or not, cascading along its chain.""" +def mark(done, identifier, complete, cascade=True): + """DONE (a set of ids) after marking IDENTIFIER complete or not, cascading along its chain. + + CASCADE=False touches only that mission, but only where the group's cascade is optional.""" if identifier not in _index(): raise ValueError("Unknown mission") - _, ids, place = _index()[identifier] + _, ids, place, optional = _index()[identifier] + if not cascade and optional: + return set(done) | {identifier} if complete else set(done) - {identifier} return set(done) | set(ids[: place + 1]) if complete else set(done) - set(ids[place:]) -def set_done(identifier, complete): - if not isinstance(complete, bool): - raise ValueError("done must be true or false") +def set_done(identifier, complete, cascade=True): + if not isinstance(complete, bool) or not isinstance(cascade, bool): + raise ValueError("done and cascade must be true or false") with lock: - done = cascade(read(), identifier, complete) + done = mark(read(), identifier, complete, cascade) _write([i for i in _index() if i in done]) return payload() diff --git a/src/quanah/store.py b/src/quanah/store.py index fb2c3a9..cf96633 100644 --- a/src/quanah/store.py +++ b/src/quanah/store.py @@ -117,12 +117,16 @@ def validate_lines(lines): return list(lines) +def _check_class(item_class): + if item_class is not None and item_class not in _class_keys(): + raise ValueError("Unknown item class") + + def add_item(slot, lines, *, raw=None, name=None, item_class=None, source="manual"): if slot not in solver.slot_keys(): raise ValueError("Choose a slot") lines = validate_lines(lines) - if item_class is not None and item_class not in _class_keys(): - raise ValueError("Unknown item class") + _check_class(item_class) with lock: data = read() if not data["class"]: @@ -150,20 +154,22 @@ def _piece(data, identifier): return item -def update_item(identifier, slot, lines): - """Correct a piece's slot and lines by hand. A scanned piece keeps what was read, marked edited.""" +def update_item(identifier, slot, lines, item_class=None): + """Correct a piece's slot, lines and class by hand. A scanned piece keeps what was read, marked edited.""" if slot not in solver.slot_keys(): raise ValueError("Choose a slot") lines = validate_lines(lines) + _check_class(item_class) with lock: data = read() item = _piece(data, identifier) - if (item["slot"], item["lines"]) != (slot, lines): + item_class = item_class or item.get("item_class") + if (item["slot"], item["lines"], item.get("item_class")) != (slot, lines, item_class): # A piece moved to another slot is no longer that slot's preferred or worn piece. if item["slot"] != slot: for flag in ONE_PER_SLOT: item.pop(flag, None) - item.update(slot=slot, lines=lines, edited=datetime.now(UTC).isoformat()) + item.update(slot=slot, lines=lines, item_class=item_class, edited=datetime.now(UTC).isoformat()) _write(data) return item diff --git a/tests/test_quanah_missions.py b/tests/test_quanah_missions.py index 8ee2695..5d05ef9 100644 --- a/tests/test_quanah_missions.py +++ b/tests/test_quanah_missions.py @@ -45,6 +45,17 @@ def test_completing_a_mission_completes_its_chain_up_to_it(): assert [i for i in done if i.startswith("floor-")] == ["floor-5", "floor-10", "floor-15"] +def test_star_force_can_skip_the_cascade_but_other_chains_cannot(): + assert missions.set_done("sf-17", True, False)["done"] == ["sf-17"] + assert missions.set_done("sf-10", True, False)["done"] == ["sf-10", "sf-17"] + # Undoing one without the cascade leaves the later ones. + assert missions.set_done("sf-10", False, False)["done"] == ["sf-17"] + # Bosses and levels always cascade. + assert missions.set_done("level-10", True, False)["done"][:2] == ["level-5", "level-10"] + with pytest.raises(ValueError): + missions.set_done("sf-5", True, "no") + + def test_undoing_a_mission_undoes_every_later_one_in_its_chain(): missions.set_done("level-50", True) missions.set_done("sf-22", True) diff --git a/tests/test_quanah_store.py b/tests/test_quanah_store.py index 40ddbc4..6fe7dc6 100644 --- a/tests/test_quanah_store.py +++ b/tests/test_quanah_store.py @@ -61,6 +61,22 @@ def test_other_class_pieces_are_flagged_and_used_as_transfers(): assert solution["picks"]["hat"] == mine and solution["picks"]["top"] == theirs +def test_a_hand_entered_piece_can_be_given_another_class_and_edited_later(client): + store.configure(class_key="warrior") + api = post(client, "/api/quanah/items", {"slot": "hat", "lines": ["dl"], "item_class": "bowman"}).get_json() + piece = next(i for i in api["items"] if i["slot"] == "hat") + assert piece["item_class"] == "bowman" and not piece["wearable"] + # Left out, it defaults to the selected class. + api = post(client, "/api/quanah/items", {"slot": "top", "lines": ["dl"]}).get_json() + assert next(i for i in api["items"] if i["slot"] == "top")["item_class"] == "warrior" + api = put(client, "/api/quanah/items/" + piece["id"], {"slot": "hat", "lines": ["dl"], "item_class": "warrior"}).get_json() + assert next(i for i in api["items"] if i["id"] == piece["id"])["wearable"] + with pytest.raises(ValueError): + store.add_item("hat", ["dl"], item_class="nope") + with pytest.raises(ValueError): + store.update_item(piece["id"], "hat", ["dl"], "nope") + + def test_one_preferred_piece_per_slot_locks_the_set(): store.configure("warrior") best = store.add_item("hat", ["dl", "wh", "reg"])["id"] diff --git a/web/pages/quanah-missions.js b/web/pages/quanah-missions.js index 9ca16cb..bc23264 100644 --- a/web/pages/quanah-missions.js +++ b/web/pages/quanah-missions.js @@ -2,7 +2,10 @@ import {api} from '../api.js'; import {escapeHtml as esc,toast} from '../ui.js'; // Dimensional missions: one checkmark each. Completing a mission completes every one before it -// in its chain (levels, floors, a boss's difficulties, star force); the server does the cascade. +// in its chain (levels, floors, a boss's difficulties); the server does the cascade. Star force is +// optional: an item can be tapped from 23 to 24 alone, so a toggle chooses whether it cascades. +const CASCADE_KEY='quanah-starforce-cascade'; +const readCascade=()=>{try{return localStorage.getItem(CASCADE_KEY)==='1';}catch{return false;}}; const fmt=n=>Number(n).toLocaleString('en-US'); const TYPE={round:'Round',permanent:'Permanent'}; @@ -26,7 +29,7 @@ export function renderSummary(model){ const box=(m,on)=>``; -export function renderGroup(group,done){ +export function renderGroup(group,done,cascade=false){ const set=new Set(done),missions=group.chains.flatMap(c=>c.missions); const finished=missions.filter(m=>set.has(m.id)); const points=finished.reduce((a,m)=>a+m.points,0),max=missions.reduce((a,m)=>a+m.points,0); @@ -36,12 +39,13 @@ export function renderGroup(group,done){ :`
Loading Quanah...
'; const alive=()=>!disposed&&root.isConnected; @@ -165,8 +167,12 @@ async function mountSolver(root){ const slots=catalog.slots.map(s=>``).join(''); const picked=Array.from({length:max},(_,i)=>pending[i]?chip(catalog,pending[i]):`line ${i+1}`).join(''); const piece=editing&&model.items.find(i=>i.id===editing); + // A piece saved without a class (older data) shows a blank choice rather than your class. + const unknown=piece&&!piece.item_class&&!pieceClass; const read=piece?.raw?.length?`Scanner read: ${piece.raw.map(esc).join(' · ')}
`:''; - return `