Method name |
Method interpretation |
init() |
Constructor initialization property |
CrateUser() |
Create user method |
GameLogin() |
Login user method |
PlayGame() |
Business interface request method |
GameOver() |
Break link method |
●The code is as follows:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#@Users: LiMu
#@Files:MM3TestTools.py
#@Times: 2022/9/30
#@Software:PyCharm
import os
import sys
import time
import uuid
import json
import websocket
from jsonpath import jsonpath
class ConnectWebsocket(object):
#Constructor initialization properties
def __init__(self,Url,ServerId):
self.ConnectServer = websocket.create_connection(Url)
self.ServerId = ServerId
#Create user interface request
def CrateUser(self,UserName: str):
#Create user parameters
crate_parameters = {"HandleCode": str(uuid.uuid4()).replace("-", ""), "ModuleName": "Player",
"MethodName": "LoginYaCeName", "PartnerId": "1001", "ServerId": self.ServerId,
"PlayerId": "", "Session": "", "GameVersionId": "1212121",
"ResourceVersionName": "2232a", "Data": [UserName,"0"]}
#Send parameters to the server
self.ConnectServer.send(json.dumps(crate_parameters))
while True:
#Accept the returned information and process it
recvs = self.ConnectServer.recv()
response = json.loads(recvs)
# Filter the request information returned
if response.get("HandleCode") == crate_parameters.get("HandleCode"):
self.playerid = jsonpath(response, "$.Value.PlayerId")[0]
self.sessionid = jsonpath(response, "$.Value.Session")[0]
print("Crate request parameters:{}".format(crate_parameters))
print("Crate return information:{}".format(response))
print("=" * 160)
break
#Request method for game login interface
def GameLogin(self,UserName:str):
#Assembly login parameters
login_parameters = {"HandleCode": str(uuid.uuid4()).replace("-",""),"ModuleName": "Player",
"MethodName": "Login_ForDebugTest","PartnerId": "1001","ServerId": self.ServerId,
"PlayerId": "","Session": "","GameVersionId": "1212121",
"ResourceVersionName": "2232a","Data":[UserName]}
#Send parameters to the server
self.ConnectServer.send(json.dumps(login_parameters))
while True:
#Accept the returned information and process it
recvs = self.ConnectServer.recv()
response = json.loads(recvs)
#Filter the request information returned
if response.get("HandleCode") == login_parameters.get("HandleCode"):
self.playerid = jsonpath(response,"$.Value.PlayerId")[0]
self.sessionid = jsonpath(response,"$.Value.Session")[0]
print("Login request parameters:{}".format(login_parameters))
print("Login return information:{}".format(response))
print("=" * 160)
break
#Game service interface request method
def PlayGame(self,ModuleAndMethod:list,Data:list):
#Parameters of assembly salesman
play_parameters = {"HandleCode": str(uuid.uuid4()).replace("-",""),"ModuleName":ModuleAndMethod[0],"MethodName":ModuleAndMethod[1],"PartnerId": "1001","ServerId": self.ServerId,"PlayerId":self.playerid,"Session":self.sessionid,"GameVersionId": "1212121","ResourceVersionName": "2232a","Data":Data}
#Send parameters to the server
self.ConnectServer.send(json.dumps(play_parameters))
while True:
#Accept the returned information and process it
recvs = self.ConnectServer.recv()
response = json.loads(recvs)
#Filter the request information returned
if play_parameters.get("HandleCode") == response.get("HandleCode"):
print("API request parameters:{}".format(play_parameters))
print("API returns information:{}".format(response))
print("=" * 160)
return response
def GameOver(self):
self.ConnectServer.close()
if __name__ == '__main__':
#Link server and player login,1041=6Port,1021=6Auto,1043=6Kuafu,6000=6Dev
ConnectServer = ConnectWebsocket("ws://10.1.0.85:30018/client","1041")
ConnectServer.GameLogin("沐歌1号")
#Front interface service interface instance
Premise = ConnectServer.PlayGame(["Friend","GetList"],[])
Business = ConnectServer.PlayGame(["Friend","GiveFriendshipCoin"],[jsonpath(Premise,"$..FriendList..[?(@.FriendPlayerName=='沐歌3号'&&@.Lv==60)].FriendPlayerId")[0]])
#print("Whether or not to give gifts to friends:",jsonpath(Premise,"$..FriendList..[?(@.FriendPlayerName=='沐歌3号'&&@.Lv==60)].IsGive"))
#print("Whether to receive a gift or not:",jsonpath(Premise, "$..FriendList..[?(@.FriendPlayerName=='沐歌3号'&&@.Lv==60)].ReceiveType"))
game_over = ConnectServer.GameOver()
●The returned result is as follows:
D:\Python\python.exe E:\pythonFiles\Interfacetest\InterfaceTest\Lj6TestTools.py
Login request parameters:{'HandleCode': '2bb293d0072c4516bea56d5201005e79', 'ModuleName': 'Player', 'MethodName': 'Login_ForDebugTest', 'PartnerId': '1001', 'ServerId': '1041', 'PlayerId': '', 'Session': '', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['沐歌1号']}
Login return information:{'HandleCode': '2bb293d0072c4516bea56d5201005e79', 'PushKey': None, 'ResultStatus': 0, 'Status': 0, 'StatusMsg': '成功', 'Value': {'PlayerId': '7110128281529090049', 'Session': '7D1C52566A4F6744D34FCCF868FFB0A7', 'IsNewPlayer': False, 'IpHome': '局域网局域网', 'PlayerName': '沐歌1号', 'UserId': '484fd038ee18459896ae0008e4f0b75a', 'HeadImageId': 26002001, 'RegisterTime': 1685344639, 'ServerOpenDays': 33, 'ServerOpenDate': 1682870400, 'ServerStartDate': 1682888400, 'UseModelType': 1, 'IsInnerPlayer': 0, 'ChatPartnerIdStr': '', 'LvChangeTime': 1685345838}, 'TimeTick': 1685687703, 'ExtValue': None}
================================================================================================================================================================
API request parameters:{'HandleCode': 'cbb636c477604537ad1a0bd17a93ab24', 'ModuleName': 'Friend', 'MethodName': 'GetList', 'PartnerId': '1001', 'ServerId': '1041', 'PlayerId': '7110128281529090049', 'Session': '7D1C52566A4F6744D34FCCF868FFB0A7', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': []}
API returns information:{'HandleCode': 'cbb636c477604537ad1a0bd17a93ab24', 'PushKey': None, 'ResultStatus': 0, 'Status': 0, 'StatusMsg': '成功', 'Value': {'FriendList': {'FriendList': [{'FriendPlayerId': '7110269019017445377', 'FriendPlayerName': '沐歌3号', 'Lv': 60, 'Fap': 8427, 'LastOnlineTime': '在线', 'ServerName': '天府之国', 'GuildName': '', 'HeadImageId': 19010000, 'CreateTime': 1685725907, 'ServerId': 1043, 'PartnerId': 1001, 'IsGive': True, 'ReceiveType': 0}]}, 'FriendInfo': {'GiveCount': 1, 'ReceiveCount': 0}}, 'TimeTick': 1685687703, 'ExtValue': None}
================================================================================================================================================================
API request parameters:{'HandleCode': '6a7ffc73b7ac4035b66d559133a9d89b', 'ModuleName': 'Friend', 'MethodName': 'GiveFriendshipCoin', 'PartnerId': '1001', 'ServerId': '1041', 'PlayerId': '7110128281529090049', 'Session': '7D1C52566A4F6744D34FCCF868FFB0A7', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['7110269019017445377']}
API returns information:{'HandleCode': '6a7ffc73b7ac4035b66d559133a9d89b', 'PushKey': None, 'ResultStatus': 1115, 'Status': 1115, 'StatusMsg': '今日已赠送过奖励', 'Value': None, 'TimeTick': 1685687703, 'ExtValue': None}
================================================================================================================================================================
Process finished with exit code 0