fix: keep automatic cultivation on one stable primary per species
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""Choose a single ranch representative per species, independently of gifts."""
|
||||
"""Choose a stable cultivation primary per species, independently of eligibility."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import random
|
||||
import threading
|
||||
|
||||
|
||||
@@ -20,14 +19,11 @@ def growth_score(catalog, mon):
|
||||
|
||||
|
||||
def quality_rank(catalog, mon):
|
||||
"""Compare letter grades before using raw growth to break exact-grade ties."""
|
||||
"""Compare S counts, then the equal-weight mean of all normalized growth."""
|
||||
qualities = catalog.quality(mon)
|
||||
if any(value is None for value in qualities):
|
||||
return None
|
||||
# A and B grades are compared per attribute. A raw average must never
|
||||
# make AASA outrank SSAS (or turn an S-grade advantage into a B-grade one).
|
||||
return (sum(value == 4 for value in qualities), sum(value == 3 for value in qualities),
|
||||
sum(value == 2 for value in qualities), growth_score(catalog, mon))
|
||||
return (sum(value == 4 for value in qualities), growth_score(catalog, mon))
|
||||
|
||||
|
||||
def quality_summary(catalog, mon):
|
||||
@@ -37,6 +33,8 @@ def quality_summary(catalog, mon):
|
||||
|
||||
|
||||
class CultivationSelector:
|
||||
"""Persist primary IDs in the caller's account/role-scoped choices file."""
|
||||
|
||||
def __init__(self, catalog, path=None):
|
||||
self.catalog = catalog
|
||||
self.path = Path(path) if path else None
|
||||
@@ -47,17 +45,23 @@ class CultivationSelector:
|
||||
self.lock = threading.RLock()
|
||||
|
||||
def rank(self, mon):
|
||||
values = growth_values(self.catalog, mon)
|
||||
"""Comparable (level, feel, S count, equal-weight normalized growth)."""
|
||||
return (int(mon.get("level") or 1), int(mon.get("feel") or 0),
|
||||
round(sum(values) / 4, 9) if values is not None else -1,
|
||||
values if values is not None else (-1,) * 4)
|
||||
*(quality_rank(self.catalog, mon) or (-1, -1)))
|
||||
|
||||
def select(self, snapshot):
|
||||
"""Return {str(typeId): current primary row} for mature ranch monsters.
|
||||
|
||||
Initial selection uses rank, then the oldest ID. An existing primary
|
||||
changes only for a strictly higher-level ranch monster or confirmed
|
||||
removal. A saved ID still present outside the ranch blocks fallback.
|
||||
"""
|
||||
with self.lock:
|
||||
owner = str(snapshot.raw["master"]["id"])
|
||||
ranch = [mon for mon in snapshot.raw["monsters"] if mon.get("process") == 3 and mon.get("typeId") is not None]
|
||||
signature = (owner, tuple((int(mon["id"]), str(mon["typeId"]), mon.get("level"), mon.get("feel"),
|
||||
*(mon.get(key) for key in ("gAtk", "gDef", "gHp", "gDex"))) for mon in ranch))
|
||||
monsters = snapshot.raw["monsters"]
|
||||
ranch = [mon for mon in monsters if mon.get("process") == 3 and mon.get("typeId") is not None]
|
||||
signature = (owner, tuple((int(mon["id"]), str(mon.get("typeId")), mon.get("process"), mon.get("level"), mon.get("feel"),
|
||||
*(mon.get(key) for key in ("gAtk", "gDef", "gHp", "gDex"))) for mon in monsters))
|
||||
if self.signature == signature:
|
||||
current = {int(mon["id"]): mon for mon in ranch}
|
||||
self.selected = {key: current[int(mon["id"])] for key, mon in self.selected.items()}
|
||||
@@ -72,17 +76,24 @@ class CultivationSelector:
|
||||
except (OSError, ValueError, TypeError, AttributeError):
|
||||
pass
|
||||
self.owner = owner
|
||||
# Snapshot.monsters is the complete authoritative roster. A removed
|
||||
# ID may be replaced, while a present non-ranch primary stays saved.
|
||||
current_ids = {int(mon["id"]) for mon in monsters}
|
||||
choices = {key: mid for key, mid in self.choices.items() if mid in current_ids}
|
||||
groups = {}
|
||||
for mon in ranch:
|
||||
groups.setdefault(str(mon["typeId"]), []).append(mon)
|
||||
selected = {}
|
||||
for key, members in groups.items():
|
||||
ranked = [(self.rank(mon), mon) for mon in members]
|
||||
highest = max(rank for rank, _ in ranked)
|
||||
tied = [mon for rank, mon in ranked if rank == highest]
|
||||
previous = next((mon for mon in tied if int(mon["id"]) == self.choices.get(key)), None)
|
||||
selected[key] = previous if previous is not None else tied[0] if len(tied) == 1 else random.choice(tied)
|
||||
choices = {key: int(mon["id"]) for key, mon in selected.items()}
|
||||
previous = next((mon for mon in members if int(mon["id"]) == choices.get(key)), None)
|
||||
if previous is None and key in choices:
|
||||
continue
|
||||
highest_level = max(int(mon.get("level") or 1) for mon in members)
|
||||
if previous is not None and int(previous.get("level") or 1) >= highest_level:
|
||||
selected[key] = previous
|
||||
else:
|
||||
selected[key] = max(members, key=lambda mon: (self.rank(mon), -int(mon["id"])))
|
||||
choices.update({key: int(mon["id"]) for key, mon in selected.items()})
|
||||
if choices != self.choices:
|
||||
if self.path:
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user