113 lines
3.4 KiB
Python
113 lines
3.4 KiB
Python
import json
|
||
import random
|
||
from typing import List, Dict, Tuple, Optional
|
||
import time
|
||
|
||
import requests
|
||
|
||
"""
|
||
此程序主要模拟用户真实使用过程中的RTP值
|
||
|
||
返奖率 RTP = 总赢 / 总投注
|
||
"""
|
||
|
||
|
||
class SugarRushSimulator:
|
||
def __init__(self):
|
||
self.session = requests.Session()
|
||
self.headers = {
|
||
'User-Agent': 'Mozilla/5.0 (Compatible; LongConnectionClient/1.0)',
|
||
'Connection': 'keep-alive' # 显式告诉服务器保持连接
|
||
}
|
||
response = self.session.post("http://localhost:8081/api/login", json={'username': '19156010758', 'password': '123456'})
|
||
data = self.do_result(response)
|
||
if data is not None:
|
||
self.session.headers.update({'Authorization': f"Bearer {data['token']}"})
|
||
|
||
def do_result(self, response):
|
||
if response.status_code == 200:
|
||
resp = json.loads(response.text)
|
||
if resp['code'] == 200:
|
||
if 'data' in resp:
|
||
return resp['data']
|
||
else:
|
||
return resp
|
||
else:
|
||
print(f"发生错误: {resp}")
|
||
return None
|
||
else:
|
||
print(f"请求发生错误: {response.status_code}")
|
||
|
||
|
||
def buy_free_spins(self,bet, buy_type: str):
|
||
response = self.session.get(f"http://localhost:8081/api/sugar/buy_free_spins?bet={bet}&kind={buy_type}", headers=self.headers, timeout=(5, 10))
|
||
return self.do_result(response)
|
||
|
||
def do_spin(self, bet):
|
||
response = self.session.get(f"http://localhost:8081/api/sugar/dospin?bet={bet}", headers=self.headers, timeout=(5, 10))
|
||
return self.do_result(response)
|
||
|
||
def simulate_batch(self, spins: int, buy_type: Optional[str] = None) -> Dict:
|
||
"""
|
||
模拟指定次数的旋转
|
||
:param spins: 模拟次数
|
||
:param buy_type: None=普通混合模式, 'standard'=只测买普通, 'super'=只测买超级
|
||
:return: 统计结果
|
||
"""
|
||
|
||
bet = 1
|
||
total_bet = 0.0
|
||
total_win = 0.0
|
||
for _ in range(spins):
|
||
|
||
if buy_type and buy_type != "normal":
|
||
r = self.buy_free_spins(bet, buy_type)
|
||
if r:
|
||
total_bet += float(r.get("cost", 0))
|
||
else:
|
||
total_bet += bet
|
||
|
||
spin_win = 0
|
||
can_spins = True
|
||
while can_spins > 0:
|
||
|
||
res = self.do_spin(bet)
|
||
if len(res['data']) == 0:
|
||
can_spins = False
|
||
else:
|
||
spin_win = res['data'][-1]['tw']
|
||
|
||
total_win += float(spin_win)
|
||
|
||
print(f"旋转{spins} 次, 总下注 {total_bet}, 总盈利 {total_win}, RTP {(total_win / total_bet) * 100}")
|
||
return {}
|
||
|
||
|
||
|
||
if __name__ == "__main__":
|
||
|
||
batchs = [
|
||
# {
|
||
# "buy_type": "normal",
|
||
# "spins": 100,
|
||
# },
|
||
# {
|
||
# "buy_type": "standard",
|
||
# "spins": 20,
|
||
# },
|
||
# {
|
||
# "buy_type": "super",
|
||
# "spins": 20,
|
||
# },
|
||
]
|
||
for batch in batchs:
|
||
sim = SugarRushSimulator()
|
||
start_time = time.time() # 记录开始时间
|
||
sim.simulate_batch(
|
||
batch["spins"],
|
||
batch['buy_type']
|
||
)
|
||
end_time = time.time() # 记录结束时间
|
||
elapsed_time = end_time - start_time
|
||
print(f"Batch {batch['buy_type']} took {elapsed_time:.4f} seconds") # 打印耗时
|