go_dreamfactory/modules/troll/module.go
2022-10-28 20:01:44 +08:00

92 lines
1.9 KiB
Go

/*
模块名:Troll
描述:巨怪商队
开发:梅雄风
*/
package troll
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"strconv"
"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) {
if troll.Buy != 0 && troll.Sell != 0 {
return
}
var fTime [3]int32
for i := 0; i < 3; i++ {
if conf := this.configure.GetTrollTrain(int32(i) + 1); conf != nil {
fTime[i] = conf.Time
}
}
var (
index int32
crossTime int32 // 经过的时间
)
// 计算时间差
crossTime = int32(time.Now().Unix() - troll.RefreshTime)
for i := 0; ; i++ {
index = int32(i) % 3
if crossTime > fTime[index] { // 获取
troll.RangeId += 1
if _d := this.configure.GetTrollCoefficient(troll.RangeId); _d != nil {
num, _ := strconv.Atoi(_d.Coefficient)
if troll.Buy <= int32(num) { //
}
}
crossTime -= fTime[index]
if crossTime < 0 {
break
}
}
}
return
}