robot.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package cli
  2. import (
  3. "f1-game/internal/code"
  4. nameRoute "f1-game/internal/name/route"
  5. "f1-game/internal/pb"
  6. "fmt"
  7. "math/rand"
  8. "syscall"
  9. "unsafe"
  10. "time"
  11. cherryError "github.com/cherry-game/cherry/error"
  12. cherryHttp "github.com/cherry-game/cherry/extend/http"
  13. cherryTime "github.com/cherry-game/cherry/extend/time"
  14. cherryLogger "github.com/cherry-game/cherry/logger"
  15. cherryClient "github.com/cherry-game/cherry/net/parser/pomelo/client"
  16. cm "github.com/cherry-game/cherry/net/parser/pomelo/message"
  17. jsoniter "github.com/json-iterator/go"
  18. "google.golang.org/protobuf/proto"
  19. )
  20. type (
  21. // Robot client robot
  22. Robot struct {
  23. *cherryClient.Client
  24. PrintLog bool
  25. Token string
  26. gameNodeID string
  27. PID int32
  28. UID int64
  29. OpenID string
  30. PlayerID int64
  31. PlayerName string
  32. StartTime cherryTime.CherryTime
  33. UserName string
  34. URL string
  35. IsNew bool
  36. kernel32DLL *syscall.DLL
  37. outputDebugStringProc *syscall.Proc
  38. }
  39. )
  40. func New(client *cherryClient.Client, isNew bool) *Robot {
  41. robot := &Robot{
  42. Client: client,
  43. IsNew: isNew,
  44. }
  45. if robot.IsNew {
  46. if kernel32DLL, err := syscall.LoadDLL("kernel32.dll"); err == nil {
  47. robot.kernel32DLL = kernel32DLL
  48. if outputDebugStringProc, err := kernel32DLL.FindProc("OutputDebugStringW"); err == nil {
  49. robot.outputDebugStringProc = outputDebugStringProc
  50. }
  51. }
  52. }
  53. return robot
  54. }
  55. // GetToken http://172.16.124.137/login?pid=2188001&account=test1&password=test1
  56. func (p *Robot) GetToken(url string, pid, userName, password string) error {
  57. requestURL := fmt.Sprintf("%s/login", url)
  58. jsonBytes, _, err := cherryHttp.GET(requestURL, map[string]string{
  59. "pid": pid,
  60. "account": userName,
  61. "password": password,
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. rsp := code.DataResult{}
  67. if err = jsoniter.Unmarshal(jsonBytes, &rsp); err != nil {
  68. return err
  69. }
  70. if code.IsFail(rsp.Code) {
  71. return cherryError.Errorf("get Token fail. [userName = %s, code = %d, message = %s]", userName, rsp.Code, rsp.Message)
  72. }
  73. p.URL = url
  74. p.UserName = userName
  75. p.Token = rsp.Data.(string)
  76. p.TagName = fmt.Sprintf("%s_%s", pid, userName)
  77. return nil
  78. }
  79. func (p *Robot) bindPush(pushKey string, push proto.Message) {
  80. p.On(pushKey, func(msg *cm.Message) {
  81. err := p.Serializer().Unmarshal(msg.Data, push)
  82. str, _ := jsoniter.MarshalToString(push)
  83. p.Debugf("[%s] %s push = %v, err = %v", p.TagName, pushKey, str, err)
  84. })
  85. }
  86. func (p *Robot) ListenerPush() {
  87. p.bindPush(nameRoute.PushGamePlayer_Quests, &pb.QuestList{})
  88. p.bindPush(nameRoute.PushMapPlayer_Quests, &pb.QuestList{})
  89. p.bindPush(nameRoute.PushGamePlayer_HeroList, &pb.HeroList{})
  90. p.bindPush(nameRoute.PushGamePlayer_HeroChange, &pb.HeroList{})
  91. p.bindPush(nameRoute.PushGamePlayer_HeroDel, &pb.I64List{})
  92. p.bindPush(nameRoute.PushGamePlayer_SkillList, &pb.SkillList{})
  93. p.bindPush(nameRoute.PushMapPlayer_Team, &pb.Teams{})
  94. p.bindPush(nameRoute.PushGamePlayer_Recruit, &pb.Recruit{})
  95. p.bindPush(nameRoute.PushGamePlayer_ItemList, &pb.ItemList{})
  96. p.bindPush(nameRoute.PushGamePlayer_ItemChange, &pb.ItemList{})
  97. p.bindPush(nameRoute.PushMapPlayer_StorageList, &pb.ItemList{})
  98. p.bindPush(nameRoute.PushMapPlayer_StoragChange, &pb.ItemList{})
  99. p.bindPush(nameRoute.PushGamePlayer_Reward, &pb.AssetList{})
  100. p.bindPush(nameRoute.PushMapPlayer_FacilityList, &pb.Facilities{})
  101. p.bindPush(nameRoute.PushMapPlayer_FacilityFinish, &pb.FacilityList{})
  102. p.bindPush(nameRoute.PushMapPlayer_FacilityAttrChange, &pb.Attrs{})
  103. p.bindPush(nameRoute.PushMapPlayer_ResDefendRecover, &pb.I64I64{})
  104. p.bindPush(nameRoute.PushMapPlayer_Conscript, &pb.Conscript{})
  105. p.bindPush(nameRoute.PushGamePlayer_Lord, &pb.Lord{})
  106. p.bindPush(nameRoute.PushGamePlayer_HeroBookList, &pb.I32List{})
  107. p.bindPush(nameRoute.PushGamePlayer_VipInfo, &pb.VipInfo{})
  108. p.bindPush(nameRoute.PushGamePlayer_ChargeData, &pb.ChargeData{})
  109. p.bindPush(nameRoute.PushGamePlayer_EquipList, &pb.EquipList{})
  110. p.bindPush(nameRoute.PushGamePlayer_EquipChange, &pb.EquipList{})
  111. p.bindPush(nameRoute.PushGamePlayer_EquipDel, &pb.I64List{})
  112. p.bindPush(nameRoute.PushChat_MsgList, &pb.ChatMsgList{})
  113. p.bindPush(nameRoute.PushChat_Delete, &pb.I64{})
  114. p.bindPush(nameRoute.PushGamePlayer_ChatData, &pb.ChatData{})
  115. p.bindPush(nameRoute.PushGamePlayer_ChatRoomUpdate, &pb.ChatRoomUpdate{})
  116. p.bindPush(nameRoute.PushGamePlayer_ChatGroupLeave, &pb.I64Bool{})
  117. p.bindPush(nameRoute.PushGamePlayer_ChatGroupInvite, &pb.GroupInvite{})
  118. p.bindPush(nameRoute.PushGamePlayer_ChatPrivateNew, &pb.ChatMsg{})
  119. p.bindPush(nameRoute.PushGamePlayer_ChatSystemNew, &pb.SystemMsg{})
  120. p.bindPush(nameRoute.PushGamePlayer_FriendInviteNewPush, &pb.FriendInvite{})
  121. p.bindPush(nameRoute.PushGamePlayer_FirendNew, &pb.FriendRelation{})
  122. p.bindPush(nameRoute.PushGamePlayer_FirendDel, &pb.I64{})
  123. p.bindPush(nameRoute.PushGamePlayer_FriendAddBack, &pb.I64I32{})
  124. p.bindPush(nameRoute.PushGamePlayer_FriendConfirmBack, &pb.I64I32{})
  125. p.bindPush(nameRoute.PushMapPusher_ObjectUpdate, &pb.MapObjects{})
  126. p.bindPush(nameRoute.PushMapPusher_PlayerUpdate, &pb.MapPlayers{})
  127. p.bindPush(nameRoute.PushMapPusher_LeagueUpdate, &pb.MapLeagues{})
  128. p.bindPush(nameRoute.PushMapPusher_ObjectDel, &pb.I64List{})
  129. p.bindPush(nameRoute.PushMapPusher_MarchState, &pb.MapMarchState{})
  130. p.bindPush(nameRoute.PushMapPusher_Battle, &pb.MapBattlePush{})
  131. p.bindPush(nameRoute.PushMapPlayer_SiegeResult, &pb.SiegeResult{})
  132. p.bindPush(nameRoute.PushGameMail_List, &pb.MailList{})
  133. p.bindPush(nameRoute.PushGameMail_New, &pb.MailList{})
  134. p.bindPush(nameRoute.PushGameMail_Delete, &pb.I64List{})
  135. p.bindPush(nameRoute.PushMapLeague_AssembleUpdate, &pb.I32{})
  136. p.bindPush(nameRoute.PushMapPlayer_MapMark, &pb.MapMarkList{})
  137. p.bindPush(nameRoute.PushMapLeague_MapMark, &pb.MapMarkList{})
  138. p.bindPush(nameRoute.PushMapLeague_MapMarkDel, &pb.I32{})
  139. p.bindPush(nameRoute.PushMapLeague_MapMarkChange, &pb.MapMark{})
  140. p.bindPush(nameRoute.PushMapLeague_BuildList, &pb.LeagueBuildList{})
  141. p.bindPush(nameRoute.PushMapLeague_BuildUpdate, &pb.MapObjectsUpdate{})
  142. p.bindPush(nameRoute.PushMapLeague_WarUpdate, &pb.I32{})
  143. p.bindPush(nameRoute.PushMapPlayer_SiegeResult, &pb.SiegeResult{})
  144. p.bindPush(nameRoute.PushMapPlayer_Reborn, &pb.RebornPush{})
  145. p.bindPush(nameRoute.PushMapPlayer_OwnerMapObjects, &pb.OwnerMapObjects{})
  146. p.bindPush(nameRoute.PushMapPlayer_MapUICastlesPush, &pb.MapUICastles{})
  147. }
  148. func (p *Robot) RequestResponse(route string, req, rsp any) error {
  149. msg, err := p.Request(route, req)
  150. if err != nil {
  151. p.Debugf("[%s] request error. = %+v", route, err)
  152. return err
  153. }
  154. err = p.Serializer().Unmarshal(msg.Data, rsp)
  155. if err != nil {
  156. p.Debugf("[%s] unmarshal error. = %+v", route, err)
  157. }
  158. return err
  159. }
  160. func (p *Robot) RandSleep() {
  161. time.Sleep(time.Duration(rand.Int31n(300)) * time.Millisecond)
  162. }
  163. func (p *Robot) Debug(args ...any) {
  164. if p.PrintLog {
  165. cherryLogger.Debug(args...)
  166. }
  167. }
  168. func (p *Robot) Debugf(template string, args ...any) {
  169. if p.PrintLog {
  170. if p.IsNew {
  171. p.NewDebugf(template, args...)
  172. } else {
  173. cherryLogger.Debugf(template, args...)
  174. }
  175. }
  176. }
  177. func (p *Robot) NewDebugf(template string, args ...any) {
  178. if !p.IsNew {
  179. return
  180. }
  181. msg := fmt.Sprintf(template, args...)
  182. msg = "f1cli_dbgMsg:" + msg
  183. utf16Msg, err := syscall.UTF16PtrFromString(msg)
  184. if err != nil {
  185. return
  186. }
  187. if p.PrintLog {
  188. p.outputDebugStringProc.Call(uintptr(unsafe.Pointer(utf16Msg)))
  189. }
  190. }