Select one cultivation target per species and retain growth improvements

This commit is contained in:
AttilaNet
2026-11-08 12:08:29 +08:00
parent b172d1ffb9
commit 1eef0f80d1
9 changed files with 366 additions and 37 deletions
+38 -22
View File
@@ -1,5 +1,6 @@
"""Conservative item gifting, using the original client's caps and cooldown."""
from .model import Action, seconds
from .cultivation import CultivationSelector
def item_reserve(item_type, ranch, quests):
@@ -7,15 +8,20 @@ def item_reserve(item_type, ranch, quests):
class GiftPlanner:
def __init__(self, catalog):
def __init__(self, catalog, cultivation=None):
self.catalog = catalog
self.cultivation = cultivation or CultivationSelector(catalog)
self.notes = []
def candidates(self, snapshot, policy):
if policy.gift_auto_representative:
return sorted(self.cultivation.select(snapshot).values(), key=lambda mon: self.cultivation.rank(mon), reverse=True)
monsters = snapshot.monsters
return [monsters[int(key)] for key, _ in sorted(policy.gift_targets.items(), key=lambda pair: pair[1])
if int(key) in monsters and monsters[int(key)].get("process") == 3]
def target(self, snapshot, policy):
for key, _ in sorted(policy.gift_targets.items(), key=lambda pair: pair[1]):
mon = snapshot.monsters.get(int(key))
if not mon or mon.get("process") != 3:
continue
for mon in self.candidates(snapshot, policy):
limit = self.catalog.max_feel(mon)
if int(mon.get("feel") or 0) < limit:
return mon, limit
@@ -28,16 +34,6 @@ class GiftPlanner:
if not any(rule.get("enabled") for rule in policy.gift_items.values()):
self.notes.append("自动赠礼:请先在“背包”勾选允许使用的物品")
return []
target, limit = self.target(snapshot, policy)
if not target:
self.notes.append("自动赠礼:未选择目标,或目标已达到当前好感上限")
return []
cooldown = int(self.catalog.data["game_setting"].get("monGiftTime", 7200))
elapsed = snapshot.now - seconds(target.get("giftTime"))
if elapsed < cooldown:
self.notes.append(f"自动赠礼:{self.catalog.monster_label(target)} 还需等待 {int(cooldown - elapsed)} 秒")
return []
likes = self.catalog.monster(target.get("typeId")).get("like") or {}
totals, blocked = {}, set()
for item in snapshot.raw["items"]:
key = str(item.get("typeId"))
@@ -54,13 +50,33 @@ class GiftPlanner:
continue
reserve = item_reserve(key, policy, quest_policy) if quest_policy else int(rule.get("reserve", 0))
available = max(0, totals[key] - max(int(rule.get("reserve", 0)), reserve))
if available and (not policy.gift_liked_only or int(likes.get(key, 0)) > 0):
if available:
stocks[key] = available
if not stocks:
self.notes.append("自动赠礼:背包中没有符合开关、保留数量和偏好规则的物品")
self.notes.append("自动赠礼:背包中没有符合开关和保留数量的物品")
return []
key = max(stocks, key=lambda k: (int(likes.get(k, 0)), stocks[k], -int(k)))
gain = int(likes.get(key, 0))
name = self.catalog.item_name(key)
return [Action("gift", f"赠礼 {self.catalog.monster_label(target)} ← {name}(好感 {target.get('feel', 0)}/{limit},预计 {gain:+d})",
"monsterService/monsterSendGift", {"monsterId": int(target["id"]), "itemId": int(key)})]
cooldown = int(self.catalog.data["game_setting"].get("monGiftTime", 7200))
candidates = self.candidates(snapshot, policy) if policy.gift_auto_representative else [self.target(snapshot, policy)[0]]
for target in candidates:
if target is None:
continue
limit = self.catalog.max_feel(target)
if int(target.get("feel") or 0) >= limit:
continue
elapsed = snapshot.now - seconds(target.get("giftTime"))
if elapsed < cooldown:
if len(self.notes) < 6:
self.notes.append(f"自动赠礼:{self.catalog.monster_label(target)} 还需等待 {int(cooldown - elapsed)} 秒")
continue
likes = self.catalog.monster(target.get("typeId")).get("like") or {}
usable = [key for key in stocks if not policy.gift_liked_only or int(likes.get(key, 0)) > 0]
if not usable:
continue
key = max(usable, key=lambda k: (int(likes.get(k, 0)), stocks[k], -int(k)))
gain = int(likes.get(key, 0))
name = self.catalog.item_name(key)
return [Action("gift", f"赠礼 {self.catalog.monster_label(target)} ← {name}(好感 {target.get('feel', 0)}/{limit},预计 {gain:+d})",
"monsterService/monsterSendGift", {"monsterId": int(target["id"]), "itemId": int(key)})]
self.notes.append("自动赠礼:培养代表已达好感上限、处于冷却或缺少允许的礼物" if policy.gift_auto_representative
else "自动赠礼:未选择目标、目标已达当前好感上限或没有符合偏好的礼物")
return []