Merge branch 'liwei' of http://git.legu.cc/liwei_3d/go_dreamfactory into meixiongfeng
This commit is contained in:
commit
5b4517d0a8
@ -28,13 +28,13 @@ func (this *Api_Comp) Getlist(session comm.IUserSession, agrs map[string]interfa
|
||||
session.SendMsg(string(this.module.GetType()), GetlistResp, &pb.Pack_Getlist_Resp{Grids: grids})
|
||||
if code == pb.ErrorCode_Success {
|
||||
go func() { //异步处理修改数据
|
||||
this.module.cache_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
||||
this.module.cache_comp.Pack_DeleteUserPack(session.GetUserId(), dels...)
|
||||
this.module.model_pack_comp.Pack_UpdateUserPack(session.GetUserId(), modifys...)
|
||||
this.module.model_pack_comp.Pack_DeleteUserPack(session.GetUserId(), dels...)
|
||||
}()
|
||||
}
|
||||
}()
|
||||
|
||||
if items, err = this.module.cache_comp.Pack_QueryUserPack(session.GetUserId()); err != nil {
|
||||
if items, err = this.module.model_pack_comp.Pack_QueryUserPack(session.GetUserId()); err != nil {
|
||||
log.Errorf("QueryUserPackReq err:%v", err)
|
||||
code = pb.ErrorCode_CacheReadError
|
||||
return
|
||||
|
@ -3,29 +3,45 @@ package pack
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/mgo"
|
||||
"go_dreamfactory/lego/sys/redis"
|
||||
"go_dreamfactory/modules"
|
||||
"go_dreamfactory/pb"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/x/bsonx"
|
||||
)
|
||||
|
||||
///背包缓存数据管理组件
|
||||
type Cache_Comp struct {
|
||||
type Model_Pack_Comp struct {
|
||||
modules.Model_Comp
|
||||
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)
|
||||
func (this *Model_Pack_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
||||
this.Model_Comp.Init(service, module, comp, opt)
|
||||
this.module = module.(*Pack)
|
||||
this.TableName = "pack"
|
||||
//创建uid索引
|
||||
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
|
||||
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
///查询用户背包数据
|
||||
func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
|
||||
func (this *Model_Pack_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemData, err error) {
|
||||
var (
|
||||
data map[string]*pb.DB_UserItemData = make(map[string]*pb.DB_UserItemData)
|
||||
)
|
||||
if err = this.GetHM(uId, data); err == nil {
|
||||
itmes = make([]*pb.DB_UserItemData, len(data))
|
||||
n := 0
|
||||
for _, v := range data {
|
||||
itmes[n] = v
|
||||
n++
|
||||
}
|
||||
}
|
||||
// var (
|
||||
// lists []interface{}
|
||||
// temp map[string]interface{}
|
||||
@ -51,50 +67,58 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD
|
||||
}
|
||||
|
||||
///查询用户指定格子的物品数据
|
||||
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{}
|
||||
)
|
||||
func (this *Model_Pack_Comp) Pack_QueryUserPackByGridId(uId string, grid string) (itme *pb.DB_UserItemData, err error) {
|
||||
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
|
||||
}
|
||||
}
|
||||
err = this.GetH(uId, grid, itme)
|
||||
|
||||
// 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{})
|
||||
func (this *Model_Pack_Comp) Pack_UpdateUserPack(uId string, itmes ...*pb.DB_UserItemData) (err error) {
|
||||
var data map[string]*pb.DB_UserItemData = make(map[string]*pb.DB_UserItemData)
|
||||
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...)
|
||||
data[v.GridId] = v
|
||||
}
|
||||
err = this.SetHM(uId, data)
|
||||
// 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) {
|
||||
func (this *Model_Pack_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...)
|
||||
}
|
||||
@ -102,7 +126,7 @@ func (this *Cache_Comp) Pack_DeleteUserPack(uId string, gridIds ...string) (err
|
||||
}
|
||||
|
||||
//查询用户背包物品数量
|
||||
func (this *Cache_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||
func (this *Model_Pack_Comp) Pack_QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||
var (
|
||||
itmes []*pb.DB_UserItemData
|
||||
err error
|
||||
@ -119,7 +143,7 @@ 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 *Model_Pack_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum int32) (err error) {
|
||||
var (
|
||||
itmes []*pb.DB_UserItemData
|
||||
modifys []*pb.DB_UserItemData
|
||||
@ -144,7 +168,7 @@ func (this *Cache_Comp) Pack_AddItemToUserPack(uId string, itemId int32, addnum
|
||||
}
|
||||
|
||||
///添加或则减少多个物品到用户背包
|
||||
func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||
func (this *Model_Pack_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int32) (err error) {
|
||||
var (
|
||||
itmes []*pb.DB_UserItemData
|
||||
modifys []*pb.DB_UserItemData
|
||||
@ -182,7 +206,7 @@ func (this *Cache_Comp) Pack_AddItemsToUserPack(uId string, items map[int32]int3
|
||||
}
|
||||
|
||||
///修改指定格子的物品数量
|
||||
func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) {
|
||||
func (this *Model_Pack_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string, itemId int32, addnum int32) (err error) {
|
||||
var (
|
||||
itme *pb.DB_UserItemData
|
||||
grid *pb.DB_UserItemData
|
||||
@ -217,7 +241,7 @@ func (this *Cache_Comp) Pack_AddItemToUserPackByGrid(uId string, gridid string,
|
||||
}
|
||||
|
||||
///添加移除物品到用户背包
|
||||
func (this *Cache_Comp) pack_addItemToUserPack(items []*pb.DB_UserItemData, itemId int32, addnum int32) (modifys []*pb.DB_UserItemData, leftnum int64) {
|
||||
func (this *Model_Pack_Comp) pack_addItemToUserPack(items []*pb.DB_UserItemData, itemId int32, addnum int32) (modifys []*pb.DB_UserItemData, leftnum int64) {
|
||||
var (
|
||||
num int64
|
||||
isNew bool
|
@ -20,10 +20,10 @@ func NewModule() core.IModule {
|
||||
|
||||
type Pack struct {
|
||||
modules.ModuleBase
|
||||
api_comp *Api_Comp
|
||||
cache_comp *Cache_Comp
|
||||
db_comp *DB_Comp
|
||||
configure_comp *Configure_Comp
|
||||
api_comp *Api_Comp
|
||||
model_pack_comp *Model_Pack_Comp
|
||||
db_comp *DB_Comp
|
||||
configure_comp *Configure_Comp
|
||||
}
|
||||
|
||||
//模块名称
|
||||
@ -41,8 +41,7 @@ func (this *Pack) Init(service core.IService, module core.IModule, options core.
|
||||
func (this *Pack) OnInstallComp() {
|
||||
this.ModuleBase.OnInstallComp()
|
||||
this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp)
|
||||
this.cache_comp = this.RegisterComp(new(Cache_Comp)).(*Cache_Comp)
|
||||
//this.db_comp = this.RegisterComp(new(DB_Comp)).(*DB_Comp)
|
||||
this.model_pack_comp = this.RegisterComp(new(Model_Pack_Comp)).(*Model_Pack_Comp)
|
||||
this.configure_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
||||
}
|
||||
|
||||
@ -50,14 +49,14 @@ func (this *Pack) OnInstallComp() {
|
||||
///查询用户背包物品数量
|
||||
func (this *Pack) QueryUserPackItemAmount(uId string, itemid int32) (amount uint32) {
|
||||
defer log.Debugf("获取物品 uId:%s itemid:%d addnum:%d ", uId, itemid, amount)
|
||||
amount = this.cache_comp.Pack_QueryUserPackItemAmount(uId, itemid)
|
||||
amount = this.model_pack_comp.Pack_QueryUserPackItemAmount(uId, itemid)
|
||||
return
|
||||
}
|
||||
|
||||
///添加单个物品到背包 (可以加物品和减物品)
|
||||
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 = this.cache_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
|
||||
if err = this.model_pack_comp.Pack_AddItemToUserPack(uId, itemid, addnum); err != nil {
|
||||
log.Errorf("给用户添加物品 uId:%s itemid:%d addnum:%d err:%v", uId, itemid, addnum, err)
|
||||
}
|
||||
return
|
||||
@ -66,7 +65,7 @@ func (this *Pack) AddItemToUserPack(uId string, itemid, addnum 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)
|
||||
if err = this.cache_comp.Pack_AddItemsToUserPack(uId, items); err != nil {
|
||||
if err = this.model_pack_comp.Pack_AddItemsToUserPack(uId, items); err != nil {
|
||||
log.Errorf("给用户添加物品 uId:%s items:%d err:%v", uId, items, err)
|
||||
}
|
||||
return
|
||||
|
@ -1,7 +1,6 @@
|
||||
package pack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go_dreamfactory/comm"
|
||||
"go_dreamfactory/lego"
|
||||
@ -17,7 +16,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func newService(ops ...rpcx.Option) core.IService {
|
||||
@ -74,8 +76,78 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func Test_Log(t *testing.T) {
|
||||
data, _ := ptypes.MarshalAny(&pb.Pack_Getlist_Req{})
|
||||
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{Method: "pack.getlist", Message: data}, &pb.RPCMessageReply{})
|
||||
// data, _ := ptypes.MarshalAny(&pb.Pack_Getlist_Req{})
|
||||
// s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{Method: "pack.getlist", Message: data}, &pb.RPCMessageReply{})
|
||||
|
||||
// items, err := module.db_comp.Pack_QueryUserPack("liwei1dao")
|
||||
// log.Debugf("item:%v err:%v", items, err)
|
||||
uid := "0_62a9afd994fe03b7aaee6773"
|
||||
Pack_UpdateGridToUserPack(
|
||||
uid,
|
||||
&pb.DB_UserItemData{
|
||||
GridId: primitive.NewObjectID().Hex(),
|
||||
UId: uid,
|
||||
IsEmpty: false,
|
||||
ItemId: 1001,
|
||||
Amount: 99,
|
||||
CTime: time.Now().Unix(),
|
||||
IsNewItem: true,
|
||||
},
|
||||
&pb.DB_UserItemData{
|
||||
GridId: primitive.NewObjectID().Hex(),
|
||||
UId: uid,
|
||||
IsEmpty: false,
|
||||
ItemId: 1001,
|
||||
Amount: 12,
|
||||
CTime: time.Now().Unix(),
|
||||
IsNewItem: true,
|
||||
},
|
||||
&pb.DB_UserItemData{
|
||||
GridId: primitive.NewObjectID().Hex(),
|
||||
UId: uid,
|
||||
IsEmpty: false,
|
||||
ItemId: 1002,
|
||||
Amount: 99,
|
||||
CTime: time.Now().Unix(),
|
||||
IsNewItem: true,
|
||||
},
|
||||
&pb.DB_UserItemData{
|
||||
GridId: primitive.NewObjectID().Hex(),
|
||||
UId: uid,
|
||||
IsEmpty: false,
|
||||
ItemId: 1003,
|
||||
Amount: 78,
|
||||
CTime: time.Now().Unix(),
|
||||
IsNewItem: true,
|
||||
},
|
||||
&pb.DB_UserItemData{
|
||||
GridId: primitive.NewObjectID().Hex(),
|
||||
UId: uid,
|
||||
IsEmpty: false,
|
||||
ItemId: 1004,
|
||||
Amount: 99,
|
||||
CTime: time.Now().Unix(),
|
||||
IsNewItem: true,
|
||||
},
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
//更新背包格子数据
|
||||
func Pack_UpdateGridToUserPack(uId string, grids ...*pb.DB_UserItemData) (err error) {
|
||||
models := make([]mongo.WriteModel, len(grids))
|
||||
for i, v := range grids {
|
||||
models[i] = mongo.NewUpdateOneModel().SetFilter(bson.M{"_id": v.GridId}).SetUpdate(
|
||||
bson.M{"$set": bson.M{
|
||||
"uid": v.UId,
|
||||
"isempty": v.IsEmpty,
|
||||
"itemid": v.ItemId,
|
||||
"amount": v.Amount,
|
||||
"ctime": v.CTime,
|
||||
"etime": v.ETime,
|
||||
"isnewitem": v.IsNewItem,
|
||||
}}).SetUpsert(true)
|
||||
}
|
||||
module.model_pack_comp.DB.BulkWrite(DB_PackTable, models, options.BulkWrite().SetOrdered(false))
|
||||
return
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ func (this *SComp_GateRouteComp) RegisterRouteCheck(methodName string, comp refl
|
||||
|
||||
//Rpc_GatewayRoute服务接口的接收函数
|
||||
func (this *SComp_GateRouteComp) ReceiveMsg(ctx context.Context, args *pb.AgentMessage, reply *pb.RPCMessageReply) error {
|
||||
defer func() {
|
||||
defer func() { //程序异常 收集异常信息传递给前端显示
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 1024)
|
||||
l := runtime.Stack(buf, false)
|
||||
@ -132,25 +132,30 @@ func (this *SComp_GateRouteComp) ReceiveMsg(ctx context.Context, args *pb.AgentM
|
||||
}
|
||||
}()
|
||||
log.Debugf("SComp_GateRouteComp ReceiveMsg agent:%s uId:%s MessageDistribution msg:%s", args.UserSessionId, args.UserId, args.Method)
|
||||
//获取用户消息处理函数
|
||||
this.mrlock.RLock()
|
||||
msghandle, ok := this.msghandles[args.Method]
|
||||
msgcheck := this.msgcheck[args.Method]
|
||||
this.mrlock.RUnlock()
|
||||
if ok {
|
||||
//封装用户会话
|
||||
session := comm.NewUserSession(this.service, args.Ip, args.UserSessionId, args.GatewayServiceId, args.UserId)
|
||||
//序列化用户消息对象
|
||||
msg := reflect.New(msghandle.msgType.Elem()).Interface()
|
||||
if err := ptypes.UnmarshalAny(args.Message, msg.(proto.Message)); err != nil {
|
||||
log.Errorf("UserMessage:%s Unmarshal err:%v", args.Method, err)
|
||||
return err
|
||||
}
|
||||
returnValues := msgcheck.fn.Func.Call([]reflect.Value{msgcheck.rcvr, reflect.ValueOf(session), reflect.ValueOf(msg)})
|
||||
// The return value for the method is an error.
|
||||
code := returnValues[1].Interface().(comm.ErrorCode)
|
||||
|
||||
//调用校验接口
|
||||
checkreturn := msgcheck.fn.Func.Call([]reflect.Value{msgcheck.rcvr, reflect.ValueOf(session), reflect.ValueOf(msg)})
|
||||
//读取校验结果 有错误直接返回错误码给用户
|
||||
code := checkreturn[1].Interface().(comm.ErrorCode)
|
||||
if code.Code != pb.ErrorCode_Success {
|
||||
log.Errorf("HandleUserMsg:%s msg:%v code:%d", args.Method, msg, code)
|
||||
reply.Code = code.Code
|
||||
reply.Message = pb.GetErrorCodeMsg(code.Code)
|
||||
if code.Data != nil {
|
||||
if code.Data != nil { //处理错误附加数据 采用json 序列化为string
|
||||
if d, err := jsoniter.Marshal(code.Data); err != nil {
|
||||
log.Errorf("HandleUserMsg:%s msg:%v code:%d err:%v", args.Method, msg, code, err)
|
||||
return nil
|
||||
@ -160,16 +165,18 @@ func (this *SComp_GateRouteComp) ReceiveMsg(ctx context.Context, args *pb.AgentM
|
||||
}
|
||||
return nil
|
||||
}
|
||||
result := returnValues[0].Interface().(map[string]interface{})
|
||||
returnValues = msghandle.fn.Func.Call([]reflect.Value{msghandle.rcvr, reflect.ValueOf(session), reflect.ValueOf(result), reflect.ValueOf(msg)})
|
||||
errcode := pb.ErrorCode(returnValues[0].Int())
|
||||
if errcode != pb.ErrorCode_Success {
|
||||
//校验结果成功 处理临时数据转移
|
||||
result := checkreturn[0].Interface().(map[string]interface{})
|
||||
//调用用户处理函数
|
||||
handlereturn := msghandle.fn.Func.Call([]reflect.Value{msghandle.rcvr, reflect.ValueOf(session), reflect.ValueOf(result), reflect.ValueOf(msg)})
|
||||
errcode := pb.ErrorCode(handlereturn[0].Int())
|
||||
if errcode != pb.ErrorCode_Success { //处理返货错误码 返回用户错误信息
|
||||
log.Errorf("HandleUserMsg:%s msg:%v code:%d", args.Method, msg, code)
|
||||
reply.Code = errcode
|
||||
reply.Message = pb.GetErrorCodeMsg(errcode)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
} else { //未找到消息处理函数
|
||||
reply.Code = pb.ErrorCode_ReqParameterError
|
||||
}
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user