PokerMasterDataProccesser.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import datetime
  2. import json
  3. import logging
  4. # 所有的下注选项
  5. OPTIONS = [101, 102, 103, 301, 302, 303, 304, 305]
  6. # 下注选项
  7. class BetOption:
  8. def __init__(self, option, odds, limit, total_bet=0):
  9. self.option = option
  10. self.odds = odds
  11. self.limitRed = limit
  12. self.total_bet = total_bet
  13. def __str__(self):
  14. return (f"BetOption(option={self.option}, odds={self.odds}, "
  15. f"limitRed={self.limitRed}, totalBet={self.total_bet})")
  16. # 用户信息
  17. class User:
  18. # 对应option的下注金额
  19. betOptionAmounts = {option: 0 for option in OPTIONS}
  20. def __init__(self, bet_count, uid, amount):
  21. self.bet_count = bet_count
  22. self.uid = uid
  23. self.betAmount = amount
  24. class UserEncoder(json.JSONEncoder):
  25. def default(self, obj):
  26. if isinstance(obj, User):
  27. return {"uid": obj.uid, "bet_count": obj.bet_count, "betAmount": obj.betAmount}
  28. # 让基类的 default 方法抛出 TypeError
  29. return json.JSONEncoder.default(self, obj)
  30. # 用于存储User对象的用户信息和次数,key为用户ID,value为User对象
  31. round_user_betinfo = {}
  32. # 本轮选项赔率和下注金额
  33. round_option_info = {option: BetOption(option, 0, 0) for option in OPTIONS}
  34. async def deal_message(message, broadcast_func):
  35. try:
  36. msg_type, data = parse_message(message)
  37. if msg_type == "BetNotify":
  38. await betNotify(data)
  39. elif msg_type == "GameDataSynNotify":
  40. await gameDataSynNotify(data)
  41. elif msg_type == "KickNotify":
  42. await kickNotify(data, broadcast_func)
  43. elif msg_type == "GameRoundEndNotify":
  44. await gameRoundEndNotify()
  45. else:
  46. logging.info(f"Received message: {message}")
  47. except Exception as e:
  48. logging.info(f"Received message: {message}")
  49. async def gameRoundEndNotify():
  50. print(f"收到结束下注GameRoundEndNotify")
  51. # 每把结束存储数据,然后清空数据
  52. with open("../user_bet.log", "a") as f:
  53. time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  54. # 将user_dict 转成json字符串
  55. f.write(f"{time}:{json.dumps(round_user_betinfo, cls=UserEncoder)}\n")
  56. round_user_betinfo.clear()
  57. async def kickNotify(data, broadcast_func):
  58. print(f"被踢下线了: {data}")
  59. await broadcast_func('''CMD:
  60. setTimeout(function(){
  61. let btnDownNode = cc.find("Layer/PushNotice_panel/close_button");
  62. if (btnDownNode && btnDownNode.getComponent(cc.Button)) {
  63. btnDownNode.getComponent(cc.Button).clickEvents[0].emit([btnDownNode]);
  64. } else {
  65. console.error("节点未找到或节点上没有cc.Button组件");
  66. }
  67. },3000);
  68. setTimeout(function(){
  69. let btnDownNode = cc.find("Canvas/container/content/smallGame/createScrollView/view/cotent/row1/pokerNode/porkermaster/allHand");
  70. if (btnDownNode && btnDownNode.getComponent(cc.Button)) {
  71. btnDownNode.getComponent(cc.Button).clickEvents[0].emit([btnDownNode]);
  72. } else {
  73. console.error("没有找到扑克大师游戏节点");
  74. }
  75. },4000);
  76. setTimeout(function(){
  77. // 点击扑克大师游戏线路1
  78. let pokerMasterGameLine1 = cc.find("Canvas/container/content/smallGame/New Node/listPokerMasterContent/main/view/content/item/main");//线路1
  79. if (pokerMasterGameLine1 && pokerMasterGameLine1.getComponent(cc.Button)) {
  80. pokerMasterGameLine1.getComponent(cc.Button).clickEvents[0].emit([pokerMasterGameLine1]);
  81. } else {
  82. console.error("未找到扑克大师游戏游戏节点1");
  83. }
  84. },5000);
  85. ''')
  86. async def gameDataSynNotify(data):
  87. # print(f"同步游戏数据: {data}")
  88. for optionInfo in data["optionInfo"]:
  89. option_ = optionInfo["option"]
  90. if option_ == 103:
  91. odds_ = 1
  92. else:
  93. odds_ = optionInfo["odds"]
  94. limit_red_ = optionInfo["limitRed"]
  95. total_bet_ = optionInfo["totalBet"]
  96. round_option_info[option_] = BetOption(option_, odds_, limit_red_, total_bet_)
  97. print(f"同步后下注信息:", round_option_info)
  98. async def betNotify(data):
  99. # {"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}
  100. # print(f"下注: {data}")
  101. # 获取用户ID
  102. uid = data["uid"]
  103. option = data["detail"]["option"]
  104. bet_amount = data["detail"]["betAmount"]
  105. # 获取用户信息
  106. user = round_user_betinfo.get(uid)
  107. if user is None:
  108. # 如果用户信息不存在,创建一个新的User对象
  109. user = User(0, "", 0)
  110. round_user_betinfo[uid] = user
  111. # 更新用户信息
  112. user.bet_count += 1
  113. user.uid = uid
  114. user.betAmount += bet_amount
  115. # 更新本轮下注选项的金额
  116. user.betOptionAmounts[option] += bet_amount
  117. # 更新本轮全部选项的金额
  118. round_option_info[option].total_bet += bet_amount
  119. print(f"用户{uid}下注次数: {user.bet_count}, 总金额: {user.betAmount}")
  120. round_user_betinfo[uid] = user
  121. def parse_message(message):
  122. # 分割消息类型和JSON字符串
  123. msg_type, json_str = message.split(':', 1)
  124. # 将JSON字符串转换为字典
  125. data = json.loads(json_str)
  126. # 返回解析结果
  127. return msg_type, data