package smithy import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "math/rand" "go.mongodb.org/mongo-driver/bson/primitive" "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) getDBCustomer(uid string) (*pb.DBCustomer, error) { customer := &pb.DBCustomer{} if err := s.Get(uid, customer); err != nil { s.module.Errorln(err) return nil, err } return customer, nil } // 初始顾客 func (s *modelTrade) addCustomer(uid string, num int32) (*pb.DBCustomer, error) { customer := &pb.DBCustomer{ Id: primitive.NewObjectID().Hex(), Uid: uid, Total: num, LastRefreshTime: configure.Now().Unix(), } for i := 0; i < int(num); i++ { customer.CustomerIds = append(customer.CustomerIds, s.getCustomerRandom()) } if err := s.Add(uid, customer); err != nil { s.module.Errorln(err) return nil, err } return customer, nil } // 移除顾客 func (s *modelTrade) removeCustomer(cus *pb.DBCustomer, customerId int32) *pb.DBCustomer { for i, v := range cus.CustomerIds { if v == customerId { cus.CustomerIds = append(cus.CustomerIds[:i], cus.CustomerIds[i+1:]...) i-- } } return cus } // 随机新顾客 func (s *modelTrade) updateCustomer(uid string, customerId int32) error { cus, err := s.getDBCustomer(uid) if err == nil { cus = s.removeCustomer(cus, customerId) cus.Total++ //上限 limit := 20 left := limit - int(cus.Total) if left <= 0 { return comm.NewCustomError(pb.ErrorCode_SmithyCustomerLimit) } cus.CustomerIds = append(cus.CustomerIds, s.getCustomerRandom()) cus.LastRefreshTime = configure.Now().Unix() update := map[string]interface{}{ "customerIds": cus.CustomerIds, "total": cus.Total, "lastRefreshTime": cus.LastRefreshTime, } if err := s.Change(uid, update); err != nil { return err } } return nil } //顾客类型策略 //交易