package user import ( "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/lego/core" "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 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) 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) } //获取用户数据 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 } //查询用户属性值 例如 金币 经验 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(uid string, attr string, add int32) (code pb.ErrorCode) { if add == 0 { log.Errorf("attr no changed,uid: %s attr: %s add: %d", uid, attr, add) code = pb.ErrorCode_ReqParameterError return } user := this.GetUser(uid) if user == nil { code = pb.ErrorCode_UserSessionNobeing return } update := make(map[string]interface{}) switch attr { case comm.ResGold: if add < 0 { if user.Gold+add < 0 { code = pb.ErrorCode_GoldNoEnough return } } update[comm.ResGold] = user.Gold + add case comm.ResExp: if add < 0 { if user.Exp+add < 0 { code = pb.ErrorCode_ResNoEnough return } } update[comm.ResExp] = user.Exp + add case comm.ResDiamond: if add < 0 { if user.Diamond+add < 0 { code = pb.ErrorCode_ResNoEnough return } } update[comm.ResDiamond] = user.Diamond + add } if err := this.modelUser.updateUserAttr(uid, update); err != nil { log.Errorf("AddAttributeValue err:%v", err) code = pb.ErrorCode_DBError } return }