go_dreamfactory/modules/troll/module.go

155 lines
4.2 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(session comm.IUserSession, troll *pb.DBTrollTrain) (code pb.ErrorCode) {
var (
trainCount int32 // 车站数量
fTime []int32 //每个车站货物刷新的时间
index int32 // 当前位置索引
crossTime int32 // 经过的时间
girdNum int32 // 格子数量
leftGirdNum int32 // 购买后剩余格子数量
// 购买的货物
bugItem map[int32]int32 // 购买的数据
bugItemPrice map[int32]int32
constGold int32
curRangeId int32
)
if troll.Buy != 0 && troll.Sell != 0 {
return
}
bugItem = make(map[int32]int32, 0)
bugItemPrice = make(map[int32]int32, 0)
troll.Buy = 840
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
}
}
curRangeId = troll.RangeId
// 计算时间差
crossTime = int32(time.Now().Unix() - troll.RefreshTime)
if crossTime < 10 {
return
}
MaxRangeId := this.configure.GetTrollMaxCoefficientNux() // 随机增幅最大值
for i := 0; ; i++ {
index = (int32(i) % trainCount)
if crossTime > fTime[index] { // 获取
crossTime -= fTime[index]
if crossTime < 0 {
break
}
curRangeId += 1
curRangeId = curRangeId%int32(MaxRangeId) + 1
if _d := this.configure.GetTrollCoefficient(curRangeId); _d != nil {
if troll.Buy <= int32(_d.Coefficient) { // 可以购买 那就尽最大的数量去买
goods := this.configure.GetTrollAllGoods() // 获取所有的货物信息
for _, v := range goods {
if leftGirdNum > 0 { // 剩余可购买格子数量
bugItemPrice[v.Id] = v.Goodsprice * _d.Coefficient / 1000 // 货物买入的价格
if troll.Items[v.Id] < v.Max { // 还可以购买的数量
// 先把这个格子填满
rd := troll.Items[v.Id] % maxgoods
buyN := v.Max - troll.Items[v.Id]
if buyN >= rd && rd != 0 { // 只能买这么多 那就全买了
troll.Items[v.Id] += rd
buyN -= rd
bugItem[v.Id] += rd
}
for {
if buyN >= maxgoods {
leftGirdNum -= 1
troll.Items[v.Id] += maxgoods
bugItem[v.Id] += maxgoods
if troll.Items[v.Id] >= v.Max {
break
}
}
if leftGirdNum <= 0 {
break
}
}
}
} else { // 补齐没有满的货物
rd := troll.Items[v.Id] % maxgoods
if rd != 0 {
troll.Items[v.Id] += (maxgoods - rd)
}
}
}
break
}
}
}
}
for k, v := range bugItem {
constGold += bugItemPrice[k] * v
}
code = this.ModuleUser.AddAttributeValue(session, comm.ResGold, -int32(constGold), true)
if code != pb.ErrorCode_Success { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
// 计算价格
return
}