go_dreamfactory/modules/smithy/model_trade.go
2023-02-16 15:45:29 +08:00

73 lines
1.6 KiB
Go

package smithy
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
cfg "go_dreamfactory/sys/configure/structs"
"math/rand"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type modelTrade struct {
modules.MCompModel
module *Smithy
}
func (this *modelTrade) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = string(comm.TableSmithyTrade)
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*Smithy)
// uid 创建索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
// 权重项目
type WeightedRandom struct {
items []*cfg.GameSmithyCustomerData
totalWeight int
}
func newWeightedRandom(items []*cfg.GameSmithyCustomerData) *WeightedRandom {
wr := &WeightedRandom{items: items}
for _, item := range wr.items {
wr.totalWeight += int(item.Weight)
}
return wr
}
func (wr *WeightedRandom) pick() *cfg.GameSmithyCustomerData {
randomNumber := rand.Intn(wr.totalWeight)
for _, item := range wr.items {
if randomNumber < int(item.Weight) {
return item
}
randomNumber -= int(item.Weight)
}
return nil
}
// 随机顾客
func (s *modelTrade) getCustomerRandom() (customerId int32) {
items := s.module.configure.GetSmithyCustomerConfList()
wr := newWeightedRandom(items)
if c := wr.pick(); c != nil {
customerId = c.CustomerId
}
return
}
// 更新顾客
func(s *modelTrade) updateCustomer(uid string) {
}
//顾客类型策略
//交易