From 941ea3a435c048828dd783d714f5c24f9fceed7a Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 9 Apr 2025 17:33:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=AF=E5=88=86=E6=89=93=E5=8A=AB=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/point_trade/config.toml | 13 ++- plugins/point_trade/main.py | 165 +++++++++++++++++++++++++++++++- 2 files changed, 173 insertions(+), 5 deletions(-) diff --git a/plugins/point_trade/config.toml b/plugins/point_trade/config.toml index 3ba275e..64d1049 100644 --- a/plugins/point_trade/config.toml +++ b/plugins/point_trade/config.toml @@ -1,9 +1,18 @@ [PointTrade] enable = true -command = ["积分交易", "积分转账", "转账积分", "积分赠送", "赠送积分", "积分转移", "转移积分", "送积分", "积分送人", "送人积分", "积分赠予", "赠予", "我的积分", "积分排行"] +command = ["积分交易", "积分转账", "转账积分", "积分赠送", "赠送积分", "积分转移", "转移积分", "送积分", "积分送人", "送人积分", "积分赠予", "赠予", "我的积分", "积分排行", "打劫"] command-format = """ 🔄转账积分: 积分转账 积分数 @用户 我的积分 - 查询个人积分详情 积分排行 - 查看群内积分排行榜 -""" \ No newline at end of file +打劫 @用户 - 尝试打劫用户积分(有风险) +""" + +# 打劫功能配置 +rob-success-rate = 0.3 # 打劫成功率,范围0-1 +rob-min-percent = 0.1 # 打劫成功时最小获取目标积分百分比 +rob-max-percent = 0.3 # 打劫成功时最大获取目标积分百分比 +rob-penalty-percent = 0.2 # 打劫失败时的惩罚百分比(扣除自身积分的比例) +rob-cooldown = 1800 # 打劫冷却时间(秒),默认1小时 +rob-min-points = 10 # 打劫最低积分要求(打劫者和目标都需满足) \ No newline at end of file diff --git a/plugins/point_trade/main.py b/plugins/point_trade/main.py index 1560488..506828f 100644 --- a/plugins/point_trade/main.py +++ b/plugins/point_trade/main.py @@ -70,14 +70,26 @@ class PointTradePlugin(MessagePluginInterface): # 从配置中获取参数 point_trade_config = self._config.get("PointTrade", {}) - self._commands = point_trade_config.get("command", ["积分交易", "积分转账", "转账积分", "我的积分", "积分排行"]) + self._commands = point_trade_config.get("command", ["积分交易", "积分转账", "转账积分", "我的积分", "积分排行", "打劫"]) self.command_format = point_trade_config.get("command-format", """ 积分交易指令: 积分转账 积分数 @用户 - 转账给指定用户 我的积分 - 查询个人积分详情 积分排行 - 查看群内积分排行榜 +打劫 @用户 - 尝试打劫用户积分(有风险) """) self.enable = point_trade_config.get("enable", True) + + # 打劫功能配置 + self.rob_success_rate = point_trade_config.get("rob-success-rate", 0.3) # 打劫成功率 + self.rob_min_percent = point_trade_config.get("rob-min-percent", 0.1) # 打劫最小百分比 + self.rob_max_percent = point_trade_config.get("rob-max-percent", 0.3) # 打劫最大百分比 + self.rob_penalty_percent = point_trade_config.get("rob-penalty-percent", 0.2) # 打劫失败惩罚百分比 + self.rob_cooldown = point_trade_config.get("rob-cooldown", 1800) # 打劫冷却时间(秒) + self.rob_min_points = point_trade_config.get("rob-min-points", 10) # 打劫最低积分要求 + + # 打劫冷却记录 {wxid: last_rob_time} + self.rob_cooldown_records = {} self.LOG.info(f"[{self.name}] 插件初始化完成,指令:{self._commands}") return True @@ -121,11 +133,12 @@ class PointTradePlugin(MessagePluginInterface): return False, "没有权限" # 处理不同的命令 - if command[0] == "我的积分": return self._handle_my_points(message) elif command[0] == "积分排行": return self._handle_points_ranking(message) + elif command[0] == "打劫": + return self._handle_rob_points(message) elif command[0] in self._commands: return self._handle_transfer_points(message) else: @@ -402,4 +415,150 @@ class PointTradePlugin(MessagePluginInterface): conn.commit() except mysql.connector.Error as e: self.LOG.error(f"更新用户积分失败: {e}") - raise \ No newline at end of file + raise + + def _handle_rob_points(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]: + """处理打劫积分命令""" + import random + import time + + content = str(message.get("content", "")).strip() + sender = message.get("sender") + roomid = message.get("roomid", "") + wcf: Wcf = message.get("wcf") + xml = message.get("xml", "") + + # 检查是否在群聊中 + if not roomid: + wcf.send_text("❌打劫功能仅在群聊中可用!", sender, "") + return True, "非群聊环境" + + # 检查冷却时间 + current_time = time.time() + if sender in self.rob_cooldown_records: + last_rob_time = self.rob_cooldown_records[sender] + time_passed = current_time - last_rob_time + if time_passed < self.rob_cooldown: + remaining_time = int(self.rob_cooldown - time_passed) + minutes, seconds = divmod(remaining_time, 60) + wcf.send_text(f"❌你最近已经打劫过了,需要冷却 {minutes}分{seconds}秒 后才能再次打劫!", + roomid, sender) + return True, "冷却中" + + # 检查@用户是否有效 + at_users = self.at_list(xml) + if len(at_users) != 1: + wcf.send_text(f"打劫失败❌\n请指定一个打劫目标!\n打劫 @用户", + roomid, sender) + return True, "目标无效" + + target_wxid = next(iter(at_users)) + robber_wxid = sender + + # 不能打劫自己 + if target_wxid == robber_wxid: + wcf.send_text("❌你不能打劫自己!", roomid, sender) + return True, "不能打劫自己" + + try: + # 获取打劫者和目标的积分信息 + robber_points = self.points_db.get_user_points(robber_wxid, roomid) + target_points = self.points_db.get_user_points(target_wxid, roomid) + + if not robber_points: + wcf.send_text("❌你没有积分记录,无法进行打劫!请先参与积分活动。", roomid, sender) + return True, "打劫者无积分" + + if not target_points: + wcf.send_text("❌目标没有积分记录,无法进行打劫!", roomid, sender) + return True, "目标无积分" + + robber_total = robber_points.get('total_points', 0) + target_total = target_points.get('total_points', 0) + + # 检查最低积分要求 + if robber_total < self.rob_min_points: + wcf.send_text(f"❌你的积分不足 {self.rob_min_points} 点,无法进行打劫!", roomid, sender) + return True, "打劫者积分不足" + + if target_total < self.rob_min_points: + wcf.send_text(f"❌目标积分不足 {self.rob_min_points} 点,不值得打劫!", roomid, sender) + return True, "目标积分不足" + + # 获取用户昵称 + robber_info = self._get_user_record(robber_wxid, roomid) + target_info = self._get_user_record(target_wxid, roomid) + + robber_name = robber_info.get('wx_nick_name', robber_wxid) if robber_info else robber_wxid + target_name = target_info.get('wx_nick_name', target_wxid) if target_info else target_wxid + + # 决定打劫是否成功 + is_success = random.random() < self.rob_success_rate + + # 更新冷却时间 + self.rob_cooldown_records[sender] = current_time + + if is_success: + # 打劫成功,随机获取目标一定比例的积分 + rob_percent = random.uniform(self.rob_min_percent, self.rob_max_percent) + rob_amount = int(target_total * rob_percent) + + # 确保至少抢到1点积分 + rob_amount = max(1, rob_amount) + + # 执行积分转移 + success, result = self.points_db.transfer_points( + target_wxid, robber_wxid, roomid, + rob_amount, f"被{robber_name}打劫" + ) + + if success: + # 获取转账后的积分信息 + from_user = result.get("from_user", {}) + to_user = result.get("to_user", {}) + + # 构建打劫成功消息 + output = ( + f"🔫 打劫成功!\n" + f"👤{robber_name} 成功打劫了 👤{target_name} {rob_amount} 积分!\n" + f"👤{robber_name} 当前积分: {to_user.get('total_points', 0)}\n" + f"👤{target_name} 当前积分: {from_user.get('total_points', 0)}" + ) + + wcf.send_text(output, roomid, sender) + return True, "打劫成功" + else: + wcf.send_text(f"❌打劫过程中出现问题:{result.get('error', '未知错误')}", roomid, sender) + return True, "打劫失败" + else: + # 打劫失败,扣除打劫者一定比例的积分作为惩罚 + penalty_amount = int(robber_total * self.rob_penalty_percent) + + # 确保至少扣除1点积分 + penalty_amount = max(1, penalty_amount) + + # 记录积分变动 + success, result = self.points_db.add_points( + robber_wxid, roomid, -penalty_amount, + PointSource.OTHER, f"打劫{target_name}失败的惩罚" + ) + + if success: + # 构建打劫失败消息 + output = ( + f"🚨 打劫失败!\n" + f"👤{robber_name} 试图打劫 👤{target_name} 但被当场抓获!\n" + f"👮‍♂️ 被罚款 {penalty_amount} 积分!\n" + f"👤{robber_name} 当前积分: {result.get('total_points', 0)}" + ) + + wcf.send_text(output, roomid, sender) + return True, "打劫失败" + else: + wcf.send_text(f"❌处理打劫惩罚时出现问题:{result.get('error', '未知错误')}", roomid, sender) + return True, "处理惩罚失败" + + except Exception as e: + self.LOG.error(f"处理打劫请求出错: {e}") + wcf.send_text(f"❌打劫过程中出现意外:{str(e)}", roomid, sender) + return True, f"处理出错: {str(e)}" \ No newline at end of file