Merge branch 'meixiongfeng' of http://git.legu.cc/liwei_3d/go_dreamfactory into dev

This commit is contained in:
meixiongfeng 2022-11-02 21:31:26 +08:00
commit fb17cad095
6 changed files with 147 additions and 99 deletions

View File

@ -1,11 +1,11 @@
[ [
{ {
"id": 1, "id": 1,
"quantity": 5 "quantity": 8
}, },
{ {
"id": 2, "id": 2,
"quantity": 20 "quantity": 10
}, },
{ {
"id": 3, "id": 3,
@ -13,18 +13,22 @@
}, },
{ {
"id": 4, "id": 4,
"quantity": 1020 "quantity": 20
}, },
{ {
"id": 5, "id": 5,
"quantity": 980 "quantity": 1020
}, },
{ {
"id": 6, "id": 6,
"quantity": 5 "quantity": 980
}, },
{ {
"id": 7, "id": 7,
"quantity": 5
},
{
"id": 8,
"quantity": 1500 "quantity": 1500
} }
] ]

View File

@ -412,6 +412,8 @@ const (
const ( const (
TrollBuyCount int32 = iota + 1 // 单日最大交易次数 TrollBuyCount int32 = iota + 1 // 单日最大交易次数
TrollAIBuyCount //离线最多交易次数
TrollItemCount //货物最大存储上限 TrollItemCount //货物最大存储上限
TrollGridCount //背包格子 TrollGridCount //背包格子

View File

@ -55,7 +55,9 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell
trolltrain.Items[k] += v trolltrain.Items[k] += v
} }
if _, ok := trolltrain.Shop[k]; !ok { if _, ok := trolltrain.Shop[k]; !ok {
trolltrain.Shop[k] = v if v > 0 {
trolltrain.Shop[k] = v // 限购
}
} else { } else {
if v > 0 { if v > 0 {
trolltrain.Shop[k] += v // 限购 trolltrain.Shop[k] += v // 限购
@ -145,6 +147,7 @@ func (this *apiComp) BuyOrSell(session comm.IUserSession, req *pb.TrollBuyOrSell
update["totalEarn"] = trolltrain.TotalEarn update["totalEarn"] = trolltrain.TotalEarn
update["gridNum"] = trolltrain.GridNum update["gridNum"] = trolltrain.GridNum
update["shop"] = trolltrain.Shop
this.module.ModifyTrollData(session.GetUserId(), update) this.module.ModifyTrollData(session.GetUserId(), update)
session.SendMsg(string(this.module.GetType()), TrollBuyOrSellResp, &pb.TrollBuyOrSellResp{Data: trolltrain}) session.SendMsg(string(this.module.GetType()), TrollBuyOrSellResp, &pb.TrollBuyOrSellResp{Data: trolltrain})
return return

View File

@ -38,9 +38,12 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
return return
} }
// 测试代码===================== aiCount := this.configure.GetTrollRule(comm.TrollAIBuyCount)
//this.module.TrollAI(session, trolltrain) if trolltrain.AiCount <= aiCount { // 小于离线挂机次数可执行
//=========================== this.module.TrollAI(session, trolltrain, aiCount)
update["aiCount"] = trolltrain.AiCount
}
maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值 maxCoefficient = this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
if maxCoefficient == 0 { if maxCoefficient == 0 {
code = pb.ErrorCode_ConfigNoFound code = pb.ErrorCode_ConfigNoFound
@ -49,7 +52,9 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
// 跨天 则清除 每日交易次数 // 跨天 则清除 每日交易次数
if !utils.IsToday(trolltrain.RefreshTime) { if !utils.IsToday(trolltrain.RefreshTime) {
trolltrain.SellCount = 0 trolltrain.SellCount = 0
update["sellCount"] = trolltrain.SellCount update["sellCount"] = trolltrain.SellCount // 重置每日交易次数
trolltrain.AiCount = 0
update["aiCount"] = trolltrain.AiCount // 重置ai 交易次数
this.module.ModifyTrollData(session.GetUserId(), update) this.module.ModifyTrollData(session.GetUserId(), update)
} }
@ -90,14 +95,17 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.TrollGetListReq)
trolltrain.RangeId += index trolltrain.RangeId += index
trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1 trolltrain.RangeId = (trolltrain.RangeId % maxCoefficient) + 1
trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1 trolltrain.TarinPos = (trolltrain.TarinPos % trainNum) + 1
update["refreshTime"] = trolltrain.RefreshTime
update["tarinPos"] = trolltrain.TarinPos
update["rangeId"] = trolltrain.RangeId
break break
} }
index += 1 index += 1
leftTime -= v leftTime -= v
} }
update["refreshTime"] = trolltrain.RefreshTime
update["tarinPos"] = trolltrain.TarinPos
update["rangeId"] = trolltrain.RangeId
update["shop"] = trolltrain.Shop
update["items"] = trolltrain.Items
update["price"] = trolltrain.Price
this.module.ModifyTrollData(session.GetUserId(), update) this.module.ModifyTrollData(session.GetUserId(), update)
session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain}) session.SendMsg(string(this.module.GetType()), TrollGetListResp, &pb.TrollGetListResp{Data: trolltrain})
return return

View File

@ -115,10 +115,12 @@ func (this *configureComp) GetTrollLv(id int32) (data *cfg.GameTrollLvData) {
} }
func (this *configureComp) GetTrollAllTrain() (data []int32) { func (this *configureComp) GetTrollAllTrain() (data []int32) {
data = make([]int32, 0)
if v, err := this.GetConfigure(game_trolltrain); err == nil { if v, err := this.GetConfigure(game_trolltrain); err == nil {
if configure, ok := v.(*cfg.GameTrollTrain); ok { if configure, ok := v.(*cfg.GameTrollTrain); ok {
for _, v := range configure.GetDataList() { for _, v := range configure.GetDataList() {
data = append(data, v.Time) time := v.Time
data = append(data, time)
} }
return return
} }

View File

@ -50,105 +50,134 @@ func (this *Troll) ModifyTrollData(uid string, data map[string]interface{}) (cod
return return
} }
// AI 玩法 func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain, aiCount int32) (code pb.ErrorCode) {
func (this *Troll) TrollAI(session comm.IUserSession, troll *pb.DBTrollTrain) (code pb.ErrorCode) {
var ( var (
trainCount int32 // 车站数量 t int64 // 上一次刷新的时间
fTime []int32 //每个车站货物刷新的时间 sellPrice map[int32]int32 // 出售货物价格
index int32 // 当前位置索引 totalGold int32
crossTime int32 // 经过的时间 pos int32
girdNum int32 // 格子数量
leftGirdNum int32 // 购买后剩余格子数量
// 购买的货物
bugItem map[int32]int32 // 购买的数据
bugItemPrice map[int32]int32
constGold int32
curRangeId int32
) )
if troll.Buy != 0 && troll.Sell != 0 { if troll.Buy != 0 && troll.Sell != 0 {
return return
} }
bugItem = make(map[int32]int32, 0) sellPrice = make(map[int32]int32)
bugItemPrice = make(map[int32]int32, 0) t = troll.RefreshTime
troll.Buy = 840 now := time.Now().Unix()
girdNum = troll.GridNum trainNum := this.configure.GetTrollMaxTraintNum()
maxGirdNum := this.configure.GetTrollRule(comm.TrollGridCount) // 获取背包最大格子数量 maxCoefficient := this.configure.GetTrollMaxCoefficientNux() // 增长幅度的最大值
maxgoods := this.configure.GetTrollRule(comm.TrollItemCount) // 获取单个物品最大上限 if maxCoefficient == 0 {
trainCount = this.configure.GetTrollMaxTraintNum() code = pb.ErrorCode_ConfigNoFound
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 return
} }
MaxRangeId := this.configure.GetTrollMaxCoefficientNux() // 随机增幅最大值 goods := this.configure.GetTrollAllGoods()
for i := 0; ; i++ { for _, v := range goods {
index = (int32(i) % trainCount) sellPrice[v.Id] = v.Goodsprice
if crossTime > fTime[index] { // 获取 }
crossTime -= fTime[index] sz := this.configure.GetTrollAllTrain()
if crossTime < 0 { if len(sz) == 0 {
break 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
} }
curRangeId += 1 if troll.Sell >= coefficient.Coefficient { // 可以出售
curRangeId = curRangeId%int32(MaxRangeId) + 1 for _, v := range goods {
if _d := this.configure.GetTrollCoefficient(curRangeId); _d != nil { sellPrice[v.Id] = v.Goodsprice * coefficient.Coefficient
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)
}
} totalGold += this.SellAllItem(troll, sellPrice)
} troll.AiCount++
if troll.AiCount > aiCount { //达到最大交易次数
troll.RefreshTime = t // 设置上次刷新的时间
break 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
} }
} }
for k, v := range bugItem { this.ModuleUser.AddAttributeValue(session, comm.ResGold, totalGold, true)
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
}
// 出售所有货物
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 return
} }