83 lines
4.2 KiB
Python
83 lines
4.2 KiB
Python
"""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):
|
||
return max(int(ranch.reserved_eggs.get(item_type, 0)), int(quests.item_reserves.get(item_type, 0)))
|
||
|
||
|
||
class GiftPlanner:
|
||
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 mon in self.candidates(snapshot, policy):
|
||
limit = self.catalog.max_feel(mon)
|
||
if int(mon.get("feel") or 0) < limit:
|
||
return mon, limit
|
||
return None, 0
|
||
|
||
def plan(self, snapshot, policy, quest_policy=None):
|
||
self.notes = []
|
||
if not policy.gift_enabled:
|
||
return []
|
||
if not any(rule.get("enabled") for rule in policy.gift_items.values()):
|
||
self.notes.append("自动赠礼:请先在“背包”勾选允许使用的物品")
|
||
return []
|
||
totals, blocked = {}, set()
|
||
for item in snapshot.raw["items"]:
|
||
key = str(item.get("typeId"))
|
||
totals[key] = totals.get(key, 0) + int(item.get("num") or 0)
|
||
if item.get("lock") or item.get("feedLock"):
|
||
blocked.add(key)
|
||
stocks = {}
|
||
for key, rule in policy.gift_items.items():
|
||
if not rule.get("enabled") or totals.get(key, 0) <= 0:
|
||
continue
|
||
if self.catalog.item_type(key) in (5, 6) or key in blocked:
|
||
continue
|
||
if not self.catalog.item(key).get("sellable"):
|
||
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:
|
||
stocks[key] = available
|
||
if not stocks:
|
||
self.notes.append("自动赠礼:背包中没有符合开关和保留数量的物品")
|
||
return []
|
||
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 []
|