package pushgiftbag import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" ) type PushGiftbag struct { modules.ModuleBase service core.IService api *apiComp configure *configureComp model *ModelPushGiftbag open bool } func NewModule() core.IModule { return &PushGiftbag{} } func (this *PushGiftbag) GetType() core.M_Modules { return comm.ModulePushgiftbag } func (this *PushGiftbag) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { if err = this.ModuleBase.Init(service, module, options); err != nil { return } this.service = service return } func (this *PushGiftbag) Start() (err error) { if err = this.ModuleBase.Start(); err != nil { return } return } func (this *PushGiftbag) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.model = this.RegisterComp(new(ModelPushGiftbag)).(*ModelPushGiftbag) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) } // 发货 func (this *PushGiftbag) Delivery(session comm.IUserSession, pId int32) (errdata *pb.ErrorData, items []*pb.UserAssets) { var ( confs []*cfg.GamePushGiftData conf *cfg.GamePushGiftData info *pb.DBPushGiftbag err error ) if confs, err = this.configure.getGamePushGift(); err != nil { this.Errorln(err) return } if info, err = this.model.getUserPushGift(session.GetUserId()); err != nil { this.Errorln(err) return } for _, v := range confs { if v.Id == pId { conf = v break } } if conf == nil { errdata = &pb.ErrorData{ Code: pb.ErrorCode_ConfigNoFound, Title: pb.ErrorCode_ConfigNoFound.String(), Message: fmt.Sprintf("no found pid:%s", pId), } return } if errdata = this.DispenseRes(session, conf.Lbid, true); err != nil { return } for i, v := range info.Item { if v.Id == conf.Id { info.Item = append(info.Item[0:i], info.Item[i+1:]...) } } this.model.Change(session.GetUserId(), map[string]interface{}{ "item": info.Item, }) session.SendMsg(string(this.GetType()), "chanage", &pb.PushGiftbagChanagePush{Item: info.Item}) go this.AsynHandleSession(session.Clone(), func(session comm.IUserSession) { this.WriteUserLog(session.GetUserId(), 0, "PushGiftbagChanagePush", conf.Lbid) }) return } // 埋点通知 func (this *PushGiftbag) BuriedsNotify(session comm.IUserSession, condis []*pb.ConIProgress) { var ( confs []*cfg.GamePushGiftData info *pb.DBPushGiftbag condisMap map[int32]*pb.ConIProgress = make(map[int32]*pb.ConIProgress) comdisScils []int32 = make([]int32, 0) targets map[int32]*cfg.GamePushGiftData = make(map[int32]*cfg.GamePushGiftData) conIProgress *pb.ConIProgress item []*pb.DBPushGiftbagItem ok bool err error ) this.Debug("收到子任务进度变化推送", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "condis", Value: condis}) if confs, err = this.configure.getGamePushGift(); err != nil { this.Errorln(err) return } if info, err = this.model.getUserPushGift(session.GetUserId()); err != nil { this.Errorln(err) return } for _, v := range condis { condisMap[v.Conid] = v } for _, v := range confs { for _, v1 := range v.Condition { if _, ok = condisMap[v1]; ok { targets[v.Id] = v comdisScils = append(comdisScils, v.Condition...) break } } } for _, v := range comdisScils { if _, ok = condisMap[v]; !ok { //条件不全需要查询全部条件 if condis, err = this.ModuleBuried.CheckCondition(session.GetUserId(), comdisScils...); err != nil { this.Error("校验玩家子任务进度数据 失败", log.Field{Key: "err", Value: err.Error()}) return } condisMap = make(map[int32]*pb.ConIProgress) for _, v := range condis { condisMap[v.Conid] = v } break } } for k, v := range targets { for _, cid := range v.Condition { if conIProgress, ok = condisMap[cid]; !ok || conIProgress.State == pb.BuriedItemFinishState_buried_unfinish { //未完成 delete(targets, k) } } for _, cid := range v.ConditionOr { if conIProgress, ok = condisMap[cid]; ok && conIProgress.State == pb.BuriedItemFinishState_buried_finish { //完成一个即可 ok = true break } ok = false } if !ok { delete(targets, k) } } if len(targets) > 0 { item = make([]*pb.DBPushGiftbagItem, 0) for _, target := range targets { ok = false for _, v := range info.Item { if target.Id == v.Id { v.Stime = configure.Now().Unix() ok = true break } } if !ok { item = append(item, &pb.DBPushGiftbagItem{ Id: target.Id, Stime: configure.Now().Unix(), }) } } if len(item) > 0 { info.Item = append(info.Item, item...) } this.model.Change(session.GetUserId(), map[string]interface{}{ "item": info.Item, }) this.Debug("推送礼包", log.Field{Key: "礼包", Value: info.Item}) session.SendMsg(string(this.GetType()), "chanage", &pb.PushGiftbagChanagePush{Item: info.Item}) } }