go_dreamfactory/modules/troll/module.go
2022-10-31 19:31:58 +08:00

93 lines
2.0 KiB
Go

/*
模块名:Troll
描述:巨怪商队
开发:梅雄风
*/
package troll
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"time"
)
type Troll struct {
modules.ModuleBase
modelTroll *modelTroll
api *apiComp
configure *configureComp
}
func NewModule() core.IModule {
return &Troll{}
}
func (this *Troll) GetType() core.M_Modules {
return comm.ModuleTroll
}
func (this *Troll) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
return
}
func (this *Troll) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelTroll = this.RegisterComp(new(modelTroll)).(*modelTroll)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
// 接口信息
func (this *Troll) ModifyTrollData(uid string, data map[string]interface{}) (code pb.ErrorCode) {
err := this.modelTroll.modifyTrollDataByObjId(uid, data)
if err != nil {
code = pb.ErrorCode_DBError
}
return
}
// AI 玩法
func (this *Troll) TrollAI(uid string, troll *pb.DBTrollTrain) (code pb.ErrorCode) {
var (
trainCount int32 // 车站数量
fTime []int32 //每个车站货物刷新的时间
index int32 // 当前位置索引
crossTime int32 // 经过的时间
)
if troll.Buy != 0 && troll.Sell != 0 {
return
}
trainCount = this.configure.GetTrollMaxTraintNum()
fTime = make([]int32, trainCount)
for i := 0; i < int(trainCount); i++ {
if conf := this.configure.GetTrollTrain(int32(i) + 1); conf != nil {
fTime[i] = conf.Time
}
}
// 计算时间差
crossTime = int32(time.Now().Unix() - troll.RefreshTime)
for i := 0; ; i++ {
index = int32(i) % trainCount
if crossTime > fTime[index] { // 获取
troll.RangeId += 1
if _d := this.configure.GetTrollCoefficient(troll.RangeId); _d != nil {
if troll.Buy <= int32(_d.Coefficient) { //
}
}
crossTime -= fTime[index]
if crossTime < 0 {
break
}
}
}
return
}