robot.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_EquipList, &pb.EquipList{})
  109. p.bindPush(nameRoute.PushGamePlayer_EquipChange, &pb.EquipList{})
  110. p.bindPush(nameRoute.PushGamePlayer_EquipDel, &pb.I64List{})
  111. p.bindPush(nameRoute.PushChat_MsgList, &pb.ChatMsgList{})
  112. p.bindPush(nameRoute.PushChat_Delete, &pb.I64{})
  113. p.bindPush(nameRoute.PushGamePlayer_ChatData, &pb.ChatData{})
  114. p.bindPush(nameRoute.PushGamePlayer_ChatRoomUpdate, &pb.ChatRoomUpdate{})
  115. p.bindPush(nameRoute.PushGamePlayer_ChatGroupLeave, &pb.I64Bool{})
  116. p.bindPush(nameRoute.PushGamePlayer_ChatGroupInvite, &pb.GroupInvite{})
  117. p.bindPush(nameRoute.PushGamePlayer_ChatPrivateNew, &pb.ChatMsg{})
  118. p.bindPush(nameRoute.PushGamePlayer_ChatSystemNew, &pb.SystemMsg{})
  119. p.bindPush(nameRoute.PushGamePlayer_FriendInviteNewPush, &pb.FriendInvite{})
  120. p.bindPush(nameRoute.PushGamePlayer_FirendNew, &pb.FriendRelation{})
  121. p.bindPush(nameRoute.PushGamePlayer_FirendDel, &pb.I64{})
  122. p.bindPush(nameRoute.PushGamePlayer_FriendAddBack, &pb.I64I32{})
  123. p.bindPush(nameRoute.PushGamePlayer_FriendConfirmBack, &pb.I64I32{})
  124. p.bindPush(nameRoute.PushMapPusher_ObjectUpdate, &pb.MapObjects{})
  125. p.bindPush(nameRoute.PushMapPusher_PlayerUpdate, &pb.MapPlayers{})
  126. p.bindPush(nameRoute.PushMapPusher_LeagueUpdate, &pb.MapLeagues{})
  127. p.bindPush(nameRoute.PushMapPusher_ObjectDel, &pb.I64List{})
  128. p.bindPush(nameRoute.PushMapPusher_MarchState, &pb.MapMarchState{})
  129. p.bindPush(nameRoute.PushMapPusher_Battle, &pb.MapBattlePush{})
  130. p.bindPush(nameRoute.PushMapPlayer_SiegeResult, &pb.SiegeResult{})
  131. p.bindPush(nameRoute.PushGameMail_List, &pb.MailList{})
  132. p.bindPush(nameRoute.PushGameMail_New, &pb.MailList{})
  133. p.bindPush(nameRoute.PushGameMail_Delete, &pb.I64List{})
  134. p.bindPush(nameRoute.PushMapLeague_AssembleUpdate, &pb.I32{})
  135. p.bindPush(nameRoute.PushMapPlayer_MapMark, &pb.MapMarkList{})
  136. p.bindPush(nameRoute.PushMapLeague_MapMark, &pb.MapMarkList{})
  137. p.bindPush(nameRoute.PushMapLeague_MapMarkDel, &pb.I32{})
  138. p.bindPush(nameRoute.PushMapLeague_MapMarkChange, &pb.MapMark{})
  139. p.bindPush(nameRoute.PushMapLeague_BuildList, &pb.LeagueBuildList{})
  140. p.bindPush(nameRoute.PushMapLeague_BuildUpdate, &pb.MapObjectsUpdate{})
  141. p.bindPush(nameRoute.PushMapLeague_WarUpdate, &pb.I32{})
  142. p.bindPush(nameRoute.PushMapPlayer_SiegeResult, &pb.SiegeResult{})
  143. p.bindPush(nameRoute.PushMapPlayer_Reborn, &pb.RebornPush{})
  144. p.bindPush(nameRoute.PushMapPlayer_OwnerMapObjects, &pb.OwnerMapObjects{})
  145. p.bindPush(nameRoute.PushMapPlayer_MapUICastlesPush, &pb.MapUICastles{})
  146. }
  147. func (p *Robot) RequestResponse(route string, req, rsp any) error {
  148. msg, err := p.Request(route, req)
  149. if err != nil {
  150. p.Debugf("[%s] request error. = %+v", route, err)
  151. return err
  152. }
  153. err = p.Serializer().Unmarshal(msg.Data, rsp)
  154. if err != nil {
  155. p.Debugf("[%s] unmarshal error. = %+v", route, err)
  156. }
  157. return err
  158. }
  159. func (p *Robot) RandSleep() {
  160. time.Sleep(time.Duration(rand.Int31n(300)) * time.Millisecond)
  161. }
  162. func (p *Robot) Debug(args ...any) {
  163. if p.PrintLog {
  164. cherryLogger.Debug(args...)
  165. }
  166. }
  167. func (p *Robot) Debugf(template string, args ...any) {
  168. if p.PrintLog {
  169. if p.IsNew {
  170. p.NewDebugf(template, args...)
  171. } else {
  172. cherryLogger.Debugf(template, args...)
  173. }
  174. }
  175. }
  176. func (p *Robot) NewDebugf(template string, args ...any) {
  177. if !p.IsNew {
  178. return
  179. }
  180. msg := fmt.Sprintf(template, args...)
  181. msg = "f1cli_dbgMsg:" + msg
  182. utf16Msg, err := syscall.UTF16PtrFromString(msg)
  183. if err != nil {
  184. return
  185. }
  186. if p.PrintLog {
  187. p.outputDebugStringProc.Call(uintptr(unsafe.Pointer(utf16Msg)))
  188. }
  189. }