main.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "f1-game/tool/client-cli/cli"
  6. "flag"
  7. "fmt"
  8. "math/rand"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "sync"
  13. "time"
  14. ctime "github.com/cherry-game/cherry/extend/time"
  15. clog "github.com/cherry-game/cherry/logger"
  16. "google.golang.org/protobuf/encoding/protojson"
  17. "google.golang.org/protobuf/reflect/protoreflect"
  18. "google.golang.org/protobuf/reflect/protoregistry"
  19. )
  20. var (
  21. requestRoutes = make(map[string]*MsgRoute)
  22. responseRoutes = make(map[string]*MsgRoute)
  23. )
  24. type MsgRoute struct {
  25. Enable bool `json:"enable"`
  26. Name string `json:"name"`
  27. Route string `json:"route"`
  28. Request string `json:"request"`
  29. Response string `json:"response"`
  30. }
  31. func ListDir(dirPath string) ([]string, error) {
  32. if _, err := os.Stat(dirPath); os.IsNotExist(err) {
  33. return nil, fmt.Errorf("no such directory:%s", dirPath)
  34. }
  35. entries, err := os.ReadDir(dirPath)
  36. if err != nil {
  37. return nil, fmt.Errorf("failed to read directory:%v", err)
  38. }
  39. var result []string
  40. for _, entry := range entries {
  41. fullPath := filepath.Join(dirPath, entry.Name())
  42. if entry.IsDir() {
  43. result = append(result, fmt.Sprintf("%s", fullPath))
  44. } else {
  45. if filepath.Ext(fullPath) == ".json" {
  46. result = append(result, fmt.Sprintf("%s\n", fullPath))
  47. content, err := os.ReadFile(fullPath)
  48. if err != nil {
  49. fmt.Printf("failed to read file:%v\n", err)
  50. continue
  51. }
  52. routes := []*MsgRoute{}
  53. if err := json.Unmarshal(content, &routes); err != nil {
  54. fmt.Printf("failed to parse route:%v\n", err)
  55. continue
  56. }
  57. for _, route := range routes {
  58. if _, found := requestRoutes[route.Route]; found {
  59. fmt.Printf("duplicate route entry\n")
  60. }
  61. requestRoutes[route.Route] = route
  62. }
  63. }
  64. }
  65. }
  66. return result, nil
  67. }
  68. /*
  69. make pull-p
  70. 启动命令
  71. ./client-cli -gateAddr=192.168.1.41:30001 -pid=2188006 -serverId=6 -account=azw -password=123
  72. 示例
  73. game.player.chargeCreate {"chargeID":80001, "chargeType":5, "chargeParams":1}
  74. debugview下载地址
  75. https://learn.microsoft.com/zh-cn/sysinternals/downloads/debugview
  76. */
  77. func main() {
  78. var (
  79. loginURL = "http://dev.f1.chun-pu.com"
  80. gateAddr = ""
  81. pid = ""
  82. serverId = ""
  83. account = ""
  84. password = ""
  85. )
  86. flag.StringVar(&gateAddr, "gateAddr", "gateAddr", "gateAddr")
  87. flag.StringVar(&pid, "pid", "pid", "gateAddrpid")
  88. flag.StringVar(&serverId, "serverId", "serverId", "serverId")
  89. flag.StringVar(&account, "account", "account", "account")
  90. flag.StringVar(&password, "password", "password", "password")
  91. flag.Parse()
  92. if gateAddr == "" {
  93. mainOld()
  94. return
  95. }
  96. cli.RegisterDevAccount(loginURL, account, password)
  97. robot := cli.NewEnterServer(loginURL, pid, account, password, gateAddr, serverId, true, true)
  98. reader := bufio.NewReader(os.Stdin)
  99. ListDir("../../_proto/")
  100. for {
  101. line, err := reader.ReadString('\n')
  102. if err != nil {
  103. break
  104. }
  105. parts := strings.Split(strings.TrimSpace(line), " ")
  106. if len(parts) < 2 {
  107. clog.Infof("insufficient arguments")
  108. continue
  109. }
  110. cmd := strings.TrimSpace(parts[0])
  111. param := strings.TrimSpace(strings.Join(parts[1:], " "))
  112. route, found := requestRoutes[cmd]
  113. if !found {
  114. clog.Infof("route not found")
  115. continue
  116. }
  117. rspMsgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(fmt.Sprintf("pb.%s", route.Response)))
  118. if err != nil {
  119. clog.Infof("request pb not found")
  120. continue
  121. }
  122. reqMsgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(fmt.Sprintf("pb.%s", route.Request)))
  123. if err != nil {
  124. clog.Infof("response pb not found")
  125. continue
  126. }
  127. reqMsg := reqMsgType.New().Interface()
  128. protojson.Unmarshal([]byte(param), reqMsg)
  129. jsonBytes, err := protojson.Marshal(reqMsg)
  130. if err != nil {
  131. clog.Infof("request json marshal error")
  132. continue
  133. }
  134. robot.NewDebugf("request %s pb.%s %s %s\n", route.Route, route.Request, string(jsonBytes), param)
  135. rspMsg := rspMsgType.New().Interface()
  136. if err := robot.RequestResponse(route.Route, reqMsg, rspMsg); err == nil {
  137. jsonBytes, err := protojson.Marshal(rspMsg)
  138. if err != nil {
  139. clog.Infof("response json marshal error")
  140. continue
  141. }
  142. robot.NewDebugf("response %s pb.%s %s\n", route.Route, route.Response, string(jsonBytes))
  143. }
  144. }
  145. }
  146. func mainOld() {
  147. var (
  148. testAccounts = make(map[string]string)
  149. maxAccountNum = 1000
  150. url = "http://172.16.124.91"
  151. addr = "172.16.124.91:21001"
  152. gameNodeID = "10001"
  153. pid = "2188001"
  154. )
  155. var (
  156. isLocal = false
  157. )
  158. if isLocal {
  159. url = "http://127.0.0.1"
  160. addr = "127.0.0.1:21000"
  161. maxAccountNum = 100
  162. testAccounts["susu137"] = "susu137"
  163. }
  164. for i := 1; i <= maxAccountNum; i++ {
  165. key := fmt.Sprintf("susu%d", i)
  166. testAccounts[key] = key
  167. }
  168. now := ctime.Now()
  169. wg := &sync.WaitGroup{}
  170. wg.Add(1)
  171. for userName, password := range testAccounts {
  172. time.Sleep(time.Duration(rand.Int31n(20)) * time.Millisecond)
  173. go func(userName, password string) {
  174. cli.EnterServer(url, pid, userName, password, addr, gameNodeID, false)
  175. //wg.Done()
  176. }(userName, password)
  177. }
  178. wg.Wait()
  179. clog.Infof("elapsedTime = %dms", now.NowDiffMillisecond())
  180. }