robot.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "fmt"
  4. jsoniter "github.com/json-iterator/go"
  5. mhayaError "github.com/mhaya/error"
  6. mhayaHttp "github.com/mhaya/extend/http"
  7. mhayaTime "github.com/mhaya/extend/time"
  8. "github.com/mhaya/game/game_cluster/internal/code"
  9. "github.com/mhaya/game/game_cluster/internal/pb"
  10. mhayaLogger "github.com/mhaya/logger"
  11. mhayaClient "github.com/mhaya/net/parser/pomelo/client"
  12. "math/rand"
  13. "time"
  14. )
  15. type (
  16. // Robot client robot
  17. Robot struct {
  18. *mhayaClient.Client
  19. PrintLog bool
  20. Token string
  21. ServerId int32
  22. PID int32
  23. UID int64
  24. OpenId string
  25. PlayerId int64
  26. PlayerName string
  27. StartTime mhayaTime.MhayaTime
  28. }
  29. )
  30. func New(client *mhayaClient.Client) *Robot {
  31. return &Robot{
  32. Client: client,
  33. }
  34. }
  35. // GetToken http登录获取token对象
  36. // http://172.16.124.137/login?pid=2126003&account=test1&password=test1
  37. func (p *Robot) GetToken(url string, pid, userName, password string) error {
  38. // http登陆获取token json对象
  39. requestURL := fmt.Sprintf("%s/login", url)
  40. jsonBytes, _, err := mhayaHttp.GET(requestURL, map[string]string{
  41. "pid": pid, //sdk包id
  42. "account": userName, //帐号名
  43. "password": password, //密码
  44. })
  45. if err != nil {
  46. return err
  47. }
  48. // 转换json对象
  49. rsp := code.Result{}
  50. if err = jsoniter.Unmarshal(jsonBytes, &rsp); err != nil {
  51. return err
  52. }
  53. if code.IsFail(rsp.Code) {
  54. return mhayaError.Errorf("get Token fail. [message = %s]", rsp.Message)
  55. }
  56. // 获取token值
  57. p.Token = rsp.Data.(string)
  58. p.TagName = fmt.Sprintf("%s_%s", pid, userName)
  59. p.StartTime = mhayaTime.Now()
  60. return nil
  61. }
  62. // UserLogin 用户登录对某游戏服
  63. func (p *Robot) UserLogin(serverId int32) error {
  64. route := "gate.user.login"
  65. p.Debugf("[%s] [UserLogin] request ServerID = %d", p.TagName, serverId)
  66. msg, err := p.Request(route, &pb.LoginRequest{
  67. ServerId: serverId,
  68. Token: p.Token,
  69. Params: nil,
  70. })
  71. if err != nil {
  72. return err
  73. }
  74. p.ServerId = serverId
  75. rsp := &pb.LoginResponse{}
  76. err = p.Serializer().Unmarshal(msg.Data, rsp)
  77. if err != nil {
  78. return err
  79. }
  80. p.UID = rsp.Uid
  81. p.PID = rsp.Pid
  82. p.OpenId = rsp.OpenId
  83. p.Debugf("[%s] [UserLogin] response = %+v", p.TagName, rsp)
  84. return nil
  85. }
  86. // PlayerSelect 查看玩家列表
  87. func (p *Robot) PlayerSelect() error {
  88. route := "db.player.select"
  89. msg, err := p.Request(route, &pb.None{})
  90. if err != nil {
  91. return err
  92. }
  93. rsp := &pb.PlayerSelectResponse{}
  94. err = p.Serializer().Unmarshal(msg.Data, rsp)
  95. if err != nil {
  96. return err
  97. }
  98. if len(rsp.List) < 1 {
  99. p.Debugf("[%s] not found player list.", p.TagName)
  100. return nil
  101. }
  102. p.PlayerId = rsp.List[0].PlayerId
  103. p.PlayerName = rsp.List[0].PlayerName
  104. p.Debugf("[%s] [PlayerSelect] response PlayerID = %d,PlayerName = %s", p.TagName, p.PlayerId, p.PlayerName)
  105. return nil
  106. }
  107. // ActorCreate 创建角色
  108. func (p *Robot) ActorCreate() error {
  109. if p.PlayerId > 0 {
  110. p.Debugf("[%s] deny create actor", p.TagName)
  111. return nil
  112. }
  113. route := "db.player.create"
  114. gender := rand.Int31n(1)
  115. req := &pb.PlayerCreateRequest{
  116. PlayerName: "p" + p.OpenId,
  117. Gender: gender,
  118. }
  119. msg, err := p.Request(route, req)
  120. if err != nil {
  121. return err
  122. }
  123. rsp := &pb.PlayerCreateResponse{}
  124. err = p.Serializer().Unmarshal(msg.Data, rsp)
  125. if err != nil {
  126. return err
  127. }
  128. p.PlayerId = rsp.Player.PlayerId
  129. p.PlayerName = rsp.Player.PlayerName
  130. p.Debugf("[%s] [ActorCreate] PlayerID = %d,ActorName = %s", p.TagName, p.PlayerId, p.PlayerName)
  131. return nil
  132. }
  133. // ActorEnter 角色进入游戏
  134. func (p *Robot) ActorEnter() error {
  135. route := "db.player.enter"
  136. req := &pb.Int64{
  137. Value: p.PlayerId,
  138. }
  139. msg, err := p.Request(route, req)
  140. if err != nil {
  141. return err
  142. }
  143. rsp := &pb.PlayerEnterResponse{}
  144. err = p.Serializer().Unmarshal(msg.Data, rsp)
  145. if err != nil {
  146. return err
  147. }
  148. p.Debugf("[%s] [ActorEnter] response PlayerID = %d,ActorName = %s", p.TagName, p.PlayerId, p.PlayerName)
  149. return nil
  150. }
  151. func (p *Robot) RandSleep() {
  152. time.Sleep(time.Duration(rand.Int31n(300)) * time.Millisecond)
  153. }
  154. func (p *Robot) Debug(args ...interface{}) {
  155. if p.PrintLog {
  156. mhayaLogger.Debug(args...)
  157. }
  158. }
  159. func (p *Robot) Debugf(template string, args ...interface{}) {
  160. if p.PrintLog {
  161. mhayaLogger.Debugf(template, args...)
  162. }
  163. }