上传 背包数据结构调整优化
This commit is contained in:
parent
20ef5c5280
commit
f78a2e20bc
@ -30,6 +30,8 @@ type (
|
|||||||
UpdateOne(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
UpdateOne(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
||||||
UpdateMany(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
UpdateMany(sqltable core.SqlTable, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
||||||
UpdateManyByCtx(sqltable core.SqlTable, ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
UpdateManyByCtx(sqltable core.SqlTable, ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
|
||||||
|
BulkWrite(sqltable core.SqlTable, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
|
||||||
|
BulkWriteByCtx(sqltable core.SqlTable, ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
|
||||||
FindOneAndDelete(sqltable core.SqlTable, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
|
FindOneAndDelete(sqltable core.SqlTable, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
|
||||||
DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
|
DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
|
||||||
DeleteMany(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
|
DeleteMany(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
|
||||||
|
@ -135,6 +135,16 @@ func (this *Mongodb) FindOneAndDelete(sqltable core.SqlTable, filter interface{}
|
|||||||
return this.Collection(sqltable).FindOneAndDelete(this.getContext(), filter, opts...)
|
return this.Collection(sqltable).FindOneAndDelete(this.getContext(), filter, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///批量写
|
||||||
|
func (this *Mongodb) BulkWrite(sqltable core.SqlTable, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) {
|
||||||
|
return this.Collection(sqltable).BulkWrite(this.getContext(), models, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
///批量写
|
||||||
|
func (this *Mongodb) BulkWriteByCtx(sqltable core.SqlTable, ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) {
|
||||||
|
return this.Collection(sqltable).BulkWrite(ctx, models, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *Mongodb) DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
func (this *Mongodb) DeleteOne(sqltable core.SqlTable, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
||||||
return this.Collection(sqltable).DeleteOne(this.getContext(), filter, opts...)
|
return this.Collection(sqltable).DeleteOne(this.getContext(), filter, opts...)
|
||||||
}
|
}
|
||||||
|
26
modules/cache_com.go
Normal file
26
modules/cache_com.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package modules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go_dreamfactory/lego/base"
|
||||||
|
"go_dreamfactory/lego/core"
|
||||||
|
"go_dreamfactory/lego/core/cbase"
|
||||||
|
"go_dreamfactory/lego/sys/redis"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
基础组件 缓存组件 读写缓存数据
|
||||||
|
*/
|
||||||
|
type MComp_CacheComp struct {
|
||||||
|
cbase.ModuleCompBase
|
||||||
|
S base.IRPCXService //rpc服务对象
|
||||||
|
M core.IModule //当前业务模块
|
||||||
|
Redis redis.ISys
|
||||||
|
}
|
||||||
|
|
||||||
|
//组件初始化接口
|
||||||
|
func (this *MComp_CacheComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||||
|
this.ModuleCompBase.Init(service, module, comp, options)
|
||||||
|
this.S = service.(base.IRPCXService)
|
||||||
|
this.M = module
|
||||||
|
return
|
||||||
|
}
|
26
modules/db_comp.go
Normal file
26
modules/db_comp.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package modules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go_dreamfactory/lego/base"
|
||||||
|
"go_dreamfactory/lego/core"
|
||||||
|
"go_dreamfactory/lego/core/cbase"
|
||||||
|
"go_dreamfactory/lego/sys/mgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
基础组件 存储组件 读写缓存数据
|
||||||
|
*/
|
||||||
|
type MComp_DBComp struct {
|
||||||
|
cbase.ModuleCompBase
|
||||||
|
S base.IRPCXService //rpc服务对象
|
||||||
|
M core.IModule //当前业务模块
|
||||||
|
DB mgo.ISys
|
||||||
|
}
|
||||||
|
|
||||||
|
//组件初始化接口
|
||||||
|
func (this *MComp_DBComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||||
|
this.ModuleCompBase.Init(service, module, comp, options)
|
||||||
|
this.S = service.(base.IRPCXService)
|
||||||
|
this.M = module
|
||||||
|
return
|
||||||
|
}
|
@ -5,7 +5,6 @@ import (
|
|||||||
"go_dreamfactory/comm"
|
"go_dreamfactory/comm"
|
||||||
"go_dreamfactory/lego/sys/log"
|
"go_dreamfactory/lego/sys/log"
|
||||||
"go_dreamfactory/pb"
|
"go_dreamfactory/pb"
|
||||||
"go_dreamfactory/sys/cache"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,35 +21,38 @@ func (this *Api_Comp) Getlist_Check(ctx context.Context, session comm.IUserSessi
|
|||||||
func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (err error) {
|
func (this *Api_Comp) Getlist(ctx context.Context, session comm.IUserSession, req *pb.GetlistReq) (err error) {
|
||||||
var (
|
var (
|
||||||
code pb.ErrorCode
|
code pb.ErrorCode
|
||||||
pack *pb.DB_UserPackData
|
items []*pb.DB_UserItemData
|
||||||
nt int64
|
nt int64
|
||||||
tempgrids []*pb.DB_GridData
|
tempgrids []*pb.DB_UserItemData
|
||||||
grids []*pb.DB_GridData
|
grids []*pb.DB_UserItemData
|
||||||
modifys []*pb.DB_GridData
|
modifys []*pb.DB_UserItemData
|
||||||
|
dels []string
|
||||||
)
|
)
|
||||||
defer func() {
|
defer func() {
|
||||||
session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids})
|
session.SendMsg(string(this.module.GetType()), GetlistResp, code, &pb.GetlistResp{Grids: grids})
|
||||||
if code == pb.ErrorCode_Success {
|
if code == pb.ErrorCode_Success {
|
||||||
go func() { //异步处理修改数据
|
go func() { //异步处理修改数据
|
||||||
cache.Defsys.Pack_UpdateGridToUserPack(session.GetUserId(), modifys...)
|
this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
||||||
|
this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success {
|
if code = this.Getlist_Check(ctx, session, req); code != pb.ErrorCode_Success {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if pack, err = cache.Defsys.Pack_QueryUserPack(session.GetUserId()); err != nil {
|
if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil {
|
||||||
log.Errorf("QueryUserPackReq err:%v", err)
|
log.Errorf("QueryUserPackReq err:%v", err)
|
||||||
code = pb.ErrorCode_CacheReadError
|
code = pb.ErrorCode_CacheReadError
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
tempgrids = this.module.configure_comp.GetPackItemByType(pack, req.IType)
|
tempgrids = this.module.configure_comp.GetPackItemByType(items, req.IType)
|
||||||
modifys = make([]*pb.DB_GridData, 0, len(tempgrids))
|
modifys = make([]*pb.DB_UserItemData, 0, len(tempgrids))
|
||||||
grids = make([]*pb.DB_GridData, 0, len(grids))
|
dels = make([]string, 0, len(tempgrids))
|
||||||
|
grids = make([]*pb.DB_UserItemData, 0, len(grids))
|
||||||
nt = time.Now().Unix()
|
nt = time.Now().Unix()
|
||||||
for _, v := range tempgrids {
|
for _, v := range tempgrids {
|
||||||
if v.ETime > 0 && v.ETime < nt { //已经过期
|
if v.ETime > 0 && v.ETime < nt { //已经过期
|
||||||
modifys = append(modifys, &pb.DB_GridData{GridId: v.GridId, IsEmpty: true})
|
dels = append(dels, v.GridId)
|
||||||
} else {
|
} else {
|
||||||
grids = append(grids, v)
|
grids = append(grids, v)
|
||||||
if v.IsNewItem {
|
if v.IsNewItem {
|
||||||
|
@ -2,29 +2,48 @@ package pack
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go_dreamfactory/lego/core/cbase"
|
"go_dreamfactory/lego/core"
|
||||||
"go_dreamfactory/lego/sys/mgo"
|
"go_dreamfactory/lego/sys/mgo"
|
||||||
"go_dreamfactory/lego/sys/redis"
|
"go_dreamfactory/lego/sys/redis"
|
||||||
|
"go_dreamfactory/modules"
|
||||||
"go_dreamfactory/pb"
|
"go_dreamfactory/pb"
|
||||||
"go_dreamfactory/sys/db"
|
"reflect"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
///背包缓存数据管理组件
|
///背包缓存数据管理组件
|
||||||
type Cache_Comp struct {
|
type Cache_Comp struct {
|
||||||
cbase.ModuleCompBase
|
modules.MComp_CacheComp
|
||||||
redis redis.ISys
|
module *Pack
|
||||||
|
}
|
||||||
|
|
||||||
|
//组件初始化接口
|
||||||
|
func (this *Cache_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
||||||
|
this.ModuleCompBase.Init(service, module, comp, options)
|
||||||
|
this.module = module.(*Pack)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///查询用户背包数据
|
///查询用户背包数据
|
||||||
func (this *Cache_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) {
|
func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
|
||||||
pack = &pb.DB_UserPackData{
|
var (
|
||||||
UserId: uId,
|
lists []interface{}
|
||||||
}
|
temp map[string]interface{}
|
||||||
if err = this.redis.Get(fmt.Sprintf(Redis_PackCache, uId), pack); err == nil {
|
)
|
||||||
|
itmes = make([]*pb.DB_UserItemData, len(lists))
|
||||||
|
if lists, err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), reflect.TypeOf(&pb.DB_UserItemData{})); err == nil {
|
||||||
|
for i, v := range lists {
|
||||||
|
itmes[i] = v.(*pb.DB_UserItemData)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
} else if err == redis.RedisNil {
|
} else if err == redis.RedisNil {
|
||||||
if pack, err = db.Defsys.Pack_QueryUserPack(uId); err == nil {
|
if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil {
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
temp = make(map[string]interface{})
|
||||||
|
for _, v := range itmes {
|
||||||
|
temp[v.GridId] = v
|
||||||
|
}
|
||||||
|
this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp)
|
||||||
} else if err == mgo.MongodbNil {
|
} else if err == mgo.MongodbNil {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
@ -32,16 +51,67 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///查询用户指定格子的物品数据
|
||||||
|
func (this *Cache_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) {
|
||||||
|
var (
|
||||||
|
itmes []*pb.DB_UserItemData
|
||||||
|
temp map[string]interface{}
|
||||||
|
)
|
||||||
|
itme = &pb.DB_UserItemData{}
|
||||||
|
if err = this.Redis.HGet(fmt.Sprintf(Redis_PackCache, uId), grid, itme); err == nil {
|
||||||
|
return
|
||||||
|
} else if err == redis.RedisNil {
|
||||||
|
if itmes, err = this.module.db_comp.Pack_QueryUserPack(uId); err == nil {
|
||||||
|
temp = make(map[string]interface{})
|
||||||
|
for _, v := range itmes {
|
||||||
|
temp[v.GridId] = v
|
||||||
|
}
|
||||||
|
this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp)
|
||||||
|
for _, v := range itmes {
|
||||||
|
if v.GridId == grid {
|
||||||
|
itme = v
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("no found uid:%s grid:%s", uId, grid)
|
||||||
|
} else if err == mgo.MongodbNil {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//更新用户的背包信息
|
||||||
|
func (this *Cache_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItemData) (err error) {
|
||||||
|
temp := make(map[string]interface{})
|
||||||
|
for _, v := range itmes {
|
||||||
|
temp[v.GridId] = v
|
||||||
|
}
|
||||||
|
if err = this.Redis.HMSet(fmt.Sprintf(Redis_PackCache, uId), temp); err != nil {
|
||||||
|
this.module.db_comp.Pack_UpdateGridToUserPack(uId, itmes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//更新用户的背包信息
|
||||||
|
func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err error) {
|
||||||
|
if err = this.Redis.HDel(fmt.Sprintf(Redis_PackCache, uId), gridIds...); err != nil {
|
||||||
|
err = this.module.db_comp.Pack_DeleteGridToUserPack(uId, gridIds...)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//查询用户背包物品数量
|
//查询用户背包物品数量
|
||||||
func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||||
var (
|
var (
|
||||||
pack *pb.DB_UserPackData
|
itmes []*pb.DB_UserItemData
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, v := range pack.Pack {
|
for _, v := range itmes {
|
||||||
if !v.IsEmpty && v.ItemId == itemid {
|
if !v.IsEmpty && v.ItemId == itemid {
|
||||||
amount += v.Amount
|
amount += v.Amount
|
||||||
}
|
}
|
||||||
@ -52,17 +122,17 @@ func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (
|
|||||||
///添加或则减少物品到用户背包
|
///添加或则减少物品到用户背包
|
||||||
func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
|
func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
|
||||||
var (
|
var (
|
||||||
pack *pb.DB_UserPackData
|
itmes []*pb.DB_UserItemData
|
||||||
modifys []*pb.DB_GridData
|
modifys []*pb.DB_UserItemData
|
||||||
leftnum int64
|
leftnum int64
|
||||||
)
|
)
|
||||||
if addnum == 0 {
|
if addnum == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
modifys, leftnum = this.pack_addItemToUserPack(pack, itemId, addnum)
|
modifys, leftnum = this.pack_addItemToUserPack(itmes, itemId, addnum)
|
||||||
if leftnum < 0 {
|
if leftnum < 0 {
|
||||||
err = ItemNotEnoughError
|
err = ItemNotEnoughError
|
||||||
return
|
return
|
||||||
@ -70,26 +140,24 @@ func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum
|
|||||||
err = PackGridNumUpper
|
err = PackGridNumUpper
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil {
|
this.Pack_UpdateUserPack(uId, modifys...)
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///添加或则减少多个物品到用户背包
|
///添加或则减少多个物品到用户背包
|
||||||
func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||||
var (
|
var (
|
||||||
pack *pb.DB_UserPackData
|
itmes []*pb.DB_UserItemData
|
||||||
modifys []*pb.DB_GridData
|
modifys []*pb.DB_UserItemData
|
||||||
tempmodifys []*pb.DB_GridData
|
tempmodifys []*pb.DB_UserItemData
|
||||||
leftnum int64
|
leftnum int64
|
||||||
iskeep bool
|
iskeep bool
|
||||||
)
|
)
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
if itmes, err = this.Pack_QueryUserPack(uId); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for k, v := range items {
|
for k, v := range items {
|
||||||
tempmodifys, leftnum = this.pack_addItemToUserPack(pack, k, v)
|
tempmodifys, leftnum = this.pack_addItemToUserPack(itmes, k, v)
|
||||||
if leftnum < 0 {
|
if leftnum < 0 {
|
||||||
err = ItemNotEnoughError
|
err = ItemNotEnoughError
|
||||||
return
|
return
|
||||||
@ -110,36 +178,25 @@ func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int3
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil {
|
this.Pack_UpdateUserPack(uId, modifys...)
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///修改指定格子的物品数量
|
///修改指定格子的物品数量
|
||||||
func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid int32, itemId int32, addnum int32) (err error) {
|
func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) {
|
||||||
var (
|
var (
|
||||||
pack *pb.DB_UserPackData
|
itme *pb.DB_UserItemData
|
||||||
grid *pb.DB_GridData
|
grid *pb.DB_UserItemData
|
||||||
num int64
|
num int64
|
||||||
amount int64
|
amount int64
|
||||||
)
|
)
|
||||||
if addnum == 0 {
|
if addnum == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
if itme, err = this.Pack_QueryUserPackByGridId(uId, gridid); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if int32(len(pack.Pack)) <= gridid {
|
amount = int64(itme.Amount)
|
||||||
err = NoFoundGirdError
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grid = pack.Pack[gridid]
|
|
||||||
if grid == nil {
|
|
||||||
err = NoFoundGirdError
|
|
||||||
return
|
|
||||||
}
|
|
||||||
amount = int64(grid.Amount)
|
|
||||||
if grid.IsEmpty {
|
if grid.IsEmpty {
|
||||||
amount = 0
|
amount = 0
|
||||||
} else if grid.ItemId != itemId {
|
} else if grid.ItemId != itemId {
|
||||||
@ -153,38 +210,15 @@ func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid int32, i
|
|||||||
err = GirdAmountUpper
|
err = GirdAmountUpper
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grid); err == nil {
|
itme.Amount = uint32(num)
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
this.Pack_UpdateUserPack(uId, itme)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///修改目标格子的新获取标识
|
|
||||||
func (this *Cache_Comp) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error) {
|
|
||||||
var (
|
|
||||||
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, Pack_Expiration)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
///修改目标格子的新获取标识
|
|
||||||
func (this *Cache_Comp) 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
|
|
||||||
}
|
|
||||||
|
|
||||||
///添加移除物品到用户背包
|
///添加移除物品到用户背包
|
||||||
func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId int32, addnum int32) (modifys []*pb.DB_GridData, leftnum int64) {
|
func (this *Cache_Comp) pack_addItemToUserPack(items []*pb.DB_UserItemData, itemId int32, addnum int32) (modifys []*pb.DB_UserItemData, leftnum int64) {
|
||||||
var (
|
var (
|
||||||
num int64
|
num int64
|
||||||
isNew bool
|
isNew bool
|
||||||
@ -194,8 +228,8 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId
|
|||||||
}
|
}
|
||||||
isNew = true
|
isNew = true
|
||||||
leftnum = int64(addnum)
|
leftnum = int64(addnum)
|
||||||
modifys = make([]*pb.DB_GridData, 0)
|
modifys = make([]*pb.DB_UserItemData, 0)
|
||||||
for _, v := range pack.Pack {
|
for _, v := range items {
|
||||||
if !v.IsEmpty && v.ItemId == itemId {
|
if !v.IsEmpty && v.ItemId == itemId {
|
||||||
isNew = false
|
isNew = false
|
||||||
num = int64(v.Amount) + int64(leftnum)
|
num = int64(v.Amount) + int64(leftnum)
|
||||||
@ -234,7 +268,7 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if leftnum > 0 { //还没有放完 寻找空的格子填充
|
if leftnum > 0 { //还没有放完 寻找空的格子填充
|
||||||
for _, v := range pack.Pack {
|
for _, v := range items {
|
||||||
if v.IsEmpty {
|
if v.IsEmpty {
|
||||||
if leftnum <= GridCapMaxNum {
|
if leftnum <= GridCapMaxNum {
|
||||||
v.IsEmpty = false
|
v.IsEmpty = false
|
||||||
@ -252,30 +286,30 @@ func (this *Cache_Comp) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
index := int32(len(pack.Pack))
|
index := int32(len(items))
|
||||||
for leftnum > 0 { //需要补充格子
|
for leftnum > 0 { //需要补充格子
|
||||||
if leftnum <= GridCapMaxNum {
|
if leftnum <= GridCapMaxNum {
|
||||||
grid := &pb.DB_GridData{
|
grid := &pb.DB_UserItemData{
|
||||||
GridId: index,
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
IsEmpty: false,
|
IsEmpty: false,
|
||||||
ItemId: itemId,
|
ItemId: itemId,
|
||||||
Amount: uint32(leftnum),
|
Amount: uint32(leftnum),
|
||||||
IsNewItem: isNew,
|
IsNewItem: isNew,
|
||||||
}
|
}
|
||||||
pack.Pack = append(pack.Pack, grid)
|
items = append(items, grid)
|
||||||
modifys = append(modifys, grid)
|
modifys = append(modifys, grid)
|
||||||
leftnum = 0
|
leftnum = 0
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
leftnum -= GridCapMaxNum
|
leftnum -= GridCapMaxNum
|
||||||
grid := &pb.DB_GridData{
|
grid := &pb.DB_UserItemData{
|
||||||
GridId: index,
|
GridId: primitive.NewObjectID().Hex(),
|
||||||
IsEmpty: false,
|
IsEmpty: false,
|
||||||
ItemId: itemId,
|
ItemId: itemId,
|
||||||
Amount: uint32(GridCapMaxNum),
|
Amount: uint32(GridCapMaxNum),
|
||||||
IsNewItem: isNew,
|
IsNewItem: isNew,
|
||||||
}
|
}
|
||||||
pack.Pack = append(pack.Pack, grid)
|
items = append(items, grid)
|
||||||
modifys = append(modifys, grid)
|
modifys = append(modifys, grid)
|
||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,8 @@ func (this *Configure_Comp) GetItemConfigure(id int32) (item *cfg.Game_itemData,
|
|||||||
}
|
}
|
||||||
|
|
||||||
//获取指定类型的物品列表
|
//获取指定类型的物品列表
|
||||||
func (this *Configure_Comp) GetPackItemByType(pack *pb.DB_UserPackData, usetype int32) (result []*pb.DB_GridData) {
|
func (this *Configure_Comp) GetPackItemByType(itmes []*pb.DB_UserItemData, usetype int32) (result []*pb.DB_UserItemData) {
|
||||||
result = make([]*pb.DB_GridData, 0, len(pack.Pack))
|
result = make([]*pb.DB_UserItemData, 0, len(itmes))
|
||||||
var (
|
var (
|
||||||
v interface{}
|
v interface{}
|
||||||
table *cfg.Game_item
|
table *cfg.Game_item
|
||||||
@ -59,7 +59,7 @@ func (this *Configure_Comp) GetPackItemByType(pack *pb.DB_UserPackData, usetype
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
table = v.(*cfg.Game_item)
|
table = v.(*cfg.Game_item)
|
||||||
for _, v := range pack.Pack {
|
for _, v := range itmes {
|
||||||
if !v.IsEmpty {
|
if !v.IsEmpty {
|
||||||
if item, ok = table.GetDataMap()[id]; ok {
|
if item, ok = table.GetDataMap()[id]; ok {
|
||||||
if item.Usetype == usetype {
|
if item.Usetype == usetype {
|
||||||
|
@ -2,9 +2,7 @@ package pack
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"go_dreamfactory/modules"
|
||||||
"go_dreamfactory/lego/core/cbase"
|
|
||||||
"go_dreamfactory/lego/sys/mgo"
|
|
||||||
"go_dreamfactory/pb"
|
"go_dreamfactory/pb"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
@ -14,23 +12,16 @@ import (
|
|||||||
|
|
||||||
///背包数据库数据管理组件
|
///背包数据库数据管理组件
|
||||||
type DB_Comp struct {
|
type DB_Comp struct {
|
||||||
cbase.ModuleCompBase
|
modules.MComp_DBComp
|
||||||
mgo mgo.ISys
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (this *DB_Comp) Pack_InitUserPack(uId string) (pack []*pb.DB_UserItemData, err error) {
|
|
||||||
// pack = &pb.DB_UserPackData{UserId: uId, Pack: make([]*pb.DB_GridData, 0)}
|
|
||||||
// _, err = this.mgo.InsertOne(DB_PackTable, pack)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
///查询用户背包数据
|
///查询用户背包数据
|
||||||
func (this *DB_Comp) Pack_QueryUserPack(uId string) (items []*pb.DB_UserItemData, err error) {
|
func (this *DB_Comp) Pack_QueryUserPack(uId string) (items []*pb.DB_UserItemData, err error) {
|
||||||
var (
|
var (
|
||||||
cursor *mongo.Cursor
|
cursor *mongo.Cursor
|
||||||
)
|
)
|
||||||
items = make([]*pb.DB_UserItemData, 0)
|
items = make([]*pb.DB_UserItemData, 0)
|
||||||
if cursor, err = this.mgo.Find(DB_PackTable, bson.M{"uid": uId}); err != nil {
|
if cursor, err = this.DB.Find(DB_PackTable, bson.M{"uid": uId}); err != nil {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
for cursor.Next(context.Background()) {
|
for cursor.Next(context.Background()) {
|
||||||
@ -45,44 +36,47 @@ func (this *DB_Comp) Pack_QueryUserPack(uId string) (items []*pb.DB_UserItemData
|
|||||||
}
|
}
|
||||||
|
|
||||||
//更新背包格子数据
|
//更新背包格子数据
|
||||||
func (this *DB_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) {
|
func (this *DB_Comp) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_UserItemData) (err error) {
|
||||||
pack = &pb.DB_UserPackData{}
|
models := make([]mongo.WriteModel, len(grids))
|
||||||
update := bson.M{}
|
for i, v := range grids {
|
||||||
for _, v := range grids {
|
models[i] = mongo.NewUpdateOneModel().SetFilter(bson.M{"_id": v.GridId}).SetUpdate(
|
||||||
update[fmt.Sprintf("pack.%d.gridid", v.GridId)] = v.GridId
|
bson.M{"$set": bson.M{
|
||||||
update[fmt.Sprintf("pack.%d.isempty", v.GridId)] = v.IsEmpty
|
"isempty": v.IsEmpty,
|
||||||
update[fmt.Sprintf("pack.%d.itemid", v.GridId)] = v.ItemId
|
"itemid": v.ItemId,
|
||||||
update[fmt.Sprintf("pack.%d.amount", v.GridId)] = v.Amount
|
"amount": v.Amount,
|
||||||
update[fmt.Sprintf("pack.%d.isnewitem", v.GridId)] = v.IsNewItem
|
"ctime": v.CTime,
|
||||||
|
"etime": v.ETime,
|
||||||
|
"isnewitem": v.IsNewItem,
|
||||||
|
}}).SetUpsert(true)
|
||||||
}
|
}
|
||||||
|
this.DB.BulkWrite(DB_PackTable, models, options.BulkWrite().SetOrdered(false))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
//更新背包格子数据
|
||||||
bson.M{"_id": uId},
|
func (this *DB_Comp) Pack_DeleteGridToUserPack(uId string, grids ...string) (err error) {
|
||||||
bson.M{"$set": update},
|
_, err = this.DB.DeleteMany(DB_PackTable, bson.M{"_id": bson.M{"$inc": grids}})
|
||||||
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//更新背包格子物品的数据
|
//更新背包格子物品的数据
|
||||||
func (this *DB_Comp) Pack_AddGridAmountToUserPack(uId string, grid int32, amount int32) (pack *pb.DB_UserPackData, err error) {
|
func (this *DB_Comp) Pack_AddGridAmountToUserPack(grid int32, amount int32) (pack *pb.DB_UserItemData, err error) {
|
||||||
pack = &pb.DB_UserPackData{}
|
pack = &pb.DB_UserItemData{}
|
||||||
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
err = this.DB.FindOneAndUpdate(DB_PackTable,
|
||||||
bson.M{"_id": uId},
|
bson.M{"_id": grid},
|
||||||
bson.M{"$inc": bson.M{
|
bson.M{"$set": bson.M{
|
||||||
fmt.Sprintf("pack.%d.amount", grid): amount,
|
"amount": amount,
|
||||||
}},
|
}},
|
||||||
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//修改背包格子IsNew标识
|
//修改背包格子IsNew标识
|
||||||
func (this *DB_Comp) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) {
|
func (this *DB_Comp) Pack_ModifyPackGridIsNewItem(grids []int32) (err error) {
|
||||||
pack = &pb.DB_UserPackData{}
|
_, err = this.DB.UpdateMany(DB_PackTable,
|
||||||
identifier := []interface{}{bson.M{"item.gridid": bson.M{"$in": grids}}}
|
bson.M{"_id": bson.M{"$inc": grids}},
|
||||||
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
||||||
bson.M{"_id": uId},
|
|
||||||
bson.M{"$set": bson.M{
|
bson.M{"$set": bson.M{
|
||||||
"pack.$[item].isNewitem": false,
|
"isNewitem": false,
|
||||||
}}, options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{Filters: identifier}).SetReturnDocument(options.After)).Decode(pack)
|
}})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,8 @@ package pack
|
|||||||
import (
|
import (
|
||||||
"go_dreamfactory/comm"
|
"go_dreamfactory/comm"
|
||||||
"go_dreamfactory/modules"
|
"go_dreamfactory/modules"
|
||||||
"go_dreamfactory/sys/cache"
|
|
||||||
"go_dreamfactory/sys/db"
|
|
||||||
|
|
||||||
"go_dreamfactory/lego/core"
|
"go_dreamfactory/lego/core"
|
||||||
"go_dreamfactory/lego/sys/event"
|
|
||||||
"go_dreamfactory/lego/sys/log"
|
"go_dreamfactory/lego/sys/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,6 +21,8 @@ func NewModule() core.IModule {
|
|||||||
type Pack struct {
|
type Pack struct {
|
||||||
modules.ModuleBase
|
modules.ModuleBase
|
||||||
api_comp *Api_Comp //背包模块 协议处理组件
|
api_comp *Api_Comp //背包模块 协议处理组件
|
||||||
|
cache_comp *Cache_Comp //缓存组件
|
||||||
|
db_comp *DB_Comp //存储组件
|
||||||
configure_comp *Configure_Comp //背包模块 配置相关接口封装组件
|
configure_comp *Configure_Comp //背包模块 配置相关接口封装组件
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +34,6 @@ func (this *Pack) GetType() core.M_Modules {
|
|||||||
//模块初始化接口 注册用户创建角色事件
|
//模块初始化接口 注册用户创建角色事件
|
||||||
func (this *Pack) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
func (this *Pack) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
||||||
err = this.ModuleBase.Init(service, module, options)
|
err = this.ModuleBase.Init(service, module, options)
|
||||||
event.RegisterGO(comm.Event_CreateUser, this.event_CreateUser)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +41,8 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core.
|
|||||||
func (this *Pack) OnInstallComp() {
|
func (this *Pack) OnInstallComp() {
|
||||||
this.ModuleBase.OnInstallComp()
|
this.ModuleBase.OnInstallComp()
|
||||||
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||||
|
this.cache_comp = this.RegisterComp(new(Api_Comp)).(*Cache_Comp)
|
||||||
|
this.db_comp = this.RegisterComp(new(Api_Comp)).(*DB_Comp)
|
||||||
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,14 +50,14 @@ func (this *Pack) OnInstallComp() {
|
|||||||
///查询用户背包物品数量
|
///查询用户背包物品数量
|
||||||
func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||||
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
|
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
|
||||||
amount = cache.Defsys.Pack_QueryUserPackItemAmount(uId, itemid)
|
amount = this.cache_comp.Pack_QueryUserPackItemAmount(uId, itemid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
///添加单个物品到背包 (可以加物品和减物品)
|
///添加单个物品到背包 (可以加物品和减物品)
|
||||||
func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error) {
|
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)
|
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 {
|
if err = this.cache_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
|
||||||
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
|
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -66,16 +66,10 @@ func (this *Pack) AddItemToUserPack(uId string, itemid, addnum int32) (err error
|
|||||||
///添加多个物品到背包 (可以加物品和减物品)
|
///添加多个物品到背包 (可以加物品和减物品)
|
||||||
func (this *Pack) AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
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)
|
defer log.Debugf("给用户添加物品 uId:%s items:%d items:%v", uId, items, err == nil)
|
||||||
if err = cache.Defsys.Pack_AddItemsToUserPack(uId, items); err != nil {
|
if err = this.cache_comp.Pack_AddItemsToUserPack(uId, items); err != nil {
|
||||||
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
|
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Evens--------------------------------------------------------------------------------------------------------------------------------
|
//Evens--------------------------------------------------------------------------------------------------------------------------------
|
||||||
//接收玩家创建角色事件
|
|
||||||
func (this *Pack) event_CreateUser(uid string) {
|
|
||||||
if _, err := db.Defsys.Pack_InitUserPack(uid); err != nil {
|
|
||||||
log.Errorf("event_CreateUser err:%v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
315
sys/cache/pack.go
vendored
315
sys/cache/pack.go
vendored
@ -1,315 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"go_dreamfactory/pb"
|
|
||||||
"go_dreamfactory/sys/db"
|
|
||||||
|
|
||||||
"go_dreamfactory/lego/sys/mgo"
|
|
||||||
"go_dreamfactory/lego/sys/redis"
|
|
||||||
)
|
|
||||||
|
|
||||||
const ( //Redis
|
|
||||||
Redis_PackCache string = "pack:%s" //背包缓存数据存放key
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
GridCapMaxNum = 99 //单个格子的最大容量
|
|
||||||
GridMaxNUm = 200 //背包格子数量上限
|
|
||||||
Pack_Expiration = -1 //背包缓存数据过期时间
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ItemNotEnoughError = errors.New("item not enough!") //物品不足
|
|
||||||
NoFoundGirdError = errors.New("no found gvrid!") //未找到格子
|
|
||||||
PackGridNumUpper = errors.New("grid amount upper!") //背包格子达到上限
|
|
||||||
GirdAmountUpper = errors.New("grid amount upper!") //格子容量达到上限
|
|
||||||
)
|
|
||||||
|
|
||||||
type IPack interface {
|
|
||||||
///查询用户背包
|
|
||||||
Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error)
|
|
||||||
//查询用户背包物品数量
|
|
||||||
Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32)
|
|
||||||
///添加物品到背包 (可以加物品和减物品)
|
|
||||||
Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error)
|
|
||||||
///添加多个物品到背包 (可以加物品和减物品)
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
///查询用户背包数据
|
|
||||||
func (this *Cache) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) {
|
|
||||||
pack = &pb.DB_UserPackData{
|
|
||||||
UserId: uId,
|
|
||||||
}
|
|
||||||
if err = this.redis.Get(fmt.Sprintf(Redis_PackCache, uId), pack); err == nil {
|
|
||||||
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, Pack_Expiration)
|
|
||||||
} else if err == mgo.MongodbNil {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//查询用户背包物品数量
|
|
||||||
func (this *Cache) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
|
||||||
var (
|
|
||||||
pack *pb.DB_UserPackData
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, v := range pack.Pack {
|
|
||||||
if !v.IsEmpty && v.ItemId == itemid {
|
|
||||||
amount += v.Amount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
///添加或则减少物品到用户背包
|
|
||||||
func (this *Cache) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
|
|
||||||
var (
|
|
||||||
pack *pb.DB_UserPackData
|
|
||||||
modifys []*pb.DB_GridData
|
|
||||||
leftnum int64
|
|
||||||
)
|
|
||||||
if addnum == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
modifys, leftnum = this.pack_addItemToUserPack(pack, itemId, addnum)
|
|
||||||
if leftnum < 0 {
|
|
||||||
err = ItemNotEnoughError
|
|
||||||
return
|
|
||||||
} else if leftnum > 0 {
|
|
||||||
err = PackGridNumUpper
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil {
|
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
///添加或则减少多个物品到用户背包
|
|
||||||
func (this *Cache) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
|
||||||
var (
|
|
||||||
pack *pb.DB_UserPackData
|
|
||||||
modifys []*pb.DB_GridData
|
|
||||||
tempmodifys []*pb.DB_GridData
|
|
||||||
leftnum int64
|
|
||||||
iskeep bool
|
|
||||||
)
|
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for k, v := range items {
|
|
||||||
tempmodifys, leftnum = this.pack_addItemToUserPack(pack, k, v)
|
|
||||||
if leftnum < 0 {
|
|
||||||
err = ItemNotEnoughError
|
|
||||||
return
|
|
||||||
} else if leftnum > 0 {
|
|
||||||
err = PackGridNumUpper
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, v1 := range tempmodifys {
|
|
||||||
iskeep = false
|
|
||||||
for _, v2 := range modifys {
|
|
||||||
if v1.GridId == v2.GridId {
|
|
||||||
iskeep = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !iskeep {
|
|
||||||
modifys = append(modifys, v1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, modifys...); err == nil {
|
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
///修改指定格子的物品数量
|
|
||||||
func (this *Cache) Pack_AddItemToUserPackByGrid(uId string, gridid int32, itemId int32, addnum int32) (err error) {
|
|
||||||
var (
|
|
||||||
pack *pb.DB_UserPackData
|
|
||||||
grid *pb.DB_GridData
|
|
||||||
num int64
|
|
||||||
amount int64
|
|
||||||
)
|
|
||||||
if addnum == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if pack, err = this.Pack_QueryUserPack(uId); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if int32(len(pack.Pack)) <= gridid {
|
|
||||||
err = NoFoundGirdError
|
|
||||||
return
|
|
||||||
}
|
|
||||||
grid = pack.Pack[gridid]
|
|
||||||
if grid == nil {
|
|
||||||
err = NoFoundGirdError
|
|
||||||
return
|
|
||||||
}
|
|
||||||
amount = int64(grid.Amount)
|
|
||||||
if grid.IsEmpty {
|
|
||||||
amount = 0
|
|
||||||
} else if grid.ItemId != itemId {
|
|
||||||
err = fmt.Errorf("target grid itemid:%d no is %d ", grid.ItemId, itemId)
|
|
||||||
}
|
|
||||||
num = amount + int64(addnum)
|
|
||||||
if num < 0 {
|
|
||||||
err = ItemNotEnoughError
|
|
||||||
} else {
|
|
||||||
if num > GridCapMaxNum {
|
|
||||||
err = GirdAmountUpper
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
if pack, err = db.Defsys.Pack_UpdateGridToUserPack(uId, grid); err == nil {
|
|
||||||
this.redis.Set(fmt.Sprintf(Redis_PackCache, uId), pack, Pack_Expiration)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
///修改目标格子的新获取标识
|
|
||||||
func (this *Cache) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (err error) {
|
|
||||||
var (
|
|
||||||
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, 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
|
|
||||||
}
|
|
||||||
|
|
||||||
///添加移除物品到用户背包
|
|
||||||
func (this *Cache) pack_addItemToUserPack(pack *pb.DB_UserPackData, itemId int32, addnum int32) (modifys []*pb.DB_GridData, leftnum int64) {
|
|
||||||
var (
|
|
||||||
num int64
|
|
||||||
isNew bool
|
|
||||||
)
|
|
||||||
if addnum == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
isNew = true
|
|
||||||
leftnum = int64(addnum)
|
|
||||||
modifys = make([]*pb.DB_GridData, 0)
|
|
||||||
for _, v := range pack.Pack {
|
|
||||||
if !v.IsEmpty && v.ItemId == itemId {
|
|
||||||
isNew = false
|
|
||||||
num = int64(v.Amount) + int64(leftnum)
|
|
||||||
if num < 0 {
|
|
||||||
leftnum += int64(v.Amount)
|
|
||||||
v.Amount = 0
|
|
||||||
v.IsEmpty = true
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
} else if num > 0 && num < int64(v.Amount) {
|
|
||||||
leftnum = 0
|
|
||||||
v.Amount = uint32(num)
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
break
|
|
||||||
} else if num > 0 && num > int64(v.Amount) {
|
|
||||||
if num <= GridCapMaxNum {
|
|
||||||
leftnum = 0
|
|
||||||
v.Amount = uint32(num)
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
if v.Amount < GridCapMaxNum {
|
|
||||||
leftnum = int64(num - GridCapMaxNum)
|
|
||||||
v.Amount = uint32(GridCapMaxNum)
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if num == 0 {
|
|
||||||
leftnum = 0
|
|
||||||
v.Amount = 0
|
|
||||||
v.IsEmpty = true
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if leftnum < 0 { //背包物品不够
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if leftnum > 0 { //还没有放完 寻找空的格子填充
|
|
||||||
for _, v := range pack.Pack {
|
|
||||||
if v.IsEmpty {
|
|
||||||
if leftnum <= GridCapMaxNum {
|
|
||||||
v.IsEmpty = false
|
|
||||||
v.ItemId = itemId
|
|
||||||
v.Amount = uint32(leftnum)
|
|
||||||
leftnum = 0
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
leftnum -= GridCapMaxNum
|
|
||||||
v.IsEmpty = false
|
|
||||||
v.ItemId = itemId
|
|
||||||
v.Amount = uint32(GridCapMaxNum)
|
|
||||||
modifys = append(modifys, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
index := int32(len(pack.Pack))
|
|
||||||
for leftnum > 0 { //需要补充格子
|
|
||||||
if leftnum <= GridCapMaxNum {
|
|
||||||
grid := &pb.DB_GridData{
|
|
||||||
GridId: index,
|
|
||||||
IsEmpty: false,
|
|
||||||
ItemId: itemId,
|
|
||||||
Amount: uint32(leftnum),
|
|
||||||
IsNewItem: isNew,
|
|
||||||
}
|
|
||||||
pack.Pack = append(pack.Pack, grid)
|
|
||||||
modifys = append(modifys, grid)
|
|
||||||
leftnum = 0
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
leftnum -= GridCapMaxNum
|
|
||||||
grid := &pb.DB_GridData{
|
|
||||||
GridId: index,
|
|
||||||
IsEmpty: false,
|
|
||||||
ItemId: itemId,
|
|
||||||
Amount: uint32(GridCapMaxNum),
|
|
||||||
IsNewItem: isNew,
|
|
||||||
}
|
|
||||||
pack.Pack = append(pack.Pack, grid)
|
|
||||||
modifys = append(modifys, grid)
|
|
||||||
index++
|
|
||||||
}
|
|
||||||
if index > GridMaxNUm { //格子已达上限
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
19
sys/cache/pack_test.go
vendored
19
sys/cache/pack_test.go
vendored
@ -1,19 +0,0 @@
|
|||||||
package cache_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"go_dreamfactory/sys/cache"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
//测试用户背包 添加物品
|
|
||||||
func Test_Pack_AddItemToUserPack(t *testing.T) {
|
|
||||||
err := cache.Defsys.Pack_AddItemToUserPack("liwei1dao", 1001, 100)
|
|
||||||
fmt.Printf("Pack_AddItemToUserPack err:%v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//测试用户背包 添加物品
|
|
||||||
func Test_Pack_AddItemsToUserPack(t *testing.T) {
|
|
||||||
err := cache.Defsys.Pack_AddItemsToUserPack("liwei1dao", map[int32]int32{1003: -100})
|
|
||||||
fmt.Printf("Pack_AddItemToUserPack err:%v\n", err)
|
|
||||||
}
|
|
@ -3,7 +3,6 @@ package db
|
|||||||
type (
|
type (
|
||||||
ISys interface {
|
ISys interface {
|
||||||
IUser
|
IUser
|
||||||
IPack
|
|
||||||
IMail
|
IMail
|
||||||
IFriend
|
IFriend
|
||||||
}
|
}
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"go_dreamfactory/pb"
|
|
||||||
|
|
||||||
"go_dreamfactory/lego/core"
|
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
)
|
|
||||||
|
|
||||||
const ( //Redis
|
|
||||||
DB_PackTable core.SqlTable = "pack" //背包表
|
|
||||||
)
|
|
||||||
|
|
||||||
type IPack interface {
|
|
||||||
///初始化用户物品表
|
|
||||||
Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error)
|
|
||||||
///查询用户背包
|
|
||||||
Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error)
|
|
||||||
///更新用户背包数据
|
|
||||||
Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error)
|
|
||||||
///修改用户背包格子的标识
|
|
||||||
Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *DB) Pack_InitUserPack(uId string) (pack *pb.DB_UserPackData, err error) {
|
|
||||||
pack = &pb.DB_UserPackData{UserId: uId, Pack: make([]*pb.DB_GridData, 0)}
|
|
||||||
_, err = this.mgo.InsertOne(DB_PackTable, pack)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
///查询用户背包数据
|
|
||||||
func (this *DB) Pack_QueryUserPack(uId string) (pack *pb.DB_UserPackData, err error) {
|
|
||||||
pack = &pb.DB_UserPackData{}
|
|
||||||
err = this.mgo.FindOne(DB_PackTable, bson.M{"_id": uId}).Decode(pack)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//更新背包格子数据
|
|
||||||
func (this *DB) Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_GridData) (pack *pb.DB_UserPackData, err error) {
|
|
||||||
pack = &pb.DB_UserPackData{}
|
|
||||||
update := bson.M{}
|
|
||||||
for _, v := range grids {
|
|
||||||
update[fmt.Sprintf("pack.%d.gridid", v.GridId)] = v.GridId
|
|
||||||
update[fmt.Sprintf("pack.%d.isempty", v.GridId)] = v.IsEmpty
|
|
||||||
update[fmt.Sprintf("pack.%d.itemid", v.GridId)] = v.ItemId
|
|
||||||
update[fmt.Sprintf("pack.%d.amount", v.GridId)] = v.Amount
|
|
||||||
update[fmt.Sprintf("pack.%d.isnewitem", v.GridId)] = v.IsNewItem
|
|
||||||
}
|
|
||||||
|
|
||||||
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
||||||
bson.M{"_id": uId},
|
|
||||||
bson.M{"$set": update},
|
|
||||||
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//更新背包格子物品的数据
|
|
||||||
func (this *DB) Pack_AddGridAmountToUserPack(uId string, grid int32, amount int32) (pack *pb.DB_UserPackData, err error) {
|
|
||||||
pack = &pb.DB_UserPackData{}
|
|
||||||
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
||||||
bson.M{"_id": uId},
|
|
||||||
bson.M{"$inc": bson.M{
|
|
||||||
fmt.Sprintf("pack.%d.amount", grid): amount,
|
|
||||||
}},
|
|
||||||
options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)).Decode(pack)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//修改背包格子IsNew标识
|
|
||||||
func (this *DB) Pack_ModifyPackGridIsNewItem(uId string, grids []int32) (pack *pb.DB_UserPackData, err error) {
|
|
||||||
pack = &pb.DB_UserPackData{}
|
|
||||||
identifier := []interface{}{bson.M{"item.gridid": bson.M{"$in": grids}}}
|
|
||||||
err = this.mgo.FindOneAndUpdate(DB_PackTable,
|
|
||||||
bson.M{"_id": uId},
|
|
||||||
bson.M{"$set": bson.M{
|
|
||||||
"pack.$[item].isNewitem": false,
|
|
||||||
}}, options.FindOneAndUpdate().SetArrayFilters(options.ArrayFilters{Filters: identifier}).SetReturnDocument(options.After)).Decode(pack)
|
|
||||||
return
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"go_dreamfactory/pb"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_Pack_InitUserPack(t *testing.T) {
|
|
||||||
_, err := db.Pack_InitUserPack("liwei1dao")
|
|
||||||
require.Nil(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Pack_UpdateGridToUserPack(t *testing.T) {
|
|
||||||
data, err := db.Pack_UpdateGridToUserPack("liwei1dao",
|
|
||||||
&pb.DB_GridData{
|
|
||||||
|
|
||||||
GridId: 0,
|
|
||||||
IsEmpty: false,
|
|
||||||
ItemId: 1001,
|
|
||||||
Amount: 202,
|
|
||||||
IsNewItem: true,
|
|
||||||
},
|
|
||||||
&pb.DB_GridData{
|
|
||||||
GridId: 1,
|
|
||||||
IsEmpty: false,
|
|
||||||
ItemId: 1002,
|
|
||||||
Amount: 202,
|
|
||||||
IsNewItem: true,
|
|
||||||
},
|
|
||||||
&pb.DB_GridData{
|
|
||||||
GridId: 2,
|
|
||||||
IsEmpty: false,
|
|
||||||
ItemId: 1003,
|
|
||||||
Amount: 202,
|
|
||||||
IsNewItem: true,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
require.Nil(t, err)
|
|
||||||
require.Nil(t, data)
|
|
||||||
}
|
|
||||||
func Test_Pack_QueryUserPack(t *testing.T) {
|
|
||||||
data, err := db.Pack_QueryUserPack("liwei1dao")
|
|
||||||
require.Nil(t, err)
|
|
||||||
require.Nil(t, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Pack_UpdateGridAmountToUserPack(t *testing.T) {
|
|
||||||
data, err := db.Pack_AddGridAmountToUserPack("liwei1dao", 0, 102)
|
|
||||||
require.Nil(t, err)
|
|
||||||
require.Nil(t, data)
|
|
||||||
}
|
|
||||||
func Test_Pack_ModifyPackGridIsNewItem(t *testing.T) {
|
|
||||||
data, err := db.Pack_ModifyPackGridIsNewItem("liwei1dao", []int32{0, 1})
|
|
||||||
require.Nil(t, err)
|
|
||||||
require.Nil(t, data)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user