diff --git a/comm/imodule.go b/comm/imodule.go index 0d976e1fa..01717759e 100644 --- a/comm/imodule.go +++ b/comm/imodule.go @@ -11,6 +11,7 @@ type ( } //背包模块对外接口定义 提供给其他模块使用的 IPack interface { + AddItemToUserPack(uId string, itemid, addnum int32) (err error) ///添加多个物品到背包 (可以加物品和减物品) AddItemsToUserPack(uId string, items map[int32]int32) (err error) } diff --git a/modules/pack/api_getlist.go b/modules/pack/api_getlist.go index 3dacc3af5..4f5921606 100644 --- a/modules/pack/api_getlist.go +++ b/modules/pack/api_getlist.go @@ -6,20 +6,37 @@ import ( "go_dreamfactory/lego/sys/log" "go_dreamfactory/pb" "go_dreamfactory/sys/cache" + "time" ) +//参数校验 +func (this *Api_Comp) getlist_check(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (code pb.ErrorCode) { + if !session.IsLogin() { + code = pb.ErrorCode_NoLogin + return + } + return +} + ///获取用户道具 func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (err error) { var ( - code pb.ErrorCode - pack *pb.DB_UserPackData - grids []*pb.DB_GridData + code pb.ErrorCode + pack *pb.DB_UserPackData + nt int64 + tempgrids []*pb.DB_GridData + grids []*pb.DB_GridData + modifys []*pb.DB_GridData ) defer func() { session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids}) + if code == pb.ErrorCode_Success { + go func() { //异步处理修改数据 + cache.Defsys.Pack_UpdateGridToUserPack(session.GetUserId(), modifys...) + }() + } }() - if !session.IsLogin() { - code = pb.ErrorCode_NoLogin + if code = this.getlist_check(ctx, session, req); code != pb.ErrorCode_Success { return } if pack, err = cache.Defsys.Pack_QueryUserPack(session.GetUserId()); err != nil { @@ -27,7 +44,21 @@ func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, re code = pb.ErrorCode_CacheReadError return } else { - grids = this.module.configure_comp.GetPackItemByType(pack, req.IType) + tempgrids = this.module.configure_comp.GetPackItemByType(pack, req.IType) + modifys = make([]*pb.DB_GridData, 0, len(tempgrids)) + grids = make([]*pb.DB_GridData, 0, len(grids)) + nt = time.Now().Unix() + for _, v := range tempgrids { + if v.ETime > 0 && v.ETime < nt { //已经过期 + modifys = append(modifys, &pb.DB_GridData{GridId: v.GridId, IsEmpty: true}) + } else { + grids = append(grids, v) + if v.IsNewItem { + v.IsNewItem = false + modifys = append(modifys, v) + } + } + } } return } diff --git a/modules/pack/api_sellItem.go b/modules/pack/api_sellItem.go index 8b8baa602..33c9a27cc 100644 --- a/modules/pack/api_sellItem.go +++ b/modules/pack/api_sellItem.go @@ -6,6 +6,15 @@ import ( "go_dreamfactory/pb" ) +//参数校验 +func (this *Api_Comp) sellItem_check(ctx context.Context, session comm.IUserSession, req *pb.SellItemReq) (code pb.ErrorCode) { + if !session.IsLogin() { + code = pb.ErrorCode_NoLogin + return + } + return +} + //出售道具 func (this *Api_Comp) SellItem(ctx context.Context, session comm.IUserSession, req *pb.SellItemReq) (err error) { var ( @@ -14,8 +23,7 @@ func (this *Api_Comp) SellItem(ctx context.Context, session comm.IUserSession, r defer func() { session.SendMsg(string(this.module.GetType()), SellItemResp, code, &pb.SellItemResp{}) }() - if !session.IsLogin() { - code = pb.ErrorCode_NoLogin + if code = this.sellItem_check(ctx, session, req); code != pb.ErrorCode_Success { return } return diff --git a/modules/pack/api_useItem.go b/modules/pack/api_useItem.go index 95292f01f..6371c7a9a 100644 --- a/modules/pack/api_useItem.go +++ b/modules/pack/api_useItem.go @@ -6,6 +6,15 @@ import ( "go_dreamfactory/pb" ) +//参数校验 +func (this *Api_Comp) useitem_check(ctx context.Context, session comm.IUserSession, req *pb.UseItemReq) (code pb.ErrorCode) { + if !session.IsLogin() { + code = pb.ErrorCode_NoLogin + return + } + return +} + //使用道具 func (this *Api_Comp) Useitem(ctx context.Context, session comm.IUserSession, req *pb.UseItemReq) (err error) { var ( @@ -14,8 +23,7 @@ func (this *Api_Comp) Useitem(ctx context.Context, session comm.IUserSession, re defer func() { session.SendMsg(string(this.module.GetType()), UseItemResp, code, &pb.UseItemResp{}) }() - if !session.IsLogin() { - code = pb.ErrorCode_NoLogin + if code = this.useitem_check(ctx, session, req); code != pb.ErrorCode_Success { return } return diff --git a/modules/pack/configure_comp.go b/modules/pack/configure_comp.go index a82f56908..412297770 100644 --- a/modules/pack/configure_comp.go +++ b/modules/pack/configure_comp.go @@ -60,12 +60,14 @@ func (this *Configure_Comp) GetPackItemByType(pack *pb.DB_UserPackData, usetype } else { table = v.(*cfg.Game_item) for _, v := range pack.Pack { - if item, ok = table.GetDataMap()[id]; ok { - if item.Usetype == usetype { - result = append(result, v) + if !v.IsEmpty { + if item, ok = table.GetDataMap()[id]; ok { + if item.Usetype == usetype { + result = append(result, v) + } + } else { + log.Errorf("no found itemConfigure:%d", id) } - } else { - log.Errorf("no found itemConfigure:%d", id) } } } diff --git a/modules/pack/module.go b/modules/pack/module.go index d0a6c5d5a..f1993503f 100644 --- a/modules/pack/module.go +++ b/modules/pack/module.go @@ -46,10 +46,21 @@ func (this *Pack) OnInstallComp() { this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp) } +//IPack------------------------------------------------------------------------------------------------------------------------------- +///添加单个物品到背包 (可以加物品和减物品) +func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) { + defer log.Debugf("给用户添加物品 uId:%s itemid:%d addnum:%d issucc:%v", uId, itemid, addnum, err == nil) + if err = cache.Defsys.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil { + log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err) + } + return +} + ///添加多个物品到背包 (可以加物品和减物品) func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) { + defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil) if err = cache.Defsys.Pack_AddItemsToUserPack(uId, items); err != nil { - log.Errorf("AddItemsToUserPack err:%v", err) + log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err) } return } diff --git a/pb/pack_db.pb.go b/pb/pack_db.pb.go index 73460579a..619e8a2ef 100644 --- a/pb/pack_db.pb.go +++ b/pb/pack_db.pb.go @@ -30,7 +30,9 @@ type DB_GridData struct { IsEmpty bool `protobuf:"varint,2,opt,name=IsEmpty,proto3" json:"IsEmpty,omitempty"` //是否是空格子 ItemId int32 `protobuf:"varint,3,opt,name=ItemId,proto3" json:"ItemId,omitempty"` //存放物品的Id Amount uint32 `protobuf:"varint,4,opt,name=Amount,proto3" json:"Amount,omitempty"` //存放物品的数量 - IsNewItem bool `protobuf:"varint,5,opt,name=IsNewItem,proto3" json:"IsNewItem,omitempty"` //是否是新的 + CTime int64 `protobuf:"varint,5,opt,name=CTime,proto3" json:"CTime,omitempty"` //物品获取时间 + ETime int64 `protobuf:"varint,6,opt,name=ETime,proto3" json:"ETime,omitempty"` //物品过期时间 + IsNewItem bool `protobuf:"varint,7,opt,name=IsNewItem,proto3" json:"IsNewItem,omitempty"` //是否是新的 } func (x *DB_GridData) Reset() { @@ -93,6 +95,20 @@ func (x *DB_GridData) GetAmount() uint32 { return 0 } +func (x *DB_GridData) GetCTime() int64 { + if x != nil { + return x.CTime + } + return 0 +} + +func (x *DB_GridData) GetETime() int64 { + if x != nil { + return x.ETime + } + return 0 +} + func (x *DB_GridData) GetIsNewItem() bool { if x != nil { return x.IsNewItem @@ -160,22 +176,24 @@ var File_pack_pack_db_proto protoreflect.FileDescriptor var file_pack_pack_db_proto_rawDesc = []byte{ 0x0a, 0x12, 0x70, 0x61, 0x63, 0x6b, 0x2f, 0x70, 0x61, 0x63, 0x6b, 0x5f, 0x64, 0x62, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x8d, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x47, 0x72, 0x69, 0x64, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xb9, 0x01, 0x0a, 0x0b, 0x44, 0x42, 0x5f, 0x47, 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x49, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x16, 0x0a, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, - 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x1c, 0x0a, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, - 0x74, 0x65, 0x6d, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, - 0x49, 0x74, 0x65, 0x6d, 0x22, 0x4b, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x50, - 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, - 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, - 0x20, 0x0a, 0x04, 0x50, 0x61, 0x63, 0x6b, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, - 0x44, 0x42, 0x5f, 0x47, 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x50, 0x61, 0x63, - 0x6b, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x18, + 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x43, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, + 0x45, 0x54, 0x69, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x45, 0x54, 0x69, + 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, 0x18, + 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x49, 0x73, 0x4e, 0x65, 0x77, 0x49, 0x74, 0x65, 0x6d, + 0x22, 0x4b, 0x0a, 0x0f, 0x44, 0x42, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x50, 0x61, 0x63, 0x6b, 0x44, + 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x04, 0x50, + 0x61, 0x63, 0x6b, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x44, 0x42, 0x5f, 0x47, + 0x72, 0x69, 0x64, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x50, 0x61, 0x63, 0x6b, 0x42, 0x06, 0x5a, + 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/pb/pack_msg.pb.go b/pb/pack_msg.pb.go index 92d88648a..cd1be6dad 100644 --- a/pb/pack_msg.pb.go +++ b/pb/pack_msg.pb.go @@ -219,7 +219,7 @@ func (*UseItemResp) Descriptor() ([]byte, []int) { return file_pack_pack_msg_proto_rawDescGZIP(), []int{3} } -//出售道具请求 +//出售道具请求sailitem type SellItemReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache diff --git a/pb/proto/pack/pack_db.proto b/pb/proto/pack/pack_db.proto index 6cafe582e..83b5bae01 100644 --- a/pb/proto/pack/pack_db.proto +++ b/pb/proto/pack/pack_db.proto @@ -8,11 +8,13 @@ message DB_GridData { bool IsEmpty = 2; //是否是空格子 int32 ItemId = 3; //存放物品的Id uint32 Amount = 4; //存放物品的数量 - bool IsNewItem = 5; //是否是新的 + int64 CTime = 5; //物品获取时间 + int64 ETime = 6; //物品过期时间 + bool IsNewItem = 7; //是否是新的 } //用户背包 message DB_UserPackData { string UserId = 1; //tags:{bson:"_id"}用户Id - repeated DB_GridData Pack = 2; //背包列表 + repeated DB_GridData Pack = 2; //背包列表 } \ No newline at end of file diff --git a/pb/proto/pack/pack_msg.proto b/pb/proto/pack/pack_msg.proto index 5a255edd9..5bd1496b2 100644 --- a/pb/proto/pack/pack_msg.proto +++ b/pb/proto/pack/pack_msg.proto @@ -4,12 +4,12 @@ import "pack/pack_db.proto"; //查询用户背包请求 message GetlistReq { - int32 IType = 1; + int32 IType = 1; //道具类型 } //查询用户背包请求 回应 message GetlistResp { - repeated DB_GridData Grids = 1; + repeated DB_GridData Grids = 1; //用户背包列表 } //使用物品请求 diff --git a/sys/cache/pack.go b/sys/cache/pack.go index 4c6c04177..ec8546c60 100644 --- a/sys/cache/pack.go +++ b/sys/cache/pack.go @@ -15,8 +15,9 @@ const ( //Redis ) const ( - GridCapMaxNum = 99 //单个格子的最大容量 - GridMaxNUm = 200 //背包格子数量上限 + GridCapMaxNum = 99 //单个格子的最大容量 + GridMaxNUm = 200 //背包格子数量上限 + Pack_Expiration = -1 //背包缓存数据过期时间 ) var ( @@ -35,6 +36,8 @@ type IPack interface { Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) //修改用户背包格子的标识 Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error) + //更新用户背包格子信息 + Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (err error) } ///查询用户背包数据 @@ -46,7 +49,7 @@ func (this *Cache) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err return } else if err == redis.RedisNil { if pack, err = db.Defsys.Pack_QueryUserPack(uId); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1) + this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) } else if err == mgo.MongodbNil { err = nil } @@ -76,7 +79,7 @@ func (this *Cache) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32 return } if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1) + this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) } return } @@ -116,7 +119,7 @@ func (this *Cache) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (e } if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1) + this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) } return } @@ -159,7 +162,7 @@ func (this *Cache) Pack_AddItemToUserPackByGrid(uId string, gridid int32, itemId return } else { if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grid); err == nil { - this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1) + this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) } } } @@ -172,7 +175,18 @@ func (this *Cache) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err pack *pb.DB_UserPackData ) if pack, err = db.Defsys.Pack_ModifyPackGridIsNewItem(uId, grids); err == nil { - err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, -1) + err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) + } + return +} + +///修改目标格子的新获取标识 +func (this *Cache) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (err error) { + var ( + pack *pb.DB_UserPackData + ) + if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grids...); err == nil { + err = this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration) } return }