|
@@ -26,11 +26,53 @@ def parse_message(message):
|
|
|
return msg_type, data
|
|
|
|
|
|
|
|
|
+class User:
|
|
|
+ def __init__(self, bet_count, uid, betAmount):
|
|
|
+ self.bet_count = bet_count
|
|
|
+ self.uid = uid
|
|
|
+ self.betAmount = betAmount
|
|
|
+
|
|
|
+
|
|
|
+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对象
|
|
|
+user_dict = {}
|
|
|
+
|
|
|
+
|
|
|
async def deal_message(message):
|
|
|
try:
|
|
|
msg_type, data = parse_message(message)
|
|
|
if msg_type == "BetNotify":
|
|
|
+ # {"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}")
|
|
|
+ # 解析日期和小时
|
|
|
+ now = datetime.datetime.now()
|
|
|
+ date = now.strftime("%Y-%m-%d")
|
|
|
+ hour = now.strftime("%H")
|
|
|
+ minute = now.strftime("%M")
|
|
|
+
|
|
|
+ # 获取用户ID
|
|
|
+ uid = data["uid"]
|
|
|
+ bet_amount = data["detail"]["betAmount"]
|
|
|
+ # 获取用户信息
|
|
|
+ user = user_dict.get(uid)
|
|
|
+ if user is None:
|
|
|
+ # 如果用户信息不存在,创建一个新的User对象
|
|
|
+ user = User(0, "", 0)
|
|
|
+ user_dict[uid] = user
|
|
|
+ # 更新用户信息
|
|
|
+ user.bet_count += 1
|
|
|
+ user.uid = uid
|
|
|
+ user.betAmount += bet_amount / 100
|
|
|
+ print(f"用户{uid}下注次数: {user.bet_count}, 总金额: {user.betAmount}")
|
|
|
+ user_dict[uid] = user
|
|
|
+
|
|
|
elif msg_type == "GameDataSynNotify":
|
|
|
print(f"同步游戏数据: {data}")
|
|
|
elif msg_type == "KickNotify":
|
|
@@ -53,6 +95,14 @@ async def deal_message(message):
|
|
|
}
|
|
|
},6000);
|
|
|
''')
|
|
|
+ elif msg_type == "StopBetNotify":
|
|
|
+ # 每把结束存储数据,然后清空数据
|
|
|
+ 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(user_dict, cls=UserEncoder)}\n")
|
|
|
+ user_dict.clear()
|
|
|
else:
|
|
|
logging.info(f"Received message: {message}")
|
|
|
except json.JSONDecodeError as e:
|
|
@@ -65,9 +115,9 @@ async def echo(websocket, path):
|
|
|
connected_clients.add(websocket)
|
|
|
try:
|
|
|
async for message in websocket:
|
|
|
- with open("ws.log", "a") as f:
|
|
|
- time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
- f.write(f"{time}:{message}\n")
|
|
|
+ # with open("ws.log", "a") as f:
|
|
|
+ # time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
+ # f.write(f"{time}:{message}\n")
|
|
|
await deal_message(message)
|
|
|
await websocket.send("Server says: 你好,服务端收到,连接建立成功")
|
|
|
except websockets.ConnectionClosed as e:
|
|
@@ -122,6 +172,11 @@ async def main():
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
+
|
|
|
+ now = datetime.datetime.now()
|
|
|
+ cache_date = now.strftime("%Y-%m-%d")
|
|
|
+ cache_hour = now.strftime("%H")
|
|
|
+
|
|
|
loop = asyncio.get_event_loop()
|
|
|
# 在另一个线程中启动 CLI
|
|
|
cli_thread = threading.Thread(target=start_cli, args=(loop,), daemon=True)
|