package cli import ( "f1-game/internal/code" nameRoute "f1-game/internal/name/route" "f1-game/internal/pb" "fmt" "math/rand" "syscall" "unsafe" "time" cherryError "github.com/cherry-game/cherry/error" cherryHttp "github.com/cherry-game/cherry/extend/http" cherryTime "github.com/cherry-game/cherry/extend/time" cherryLogger "github.com/cherry-game/cherry/logger" cherryClient "github.com/cherry-game/cherry/net/parser/pomelo/client" cm "github.com/cherry-game/cherry/net/parser/pomelo/message" jsoniter "github.com/json-iterator/go" "google.golang.org/protobuf/proto" ) type ( // Robot client robot Robot struct { *cherryClient.Client PrintLog bool Token string gameNodeID string PID int32 UID int64 OpenID string PlayerID int64 PlayerName string StartTime cherryTime.CherryTime UserName string URL string IsNew bool kernel32DLL *syscall.DLL outputDebugStringProc *syscall.Proc } ) func New(client *cherryClient.Client, isNew bool) *Robot { robot := &Robot{ Client: client, IsNew: isNew, } if robot.IsNew { if kernel32DLL, err := syscall.LoadDLL("kernel32.dll"); err == nil { robot.kernel32DLL = kernel32DLL if outputDebugStringProc, err := kernel32DLL.FindProc("OutputDebugStringW"); err == nil { robot.outputDebugStringProc = outputDebugStringProc } } } return robot } // GetToken http://172.16.124.137/login?pid=2188001&account=test1&password=test1 func (p *Robot) GetToken(url string, pid, userName, password string) error { requestURL := fmt.Sprintf("%s/login", url) jsonBytes, _, err := cherryHttp.GET(requestURL, map[string]string{ "pid": pid, "account": userName, "password": password, }) if err != nil { return err } rsp := code.DataResult{} if err = jsoniter.Unmarshal(jsonBytes, &rsp); err != nil { return err } if code.IsFail(rsp.Code) { return cherryError.Errorf("get Token fail. [userName = %s, code = %d, message = %s]", userName, rsp.Code, rsp.Message) } p.URL = url p.UserName = userName p.Token = rsp.Data.(string) p.TagName = fmt.Sprintf("%s_%s", pid, userName) return nil } func (p *Robot) bindPush(pushKey string, push proto.Message) { p.On(pushKey, func(msg *cm.Message) { err := p.Serializer().Unmarshal(msg.Data, push) str, _ := jsoniter.MarshalToString(push) p.Debugf("[%s] %s push = %v, err = %v", p.TagName, pushKey, str, err) }) } func (p *Robot) ListenerPush() { p.bindPush(nameRoute.PushGamePlayer_Quests, &pb.QuestList{}) p.bindPush(nameRoute.PushMapPlayer_Quests, &pb.QuestList{}) p.bindPush(nameRoute.PushGamePlayer_HeroList, &pb.HeroList{}) p.bindPush(nameRoute.PushGamePlayer_HeroChange, &pb.HeroList{}) p.bindPush(nameRoute.PushGamePlayer_HeroDel, &pb.I64List{}) p.bindPush(nameRoute.PushGamePlayer_SkillList, &pb.SkillList{}) p.bindPush(nameRoute.PushMapPlayer_Team, &pb.Teams{}) p.bindPush(nameRoute.PushGamePlayer_Recruit, &pb.Recruit{}) p.bindPush(nameRoute.PushGamePlayer_ItemList, &pb.ItemList{}) p.bindPush(nameRoute.PushGamePlayer_ItemChange, &pb.ItemList{}) p.bindPush(nameRoute.PushMapPlayer_StorageList, &pb.ItemList{}) p.bindPush(nameRoute.PushMapPlayer_StoragChange, &pb.ItemList{}) p.bindPush(nameRoute.PushGamePlayer_Reward, &pb.AssetList{}) p.bindPush(nameRoute.PushMapPlayer_FacilityList, &pb.Facilities{}) p.bindPush(nameRoute.PushMapPlayer_FacilityFinish, &pb.FacilityList{}) p.bindPush(nameRoute.PushMapPlayer_FacilityAttrChange, &pb.Attrs{}) p.bindPush(nameRoute.PushMapPlayer_ResDefendRecover, &pb.I64I64{}) p.bindPush(nameRoute.PushMapPlayer_Conscript, &pb.Conscript{}) p.bindPush(nameRoute.PushGamePlayer_Lord, &pb.Lord{}) p.bindPush(nameRoute.PushGamePlayer_HeroBookList, &pb.I32List{}) p.bindPush(nameRoute.PushGamePlayer_VipInfo, &pb.VipInfo{}) p.bindPush(nameRoute.PushGamePlayer_ChargeData, &pb.ChargeData{}) p.bindPush(nameRoute.PushGamePlayer_EquipList, &pb.EquipList{}) p.bindPush(nameRoute.PushGamePlayer_EquipChange, &pb.EquipList{}) p.bindPush(nameRoute.PushGamePlayer_EquipDel, &pb.I64List{}) p.bindPush(nameRoute.PushChat_MsgList, &pb.ChatMsgList{}) p.bindPush(nameRoute.PushChat_Delete, &pb.I64{}) p.bindPush(nameRoute.PushGamePlayer_ChatData, &pb.ChatData{}) p.bindPush(nameRoute.PushGamePlayer_ChatRoomUpdate, &pb.ChatRoomUpdate{}) p.bindPush(nameRoute.PushGamePlayer_ChatGroupLeave, &pb.I64Bool{}) p.bindPush(nameRoute.PushGamePlayer_ChatGroupInvite, &pb.GroupInvite{}) p.bindPush(nameRoute.PushGamePlayer_ChatPrivateNew, &pb.ChatMsg{}) p.bindPush(nameRoute.PushGamePlayer_ChatSystemNew, &pb.SystemMsg{}) p.bindPush(nameRoute.PushGamePlayer_FriendInviteNewPush, &pb.FriendInvite{}) p.bindPush(nameRoute.PushGamePlayer_FirendNew, &pb.FriendRelation{}) p.bindPush(nameRoute.PushGamePlayer_FirendDel, &pb.I64{}) p.bindPush(nameRoute.PushGamePlayer_FriendAddBack, &pb.I64I32{}) p.bindPush(nameRoute.PushGamePlayer_FriendConfirmBack, &pb.I64I32{}) p.bindPush(nameRoute.PushMapPusher_ObjectUpdate, &pb.MapObjects{}) p.bindPush(nameRoute.PushMapPusher_PlayerUpdate, &pb.MapPlayers{}) p.bindPush(nameRoute.PushMapPusher_LeagueUpdate, &pb.MapLeagues{}) p.bindPush(nameRoute.PushMapPusher_ObjectDel, &pb.I64List{}) p.bindPush(nameRoute.PushMapPusher_MarchState, &pb.MapMarchState{}) p.bindPush(nameRoute.PushMapPusher_Battle, &pb.MapBattlePush{}) p.bindPush(nameRoute.PushMapPlayer_SiegeResult, &pb.SiegeResult{}) p.bindPush(nameRoute.PushGameMail_List, &pb.MailList{}) p.bindPush(nameRoute.PushGameMail_New, &pb.MailList{}) p.bindPush(nameRoute.PushGameMail_Delete, &pb.I64List{}) p.bindPush(nameRoute.PushMapLeague_AssembleUpdate, &pb.I32{}) p.bindPush(nameRoute.PushMapPlayer_MapMark, &pb.MapMarkList{}) p.bindPush(nameRoute.PushMapLeague_MapMark, &pb.MapMarkList{}) p.bindPush(nameRoute.PushMapLeague_MapMarkDel, &pb.I32{}) p.bindPush(nameRoute.PushMapLeague_MapMarkChange, &pb.MapMark{}) p.bindPush(nameRoute.PushMapLeague_BuildList, &pb.LeagueBuildList{}) p.bindPush(nameRoute.PushMapLeague_BuildUpdate, &pb.MapObjectsUpdate{}) p.bindPush(nameRoute.PushMapLeague_WarUpdate, &pb.I32{}) p.bindPush(nameRoute.PushMapPlayer_SiegeResult, &pb.SiegeResult{}) p.bindPush(nameRoute.PushMapPlayer_Reborn, &pb.RebornPush{}) p.bindPush(nameRoute.PushMapPlayer_OwnerMapObjects, &pb.OwnerMapObjects{}) p.bindPush(nameRoute.PushMapPlayer_MapUICastlesPush, &pb.MapUICastles{}) } func (p *Robot) RequestResponse(route string, req, rsp any) error { msg, err := p.Request(route, req) if err != nil { p.Debugf("[%s] request error. = %+v", route, err) return err } err = p.Serializer().Unmarshal(msg.Data, rsp) if err != nil { p.Debugf("[%s] unmarshal error. = %+v", route, err) } return err } func (p *Robot) RandSleep() { time.Sleep(time.Duration(rand.Int31n(300)) * time.Millisecond) } func (p *Robot) Debug(args ...any) { if p.PrintLog { cherryLogger.Debug(args...) } } func (p *Robot) Debugf(template string, args ...any) { if p.PrintLog { if p.IsNew { p.NewDebugf(template, args...) } else { cherryLogger.Debugf(template, args...) } } } func (p *Robot) NewDebugf(template string, args ...any) { if !p.IsNew { return } msg := fmt.Sprintf(template, args...) msg = "f1cli_dbgMsg:" + msg utf16Msg, err := syscall.UTF16PtrFromString(msg) if err != nil { return } if p.PrintLog { p.outputDebugStringProc.Call(uintptr(unsafe.Pointer(utf16Msg))) } }