Browse Source

优化数据统计

Alex 1 year ago
parent
commit
14c1501690
1 changed files with 93 additions and 30 deletions
  1. 93 30
      yyyy_js/python-server/pokermaster/PokerMasterDataProccesser.py

+ 93 - 30
yyyy_js/python-server/pokermaster/PokerMasterDataProccesser.py

@@ -1,6 +1,7 @@
 import datetime
 import json
 import logging
+from itertools import product
 
 # 所有的下注选项
 OPTIONS = [101, 102, 103, 301, 302, 303, 304, 305]
@@ -12,7 +13,8 @@ class BetOption:
         self.option = option
         if odds == 0:
             self.odds = 1
-        self.odds = odds
+        else:
+            self.odds = odds
         self.limitRed = limit
         self.total_bet = total_bet
 
@@ -85,18 +87,87 @@ async def deal_message(message, broadcast_func):
 
 async def gameRoundEndNotify(data):
     global banker_finance
-    print(f"收到结束下注GameRoundEndNotify")
+    time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+
+    print(f"{time}:收到结束下注GameRoundEndNotify")
     # 开始结算
-    # 总收入
+    # 本轮收入
     current_round_income = 0
-    all_bet_amount = 0
+    # 本轮总下注金额
+    current_round_bet_amount = 0
 
     for option in round_option_info.values():
-        all_bet_amount += option.total_bet
-        current_round_income = all_bet_amount
+        current_round_bet_amount += option.total_bet
+        current_round_income = current_round_bet_amount
 
     # 开奖结果
     option_results = data["optionResult"]
+    win_option = [x["option"] for x in option_results if x["result"] == 1]
+    print(f"本轮中奖选项: {win_option}")
+    # 庄家结算
+    current_round_income = await bankerSettlement(current_round_income, option_results)
+    # 玩家结算
+    await playerSettlement(option_results)
+
+    await win_option_combo_analysis(current_round_bet_amount, win_option)
+
+    banker_finance += current_round_income
+    print(
+        f"庄家金币: {round(banker_finance / 100, 2)}¥    本轮总下注{round(current_round_bet_amount / 100, 2)}¥ 收入: {round(current_round_income / 100, 2)}¥")
+
+    # 格式化一下,避免数字位数导致各种缩进
+    with open("庄家盈亏记录.txt", "a", encoding="utf-8") as f:
+        finalxx = f"{time}:庄家金币: {banker_finance / 100}¥    本轮总下注{current_round_bet_amount / 100}¥ 收入: {current_round_income / 100}¥"
+        print(finalxx)
+        f.write(finalxx + "\n")
+    # 每把结束存储数据,然后清空数据
+    with open("用户记录.txt", "a", encoding="utf-8") as f:
+        # 将user_dict 转成json字符串
+        f.write(f"{time}:{json.dumps(round_user_betinfo, cls=UserEncoder)}\n")
+    round_user_betinfo.clear()
+    init_round_option_info()
+
+
+#     计算所有组合的可能性及其赔付金额
+async def win_option_combo_analysis(current_round_bet_amount, win_option):
+    # 统计所有结果的可能性
+    try:
+        win_npc = {key: value for key, value in round_option_info.items() if key in [101, 102, 103]}
+        win_card = {key: value for key, value in round_option_info.items() if key in [301, 302, 303, 304, 305]}
+        # 计算所有组合的可能性及其赔付金额
+        combinations = list(product(win_npc.keys(), win_card.keys()))
+        # 计算每个组合的赔付金额
+        combination_payouts = []
+        for npc_key, card_key in combinations:
+            npc_info = win_npc[npc_key]
+            card_info = win_card[card_key]
+            payout = (npc_info.odds * npc_info.total_bet) + (card_info.odds * card_info.total_bet)
+            combination_payouts.append({
+                "combo": f"{npc_key}x{card_key}",
+                "payout": payout,
+                "open": npc_key in win_option and card_key in win_option
+            })
+        # 按赔付金额排序
+        sorted_combinations = sorted(combination_payouts, key=lambda x: x["payout"], reverse=True)
+        # 输出排序后的组合和对应的赔付金额
+        with open("庄家盈亏记录.txt", "a", encoding="utf-8") as f:
+            label = f"{'标记':<2} {'组合':<10} {'赔付':<15} {'收入':<10}\n"
+            print(label)
+            f.write(label)
+            for item in sorted_combinations:
+                combo_key = item["combo"]
+                payout = round(item["payout"]/100, 2)
+                income = round((current_round_bet_amount - payout) / 100, 2)
+                badge = "√" if item["open"] else "×"
+                combos = f"{badge:<2} {combo_key:<10} {payout:<15}¥ {income:<10}¥\n"
+                print(combos)
+                f.write(combos)
+    except Exception as e:
+        print(e)
+
+
+#     庄家结算统计方法
+async def bankerSettlement(current_round_income, option_results):
     for open_result in option_results:
         # 开奖结果
         option = open_result["option"]
@@ -109,32 +180,24 @@ async def gameRoundEndNotify(data):
             final_out = option_info_option_.total_bet * odds_rate
             current_round_income -= final_out
             print(
-                f"选项{option}中奖了, 赔率{odds_rate}, 该选项总下注{option_info_option_.total_bet / 100}¥, 赔付了{final_out / 100}¥")
+                f"选项{option}中奖了, 赔率{odds_rate}, 该选项总下注{round(option_info_option_.total_bet / 100, 2)}¥, 赔付了{round(final_out / 100, 2)}¥")
+    return current_round_income
 
-    # 玩家结算
-    for user in round_user_betinfo.values():
-        for option in user.betOptionAmounts:
-            open_result = next(filter(lambda opt: opt["option"] == option, option_results))
-            odds_rate = round_option_info[option].odds
-            if open_result is not None and open_result["result"] == 1:
-                user.finance += user.betOptionAmounts[option] * odds_rate
-            else:
-                user.finance -= user.betOptionAmounts[option]
-    banker_finance += current_round_income
-    print(f"庄家金币: {banker_finance / 100}¥    本轮总下注{all_bet_amount / 100}¥ 收入: {current_round_income / 100}¥")
 
-    time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
-    # 格式化一下,避免数字位数导致各种缩进
-    with open("庄家盈亏记录.txt", "a", encoding="utf-8") as f:
-        finalxx = f"{time}:庄家金币: {banker_finance / 100}¥    本轮总收入: {current_round_income / 100}¥"
-        print(finalxx)
-        f.write(finalxx + "\n")
-    # 每把结束存储数据,然后清空数据
-    with open("用户记录.txt", "a", encoding="utf-8") as f:
-        # 将user_dict 转成json字符串
-        f.write(f"{time}:{json.dumps(round_user_betinfo, cls=UserEncoder)}\n")
-    round_user_betinfo.clear()
-    init_round_option_info()
+#     玩家结算统计方法
+async def playerSettlement(option_results):
+    # 玩家结算
+    try:
+        for user in round_user_betinfo.values():
+            for option in user.betOptionAmounts:
+                open_result = next(filter(lambda x: x["option"] == option, option_results))
+                odds_rate = round_option_info[option].odds
+                if open_result is not None and open_result["result"] == 1:
+                    user.finance += user.betOptionAmounts[option] * odds_rate
+                else:
+                    user.finance -= user.betOptionAmounts[option]
+    except Exception as e:
+        pass
 
 
 async def kickNotify(data, broadcast_func):