package user import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/db" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/event" ) var _ comm.IUser = (*User)(nil) func NewModule() core.IModule { m := new(User) return m } type User struct { modules.ModuleBase api *apiComp modelUser *ModelUser modelSession *ModelSession modelSetting *ModelSetting modelExpand *ModelExpand 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) this.modelExpand = this.RegisterComp(new(ModelExpand)).(*ModelExpand) } //获取用户数据 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 { return this.modelSession.getUserSession(uid) } // 清除session func (this *User) CleanSession(session comm.IUserSession) { this.modelUser.updateOfflineTime(session.GetUserId()) sId := fmt.Sprintf("%s-%s", comm.RDS_SESSION, session.GetUserId()) this.modelSession.Del(sId, db.SetDBMgoLog(false)) this.modelSession.DelListlds(comm.RDS_SESSION, session.GetUserId()) this.modelUser.Del(session.GetUserId(), db.SetDBMgoLog(false)) this.modelExpand.Del(session.GetUserId(), db.SetDBMgoLog(false)) this.modelSetting.Del(session.GetUserId(), db.SetDBMgoLog(false)) } // 在线玩家列表 func (this *User) UserOnlineList() ([]*pb.CacheUser, error) { var cache []*pb.CacheUser if err := this.modelSession.GetList(comm.RDS_SESSION, &cache); err != nil { return nil, err } return cache, nil } //查询用户属性值 例如 金币 经验 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 case comm.ResDiamond: return user.Diamond } return } //用户资源 func (this *User) AddAttributeValue(session comm.IUserSession, attr string, add int32, bPush bool) (code pb.ErrorCode) { if add == 0 { this.Errorf("attr no changed,uid: %s attr: %s add: %d", session.GetUserId(), attr, add) 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 { this.Warn("AddAttributeValue param is empty") return } if err := this.modelUser.updateUserAttr(session.GetUserId(), update); err != nil { this.Errorf("AddAttributeValue err:%v", err) code = pb.ErrorCode_DBError } if bPush { //推送玩家账号信息变化消息 session.SendMsg(string(this.GetType()), "reschange", _change) } return } //用户资源 func (this *User) AddAttributeValues(session comm.IUserSession, attrs map[string]int32, bPush bool) (code pb.ErrorCode) { 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{}) for key, add := range attrs { if add == 0 { this.Errorf("attr no changed,uid: %s attr: %s add: %d", session.GetUserId(), key, add) continue } switch key { 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 { this.Warn("AddAttributeValue param is empty") return } if err := this.modelUser.updateUserAttr(session.GetUserId(), update); err != nil { this.Errorf("AddAttributeValue err:%v", err) code = pb.ErrorCode_DBError } if bPush { //推送玩家账号信息变化消息 session.SendMsg(string(this.GetType()), "reschange", _change) } 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) } func (this *User) GetUserExpand(uid string) (result *pb.DBUserExpand, err error) { return this.modelExpand.GetUserExpand(uid) } func (this *User) ChangeUserExpand(uid string, value map[string]interface{}) error { return this.modelExpand.ChangeUserExpand(uid, value) }