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" "go_dreamfactory/lego/sys/log" "github.com/pkg/errors" ) 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 int64) { 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) change(session comm.IUserSession, attr string, add int32) (change *pb.UserResChangedPush, 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 } userEx, err := this.GetUserExpand(session.GetUserId()) if userEx == nil || err != nil { code = pb.ErrorCode_UserExpandNull return } change = &pb.UserResChangedPush{ Gold: user.Gold, Exp: user.Exp, Diamond: user.Diamond, Friend: userEx.FriendPoint, } switch attr { case comm.ResGold: if add < 0 { if user.Gold+int64(add) < 0 { code = pb.ErrorCode_GoldNoEnough return } } change.Gold += int64(add) case comm.ResExp: if add < 0 { if user.Exp+int64(add) < 0 { code = pb.ErrorCode_UserExpNoEnough return } } change.Exp += int64(add) case comm.ResDiamond: if add < 0 { if user.Diamond+int64(add) < 0 { code = pb.ErrorCode_DiamondNoEnough return } } change.Diamond += int64(add) case comm.ResFriend: if add < 0 { if userEx.FriendPoint+add < 0 { code = pb.ErrorCode_UserFriendNoEnough return } } change.Friend += add default: err = errors.New(fmt.Sprintf("%s no supported", attr)) return } //user update := map[string]interface{}{ comm.ResGold: change.Gold, comm.ResDiamond: change.Diamond, comm.ResExp: change.Exp, } //user ex updateEx := map[string]interface{}{ comm.ResFriend: change.Friend, } if err := this.modelUser.updateUserAttr(session.GetUserId(), update); err != nil { this.Errorf("AddAttributeValue err:%v", err) code = pb.ErrorCode_DBError } if err := this.modelExpand.ChangeUserExpand(session.GetUserId(), updateEx); err != nil { this.Errorf("AddAttributeValue ex err:%v", err) code = pb.ErrorCode_DBError } this.ModuleUser.EventUserChanged(session) return } //用户资源 func (this *User) AddAttributeValue(session comm.IUserSession, attr string, add int32, bPush bool) (code pb.ErrorCode) { var _change *pb.UserResChangedPush _change, code = this.change(session, attr, add) if code != pb.ErrorCode_Success { return } if _change == nil { return } 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) { for key, add := range attrs { var _change *pb.UserResChangedPush _change, code = this.change(session, key, add) if code != pb.ErrorCode_Success { return } if _change == nil { continue } 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) }