55 lines
2.7 KiB
Python
55 lines
2.7 KiB
Python
from review_rules.comprehension_history import DIMENSIONS, PLACE, place_dimensions, Seat
|
|
from failure import Failure
|
|
from larv_review.seats import FIRST_SEATS, SPLIT_SEATS, vote_line
|
|
|
|
VOTES = ("better", "same", "worse")
|
|
|
|
|
|
def votes(seats: list[Seat]) -> dict[str, list[str]] | Failure:
|
|
"""Each seat's five votes as written, in DIMENSIONS' order. Fails unless the seats come in a votes file's order and each seat's vote line and places have its shape. `counted` decides what the votes count as."""
|
|
names = [seat.name for seat in seats]
|
|
if names[:2] != list(FIRST_SEATS) or names[2:] not in ([], list(SPLIT_SEATS)):
|
|
return Failure(f"a paired ruling's seats are {' and '.join(FIRST_SEATS)}, then, when their overall votes differ, {' and '.join(SPLIT_SEATS)}, in that order; the file has {', '.join(names) or 'none'}")
|
|
said = {}
|
|
for seat in seats:
|
|
line = vote_line(seat.name, seat.said)
|
|
if isinstance(line, Failure):
|
|
return line
|
|
if unnamed := [place for place in seat.places if not place_dimensions(place)]:
|
|
return Failure(f"{seat.name}'s place does not open with the dimensions it moves, each one of {', '.join(DIMENSIONS)}, then `: `: {unnamed[0]}")
|
|
said[seat.name] = line
|
|
return said
|
|
|
|
|
|
def overall(votes: list[str]) -> str:
|
|
"""The overall vote, which DIMENSIONS lists last."""
|
|
return votes[-1]
|
|
|
|
|
|
def counted(seats: list[Seat], said: dict[str, list[str]]) -> dict[str, list[str]]:
|
|
"""Each seat's votes as they count: a dimension's vote counts as same unless one of the seat's `path:line` places opens with that dimension, and the overall vote unless the seat names any such place."""
|
|
return {seat.name: [vote if names_a_place(seat, name) else "same" for name, vote in zip(DIMENSIONS, said[seat.name])] for seat in seats}
|
|
|
|
|
|
def names_a_place(seat: Seat, dimension: str) -> bool:
|
|
return any(PLACE.search(place) and (dimension == DIMENSIONS[-1] or dimension in place_dimensions(place)) for place in seat.places)
|
|
|
|
|
|
def surplus(overall: list[str]) -> bool:
|
|
"""Whether seats beyond the first two voted, though the first two agree overall: their agreement settles the ruling by itself."""
|
|
return len(overall) > 2 and overall[0] == overall[1]
|
|
|
|
|
|
def split(overall: list[str]) -> bool:
|
|
"""Whether only the first two seats voted overall and they differ, so two more must vote: two votes that differ cannot reach a lead of two."""
|
|
return len(overall) == 2 and overall[0] != overall[1]
|
|
|
|
|
|
def ruling(overall: list[str]) -> str:
|
|
return lead(overall) or "same"
|
|
|
|
|
|
def lead(votes: list[str]) -> str | None:
|
|
"""The vote that leads each other vote by two or more, if one does."""
|
|
return next((vote for vote in VOTES if all(votes.count(vote) >= votes.count(other) + 2 for other in VOTES if other != vote)), None)
|