def RobotFunctionRun(self): #棣栧厛鍙戦�丩istRole璇锋眰 stListRoleMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stListRoleMsg, MsgID_pb2.MSGID_ACCOUNT_LISTROLE_REQUEST) stAccountListReq = stListRoleMsg.m_stMsgBody.m_stAccountListRoleRequest stAccountListReq.uin = self.GetRobotUin() stAccountListReq.world = 1 self.RobotSendMsg(stListRoleMsg) #鎺ユ敹ListRole杩斿洖 stResponseMsg = self.RobotRecvMsgByID( MsgID_pb2.MSGID_ACCOUNT_LISTROLE_RESPONSE) stAccountListResp = stResponseMsg.m_stMsgBody.m_stAccountListRoleResponse if (stAccountListResp.iResult != 0): raise RuntimeError("Failed to list role, result %d\n" % (stAccountListResp.iResult)) elif (len(stAccountListResp.roles) != 0): print("role already exist, uin %u, seq %u\n" % (stAccountListResp.roles[0].stRoleID.uin, stAccountListResp.roles[0].stRoleID.uiSeq)) return #濡傛灉瑙掕壊瀛樺湪鍒欑洿鎺ヨ繑鍥� #if(stAccountListResp.bRoleExist): # print "account uin %u exists, thread name %s!\n" %(self.GetRobotUin(), self.GetThread().getName()) # return #瑙掕壊涓嶅瓨鍦紝鍙戦�丆reateRole鐨勮姹� stCreateRoleMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stCreateRoleMsg, MsgID_pb2.MSGID_ACCOUNT_CREATEROLE_REQUEST) stCreateRoleMsg.m_stMsgHead.m_strSessionKey = "qlYal0H1FRCWIBWnKiezn2k5K5Cuz58Di9kAYL+Pup0=" stAccountCreateReq = stCreateRoleMsg.m_stMsgBody.m_stAccountCreateRoleRequest #stAccountCreateReq.szNickName = "Jason"+str(self.GetRobotUin()) stAccountCreateReq.uin = self.GetRobotUin() stAccountCreateReq.worldID = 1 stAccountCreateReq.szNickName = "aaaa1%u" % (self.GetRobotUin()) #stAccountCreateReq.uGender = 101 self.RobotSendMsg(stCreateRoleMsg) #鎺ユ敹CreateRole鐨勮繑鍥�,鍑洪敊鍒欐墦鍗伴敊璇爜 stResponseMsg = self.RobotRecvMsgByID( MsgID_pb2.MSGID_ACCOUNT_CREATEROLE_RESPONSE) if (stResponseMsg.m_stMsgBody.m_stAccountCreateRoleResponse.iResult != 0): raise RuntimeError( "Failed to create %d, ret %d\n", self.GetRobotUin(), stResponseMsg.m_stMsgBody.m_stAccountCreateRoleResponse.iResult ) print "Success to create account %d, thread name %s\n" % ( self.GetRobotUin(), self.GetThread().getName()) return
def RobotFunctionRun(self): #首先测试创建游客帐号 stRegisterMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stRegisterMsg, MsgID_pb2.MSGID_REGAUTH_REGACCOUNT_REQUEST) stRegisterReq = stRegisterMsg.m_stMsgBody.m_stRegAuth_RegAccount_Request stRegisterReq.stAccountID.strAccount = "Jason" + str( self.GetRobotUin()) stRegisterReq.stAccountID.iAccountType = 1 stRegisterReq.strPassword = "******" self.RobotSendMsg(stRegisterMsg) #接收增加游客帐号的返回 stResponseMsg = self.RobotRecvMsgByID( MsgID_pb2.MSGID_REGAUTH_REGACCOUNT_RESPONSE) stRegisterResp = stResponseMsg.m_stMsgBody.m_stRegAuth_RegAccount_Response if (stRegisterResp.iResult == 150999042): print "Account %d is registered!\n" % (self.GetRobotUin()) elif (stRegisterResp.iResult != 0): raise RuntimeError("Failed to register account, result %d\n" % (stRegisterResp.iResult)) else: print "Success to register account %d\n" % (self.GetRobotUin()) #发送登录请求,登录帐号平台 stAuthMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stAuthMsg, MsgID_pb2.MSGID_REGAUTH_AUTHACCOUNT_REQUEST) stAuthReq = stAuthMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Request stAuthReq.stAccountID.strAccount = "Jason" + str(self.GetRobotUin()) stAuthReq.stAccountID.iAccountType = 1 stAuthReq.strPassword = "******" self.RobotSendMsg(stAuthMsg) #接收登录请求的返回 stResponseMsg = self.RobotRecvMsgByID( MsgID_pb2.MSGID_REGAUTH_AUTHACCOUNT_RESPONSE) if (stResponseMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Response.iResult != 0): raise RuntimeError( "Failed to do auth %u, ret %d\n", self.GetRobotUin(), stResponseMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Response. iResult) print "Success to do auth account %d, session key %s\n" % ( stResponseMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Response.uin, stResponseMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Response. strSessionKey) return
def RobotMovePosition(self, startPos, endPos): #发送移动消息 stMoveMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stMoveMsg, MsgID_pb2.MSGID_ZONE_MOVEPOSITION_REQUEST) stMoveReq = stMoveMsg.m_stMsgBody.m_stMovePosition_Request MyStartPos = stMoveReq.stPath.stPoses.add() MyEndPos = stMoveReq.stPath.stPoses.add() MyStartPos.uPosX = startPos.uPosX MyStartPos.uPosY = startPos.uPosY MyEndPos.uPosX = endPos.uPosX MyEndPos.uPosY = endPos.uPosY self.RobotSendMsg(stMoveMsg) #接收返回 stResponseMsg = self.RobotRecvMsgByID( MsgID_pb2.MSGID_ZONE_MOVEPOSITION_RESPONSE) stMoveResp = stResponseMsg.m_stMsgBody.m_stMovePosition_Response if (stMoveResp.iResult != 0): raise RuntimeError("Failed to do role move, result %d, uin %u\n" % (stMoveResp.iResult, self.GetRobotUin())) #确保移动完成 iDistance = math.sqrt( math.pow((startPos.uPosX - endPos.uPosX), 2) + math.pow((startPos.uPosY - endPos.uPosY), 2)) time.sleep(iDistance / 400.0)
def RobotRecvMsgByID(self, iMsgID): while(1): #self.__robot_sock.settimeout(20) #先收2字节的包长度 strRecvMsgLen = ""; while(len(strRecvMsgLen) < 2): strRecvMsgLen += self.__robot_sock.recv(2) arRecvMsgLen = struct.unpack("H", strRecvMsgLen) iRecvMsgLen = socket.ntohs(arRecvMsgLen[0]) iLeftLen = iRecvMsgLen-2 RecvMsg = "" while iLeftLen>0: RecvMsg = RecvMsg + self.__robot_sock.recv(iLeftLen) iLeftLen = iLeftLen - len(RecvMsg) #RecvMsg = self.ClientConn.recv(40960) if len(RecvMsg) == 0: raise RuntimeError("Socket recv data error!"); MsgBodyData = RecvMsg RetProtoMsg = CS_pb2.ProtocolCSMsg() RetProtoMsg.ParseFromString(MsgBodyData) #print "Recv Proto MSGID %d\n" %(RetProtoMsg.m_stMsgHead.m_uiMsgID) if(RetProtoMsg.m_stMsgHead.m_uiMsgID == iMsgID): return RetProtoMsg raise RuntimeError("Failed to recv msg by id %d" %iMsgID)
def AccountListRoleRespHandler(AsyncAccountPressRobot, RecvProtoMsg): #获取ListRole的消息体 stListRoleResp = RecvProtoMsg.m_stMsgBody.m_stAccountListRoleResponse if (stListRoleResp.iResult != 0): raise RuntimeError("Failed to list game role, ret 0x%0x\n", stListRoleResp.iResult) if (stListRoleResp.bRoleExist): AsyncAccountPressRobot.GetRobotSock().close() print "Account uin %u already exists!\n" % ( AsyncAccountPressRobot.GetRobotUin()) return #没有帐号角色,发送创建帐号的请求 stCreateRoleMsg = CS_pb2.ProtocolCSMsg() AsyncAccountPressRobot.GenerateMsgHead( stCreateRoleMsg, MsgID_pb2.MSGID_ACCOUNT_CREATEROLE_REQUEST) stCreateRoleMsg.m_stMsgHead.m_strSessionKey = "qlYal0H1FRCWIBWnKiezn2k5K5Cuz58Di9kAYL+Pup0=" #封装消息体 stCreateRoleReq = stCreateRoleMsg.m_stMsgBody.m_stAccountCreateRoleRequest stCreateRoleReq.worldID = 1 stCreateRoleReq.szNickName = "jasonxiong" + str( AsyncAccountPressRobot.GetRobotUin()) stCreateRoleReq.uin = AsyncAccountPressRobot.GetRobotUin() AsyncAccountPressRobot.RobotSendMsg(stCreateRoleMsg)
def AuthRespHandler(AsyncRegAuthPressRobot, RecvProtoMsg): #认证帐号返回消息的处理 stAuthResp = RecvProtoMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Response if (stAuthResp.iResult != 0): #print "Failed to create account %u\n" %(AsyncRegAuthPressRobot.GetRobotUin()) #发送注册帐号的请求 stRegAccountMsg = CS_pb2.ProtocolCSMsg() AsyncRegAuthPressRobot.GenerateMsgHead( stRegAccountMsg, MsgID_pb2.MSGID_REGAUTH_REGACCOUNT_REQUEST) #封装注册帐号的消息体 stRegAccountReq = stRegAccountMsg.m_stMsgBody.m_stRegAuth_RegAccount_Request stRegAccountReq.iRegOpType = 1 stRegAccountReq.stAccountID.iAccountType = 1 stRegAccountReq.stAccountID.strAccount = "jason" + str( AsyncRegAuthPressRobot.GetRobotUin()) AsyncRegAuthPressRobot.RobotSendMsg(stRegAccountMsg) return #print "Success to auth account %u\n" %(AsyncRegAuthPressRobot.GetRobotUin()) #将socket fd从监听事件列表中去掉 IOLoop.instance().remove_handler(AsyncRegAuthPressRobot.GetRobotFD()) AsyncRegAuthPressRobot.GetRobotSock().close()
def RobotFunctionRun(self): #首先发送Login请求 stLoginMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stLoginMsg, MsgID_pb2.MSGID_ZONE_LOGINSERVER_REQUEST) stLoginMsg.m_stMsgHead.m_strSessionKey = "qlYal0H1FRCWIBWnKiezn2k5K5Cuz58Di9kAYL+Pup0=" stZoneLoginReq = stLoginMsg.m_stMsgBody.m_stZone_LoginServer_Request stZoneLoginReq.stRoleID.uin = self.GetRobotUin() stZoneLoginReq.stRoleID.uiSeq = 53100011 self.RobotSendMsg(stLoginMsg) print "Send Login Msg" #接收Login stResponseMsg = self.RobotRecvMsgByID( MsgID_pb2.MSGID_ZONE_LOGINSERVER_RESPONSE) stLoginResp = stResponseMsg.m_stMsgBody.m_stZone_LoginServer_Response if (stLoginResp.iResult != 0): raise RuntimeError( "Failed to login to game server, result %d, uin %u\n" % (stLoginResp.iResult, self.GetRobotUin())) #让角色循环移动 while (1): #第一段移动 startPos = Common_pb2.UnitPosition() endPos = Common_pb2.UnitPosition() startPos.uPosX = 974 startPos.uPosY = 839 endPos.uPosX = 760 endPos.uPosY = 560 self.RobotMovePosition(startPos, endPos) #第二段移动 startPos.uPosX = 760 startPos.uPosY = 560 endPos.uPosX = 420 endPos.uPosY = 690 self.RobotMovePosition(startPos, endPos) #第三段移动 startPos.uPosX = 420 startPos.uPosY = 690 endPos.uPosX = 974 endPos.uPosY = 839 self.RobotMovePosition(startPos, endPos) #sleep 3s time.sleep(3) #print "Success to test pve fight, uin %u\n" %(self.GetRobotUin()) return
def SendInitProtoData(self): #发送机器人起始驱动数据包, ListRole stListRoleMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stListRoleMsg, MSGID_ACCOUNT_LISTROLE_REQUEST) stListRoleReq = stListRoleMsg.m_stMsgBody.m_stAccountListRoleRequest stListRoleReq.uin = self.GetRobotUin() stListRoleReq.world = 1 self.RobotSendMsg(stListRoleMsg)
def SendInitProtoData(self): #通过发送登录请求包驱动机器人 stLoginMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stLoginMsg, MsgID_pb2.MSGID_ZONE_LOGINSERVER_REQUEST) stLoginReq = stLoginMsg.m_stMsgBody.m_stZone_LoginServer_Request stLoginReq.uin = self.GetRobotUin() stLoginReq.iWorldID = 1 self.RobotSendMsg(stLoginMsg)
def SendInitProtoData(self): #由认证帐号的请求开始驱动压测机器人 stAuthAccountMsg = CS_pb2.ProtocolCSMsg() self.GenerateMsgHead(stAuthAccountMsg, MsgID_pb2.MSGID_REGAUTH_AUTHACCOUNT_REQUEST) stAuthReq = stAuthAccountMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Request stAuthReq.stAccountID.strAccount = "jason" + str(self.GetRobotUin()) stAuthReq.stAccountID.iAccountType = 1 self.RobotSendMsg(stAuthAccountMsg)
def ParseAndHandlerData(self): while (1): if (len(self.__recv_buff) < 2): return #首先解析2字节头 iRecvMsgLen = socket.ntohs( struct.unpack("H", self.__recv_buff[:2])[0]) - 2 if (len(self.__recv_buff[2:]) < iRecvMsgLen): #未收到1个完整的网络包,直接返回 return #尝试解析消息体 RecvProtoMsg = CS_pb2.ProtocolCSMsg() RecvProtoMsg.ParseFromString(self.__recv_buff[2:(iRecvMsgLen + 2)]) self.__recv_buff = self.__recv_buff[(iRecvMsgLen + 2):] self.RobotFunctionRun(RecvProtoMsg)
def LoginRespHandler(AsyncGamePressRobot, RecvProtoMsg): #登录请求的响应包 stLoginResp = RecvProtoMsg.m_stMsgBody.m_stZone_LoginServer_Response if (stLoginResp.iResult != 0): raise RuntimeError("Failed to login game server, ret 0x%0x\n", stLoginResp.iResult) #print "Success login to game server, uin %u\n"%(AsyncGamePressRobot.GetRobotUin()) #发送开始PVE关卡战斗的包 stStartPveMsg = CS_pb2.ProtocolCSMsg() AsyncGamePressRobot.GenerateMsgHead( stStartPveMsg, MsgID_pb2.MSGID_ZONE_PVESTARTFIGHT_REQUEST) #封装开始PVE关卡战斗的消息体 stStartPveReq = stStartPveMsg.m_stMsgBody.m_stZone_PveStartFight_Request stStartPveReq.uPinstanceID = 3010 stStartPveReq.uCrossID = 30101 AsyncGamePressRobot.RobotSendMsg(stStartPveMsg)
def FinPveRespHandler(AsyncGamePressRobot, RecvProtoMsg): #结束PVE战斗的返回包 stFinPveResp = RecvProtoMsg.m_stMsgBody.m_stZone_PveFinFight_Response if (stFinPveResp.iResult != 0): return raise RuntimeError("Failed to fin pve fight, uin %u\n" % (AsyncGamePressRobot.GetRobotUin())) print "Success to fin pve, uin %u\n" % (AsyncGamePressRobot.GetRobotUin()) #发送开始PVE战斗的请求,重新开始PVE stStartPveMsg = CS_pb2.ProtocolCSMsg() AsyncGamePressRobot.GenerateMsgHead( stStartPveMsg, MsgID_pb2.MSGID_ZONE_PVESTARTFIGHT_REQUEST) #封装PVE战斗的请求消息体 stStartPveReq = stStartPveMsg.m_stMsgBody.m_stZone_PveStartFight_Request stStartPveReq.uPinstanceID = 3010 stStartPveReq.uCrossID = 30101 AsyncGamePressRobot.RobotSendMsg(stStartPveMsg)
def RegisterRespHandler(AsyncRegAuthPressRobot, RecvProtoMsg): #注册帐号的返回 stRegResp = RecvProtoMsg.m_stMsgBody.m_stRegAuth_RegAccount_Response if (stRegResp.iResult != 0): raise RuntimeError("Failed to register account, ret 0x%0x\n", stRegResp.iResult) #print "Success to register account %d\n" %(AsyncRegAuthPressRobot.GetRobotUin()) #注册帐号成功,发送认证请求 stAuthAccountMsg = CS_pb2.ProtocolCSMsg() AsyncRegAuthPressRobot.GenerateMsgHead( stAuthAccountMsg, MsgID_pb2.MSGID_REGAUTH_AUTHACCOUNT_REQUEST) #发送认证帐号的请求 stAuthAccountReq = stAuthAccountMsg.m_stMsgBody.m_stRegAuth_AuthAccount_Request stAuthAccountReq.stAccountID.iAccountType = 1 stAuthAccountReq.stAccountID.strAccount = "jason" + str( AsyncRegAuthPressRobot.GetRobotUin()) AsyncRegAuthPressRobot.RobotSendMsg(stAuthAccountMsg)