airobot/busi/sociaty.go
2022-12-29 19:24:57 +08:00

118 lines
2.5 KiB
Go

package busi
import (
"fmt"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/sirupsen/logrus"
"legu.airobot/lib"
"legu.airobot/pb"
)
var _ lib.IScene = (*SociatyScene)(nil)
const (
SociatyMainType = "sociaty"
)
type SociatyScene struct {
lib.Action
}
func (s *SociatyScene) Info() lib.SceneInfo {
return lib.SceneInfo{
Name: "公会",
Desc: "模拟公会所有操作",
}
}
func (s *SociatyScene) Run(robot lib.IRobot) error {
randInt := randomdata.Number(100)
if randInt%2 == 0 {
s.createSociaty(robot, false)
} else {
s.createSociaty(robot, true)
}
time.Sleep(time.Second)
s.mine(robot)
var sociatyMineResp *pb.SociatyMineResp
sociatyMineI := robot.Get(SOCIATY_MINE)
if sociatyMineI != nil {
sociatyMineResp = sociatyMineI.(*pb.SociatyMineResp)
if sociatyMineResp.Sociaty == nil {
s.queryAndApply(robot)
}
} else {
s.queryAndApply(robot)
}
return nil
}
// 创建公会
func (s *SociatyScene) createSociaty(robot lib.IRobot, isApplyCheck bool) {
req := &pb.SociatyCreateReq{}
rsp := &pb.SociatyCreateResp{}
req.Name = fmt.Sprintf("%s_%s", randomdata.SillyName(), randomdata.City())
req.IsApplyCheck = isApplyCheck
req.ApplyLv = 1
if code := robot.SendMsg(SociatyMainType, "create", req, rsp); code != pb.ErrorCode_Success {
logrus.Debugf("公会创建:%v", code)
return
}
logrus.Debug("创建公会完成")
}
func (s *SociatyScene) mine(robot lib.IRobot) {
req := &pb.SociatyMineReq{}
rsp := &pb.SociatyMineResp{}
if code := robot.SendMsg(SociatyMainType, "mine", req, rsp); code != pb.ErrorCode_Success {
logrus.Debugf("我的公会:%v", code)
return
}
robot.Store(SOCIATY_MINE, rsp)
logrus.Debug("我的公会")
}
func (s *SociatyScene) list(robot lib.IRobot) {
req := &pb.SociatyListReq{
Filter: pb.SociatyListFilter_ALL,
}
rsp := &pb.SociatyListResp{}
if code := robot.SendMsg("sociaty", "list", req, rsp); code == pb.ErrorCode_Success {
robot.Store(SOCIATY_LIST, rsp)
}
logrus.Debug("公会列表")
}
func (s *SociatyScene) apply(robot lib.IRobot, sociatyId string) {
req := &pb.SociatyApplyReq{
SociatyId: sociatyId,
}
rsp := &pb.SociatyApplyResp{}
if code := robot.SendMsg("sociaty", "apply", req, rsp); code == pb.ErrorCode_Success {
s.mine(robot)
}
}
func (s *SociatyScene) queryAndApply(robot lib.IRobot) {
s.list(robot)
var sociatyListResp *pb.SociatyListResp
listRspI := robot.Get(SOCIATY_LIST)
if listRspI != nil {
sociatyListResp = listRspI.(*pb.SociatyListResp)
if len(sociatyListResp.List) > 0 {
//申请公会
s.apply(robot, sociatyListResp.List[0].Id)
}
}
}