analysis2.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import numpy as np
  2. import json
  3. import matplotlib.pyplot as plt
  4. # 假设数据文件名为 'betting_data.txt',每行为一个时间戳和JSON数据
  5. file_path = '开奖记录.log'
  6. index = 0
  7. total_payout_by_bet_odd = 0
  8. total_payout_by_settle = 0
  9. result_option = []
  10. bet_101 = 0
  11. bet_102 = 0
  12. options_pay_out = {}
  13. wrong_data_key = []
  14. cacheRoundInfo = []
  15. class RoundInfo:
  16. def __init__(self, key, _result_option, _options_pay_out, _total_payout_by_bet_odd, _total_payout_by_settle):
  17. self.key = key
  18. self.result_option = _result_option
  19. self.options_pay_out = _options_pay_out
  20. self.total_payout_by_bet_odd = _total_payout_by_bet_odd
  21. self.total_payout_by_settle = _total_payout_by_settle
  22. # json解析 RoundInfo
  23. class RoundInfoDecoder(json.JSONEncoder):
  24. def default(self, obj):
  25. if isinstance(obj, RoundInfo):
  26. return f"{obj.key} odd结果:{obj.total_payout_by_bet_odd} 开奖结果:{obj.total_payout_by_settle} 一致数据可以保存用于分析"
  27. # Let the base class default method raise the TypeError
  28. return json.JSONEncoder.default(self, obj)
  29. def process_settle(key, data):
  30. global total_payout_by_settle, total_payout_by_bet_odd, result_option
  31. player_settle = data["playerSettle"]
  32. for player in player_settle:
  33. total_payout_by_settle += player["totalWinAmount"]
  34. other_players = data["otherPlayers"]
  35. total_payout_by_settle += other_players["totalWinAmount"]
  36. # 开奖结果
  37. for result in data["optionResult"]:
  38. if result["result"] == 1:
  39. result_option_ = result["option"]
  40. result_option.append(result_option_)
  41. total_payout_by_bet_odd += options_pay_out[result_option_]
  42. def compare_obb_and_settle(key, data):
  43. global total_payout_by_settle, total_payout_by_bet_odd
  44. diff_value = (total_payout_by_settle - total_payout_by_bet_odd) / 100
  45. diff_value = round(diff_value, 2)
  46. if diff_value != 0:
  47. print(
  48. f"{key} odd结果:{total_payout_by_bet_odd} 开奖结果:{total_payout_by_settle} 差值:{diff_value}¥ 数值不同不予计入,待删除")
  49. wrong_data_key.append(key)
  50. else:
  51. # print(f"{key} odd结果:{total_payout_by_bet_odd} 开奖结果:{total_payout_by_settle} 差值:{diff_value}¥")
  52. pass
  53. def save_and_clear(key):
  54. global bet_101, bet_102, total_payout_by_bet_odd, total_payout_by_settle, result_option
  55. if total_payout_by_settle == total_payout_by_bet_odd:
  56. # print(f"{key} odd结果:{total_payout_by_bet_odd} 开奖结果:{total_payout_by_settle} 一致数据可以保存用于分析")
  57. cacheRoundInfo.append(
  58. RoundInfo(key, result_option.copy(), options_pay_out.copy(), total_payout_by_bet_odd,
  59. total_payout_by_settle))
  60. bet_101 = 0
  61. bet_102 = 0
  62. total_payout_by_bet_odd = 0
  63. total_payout_by_settle = 0
  64. options_pay_out.clear()
  65. result_option.clear()
  66. # 分析和绘图函数
  67. def analyze_group_positions(round_info_list, group1, group2):
  68. # 存储每个中奖选项在排序后赔付列表中的位置
  69. positions_group1 = []
  70. positions_group2 = []
  71. for info in round_info_list:
  72. # 分别处理两组
  73. group1_payouts = {k: info.options_pay_out[k] for k in group1 if k in info.options_pay_out}
  74. group2_payouts = {k: info.options_pay_out[k] for k in group2 if k in info.options_pay_out}
  75. # 排序
  76. sorted_group1 = sorted(group1_payouts.items(), key=lambda x: x[1])
  77. sorted_group2 = sorted(group2_payouts.items(), key=lambda x: x[1])
  78. sorted_keys_group1 = [item[0] for item in sorted_group1]
  79. sorted_keys_group2 = [item[0] for item in sorted_group2]
  80. # 定位中奖选项
  81. for res in info.result_option:
  82. if res in sorted_keys_group1:
  83. positions_group1.append(sorted_keys_group1.index(res) + 1)
  84. if res in sorted_keys_group2:
  85. positions_group2.append(sorted_keys_group2.index(res) + 1)
  86. # 绘制两个组的结果
  87. plt.figure(figsize=(12, 6))
  88. plt.subplot(1, 2, 1)
  89. # plt.hist(positions_group1, bins=len(group1), alpha=0.75, edgecolor='black', align='left', rwidth=0.5)
  90. plt.hist(positions_group1, bins=np.arange(1, len(group1) + 2) - 0.5, alpha=0.75, edgecolor='black')
  91. plt.xlabel('Position in Payout Sorting (Group 1)')
  92. plt.ylabel('Frequency')
  93. plt.title('Frequency of Winning Options in Group 1')
  94. plt.xticks(np.arange(1, len(group1) + 1))
  95. plt.subplot(1, 2, 2)
  96. # plt.hist(positions_group2, bins=len(group2), alpha=0.75, edgecolor='black', align='left', rwidth=0.5)
  97. plt.hist(positions_group2, bins=np.arange(1, len(group2) + 2) - 0.5, alpha=0.75, edgecolor='black')
  98. plt.xlabel('Position in Payout Sorting (Group 2)')
  99. plt.ylabel('Frequency')
  100. plt.title('Frequency of Winning Options in Group 2')
  101. plt.xticks(np.arange(1, len(group2) + 1))
  102. plt.tight_layout()
  103. plt.show()
  104. def parse_data_from_file(_file_path):
  105. clean_data(_file_path)
  106. delete_wrong_data(_file_path)
  107. # print(json.dumps(cacheRoundInfo, cls=RoundInfoDecoder))
  108. # 定义两组选项
  109. group1 = [101, 102, 103]
  110. group2 = [301, 302, 303, 304, 305]
  111. # 调用函数
  112. analyze_group_positions(cacheRoundInfo, group1, group2)
  113. def delete_wrong_data(_file_path):
  114. print("错误数据key:", wrong_data_key)
  115. if not wrong_data_key:
  116. return
  117. # 删除文件的某一行以及上一行
  118. with open(_file_path, "r", encoding="utf-8") as f:
  119. lines = f.readlines()
  120. with open(_file_path, "w", encoding="utf-8") as f_w:
  121. for line in lines:
  122. # line 不包含任意一个错误数据key
  123. if not any([i in line for i in wrong_data_key]):
  124. f_w.write(line)
  125. def clean_data(_file_path):
  126. global index
  127. with open(_file_path, 'r', encoding='utf-8') as file:
  128. for line in file:
  129. # print(line)
  130. line_strip = line.strip()
  131. line_strip = line_strip.replace("'", '"')
  132. line_strip = line_strip.replace("False", "false")
  133. line_strip = line_strip.replace("True", "true")
  134. timestamp = line_strip[:19]
  135. json_data = line_strip[20:]
  136. key = timestamp
  137. data = json.loads(json_data)
  138. if index % 2 == 0:
  139. process_obb(key, data)
  140. else:
  141. process_settle(key, data)
  142. compare_obb_and_settle(key, data)
  143. save_and_clear(key)
  144. index += 1
  145. def process_obb(key, data):
  146. global total_payout_by_bet_odd, bet_101, bet_102
  147. for item in data.values():
  148. option_ = item["option"]
  149. odds_ = item["odds"]
  150. bet_ = item["total_bet"]
  151. odds__bet_ = odds_ * bet_
  152. options_pay_out[option_] = round(odds__bet_, 2)
  153. if option_ == 101:
  154. bet_101 = bet_
  155. elif option_ == 102:
  156. bet_102 = bet_
  157. elif option_ == 103:
  158. options_pay_out[option_] = bet_101 + bet_102
  159. # 开始解析文件
  160. if __name__ == '__main__':
  161. parse_data_from_file(file_path)