import datetime import json import logging # 所有的下注选项 OPTIONS = [101, 102, 103, 301, 302, 303, 304, 305] # 下注选项 class BetOption: def __init__(self, option, odds, limit, total_bet=0): self.option = option self.odds = odds self.limitRed = limit self.total_bet = total_bet def __str__(self): return (f"BetOption(option={self.option}, odds={self.odds}, " f"limitRed={self.limitRed}, totalBet={self.total_bet})") # 用户信息 class User: # 对应option的下注金额 betOptionAmounts = {option: 0 for option in OPTIONS} def __init__(self, bet_count, uid, amount): self.bet_count = bet_count self.uid = uid self.betAmount = amount class UserEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, User): return {"uid": obj.uid, "bet_count": obj.bet_count, "betAmount": obj.betAmount} # 让基类的 default 方法抛出 TypeError return json.JSONEncoder.default(self, obj) # 用于存储User对象的用户信息和次数,key为用户ID,value为User对象 round_user_betinfo = {} # 本轮选项赔率和下注金额 round_option_info = {option: BetOption(option, 0, 0) for option in OPTIONS} async def deal_message(message, broadcast_func): try: msg_type, data = parse_message(message) if msg_type == "BetNotify": await betNotify(data) elif msg_type == "GameDataSynNotify": await gameDataSynNotify(data) elif msg_type == "KickNotify": await kickNotify(data, broadcast_func) elif msg_type == "GameRoundEndNotify": await gameRoundEndNotify() else: logging.info(f"Received message: {message}") except Exception as e: logging.info(f"Received message: {message}") async def gameRoundEndNotify(): print(f"收到结束下注GameRoundEndNotify") # 每把结束存储数据,然后清空数据 with open("../user_bet.log", "a") as f: time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 将user_dict 转成json字符串 f.write(f"{time}:{json.dumps(round_user_betinfo, cls=UserEncoder)}\n") round_user_betinfo.clear() async def kickNotify(data, broadcast_func): print(f"被踢下线了: {data}") await broadcast_func('''CMD: setTimeout(function(){ let btnDownNode = cc.find("Layer/PushNotice_panel/close_button"); if (btnDownNode && btnDownNode.getComponent(cc.Button)) { btnDownNode.getComponent(cc.Button).clickEvents[0].emit([btnDownNode]); } else { console.error("节点未找到或节点上没有cc.Button组件"); } },3000); setTimeout(function(){ let btnDownNode = cc.find("Canvas/container/content/smallGame/createScrollView/view/cotent/row1/pokerNode/porkermaster/allHand"); if (btnDownNode && btnDownNode.getComponent(cc.Button)) { btnDownNode.getComponent(cc.Button).clickEvents[0].emit([btnDownNode]); } else { console.error("没有找到扑克大师游戏节点"); } },4000); setTimeout(function(){ // 点击扑克大师游戏线路1 let pokerMasterGameLine1 = cc.find("Canvas/container/content/smallGame/New Node/listPokerMasterContent/main/view/content/item/main");//线路1 if (pokerMasterGameLine1 && pokerMasterGameLine1.getComponent(cc.Button)) { pokerMasterGameLine1.getComponent(cc.Button).clickEvents[0].emit([pokerMasterGameLine1]); } else { console.error("未找到扑克大师游戏游戏节点1"); } },5000); ''') async def gameDataSynNotify(data): # print(f"同步游戏数据: {data}") for optionInfo in data["optionInfo"]: option_ = optionInfo["option"] if option_ == 103: odds_ = 1 else: odds_ = optionInfo["odds"] limit_red_ = optionInfo["limitRed"] total_bet_ = optionInfo["totalBet"] round_option_info[option_] = BetOption(option_, odds_, limit_red_, total_bet_) print(f"同步后下注信息:", round_option_info) async def betNotify(data): # {"uid":4174549,"detail":{"option":302,"betAmount":1000,"auto":false,"is_shot":false,"win_amt":0,"odds":0,"betGameCoin":0},"curCoin":767943,"selfBet":1000,"totalBet":67000,"curUsdt":0} # print(f"下注: {data}") # 获取用户ID uid = data["uid"] option = data["detail"]["option"] bet_amount = data["detail"]["betAmount"] # 获取用户信息 user = round_user_betinfo.get(uid) if user is None: # 如果用户信息不存在,创建一个新的User对象 user = User(0, "", 0) round_user_betinfo[uid] = user # 更新用户信息 user.bet_count += 1 user.uid = uid user.betAmount += bet_amount # 更新本轮下注选项的金额 user.betOptionAmounts[option] += bet_amount # 更新本轮全部选项的金额 round_option_info[option].total_bet += bet_amount print(f"用户{uid}下注次数: {user.bet_count}, 总金额: {user.betAmount}") round_user_betinfo[uid] = user def parse_message(message): # 分割消息类型和JSON字符串 msg_type, json_str = message.split(':', 1) # 将JSON字符串转换为字典 data = json.loads(json_str) # 返回解析结果 return msg_type, data