go_dreamfactory/modules/troll/module.go
2022-11-02 21:31:07 +08:00

184 lines
4.8 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
}
func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain, aiCount int32) (code pb.ErrorCode) {
var (
t int64 // 上一次刷新的时间
sellPrice map[int32]int32 // 出售货物价格
totalGold int32
pos int32
)
if troll.Buy != 0 && troll.Sell != 0 {
return
}
sellPrice = make(map[int32]int32)
t = troll.RefreshTime
now := time.Now().Unix()
trainNum := this.configure.GetTrollMaxTraintNum()
maxCoefficient := this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
if maxCoefficient == 0 {
code = pb.ErrorCode_ConfigNoFound
return
}
goods := this.configure.GetTrollAllGoods()
for _, v := range goods {
sellPrice[v.Id] = v.Goodsprice
}
sz := this.configure.GetTrollAllTrain()
if len(sz) == 0 {
return
}
iCount := this.configure.GetTrollRule(comm.TrollSurprise)
for {
pos++
if pos > iCount {
break
}
index := int32(pos) % trainNum
if int32(len(sz)) <= index {
break
}
t += int64(sz[index])
if now < t {
troll.RangeId++
troll.TarinPos++
troll.RangeId = (troll.RangeId % maxCoefficient) + 1
troll.TarinPos = (troll.TarinPos % trainNum) + 1
coefficient := this.configure.GetTrollCoefficient(troll.RangeId)
if coefficient == nil {
return
}
if troll.Sell >= coefficient.Coefficient { // 可以出售
for _, v := range goods {
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient
}
totalGold += this.SellAllItem(troll, sellPrice)
troll.AiCount++
if troll.AiCount > aiCount { //达到最大交易次数
troll.RefreshTime = t // 设置上次刷新的时间
break
}
}
if troll.Buy >= coefficient.Coefficient { // 可以购买
for _, v := range goods {
sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient
}
troll.Shop = make(map[int32]int32) // 买之前清除购买上限
totalGold += this.BuyAllItem(session.GetUserId(), troll, sellPrice)
}
} else { // 超过当前时间
troll.RefreshTime = t
break
}
}
this.ModuleUser.AddAttributeValue(session, comm.ResGold, totalGold, true)
return
}
// 出售所有货物
func (this *Troll) SellAllItem(troll *pb.DBTrollTrain, price map[int32]int32) (gold int32) {
for k, v := range troll.Items {
if _, ok := price[k]; ok {
gold += price[k] * v
}
delete(troll.Items, k) // 清除数据
}
return
}
// 可以购买商品
func (this *Troll) BuyAllItem(uid string, troll *pb.DBTrollTrain, price map[int32]int32) (gold int32) {
var (
box map[int32]int32 // 盒子 存放可购买的物品
leftGirdNum int32 // 剩余可购买格子数量
costGold int32
)
maxGirdNum := this.configure.GetTrollRule(comm.TrollGridCount) // 获取背包最大格子数量
maxgoods := this.configure.GetTrollRule(comm.TrollItemCount) // 获取单个物品最大上限 20个
leftGirdNum = maxGirdNum - troll.GridNum
box = make(map[int32]int32, 0)
goods := this.configure.GetTrollAllGoods()
for _, v := range goods {
if leftGirdNum > 0 && troll.Shop[v.Id] < v.Max {
leftGirdNum--
troll.Items[v.Id] += maxgoods
troll.Shop[v.Id] += maxgoods
box[v.Id] += maxgoods // 加入篮子
}
// 检查该位置的格子没有补满
full := (troll.Items[v.Id] + box[v.Id]) % maxgoods
if full != 0 {
box[v.Id] += (maxgoods - full) // 格子补满
}
}
// 通过金币来校验哪些物品可以买
curGold := this.ModuleUser.QueryAttributeValue(uid, comm.ResGold)
for k, v := range box {
for i := 0; i < int(v); i++ { //0 1 2 3
curGold -= int64(price[k])
costGold -= price[k]
if curGold < 0 {
box[k] = int32(i)
costGold += price[k] // 返还之前扣的
break
}
}
}
troll.Items = box
gold = costGold
return
}