package user import ( "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/event" "go_dreamfactory/lego/sys/log" ) func NewModule() core.IModule { m := new(User) return m } type User struct { modules.ModuleBase api *apiComp modelUser *ModelUser modelSession *ModelSession modelSetting *ModelSetting configure *modules.MCompConfigure } func (this *User) GetType() core.M_Modules { return comm.ModuleUser } func (this *User) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) return } func (this *User) Start() (err error) { err = this.ModuleBase.Start() event.RegisterGO(comm.EventUserOffline, this.CleanSession) return } func (this *User) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.modelUser = this.RegisterComp(new(ModelUser)).(*ModelUser) this.modelSession = this.RegisterComp(new(ModelSession)).(*ModelSession) this.modelSetting = this.RegisterComp(new(ModelSetting)).(*ModelSetting) } //获取用户数据 func (this *User) GetUser(uid string) *pb.DBUser { user := this.modelUser.GetUser(uid) if user.Id == "" { return nil } return user } //获取用户会话 func (this *User) GetUserSession(uid string) *pb.CacheUser { cuser := this.modelSession.getUserSession(uid) if cuser.Uid == "" { return nil } return cuser } // 清除session func (this *User) CleanSession(session comm.IUserSession) { this.modelSession.cleanSession(session.GetUserId()) } //查询用户属性值 例如 金币 经验 func (this *User) QueryAttributeValue(uid string, attr string) (value int32) { user := this.modelUser.GetUser(uid) if user == nil { return } switch attr { case comm.ResGold: return user.Gold case comm.ResExp: return user.Exp } return } //用户资源 func (this *User) AddAttributeValue(session comm.IUserSession, attr string, add int32, bPush bool) (code pb.ErrorCode) { if add == 0 { log.Errorf("attr no changed,uid: %s attr: %s add: %d", session.GetUserId(), attr, add) code = pb.ErrorCode_ReqParameterError return } user := this.GetUser(session.GetUserId()) if user == nil { code = pb.ErrorCode_UserSessionNobeing return } _change := &pb.UserResChangePush{ Gold: user.Gold, Exp: user.Exp, Lv: user.Lv, Vip: user.Vip, Diamond: user.Diamond, } update := make(map[string]interface{}) switch attr { case comm.ResGold: if add < 0 { if user.Gold+add < 0 { code = pb.ErrorCode_GoldNoEnough return } } _change.Gold += add update[comm.ResGold] = user.Gold + add case comm.ResExp: if add < 0 { if user.Exp+add < 0 { code = pb.ErrorCode_ResNoEnough return } } _change.Exp += add update[comm.ResExp] = user.Exp + add case comm.ResDiamond: if add < 0 { if user.Diamond+add < 0 { code = pb.ErrorCode_ResNoEnough return } } _change.Diamond += add update[comm.ResDiamond] = user.Diamond + add default: code = pb.ErrorCode_Unknown } if len(update) == 0 { log.Warn("AddAttributeValue param is empty") return } if err := this.modelUser.updateUserAttr(session.GetUserId(), update); err != nil { log.Errorf("AddAttributeValue err:%v", err) code = pb.ErrorCode_DBError } data := &pb.UserResChangePush{} var _cache = &pb.CacheUser{} err := this.modelUser.MCompModel.Get(session.GetUserId(), _cache) if err != nil { this.SendMsgToUser(string(this.GetType()), "reschange", data, _cache) } if bPush { this.UserChangePush(session, _change) // 推送玩家数据变化 } return } //推送玩家账号信息变化消息 func (this *User) UserChangePush(session comm.IUserSession, resChange *pb.UserResChangePush) (err error) { session.SendMsg(string(this.GetType()), "reschange", resChange) return } // 设置record func (this *User) GetUserRecordData(uid string, typeValue int32, parmare int32) (value int32, err error) { //pb.DBUserRecord return } func (this *User) EventUserChanged(session comm.IUserSession) { ul := new(UserListen) user := this.GetUser(session.GetUserId()) if user != nil { ul.session = session ul.exp = user.Exp ul.lv = user.Lv } this.modelUser.eventApp.Dispatch(comm.EventUserChanged, ul) }