package oldtimes import ( "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "sort" "go.mongodb.org/mongo-driver/mongo" ) type ModelOldtimes struct { modules.MCompModel moduleOldtimes *Oldtimes service core.IService } type status int32 const ( lock status = 0 //未解锁 unlock status = 1 //已解锁 ongoing status = 2 //进行中 finish status = 3 //完成 ) func (this *ModelOldtimes) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { err = this.MCompModel.Init(service, module, comp, options) this.TableName = comm.TableOldtimes this.moduleOldtimes = module.(*Oldtimes) this.service = service return } func (this *ModelOldtimes) firstChapter() int32 { gltl, err := this.moduleOldtimes.configure.getTimelineCfg() if err != nil { return 0 } list := gltl.GetDataList() if len(list) == 0 { return 0 } sort.SliceStable(list, func(i int, j int) bool { return list[i].Order < list[j].Order }) return list[0].Group } func (this *ModelOldtimes) firstLevel(groupId int32) int32 { glmt, err := this.moduleOldtimes.configure.getMaintaskCfg() if err != nil { return 0 } for _, l := range glmt.GetDataList() { if l.Group == groupId && l.SubTask == 0 { return l.Id } } return 0 } func (this *ModelOldtimes) getDBOldtimes(uid string) *pb.DBOldtimes { ot := &pb.DBOldtimes{} if err := this.Get(uid, ot); err != nil { if err == mongo.ErrNoDocuments { ot.Uid = uid //默认启动第一章第一关卡 groupId := this.firstChapter() levelId := this.firstLevel(groupId) if groupId == 0 || levelId == 0 { this.moduleOldtimes.Error("init oldtimes id err", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err}) return nil } chapter := &pb.Chapter{ Cid: groupId, Status: int32(unlock), //解锁 } chapter.Levels = append(chapter.Levels, &pb.Level{ Lid: levelId, Status: int32(unlock), }) ot.Chapters = append(ot.Chapters, chapter) if err := this.Add(uid, ot); err != nil { this.moduleOldtimes.Error("add oldtimes err", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err}) return nil } } } return ot } func (this *ModelOldtimes) getChatper(chapterId int32, data *pb.DBOldtimes) *pb.Chapter { for _, d := range data.Chapters { if d.Cid == chapterId { return d } } return nil } func (this *ModelOldtimes) getLevel(levelId int32, chapter *pb.Chapter) *pb.Level { if chapter == nil { return nil } for _, d := range chapter.Levels { if d.Lid == levelId { return d } } return nil } func (this *ModelOldtimes) isExist(levelId int32, ot *pb.DBOldtimes) bool { for _, chapter := range ot.Chapters { for _, level := range chapter.Levels { if level.Lid == levelId { return true } } } return false } // 更新旧时光 func (this *ModelOldtimes) updateOldtimes(uid string, ot *pb.DBOldtimes) error { update := map[string]interface{}{ "chapters": ot.Chapters, } return this.Change(uid, update) }