| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package main
- import (
- "bufio"
- "encoding/json"
- "f1-game/tool/client-cli/cli"
- "flag"
- "fmt"
- "math/rand"
- "os"
- "path/filepath"
- "strings"
- "sync"
- "time"
- ctime "github.com/cherry-game/cherry/extend/time"
- clog "github.com/cherry-game/cherry/logger"
- "google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/reflect/protoreflect"
- "google.golang.org/protobuf/reflect/protoregistry"
- )
- var (
- requestRoutes = make(map[string]*MsgRoute)
- responseRoutes = make(map[string]*MsgRoute)
- )
- type MsgRoute struct {
- Enable bool `json:"enable"`
- Name string `json:"name"`
- Route string `json:"route"`
- Request string `json:"request"`
- Response string `json:"response"`
- }
- func ListDir(dirPath string) ([]string, error) {
- if _, err := os.Stat(dirPath); os.IsNotExist(err) {
- return nil, fmt.Errorf("no such directory:%s", dirPath)
- }
- entries, err := os.ReadDir(dirPath)
- if err != nil {
- return nil, fmt.Errorf("failed to read directory:%v", err)
- }
- var result []string
- for _, entry := range entries {
- fullPath := filepath.Join(dirPath, entry.Name())
- if entry.IsDir() {
- result = append(result, fmt.Sprintf("%s", fullPath))
- } else {
- if filepath.Ext(fullPath) == ".json" {
- result = append(result, fmt.Sprintf("%s\n", fullPath))
- content, err := os.ReadFile(fullPath)
- if err != nil {
- fmt.Printf("failed to read file:%v\n", err)
- continue
- }
- routes := []*MsgRoute{}
- if err := json.Unmarshal(content, &routes); err != nil {
- fmt.Printf("failed to parse route:%v\n", err)
- continue
- }
- for _, route := range routes {
- if _, found := requestRoutes[route.Route]; found {
- fmt.Printf("duplicate route entry\n")
- }
- requestRoutes[route.Route] = route
- }
- }
- }
- }
- return result, nil
- }
- /*
- make pull-p
- 启动命令
- ./client-cli -gateAddr=192.168.1.41:30001 -pid=2188006 -serverId=6 -account=azw -password=123
- 示例
- game.player.chargeCreate {"chargeID":80001, "chargeType":5, "chargeParams":1}
- debugview下载地址
- https://learn.microsoft.com/zh-cn/sysinternals/downloads/debugview
- */
- func main() {
- var (
- loginURL = "http://dev.f1.chun-pu.com"
- gateAddr = ""
- pid = ""
- serverId = ""
- account = ""
- password = ""
- )
- flag.StringVar(&gateAddr, "gateAddr", "gateAddr", "gateAddr")
- flag.StringVar(&pid, "pid", "pid", "gateAddrpid")
- flag.StringVar(&serverId, "serverId", "serverId", "serverId")
- flag.StringVar(&account, "account", "account", "account")
- flag.StringVar(&password, "password", "password", "password")
- flag.Parse()
- if gateAddr == "" {
- mainOld()
- return
- }
- cli.RegisterDevAccount(loginURL, account, password)
- robot := cli.NewEnterServer(loginURL, pid, account, password, gateAddr, serverId, true, true)
- reader := bufio.NewReader(os.Stdin)
- ListDir("../../_proto/")
- for {
- line, err := reader.ReadString('\n')
- if err != nil {
- break
- }
- parts := strings.Split(strings.TrimSpace(line), " ")
- if len(parts) < 2 {
- clog.Infof("insufficient arguments")
- continue
- }
- cmd := strings.TrimSpace(parts[0])
- param := strings.TrimSpace(strings.Join(parts[1:], " "))
- route, found := requestRoutes[cmd]
- if !found {
- clog.Infof("route not found")
- continue
- }
- rspMsgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(fmt.Sprintf("pb.%s", route.Response)))
- if err != nil {
- clog.Infof("request pb not found")
- continue
- }
- reqMsgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(fmt.Sprintf("pb.%s", route.Request)))
- if err != nil {
- clog.Infof("response pb not found")
- continue
- }
- reqMsg := reqMsgType.New().Interface()
- protojson.Unmarshal([]byte(param), reqMsg)
- jsonBytes, err := protojson.Marshal(reqMsg)
- if err != nil {
- clog.Infof("request json marshal error")
- continue
- }
- robot.NewDebugf("request %s pb.%s %s %s\n", route.Route, route.Request, string(jsonBytes), param)
- rspMsg := rspMsgType.New().Interface()
- if err := robot.RequestResponse(route.Route, reqMsg, rspMsg); err == nil {
- jsonBytes, err := protojson.Marshal(rspMsg)
- if err != nil {
- clog.Infof("response json marshal error")
- continue
- }
- robot.NewDebugf("response %s pb.%s %s\n", route.Route, route.Response, string(jsonBytes))
- }
- }
- }
- func mainOld() {
- var (
- testAccounts = make(map[string]string)
- maxAccountNum = 1000
- url = "http://172.16.124.91"
- addr = "172.16.124.91:21001"
- gameNodeID = "10001"
- pid = "2188001"
- )
- var (
- isLocal = false
- )
- if isLocal {
- url = "http://127.0.0.1"
- addr = "127.0.0.1:21000"
- maxAccountNum = 100
- testAccounts["susu137"] = "susu137"
- }
- for i := 1; i <= maxAccountNum; i++ {
- key := fmt.Sprintf("susu%d", i)
- testAccounts[key] = key
- }
- now := ctime.Now()
- wg := &sync.WaitGroup{}
- wg.Add(1)
- for userName, password := range testAccounts {
- time.Sleep(time.Duration(rand.Int31n(20)) * time.Millisecond)
- go func(userName, password string) {
- cli.EnterServer(url, pid, userName, password, addr, gameNodeID, false)
- //wg.Done()
- }(userName, password)
- }
- wg.Wait()
- clog.Infof("elapsedTime = %dms", now.NowDiffMillisecond())
- }
|