Skip to content

Commit 130ba40

Browse files
committed
BUFF自动发货支持多Steam账号发货
1 parent 1ea5a28 commit 130ba40

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

plugins/BuffAutoAcceptOffer.py

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22

3+
import utils.static as static
34
from BuffApi import BuffAccount
45
from utils.buff_helper import get_valid_session_for_buff
56
from utils.logger import PluginLogger, handle_caught_exception
@@ -8,6 +9,7 @@
89

910
logger = PluginLogger("BuffAutoAcceptOffer")
1011

12+
1113
class BuffAutoAcceptOffer:
1214
def __init__(self, steam_client, steam_client_mutex, config):
1315
self.steam_client = steam_client
@@ -30,6 +32,10 @@ def require_buyer_send_offer(self):
3032
except Exception as e:
3133
logger.error(f"开启买家发起交易报价功能失败: {str(e)}")
3234

35+
def get_steam_info(self):
36+
steam_info = self.buff_account.get('https://buff.163.com/account/api/steam/info').json()['data']
37+
return steam_info
38+
3339
def check_buff_account_state(self):
3440
try:
3541
username = self.buff_account.get_user_nickname()
@@ -52,11 +58,8 @@ def format_item_info(self, trade):
5258

5359
for good_id, good_item in trade["goods_infos"].items():
5460
result += f"发货商品:{good_item['name']}"
55-
if trade.get('items_to_trade'):
56-
if len(trade["items_to_trade"]) > 1:
57-
result += f" 等{len(trade['items_to_trade'])}个物品"
58-
else:
59-
result += " (求购报价,数量未知)"
61+
if len(trade.get('items_to_trade', [])) > 1:
62+
result += f" 等{len(trade['items_to_trade'])}个物品"
6063

6164
if trade["tradeofferid"] in self.order_info:
6265
price = float(self.order_info[trade["tradeofferid"]]["price"])
@@ -74,19 +77,28 @@ def exec(self):
7477

7578
try:
7679
user_info = self.buff_account.get_user_info()
77-
if not user_info or "steamid" not in user_info:
78-
logger.error("获取BUFF绑定的SteamID失败")
79-
exit_code.set(1)
80-
return 1
81-
82-
steamid_buff = user_info["steamid"]
80+
steamid_buff = user_info['steamid']
81+
logger.info('为了避免访问接口过于频繁,休眠5秒...')
82+
time.sleep(5)
83+
steam_info = self.get_steam_info()
8384
except Exception as e:
84-
logger.error(f"获取BUFF绑定的SteamID失败: {str(e)}")
85+
logger.error(f"获取BUFF用户信息失败!")
86+
handle_caught_exception(e, "BuffAutoAcceptOffer")
8587
exit_code.set(1)
8688
return 1
8789

88-
if self.steam_client.get_steam64id_from_cookies() != steamid_buff:
89-
logger.error("当前登录账号与BUFF绑定的Steam账号不一致!")
90+
to_exit = True
91+
if steam_info['max_bind_count'] == 1:
92+
if str(static.STEAM_64_ID) == steamid_buff:
93+
to_exit = False
94+
else:
95+
for account in steam_info['items']:
96+
if account['steamid'] == str(static.STEAM_64_ID):
97+
logger.info(f'检测到当前已经登录多个Steam账号,只会处理SteamID为{static.STEAM_64_ID}的交易')
98+
to_exit = False
99+
break
100+
if to_exit:
101+
logger.error(f"当前登录的Steam账号不在BUFF账号绑定列表中,无法进行自动发货!")
90102
exit_code.set(1)
91103
return 1
92104

@@ -97,7 +109,8 @@ def exec(self):
97109
else:
98110
logger.info("当前账号已开启只允许买家发起报价功能")
99111

100-
ignored_offer = []
112+
ignored_offer = {} # 使用字典记录忽略次数
113+
REPROCESS_THRESHOLD = 10 # 定义重新处理的阈值
101114
interval = self.config["buff_auto_accept_offer"]["interval"]
102115
dota2_support = self.config["buff_auto_accept_offer"].get("dota2_support", False)
103116

@@ -109,7 +122,7 @@ def exec(self):
109122

110123
while True:
111124
try:
112-
logger.info("正在检查BUFF账户登录状态...")
125+
logger.info("正在进行BUFF待发货/待收货饰品检查...")
113126
username = self.check_buff_account_state()
114127
if username == "":
115128
logger.info("BUFF账户登录状态失效, 尝试重新登录...")
@@ -118,10 +131,7 @@ def exec(self):
118131
logger.error("BUFF账户登录状态失效, 无法自动重新登录!")
119132
return
120133
self.buff_account = BuffAccount(session)
121-
logger.info("为了避免访问接口过于频繁,休眠5秒...")
122-
time.sleep(5)
123-
124-
logger.info("正在进行BUFF待发货/待收货饰品检查...")
134+
125135
notification = self.buff_account.get_notification()
126136

127137
# 处理响应检查是否有错误
@@ -161,8 +171,9 @@ def exec(self):
161171
if trade_offer["tradeofferid"] is not None and trade_offer["tradeofferid"] != "":
162172
self.order_info[trade_offer["tradeofferid"]] = trade_offer
163173
if not any(trade_offer["tradeofferid"] == trade["tradeofferid"] for trade in trades):
164-
165-
for goods_id,goods_info in response_data["goods_infos"].items():
174+
if str(trade_offer['seller_steamid']) != str(static.STEAM_64_ID):
175+
continue # 跳过不是当前账号的报价
176+
for goods_id, goods_info in response_data["goods_infos"].items():
166177
goods_id = str(goods_id)
167178
trade_offer["goods_id"] = str(trade_offer["goods_id"])
168179
if goods_id == trade_offer["goods_id"]:
@@ -183,24 +194,39 @@ def exec(self):
183194
if len(trades) != 0:
184195
for i, trade in enumerate(trades):
185196
offer_id = trade["tradeofferid"]
186-
187197
logger.info(f"正在处理第 {i+1} 个交易报价 报价ID:{offer_id}")
188-
if offer_id not in ignored_offer:
198+
199+
process_this_offer = False # 标记是否需要处理当前报价
200+
201+
if offer_id in ignored_offer:
202+
ignored_offer[offer_id] += 1 # 增加计数
203+
if ignored_offer[offer_id] > REPROCESS_THRESHOLD:
204+
logger.warning(f"报价 {offer_id} 已被忽略 {ignored_offer[offer_id]-1} 次,超过阈值 {REPROCESS_THRESHOLD},将尝试重新处理")
205+
del ignored_offer[offer_id] # 从忽略字典中移除
206+
process_this_offer = True # 标记需要处理
207+
else:
208+
logger.info(f"该报价已被处理过,跳过")
209+
process_this_offer = False # 标记不需要处理
210+
else:
211+
# 如果不在忽略列表里,标记需要处理
212+
process_this_offer = True
213+
214+
if process_this_offer:
189215
try:
190216
logger.info("正在接受报价...")
191217
desc = self.format_item_info(trade)
192218
if accept_trade_offer(self.steam_client, self.steam_client_mutex, offer_id, desc=desc):
193-
ignored_offer.append(offer_id)
219+
ignored_offer[offer_id] = 1 # 成功接受后,加入忽略字典,计数为1
194220
logger.info("接受完成! 已经将此交易报价加入忽略名单!")
221+
# else: # 可选:处理 accept_trade_offer 返回 False 但未抛出异常的情况
222+
# logger.warning(f"尝试接受报价 {offer_id} 失败,但未添加到忽略列表。")
195223

196224
if trades.index(trade) != len(trades) - 1:
197225
logger.info("为了避免频繁访问Steam接口, 等待5秒后继续...")
198226
time.sleep(5)
199227
except Exception as e:
200228
logger.error(f"处理交易报价时出错: {str(e)}", exc_info=True)
201229
logger.info("出现错误, 稍后再试!")
202-
else:
203-
logger.info("该报价已经被处理过, 跳过.")
204230

205231
except Exception as e:
206232
handle_caught_exception(e, "BuffAutoAcceptOffer")

0 commit comments

Comments
 (0)