go_dreamfactory/modules/troll/module.go

129 lines
3.4 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 // 经过的时间
girdNum int32 // 格子数量
leftGirdNum int32 // 购买后剩余格子数量
)
if troll.Buy != 0 && troll.Sell != 0 {
return
}
girdNum = troll.GridNum
maxGirdNum := this.configure.GetTrollRule(comm.TrollGridCount) // 获取背包最大格子数量
maxgoods := this.configure.GetTrollRule(comm.TrollItemCount) // 获取单个物品最大上限
trainCount = this.configure.GetTrollMaxTraintNum()
leftGirdNum = maxGirdNum - girdNum
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)
MaxRangeId := this.configure.GetTrollMaxCoefficientNux() // 随机增幅最大值
for i := 0; ; i++ {
index = int32(i) % trainCount
if crossTime > fTime[index] { // 获取
crossTime -= fTime[index]
if crossTime < 0 {
break
}
troll.RangeId += 1
troll.RangeId = troll.RangeId%int32(MaxRangeId) + 1
if _d := this.configure.GetTrollCoefficient(troll.RangeId); _d != nil {
if troll.Buy <= int32(_d.Coefficient) { // 可以购买 那就尽最大的数量去买
goods := this.configure.GetTrollAllGoods() // 获取所有的货物信息
for _, v := range goods {
if leftGirdNum > 0 { // 剩余可购买格子数量
if troll.Items[v.Id] < v.Max { // 还可以购买的数量
// 先把这个格子填满
rd := troll.Items[v.Id] % maxgoods
buyN := v.Max - troll.Items[v.Id]
if buyN >= rd { // 只能买这个多 那就全买了
troll.Items[v.Id] += rd
buyN -= rd
} else {
break
}
for {
if buyN >= maxgoods {
leftGirdNum -= 1
troll.Items[v.Id] += maxgoods
}
if leftGirdNum <= 0 {
break
}
}
}
} else { // 补齐没有满的货物
rd := troll.Items[v.Id] % maxgoods
troll.Items[v.Id] += (maxgoods - rd)
}
}
}
}
}
}
return
}