Files
myrzx_bot/ranch_assistant/gifts.py
T

67 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Conservative item gifting, using the original client's caps and cooldown."""
from .model import Action, seconds
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):
self.catalog = catalog
self.notes = []
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
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 []
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"))
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 and (not policy.gift_liked_only or int(likes.get(key, 0)) > 0):
stocks[key] = available
if not stocks:
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)})]