diff --git a/cmd/robot/friend.go b/cmd/robot/friend.go index ee32473e8..de8aa0bd0 100644 --- a/cmd/robot/friend.go +++ b/cmd/robot/friend.go @@ -36,7 +36,7 @@ var ( subType: friend.FriendSubTypeApply, req: &pb.FriendApplyReq{}, rsp: &pb.FriendApplyRsp{}, - enabled: true, + // enabled: true, }, { //applylist mainType: string(comm.ModuleFriend), @@ -60,7 +60,7 @@ var ( mainType: string(comm.ModuleFriend), subType: friend.FriendSubTypeAddBlack, req: &pb.FriendBlackAddReq{ - FriendId: "0_62bd1c804cf36044f36bff33", + FriendId: "0_62be9f40f67327fb53039b70", }, rsp: &pb.FriendBlackAddRsp{}, enabled: true, diff --git a/cmd/robot/robot.go b/cmd/robot/robot.go index 994baa4d6..6cf465a03 100644 --- a/cmd/robot/robot.go +++ b/cmd/robot/robot.go @@ -164,7 +164,7 @@ func (r *Robot) onUserLoaded() { //hero r.RunHero() //friend - // r.RunFriend() + r.RunFriend() //pack // r.RunPack() diff --git a/cmd/robot/user.go b/cmd/robot/user.go index 5905f2aaa..1f709eb08 100644 --- a/cmd/robot/user.go +++ b/cmd/robot/user.go @@ -17,7 +17,7 @@ var user_builders = []*builder{ NickName: "乐谷6301", }, rsp: &pb.UserCreateRsp{}, - enabled: true, + // enabled: true, }, { desc: "添加资源", mainType: string(comm.ModuleUser), @@ -27,7 +27,7 @@ var user_builders = []*builder{ Count: 100, }, rsp: &pb.UserAddResResp{}, - enabled: true, + // enabled: true, }, } diff --git a/comm/const.go b/comm/const.go index c98c242db..9f2b45254 100644 --- a/comm/const.go +++ b/comm/const.go @@ -65,9 +65,13 @@ const ( ) const ( - PropertyHp string = "hp" //生命 - PropertyAtk string = "atk" //攻击 - PropertyDef string = "def" //防御 + Hp string = "hp" //生命 + Atk string = "atk" //攻击 + Def string = "def" //防御 + HpPro string = "hppro" //生命附加值 + AtkPro string = "atkpro" //攻击附加值 + DefPro string = "defpro" //防御附加值 + Speed string = "speed" //速度 ) const ( diff --git a/comm/imodule.go b/comm/imodule.go index 82a11fbb4..f6e3a7782 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -51,6 +51,7 @@ type ( //玩家 IUser interface { + GetUser(uid string) *pb.DBUser //查询用户属性值 例如 金币 经验 QueryAttributeValue(uid string, attr string) (value int32) //添加/减少属性值 diff --git a/modules/friend/api_addblack.go b/modules/friend/api_addblack.go index da49b1560..71d29b83e 100644 --- a/modules/friend/api_addblack.go +++ b/modules/friend/api_addblack.go @@ -5,7 +5,6 @@ import ( "go_dreamfactory/pb" "go_dreamfactory/utils" - "github.com/spf13/cast" "google.golang.org/protobuf/proto" ) @@ -13,16 +12,16 @@ func (this *apiComp) AddblackCheck(session comm.IUserSession, req *pb.FriendBlac if req.FriendId == "" { code = pb.ErrorCode_ReqParameterError } - return } //加入黑名单 func (this *apiComp) Addblack(session comm.IUserSession, req *pb.FriendBlackAddReq) (code pb.ErrorCode, data proto.Message) { var ( - err error - self *pb.DBFriend - rsp *pb.FriendBlackAddRsp + err error + self *pb.DBFriend + target *pb.DBFriend + rsp *pb.FriendBlackAddRsp ) defer func() { @@ -30,22 +29,17 @@ func (this *apiComp) Addblack(session comm.IUserSession, req *pb.FriendBlackAddR FriendId: req.FriendId, UserId: session.GetUserId(), } - if err := session.SendMsg(string(this.module.GetType()), FriendSubTypeAddBlack, rsp); err != nil { - code = pb.ErrorCode_SystemError - } + session.SendMsg(string(this.module.GetType()), FriendSubTypeAddBlack, rsp) }() - self = &pb.DBFriend{UId: session.GetUserId()} - target := &pb.DBFriend{UId: req.FriendId} - - err = this.module.modelFriend.Get(session.GetUserId(), self) - if self == nil || err != nil { + self = this.module.modelFriend.GetFriend(session.GetUserId()) + if self == nil { code = pb.ErrorCode_FriendSelfNoData return } - err = this.module.modelFriend.Get(req.FriendId, target) - if target == nil || err != nil { + target = this.module.modelFriend.GetFriend(req.FriendId) + if target == nil { code = pb.ErrorCode_FriendTargetNoData return } @@ -67,13 +61,9 @@ func (this *apiComp) Addblack(session comm.IUserSession, req *pb.FriendBlackAddR code = pb.ErrorCode_FriendTargetBlackYet return } - blackNumMax := 0 - if maxhy := this.module.configure.GetGlobalConf("max_hy"); maxhy != "" { - blackNumMax = cast.ToInt(maxhy) - } // 判断是否黑名单人数已满 - if len(self.BlackIds) >= blackNumMax { + if len(self.BlackIds) >= this.module.getBlackMax() { code = pb.ErrorCode_FriendBlackMax return } diff --git a/modules/friend/api_agree.go b/modules/friend/api_agree.go index 620e0bba3..b013ea8aa 100644 --- a/modules/friend/api_agree.go +++ b/modules/friend/api_agree.go @@ -21,6 +21,7 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c var ( err error self *pb.DBFriend + target *pb.DBFriend rsp *pb.FriendAgreeRsp optNum int32 ) @@ -36,11 +37,9 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c } }() - self = &pb.DBFriend{UId: session.GetUserId()} - //获取玩家自己好友数据 - err = this.module.modelFriend.Get(session.GetUserId(), self) - if self == nil || err != nil { + self = this.module.modelFriend.GetFriend(session.GetUserId()) + if self == nil { code = pb.ErrorCode_FriendSelfNoData return } @@ -64,11 +63,10 @@ func (this *apiComp) Agree(session comm.IUserSession, req *pb.FriendAgreeReq) (c } //双向添加:将自己加入到申请人的好友列表中 - target := &pb.DBFriend{} - err := this.module.modelFriend.Get(userId, target) - if target == nil || err != nil { + target = this.module.modelFriend.GetFriend(userId) + if target == nil { code = pb.ErrorCode_FriendTargetNoData - + return } if _, ok := utils.Find(target.FriendIds, self.UId); !ok { if target.FriendIds == nil { diff --git a/modules/friend/api_apply.go b/modules/friend/api_apply.go index a663875ba..938997d1d 100644 --- a/modules/friend/api_apply.go +++ b/modules/friend/api_apply.go @@ -43,18 +43,15 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c } }() - self = &pb.DBFriend{UId: session.GetUserId()} - target = &pb.DBFriend{UId: req.FriendId} - //获取玩家自己好友数据 - err = this.module.modelFriend.Get(session.GetUserId(), self) + self = this.module.modelFriend.GetFriend(session.GetUserId()) if self == nil || err != nil { code = pb.ErrorCode_FriendSelfNoData return } //获取好友数据 - err = this.module.modelFriend.Get(req.FriendId, target) + target = this.module.modelFriend.GetFriend(req.FriendId) if target == nil || err != nil { code = pb.ErrorCode_FriendTargetNoData return @@ -67,17 +64,13 @@ func (this *apiComp) Apply(session comm.IUserSession, req *pb.FriendApplyReq) (c } //判断是否超过最大好友数量 - //TODO 最大数从全局配置中获取 - var max int = 50 - total := len(self.FriendIds) - if total >= max { + if len(self.FriendIds) >= this.module.getFriendMax() { code = pb.ErrorCode_FriendSelfMax return } //判断对方是否也超过最大好友数量 - ttotal := len(target.FriendIds) - if ttotal >= max { + if len(target.FriendIds) >= this.module.getFriendMax() { code = pb.ErrorCode_FriendTargetMax return } diff --git a/modules/friend/api_applylist.go b/modules/friend/api_applylist.go index 41c62eec9..eb06c5ab3 100644 --- a/modules/friend/api_applylist.go +++ b/modules/friend/api_applylist.go @@ -13,6 +13,7 @@ func (this *apiComp) ApplyListCheck(session comm.IUserSession, req *pb.FriendApp //申请列表 func (this *apiComp) ApplyList(session comm.IUserSession, req *pb.FriendApplyListReq) (code pb.ErrorCode) { var ( + err error self *pb.DBFriend rsp *pb.FriendApplyListRsp list []*pb.FriendBase @@ -29,8 +30,7 @@ func (this *apiComp) ApplyList(session comm.IUserSession, req *pb.FriendApplyLis } }() - self = &pb.DBFriend{UId: session.GetUserId()} - err := this.module.modelFriend.Get(session.GetUserId(), self) + self = this.module.modelFriend.GetFriend(session.GetUserId()) if self == nil || err != nil { code = pb.ErrorCode_FriendSelfNoData return diff --git a/modules/friend/api_blacklist.go b/modules/friend/api_blacklist.go index 437575f3c..000f5c957 100644 --- a/modules/friend/api_blacklist.go +++ b/modules/friend/api_blacklist.go @@ -8,13 +8,13 @@ import ( ) func (this *apiComp) BlacklistCheck(session comm.IUserSession, req *pb.FriendBlackListReq) (code pb.ErrorCode) { - return } //黑名单 func (this *apiComp) Blacklist(session comm.IUserSession, req *pb.FriendBlackListReq) (code pb.ErrorCode, data proto.Message) { var ( + err error self *pb.DBFriend rsp *pb.FriendBlackListRsp list []*pb.FriendBase @@ -32,8 +32,7 @@ func (this *apiComp) Blacklist(session comm.IUserSession, req *pb.FriendBlackLis } }() - self = &pb.DBFriend{UId: session.GetUserId()} - err := this.module.modelFriend.Get(session.GetUserId(), self) + self = this.module.modelFriend.GetFriend(session.GetUserId()) if self == nil || err != nil { code = pb.ErrorCode_FriendSelfNoData return diff --git a/modules/friend/api_delblack.go b/modules/friend/api_delblack.go index 88c760874..91f579c1c 100644 --- a/modules/friend/api_delblack.go +++ b/modules/friend/api_delblack.go @@ -18,6 +18,7 @@ func (this *apiComp) DelblackCheck(session comm.IUserSession, req *pb.FriendDelB //删除黑名单 func (this *apiComp) Delblack(session comm.IUserSession, req *pb.FriendDelBlackReq) (code pb.ErrorCode, data proto.Message) { var ( + err error self *pb.DBFriend rsp *pb.FriendDelBlackRsp ) @@ -35,8 +36,7 @@ func (this *apiComp) Delblack(session comm.IUserSession, req *pb.FriendDelBlackR } }() - self = &pb.DBFriend{UId: session.GetUserId()} - err := this.module.modelFriend.Get(session.GetUserId(), self) + self = this.module.modelFriend.GetFriend(session.GetUserId()) if self == nil || err != nil { code = pb.ErrorCode_FriendSelfNoData return diff --git a/modules/friend/api_list.go b/modules/friend/api_list.go index c0e45f456..e41bfdfe4 100644 --- a/modules/friend/api_list.go +++ b/modules/friend/api_list.go @@ -8,7 +8,6 @@ import ( ) func (this *apiComp) ListCheck(session comm.IUserSession, req *pb.FriendListReq) (code pb.ErrorCode) { - return } @@ -31,9 +30,8 @@ func (this *apiComp) List(session comm.IUserSession, chk map[string]interface{}, } }() - self = &pb.DBFriend{UId: session.GetUserId()} - err := this.module.modelFriend.Get(session.GetUserId(), self) - if self == nil || err != nil { + self = this.module.modelFriend.GetFriend(session.GetUserId()) + if self == nil { code = pb.ErrorCode_FriendSelfNoData return } diff --git a/modules/friend/api_refuse.go b/modules/friend/api_refuse.go index 7e19ec0c5..95b9f5ea9 100644 --- a/modules/friend/api_refuse.go +++ b/modules/friend/api_refuse.go @@ -37,10 +37,8 @@ func (this *apiComp) Refuse(session comm.IUserSession, req *pb.FriendRefuseReq) } }() - self = &pb.DBFriend{UId: session.GetUserId()} - //获取玩家自己好友数据 - err = this.module.modelFriend.Get(session.GetUserId(), self) + self = this.module.modelFriend.GetFriend(session.GetUserId()) if self == nil || err != nil { code = pb.ErrorCode_FriendSelfNoData return diff --git a/modules/friend/api_search.go b/modules/friend/api_search.go index ae5c0a02e..b1de7dc9d 100644 --- a/modules/friend/api_search.go +++ b/modules/friend/api_search.go @@ -3,9 +3,11 @@ package friend import ( "go_dreamfactory/comm" "go_dreamfactory/pb" + + "google.golang.org/protobuf/proto" ) -func (this *apiComp) SearchCheck(session comm.IUserSession, req *pb.FriendSearchReq) (chk map[string]interface{}, code comm.ErrorCode) { +func (this *apiComp) SearchCheck(session comm.IUserSession, req *pb.FriendSearchReq) (code comm.ErrorCode) { if req.NickName == "" { code = comm.ErrorCode{Code: pb.ErrorCode_FriendSearchNameEmpty} return @@ -14,7 +16,7 @@ func (this *apiComp) SearchCheck(session comm.IUserSession, req *pb.FriendSearch } //搜索 -func (this *apiComp) Search(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendSearchReq) (code pb.ErrorCode) { +func (this *apiComp) Search(session comm.IUserSession, req *pb.FriendSearchReq) (code pb.ErrorCode, data proto.Message) { var ( rsp *pb.FriendSearchRsp friend *pb.FriendBase @@ -25,7 +27,9 @@ func (this *apiComp) Search(session comm.IUserSession, chk map[string]interface{ Friend: friend, } } - session.SendMsg(string(this.module.GetType()), FriendSubTypeSearch, rsp) + if err := session.SendMsg(string(this.module.GetType()), FriendSubTypeSearch, rsp); err != nil { + code = pb.ErrorCode_SystemError + } }() user := this.module.modelFriend.Frined_FindCond(req.NickName) diff --git a/modules/friend/model_friend.go b/modules/friend/model_friend.go index 250648b24..b5f5465c8 100644 --- a/modules/friend/model_friend.go +++ b/modules/friend/model_friend.go @@ -3,6 +3,7 @@ package friend import ( "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" + "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" @@ -35,3 +36,16 @@ func (this *ModelFriend) Frined_FindCond(nickName string) *pb.DBUser { } return user } + +//查询好友 +func (this *ModelFriend) GetFriend(uid string) *pb.DBFriend { + friend := &pb.DBFriend{UId: uid} + if err := this.Get(uid, friend); err != nil { + if err == redis.RedisNil { + if err := this.Add(uid, friend); err != nil { + return nil + } + } + } + return friend +} diff --git a/modules/friend/module.go b/modules/friend/module.go index 7d5cd709d..48abcae4f 100644 --- a/modules/friend/module.go +++ b/modules/friend/module.go @@ -5,6 +5,8 @@ import ( "go_dreamfactory/modules" "go_dreamfactory/lego/core" + + "github.com/spf13/cast" ) func NewModule() core.IModule { @@ -34,3 +36,19 @@ func (this *Friend) OnInstallComp() { this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.modelFriend = this.RegisterComp(new(ModelFriend)).(*ModelFriend) } + +//获取最大好友数 +func (this *Friend) getFriendMax() int { + if maxHy := this.configure.GetGlobalConf("max_hy"); maxHy != "" { + return cast.ToInt(maxHy) + } + return 0 +} + +//获取最大黑名单数 +func (this *Friend) getBlackMax() int { + if maxHy := this.configure.GetGlobalConf("max_hmd"); maxHy != "" { + return cast.ToInt(maxHy) + } + return 0 +} diff --git a/modules/hero/model_hero.go b/modules/hero/model_hero.go index 47746a9e2..c548e22f4 100644 --- a/modules/hero/model_hero.go +++ b/modules/hero/model_hero.go @@ -162,9 +162,9 @@ func (this *ModelHero) mergeMainProperty(uid, heroId string, data map[string]int return } - hero.Property[comm.PropertyHp] += data[comm.PropertyHp] - hero.Property[comm.PropertyAtk] += data[comm.PropertyAtk] - hero.Property[comm.PropertyDef] += data[comm.PropertyDef] + hero.Property[comm.Hp] += data[comm.Hp] + hero.Property[comm.Atk] += data[comm.Atk] + hero.Property[comm.Def] += data[comm.Def] } //合并附加属性 @@ -173,9 +173,9 @@ func (this *ModelHero) mergeAddProperty(uid, heroId string, data map[string]int3 if hero == nil { return } - hero.AddProperty[comm.PropertyHp] += data[comm.PropertyHp] - hero.AddProperty[comm.PropertyAtk] += data[comm.PropertyAtk] - hero.AddProperty[comm.PropertyDef] += data[comm.PropertyDef] + hero.AddProperty[comm.Hp] += data[comm.Hp] + hero.AddProperty[comm.Atk] += data[comm.Atk] + hero.AddProperty[comm.Def] += data[comm.Def] } //属性计算 - 暂时放在modelHero里实现 @@ -216,25 +216,25 @@ func (this *ModelHero) PropertyCompute(uid, heroId string) map[string]int32 { return nil } - curHp := hero.Property[comm.PropertyHp] + curHp := hero.Property[comm.Hp] exprHp := fmt.Sprintf("%v + %v * %v/1000 + %v * %v", (curHp + lvGrow.Hp), heroLvCfg.Hp, lvGrow.Hpgrow, heroStarCfg.Hp, stargrowCfg.StarupHp) hp, _ := mengine.ParseAndExec(exprHp) - curAtk := hero.Property[comm.PropertyAtk] + curAtk := hero.Property[comm.Atk] exprAtk := fmt.Sprintf("%v +%v * %v/1000 + %v * %v", (curAtk + lvGrow.Atk), heroLvCfg.Atk, lvGrow.Atkgrow, heroStarCfg.Atk, stargrowCfg.StarupAtk) atk, _ := mengine.ParseAndExec(exprAtk) - curDef := hero.Property[comm.PropertyDef] + curDef := hero.Property[comm.Def] exprDef := fmt.Sprintf("%v +%v * %v/1000 + %v * %v", (curDef + lvGrow.Def), heroLvCfg.Def, lvGrow.Defgrow, heroStarCfg.Def, stargrowCfg.StarupDef) def, _ := mengine.ParseAndExec(exprDef) return map[string]int32{ - comm.PropertyHp: int32(math.Floor(hp)), - comm.PropertyAtk: int32(math.Floor(atk)), - comm.PropertyDef: int32(math.Floor(def)), + comm.Hp: int32(math.Floor(hp)), + comm.Atk: int32(math.Floor(atk)), + comm.Def: int32(math.Floor(def)), } } diff --git a/modules/user/api_res.go b/modules/user/api_res.go index ba72c9461..91b76e42f 100644 --- a/modules/user/api_res.go +++ b/modules/user/api_res.go @@ -24,7 +24,7 @@ func (this *apiComp) AddRes(session comm.IUserSession, req *pb.UserAddResReq) (c } }() - user := this.module.modelUser.getUser(session.GetUserId()) + user := this.module.modelUser.GetUser(session.GetUserId()) if user == nil { code = pb.ErrorCode_UserSessionNobeing return diff --git a/modules/user/configure.go b/modules/user/configure.go deleted file mode 100644 index cc8ac1c0b..000000000 --- a/modules/user/configure.go +++ /dev/null @@ -1,16 +0,0 @@ -package user - -import ( - "go_dreamfactory/lego/core" - "go_dreamfactory/modules" -) - -type configureComp struct { - modules.MCompConfigure -} - -//组件初始化接口 -func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { - err = this.ModuleCompBase.Init(service, module, comp, options) - return -} diff --git a/modules/user/model_user.go b/modules/user/model_user.go index 702ab5ac5..5cc0607e5 100644 --- a/modules/user/model_user.go +++ b/modules/user/model_user.go @@ -44,11 +44,11 @@ func (this *ModelUser) User_Create(user *pb.DBUser) (err error) { user.Uid = fmt.Sprintf("%d_%s", user.Sid, _id) user.Uuid = uuid.NewV4().String() user.Ctime = time.Now().Unix() - return this.Add(user.Uid,user) + return this.Add(user.Uid, user) } //获取用户 -func (this *ModelUser) getUser(uid string) *pb.DBUser { +func (this *ModelUser) GetUser(uid string) *pb.DBUser { dbUser := &pb.DBUser{} if err := this.Get(uid, dbUser); err != nil { log.Errorf("getUser err:%v", err) diff --git a/modules/user/module.go b/modules/user/module.go index 03051fe63..ea26b407f 100644 --- a/modules/user/module.go +++ b/modules/user/module.go @@ -18,7 +18,7 @@ type User struct { api *apiComp modelUser *ModelUser modelSession *ModelSession - configure *configureComp + configure *modules.MCompConfigure } func (this *User) GetType() core.M_Modules { @@ -36,17 +36,17 @@ func (this *User) OnInstallComp() { this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.modelUser = this.RegisterComp(new(ModelUser)).(*ModelUser) this.modelSession = this.RegisterComp(new(ModelSession)).(*ModelSession) - this.configure = this.RegisterComp(new(configureComp)).(*configureComp) + } //获取英雄列表 -func (this *User) GetHeroList(uid string) []*pb.DBHero { - return nil +func (this *User) GetUser(uid string) *pb.DBUser { + return this.modelUser.GetUser(uid) } //查询用户属性值 例如 金币 经验 func (this *User) QueryAttributeValue(uid string, attr string) (value int32) { - user := this.modelUser.getUser(uid) + user := this.modelUser.GetUser(uid) if user == nil { return } diff --git a/modules/web/module.go b/modules/web/module.go index 6d7788448..307d28bda 100644 --- a/modules/web/module.go +++ b/modules/web/module.go @@ -2,10 +2,10 @@ package web import ( "go_dreamfactory/comm" - "go_dreamfactory/modules" "go_dreamfactory/modules/user" "go_dreamfactory/lego/core" + "go_dreamfactory/lego/core/cbase" ) /* @@ -19,7 +19,7 @@ func NewModule() core.IModule { } type Web struct { - modules.ModuleBase + cbase.ModuleBase options *Options api_comp *Api_Comp //提供weba pi服务的组件 modelUser *user.ModelUser