123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package main
- import (
- "fmt"
- jsoniter "github.com/json-iterator/go"
- mhayaError "github.com/mhaya/error"
- mhayaHttp "github.com/mhaya/extend/http"
- mhayaTime "github.com/mhaya/extend/time"
- "github.com/mhaya/game/game_cluster/internal/code"
- "github.com/mhaya/game/game_cluster/internal/pb"
- mhayaLogger "github.com/mhaya/logger"
- mhayaClient "github.com/mhaya/net/parser/pomelo/client"
- "math/rand"
- "time"
- )
- type (
- // Robot client robot
- Robot struct {
- *mhayaClient.Client
- PrintLog bool
- Token string
- ServerId int32
- PID int32
- UID int64
- OpenId string
- PlayerId int64
- PlayerName string
- StartTime mhayaTime.MhayaTime
- }
- )
- func New(client *mhayaClient.Client) *Robot {
- return &Robot{
- Client: client,
- }
- }
- // GetToken http登录获取token对象
- // http://172.16.124.137/login?pid=2126003&account=test1&password=test1
- func (p *Robot) GetToken(url string, pid, userName, password string) error {
- // http登陆获取token json对象
- requestURL := fmt.Sprintf("%s/login", url)
- jsonBytes, _, err := mhayaHttp.GET(requestURL, map[string]string{
- "pid": pid, //sdk包id
- "account": userName, //帐号名
- "password": password, //密码
- })
- if err != nil {
- return err
- }
- // 转换json对象
- rsp := code.Result{}
- if err = jsoniter.Unmarshal(jsonBytes, &rsp); err != nil {
- return err
- }
- if code.IsFail(rsp.Code) {
- return mhayaError.Errorf("get Token fail. [message = %s]", rsp.Message)
- }
- // 获取token值
- p.Token = rsp.Data.(string)
- p.TagName = fmt.Sprintf("%s_%s", pid, userName)
- p.StartTime = mhayaTime.Now()
- return nil
- }
- // UserLogin 用户登录对某游戏服
- func (p *Robot) UserLogin(serverId int32) error {
- route := "gate.user.login"
- p.Debugf("[%s] [UserLogin] request ServerID = %d", p.TagName, serverId)
- msg, err := p.Request(route, &pb.LoginRequest{
- ServerId: serverId,
- Token: p.Token,
- Params: nil,
- })
- if err != nil {
- return err
- }
- p.ServerId = serverId
- rsp := &pb.LoginResponse{}
- err = p.Serializer().Unmarshal(msg.Data, rsp)
- if err != nil {
- return err
- }
- p.UID = rsp.Uid
- p.PID = rsp.Pid
- p.OpenId = rsp.OpenId
- p.Debugf("[%s] [UserLogin] response = %+v", p.TagName, rsp)
- return nil
- }
- // PlayerSelect 查看玩家列表
- func (p *Robot) PlayerSelect() error {
- route := "database.player.select"
- msg, err := p.Request(route, &pb.None{})
- if err != nil {
- return err
- }
- rsp := &pb.PlayerSelectResponse{}
- err = p.Serializer().Unmarshal(msg.Data, rsp)
- if err != nil {
- return err
- }
- if len(rsp.List) < 1 {
- p.Debugf("[%s] not found player list.", p.TagName)
- return nil
- }
- p.PlayerId = rsp.List[0].PlayerId
- p.PlayerName = rsp.List[0].PlayerName
- p.Debugf("[%s] [PlayerSelect] response PlayerID = %d,PlayerName = %s", p.TagName, p.PlayerId, p.PlayerName)
- return nil
- }
- // ActorCreate 创建角色
- func (p *Robot) ActorCreate() error {
- if p.PlayerId > 0 {
- p.Debugf("[%s] deny create actor", p.TagName)
- return nil
- }
- route := "database.player.create"
- gender := rand.Int31n(1)
- req := &pb.PlayerCreateRequest{
- PlayerName: "p" + p.OpenId,
- Gender: gender,
- }
- msg, err := p.Request(route, req)
- if err != nil {
- return err
- }
- rsp := &pb.PlayerCreateResponse{}
- err = p.Serializer().Unmarshal(msg.Data, rsp)
- if err != nil {
- return err
- }
- p.PlayerId = rsp.Player.PlayerId
- p.PlayerName = rsp.Player.PlayerName
- p.Debugf("[%s] [ActorCreate] PlayerID = %d,ActorName = %s", p.TagName, p.PlayerId, p.PlayerName)
- return nil
- }
- // ActorEnter 角色进入游戏
- func (p *Robot) ActorEnter() error {
- route := "database.player.enter"
- req := &pb.Int64{
- Value: p.PlayerId,
- }
- msg, err := p.Request(route, req)
- if err != nil {
- return err
- }
- rsp := &pb.PlayerEnterResponse{}
- err = p.Serializer().Unmarshal(msg.Data, rsp)
- if err != nil {
- return err
- }
- p.Debugf("[%s] [ActorEnter] response PlayerID = %d,ActorName = %s", p.TagName, p.PlayerId, p.PlayerName)
- return nil
- }
- func (p *Robot) RandSleep() {
- time.Sleep(time.Duration(rand.Int31n(300)) * time.Millisecond)
- }
- func (p *Robot) Debug(args ...interface{}) {
- if p.PrintLog {
- mhayaLogger.Debug(args...)
- }
- }
- func (p *Robot) Debugf(template string, args ...interface{}) {
- if p.PrintLog {
- mhayaLogger.Debugf(template, args...)
- }
- }
|