package moonfantasy import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/mgo" "go_dreamfactory/lego/sys/timewheel" "go_dreamfactory/modules" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "go_dreamfactory/sys/db" "time" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) ///论坛 数据组件 type modelDreamComp struct { modules.MCompModel module *Moonfantasy } //组件初始化接口 func (this *modelDreamComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.MCompModel.Init(service, module, comp, opt) this.module = module.(*Moonfantasy) this.TableName = comm.TableMoonFantasy this.Expired = 0 //创建uid索引 this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } ///添加月之秘境记录 func (this *modelDreamComp) querymfantasy(mid string) (result *pb.DBMoonFantasy, err error) { result = &pb.DBMoonFantasy{} if err = this.Get(mid, result); err != nil && err != mongo.ErrNilDocument { this.module.Errorln(err) } return } ///插叙用户秘境列表 func (this *modelDreamComp) querymfantasys(mids []string) (fantasys []*pb.DBMoonFantasy, err error) { fantasys = make([]*pb.DBMoonFantasy, 0) if err = this.Gets(mids, fantasys); err != nil { this.module.Errorf("err:%v", err) } return } ///添加月之秘境记录 func (this *modelDreamComp) addDreamData(user *pb.UserInfo, boss *cfg.GameDreamlandBoosData) (result *pb.DBMoonFantasy, err error) { result = &pb.DBMoonFantasy{ Id: primitive.NewObjectID().Hex(), Uid: user.Uid, Monster: boss.Bossid, Ctime: time.Now().Unix(), Join: []*pb.UserInfo{user}, Numup: boss.Challengenum, Unitmup: boss.Fightnum, Record: make(map[string]int32), } if err = this.Add(result.Id, result); err != nil { this.module.Errorln(err) } return } ///查询好友数据 func (this *modelDreamComp) noticeuserfriend(stag, uid, mid string, chat *pb.DBChat) (code pb.ErrorCode) { var ( err error ) model := db.NewDBModel(comm.TableFriend, 0, db.ServerDBConn(stag)) friend := &pb.DBFriend{Uid: uid, FriendIds: make([]string, 0)} if err = model.Get(uid, friend); err != nil && err != mgo.MongodbNil { this.module.Errorln(err) return } for _, v := range friend.FriendIds { temp := *chat temp.Id = primitive.NewObjectID().Hex() temp.Channel = pb.ChatChannel_Private temp.Ruid = v this.module.chat.SendUserChat(&temp) } chat.Id = primitive.NewObjectID().Hex() chat.Channel = pb.ChatChannel_World // code = this.module.chat.SendWorldChat(stag, chat) this.delaynoticeWorld(stag, mid, chat) return } //延迟推送到 世界聊天频道 func (this *modelDreamComp) delaynoticeWorld(stag, mid string, chat *pb.DBChat) { timewheel.Add(time.Minute*15, func(t *timewheel.Task, i ...interface{}) { _mid := i[0].(string) _chat := i[1].(*pb.DBChat) if mdata, err := this.querymfantasy(_mid); err != nil { return } else { if len(mdata.Join) >= int(mdata.Numup) { //参与人数已达上限 不予推送 return } } this.module.chat.SendWorldChat(_chat) }, mid, chat) }