Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ tests/fixtures/address-in-prose.json -text
tests/fixtures/name-finder.cases.json -text
# The Kanka door's tables and wording, the same bytes as the npm SDK's copy (both tests hold its sha256).
src/onlyworlds/convert/kanka.json -text
# The known Pins reader's shared cases, the same bytes as the npm SDK's copy (both tests hold its sha256).
tests/fixtures/pins-known-to.cases.json -text
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.148.0 (2026-11-24)

- **Added: `pins_known_to(pins, served)`**: which of a map's Pins one reader knows, for a map drawn as one Character sees it. A Pin is known when the element it places is among `served`, the elements as that reader was served them (a list read with the reader's own key, or the owner's read with `view_as`); the function reads nothing else, so it cannot show a reader more than the world told them. The answer is `{"known", "unknown", "out"}`: `known` lists each known Pin as `{"pin", "map", "element_type", "element_id", "how"}` by Pin id in code point order, where `how` is `"whole"` or, when the element reached the reader as a stub it only knows of (`known_of` exactly `True`), `"known_of"`, so a drawing can tell a place heard of from a place known; `unknown` counts the Pins that place an element the reader was not served, as a number and never as ids; `out["no_link"]` counts the Pins that place nothing. The type and the id are compared as written, the last of two served rows with one type and id decides, and the answer carries no name or body of any element. It does not ask whether a Pin has a point or its Map exists: `map_space` reads that, and a drawing keeps the Pins both name. `pins` may be a whole world's list: only its rows of type `pin` are read. The Pins are the caller's to bring, from whichever read holds the map (an open world's, the owner's): a reader's own list need not hold them. A list or a tuple is taken for either argument; anything else is a `TypeError`. A rule of this package and the npm SDK (`pinsKnownTo` in `@onlyworlds/sdk` 4.200.0), not of the standard: the two run the same 31 cases and 8 refusals (`tests/fixtures/pins-known-to.cases.json`). Types `PinsKnownTo`, `PinKnown`.

## 0.147.0 (2026-11-24)

- **Added: `find.shared` on `name_finder`'s answer**: every name two or more elements hold, each a `NameShared` (`name`, `elements`), in the order each name was first met. `name` is the name as it is compared (trimmed, and folded when `case` is `"folded"`); `elements` is a tuple of the objects that were passed in, in the order given. `find` never answers such a name, and until now nothing said it was there: a tool reads the list to tell a writer why a name stays plain, or to test that no two elements of a world share one. A name too short to be looked for is not on it. `shared` is a read-only tuple, read once when the finder is made. New public names: `NameFinder` (what `name_finder` answers, called on a text as before) and `NameShared`.
Expand Down
16 changes: 15 additions & 1 deletion src/onlyworlds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@
trail_by_place,
)
from .lens import DATE_FIELD, LensMail, LensReading, date_of, is_lens, is_template, lenses_in_use, resolve_lens
from .map_space import MapLayout, MapSpace, MapSpaceMap, MapSpaceOutline, MapSpacePin, layout_of, map_space
from .map_space import (
MapLayout,
MapSpace,
MapSpaceMap,
MapSpaceOutline,
MapSpacePin,
PinKnown,
PinsKnownTo,
layout_of,
map_space,
pins_known_to,
)
from .message import (
EditRead,
SenderRead,
Expand Down Expand Up @@ -242,6 +253,8 @@
"NameShared",
"Opener",
"Patch",
"PinKnown",
"PinsKnownTo",
"PreconditionFailed",
"ProposedError",
"ProposedMark",
Expand Down Expand Up @@ -324,6 +337,7 @@
"out_degree_of",
"parse_ow_link",
"parse_retry_after",
"pins_known_to",
"plan_push",
"proposed_mark_of",
"push",
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.147.0"
__version__ = "0.148.0"
92 changes: 91 additions & 1 deletion src/onlyworlds/map_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
(``x_layout`` with a non-empty string ``method``; ``"hand"`` is a board), the served Map its layout stands on, and
the Pins a new lay-out keeps where they are. ``map_space`` reads none of these keys. The fixture's ``layout_cases``
are replayed whole by ``tests/test_layout_of.py``.

``pins_known_to(pins, served)`` is not the profile's: a rule of this package and the npm SDK. It answers which of a
map's Pins one reader knows, from the elements that reader was served and nothing else.
"""

from __future__ import annotations

from collections.abc import Iterable, Mapping
from collections.abc import Iterable, Mapping, Sequence
from typing import Any, Literal, NotRequired, TypedDict

from .lens import _whole
Expand Down Expand Up @@ -92,6 +95,29 @@ class MapLayout(TypedDict):
hand: list[str]


PinKnownHow = Literal["whole", "known_of"]


class PinKnown(TypedDict):
"""A Pin a reader knows: the element it places was served to that reader, ``whole`` or as a stub (``known_of``)."""

pin: str
#: The Pin's ``map`` as written when it is a non-empty string, else None. Not checked against any Map.
map: str | None
element_type: str
element_id: str
how: PinKnownHow


class PinsKnownTo(TypedDict):
"""``pins_known_to``'s answer: ``known`` by Pin id in code point order, ``unknown`` a count of the Pins that place
an element the reader was not served, ``out`` with ``no_link`` (a Pin that places nothing) when one was counted."""

known: list[PinKnown]
unknown: int
out: dict[str, int]


def _id(value: Any) -> str | None:
"""An id: a non-empty string, else None."""
return value if isinstance(value, str) and value != "" else None
Expand Down Expand Up @@ -252,3 +278,67 @@ def layout_of(
if this is not None and pin.get("map") == this and (method == "hand" or pin.get("x_hand") is True)
)
return {"layout": method, "ground": ground, "hand": hand}


def pins_known_to(pins: Sequence[Any], served: Sequence[Any]) -> PinsKnownTo:
"""Which of ``pins`` a reader knows, for a map drawn as one Character sees it: ``{known, unknown, out}``, all
three keys always present.

``pins`` is a list of elements, of which the rows of type ``pin`` are read (so a whole world's list can be
passed). ``served`` is the list of elements as that reader was served them, stubs included: a list read with the
reader's own key, or the owner's read as that Character (``view_as``). The function reads nothing else, so it
cannot show a reader more than the world told them. It does not ask whether a Pin has a point or its Map exists:
that is ``map_space``'s reading, and a drawing keeps the Pins both name.

A Pin is known when a row of ``served`` has the Pin's ``element_type`` as its ``type`` and the Pin's
``element_id`` as its ``id``, both as written (``Location`` is no ``location``, and the same id under another
type is another element). ``how`` is ``known_of`` when that row is a stub (``known_of`` exactly ``True``), else
``whole``; of two served rows with one type and id, the last one decides. A Pin with only one of ``element_type``
and ``element_id`` places nothing: it is counted in ``out["no_link"]`` and never known. A row without a string
``type`` and a non-empty string ``id`` is no element and is not counted; of two Pin rows with one id, the last
one is read. ``known`` is in Pin id order by code point; ``unknown`` is a count, never ids.

The answer carries ids and the two words only, never an element's name or body. A rule of this package and the
npm SDK (``pinsKnownTo``), not of the standard; the shared cases are ``tests/fixtures/pins-known-to.cases.json``.
Raises ``TypeError`` when ``pins`` or ``served`` is not a list or a tuple.
"""
if not isinstance(pins, (list, tuple)):
raise TypeError("pins_known_to: pins is a list of elements.")
if not isinstance(served, (list, tuple)):
raise TypeError("pins_known_to: served is a list of elements.")

served_how: dict[str, dict[str, PinKnownHow]] = {}
for element in served:
if not isinstance(element, Mapping) or not isinstance(element.get("type"), str):
continue
element_id = _id(element.get("id"))
if element_id is None:
continue
served_how.setdefault(element["type"], {})[element_id] = (
"known_of" if element.get("known_of") is True else "whole"
)

rows: dict[str, Mapping[str, Any]] = {}
for pin in pins:
if isinstance(pin, Mapping) and pin.get("type") == "pin" and (pin_id := _id(pin.get("id"))) is not None:
rows[pin_id] = pin

known: list[PinKnown] = []
unknown = 0
no_link = 0
for pin_id in sorted(rows):
pin = rows[pin_id]
placed_type, placed_id = _id(pin.get("element_type")), _id(pin.get("element_id"))
if placed_type is None or placed_id is None:
no_link += 1
continue
how = served_how.get(placed_type, {}).get(placed_id)
if how is None:
unknown += 1
continue
placed_map = _id(pin.get("map"))
known.append(
{"pin": pin_id, "map": placed_map, "element_type": placed_type, "element_id": placed_id, "how": how}
)
out: dict[str, int] = {"no_link": no_link} if no_link else {}
return {"known": known, "unknown": unknown, "out": out}
Loading
Loading