/* 模块名:Troll 描述:巨怪商队 开发:梅雄风 */ package troll import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" "go_dreamfactory/pb" "math" "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 ( sellPrice map[int32]int32 // 出售货物价格 totalGold int32 index int32 update map[string]interface{} ) if troll.Buy == 0 && troll.Sell == 0 { return } update = make(map[string]interface{}) sellPrice = make(map[int32]int32) 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 { this.Errorf("GetTrollAllTrain configure err") // 配置异常 打个日志 return } iCount := this.configure.GetTrollRule(comm.TrollAIBuyCount) for index = 0; ; index++ { index := int32(index) % trainNum troll.RefreshTime += int64(sz[index]) if now >= troll.RefreshTime { troll.TarinPos++ troll.RangeId++ 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 { // 可以出售 var preGold int32 // 成本价 for _, v := range goods { sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient / 1000 preGold += troll.Price[v.Id] * troll.Items[v.Id] } // 出售之前算成本 if len(troll.Items) > 0 { sellGold := this.SellAllItem(troll, sellPrice) totalGold += sellGold // 计算本次出售赚的金币 if sellGold-preGold > 0 { troll.TotalEarn += int64(sellGold - preGold) } troll.AiCount++ if troll.AiCount > aiCount { //达到最大交易次数 break } } } else if troll.Buy >= coefficient.Coefficient { // 可以购买 for _, v := range goods { sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient / 1000 } troll.Shop = make(map[int32]int32) // 买之前清除购买上限 totalGold += this.BuyAllItem(session.GetUserId(), troll, sellPrice) } } else { // 超过当前时间 troll.RefreshTime -= int64(sz[index]) update["refreshTime"] = troll.RefreshTime update["tarinPos"] = troll.TarinPos update["rangeId"] = troll.RangeId update["shop"] = troll.Shop update["items"] = troll.Items update["price"] = troll.Price update["aiCount"] = troll.AiCount update["gridNum"] = troll.GridNum this.ModifyTrollData(session.GetUserId(), update) break } if index > iCount*maxCoefficient { // ai挂机最大限制 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) // 清除数据 } troll.Price = make(map[int32]int32, 0) // 原来的价格也清除 troll.GridNum = 0 // 清空格子 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 { for { if leftGirdNum > 0 && troll.Shop[v.Id] < v.Max { leftGirdNum-- //troll.Items[v.Id] += maxgoods troll.Shop[v.Id] += maxgoods box[v.Id] += maxgoods // 加入篮子 } else { break } } // 检查该位置的格子没有补满 full := (troll.Items[v.Id] + box[v.Id]) % maxgoods if full != 0 { box[v.Id] += (maxgoods - full) // 格子补满 } g := troll.Items[v.Id] * troll.Price[v.Id] g += box[v.Id] * price[v.Id] if (troll.Items[v.Id] + box[v.Id]) != 0 { troll.Price[v.Id] = g / (troll.Items[v.Id] + box[v.Id]) } } // 通过金币来校验哪些物品可以买 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 // 统计格子 troll.GridNum = 0 for _, v := range troll.Items { if v > 0 { troll.GridNum += int32(math.Ceil(float64(v) / float64(maxgoods))) } } return }