From 4255fd8ecca81bea17c410cb63cc78ec632a4e42 Mon Sep 17 00:00:00 2001 From: zqj <582132116@qq.com> Date: Tue, 29 Mar 2022 11:09:22 +0800 Subject: [PATCH] feat: 更新字段 --- common/components/icompontents.go | 6 +++++- common/utils.go | 25 +++++++++++++++++++++++++ common/utils_test.go | 11 +++++------ models/role.go | 28 +++++++++++++++++++++++++++- models/role_test.go | 21 +++++++++++++++++++++ models/schema.go | 42 ++++++++++++++++++++++++++++++------------ pb/game.pb.go | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- pb/protocode.pb.go | 35 ++++++++++++++++++++--------------- 8 files changed, 227 insertions(+), 55 deletions(-) diff --git a/common/components/icompontents.go b/common/components/icompontents.go index df25d4d..f90bd2e 100644 --- a/common/components/icompontents.go +++ b/common/components/icompontents.go @@ -1,5 +1,7 @@ package components +import "google.golang.org/protobuf/reflect/protoreflect" + //----------------- //----net start---- //----------------- @@ -38,7 +40,7 @@ type ( GetID() uint32 Start() Stop() - Send(code int32, cmd uint32, b []byte) error + Send(errCode int32, cmd uint32, b []byte) error SetConnectionCallback(ConnectionCallback) SetMessageCallback(MessageCallback) @@ -120,10 +122,12 @@ type ( Load() error Create() error + Save() error Update() SetProperty(key string, val interface{}) SetProperties(properties map[string]interface{}) + ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 } ) diff --git a/common/utils.go b/common/utils.go index 29118c8..070a714 100644 --- a/common/utils.go +++ b/common/utils.go @@ -3,6 +3,7 @@ package common import ( "crypto/md5" "encoding/hex" + "google.golang.org/protobuf/reflect/protoreflect" "math/rand" "strings" "time" @@ -47,3 +48,27 @@ func RandomName(name [][]string) string { return builder.String() } +//英文字符串,第一个字符大写 +func FirstCharToUpper(key string) string { + first := strings.ToUpper(key[0:1]) + str := strings.Builder{} + str.WriteString(first) + str.WriteString(key[1:]) + return str.String() +} + + +func ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 { + ids := make([]int32,0) + for k, v := range properties { + name := protoreflect.Name(strings.ToLower(k)) + field := message.Descriptor().Fields().ByName(name) + if field == nil { + continue + } + + ids = append(ids, int32(field.Index())) + message.Set(field, protoreflect.ValueOf(v)) + } + return ids +} \ No newline at end of file diff --git a/common/utils_test.go b/common/utils_test.go index 0e34527..e32d732 100644 --- a/common/utils_test.go +++ b/common/utils_test.go @@ -3,8 +3,6 @@ package common import ( "fmt" "math/rand" - "pro2d/common/db/redisproxy" - "pro2d/common/logger" "testing" ) @@ -14,8 +12,9 @@ func TestRandomString(t *testing.T) { } func TestRandomName(t *testing.T) { - if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil { - logger.Error(err) - return - } + //if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil { + // logger.Error(err) + // return + //} + fmt.Println(FirstCharToUpper("name")) } diff --git a/models/role.go b/models/role.go index 914f524..e927201 100644 --- a/models/role.go +++ b/models/role.go @@ -1,6 +1,7 @@ package models import ( + "github.com/golang/protobuf/proto" "pro2d/common" "pro2d/common/components" "pro2d/common/db/mongoproxy" @@ -129,7 +130,32 @@ func (m *RoleModel) LoadAll() { m.LoadTeams() } -func (m *RoleModel) updateProperty(property map[string]interface{}) { +func (m *RoleModel) UpdateProperty(conn components.IConnection, key string, val interface{}, notify bool) { + m.UpdateProperties(conn, map[string]interface{}{key:val}, notify) +} + +func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[string]interface{}, notify bool) { + if len(property) < 1 { + return + } + + role := &pb.Role{} + ids := m.ParseFields(role.ProtoReflect(), property) + if len(ids) == 0 { + logger.Error("ParseFields err, len is 0") + return + } + + update := &pb.UpdateRolePropertyRsp{ + Id: ids, + Role: role, + } + if rsp, err := proto.Marshal(update); err != nil { + logger.Error("id %s, err:", m.Role.Id, err) + return + }else { + conn.Send(0, uint32(pb.ProtoCode_UpdateRolePropertyRsp), rsp) + } } func (m *RoleModel) GetAllHero() []*pb.Hero { diff --git a/models/role_test.go b/models/role_test.go index 3cce8ba..a71285f 100644 --- a/models/role_test.go +++ b/models/role_test.go @@ -65,4 +65,25 @@ func TestRoleModel_AddHero(t *testing.T) { } role.InitRole() +} + +func TestRoleModel_ProtoReflect(t *testing.T) { + err := mongoproxy.ConnectMongo(common.GlobalConf.GameConf.MongoConf) + if err != nil { + logger.Error(err) + return + } + + role := &pb.Role{} + sch := NewSchema("", role) + mp := map[string]interface{}{ + "Id": "1", + "Device": "12312312312", + } + ids := sch.ParseFields(role.ProtoReflect(), mp) + if len(ids) == 0 { + return + } + + fmt.Println(role) } \ No newline at end of file diff --git a/models/schema.go b/models/schema.go index 7461561..e4103d6 100644 --- a/models/schema.go +++ b/models/schema.go @@ -1,6 +1,8 @@ package models import ( + "google.golang.org/protobuf/reflect/protoreflect" + "pro2d/common" "pro2d/common/components" "pro2d/common/db/mongoproxy" "pro2d/common/logger" @@ -21,7 +23,6 @@ type SchemaMap map[string]components.ISchema type Schema struct { db components.IDB reflectValue *reflect.Value - reflectType reflect.Type cacheFields map[string]interface{} @@ -36,7 +37,6 @@ func NewSchema(key string, schema interface{}) *Schema { } sch := &Schema{ reflectValue: &s, - reflectType: s.Type(), cacheFields: make(map[string]interface{}), schema: schema, } @@ -48,16 +48,16 @@ func NewSchema(key string, schema interface{}) *Schema { func (s *Schema) FindIndex() (string, []string) { var index []string - for i := 0; i < s.reflectType.NumField(); i++ { - if s.reflectType.Field(i).Tag.Get("index") != "" { - js := strings.Split(s.reflectType.Field(i).Tag.Get("json"), ",") + for i := 0; i < s.reflectValue.Type().NumField(); i++ { + if s.reflectValue.Type().Field(i).Tag.Get("index") != "" { + js := strings.Split(s.reflectValue.Type().Field(i).Tag.Get("json"), ",") if len(js) == 0 { continue } index = append(index, js[0]) } } - return strings.ToLower(s.reflectType.Name()), index + return strings.ToLower(s.reflectValue.Type().Name()), index } func (s *Schema) Init() { @@ -87,7 +87,7 @@ func (s *Schema) GetSchema() interface{} { } func (s *Schema) GetSchemaName() string { - return strings.ToLower(s.reflectType.Name()) + return strings.ToLower(s.reflectValue.Type().Name()) } func (s *Schema) Load() error { @@ -111,22 +111,40 @@ func (s *Schema) Update() { } func (s *Schema) SetProperty(key string, val interface{}) { - s.reflectValue.FieldByName(key).Set(reflect.ValueOf(val)) + s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val)) s.cacheFields[strings.ToLower(key)] = val } func (s *Schema) SetProperties(properties map[string]interface{}) { for key, val := range properties { - s.reflectValue.FieldByName(key).Set(reflect.ValueOf(val)) + s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val)) s.cacheFields[strings.ToLower(key)] = val } } +func (s *Schema) ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 { + ids := make([]int32, 0, len(properties)) + for k, v := range properties { + name := protoreflect.Name(strings.ToLower(k)) + field := message.Descriptor().Fields().ByName(name) + if field == nil { + continue + } + + ids = append(ids, int32(field.Index())) + message.Set(field, protoreflect.ValueOf(v)) + + s.SetProperty(k, v) + } + + return ids +} + func (s *Schema) getPriTag() string { var pri string - for i := 0; i < s.reflectType.NumField(); i++ { - if s.reflectType.Field(i).Tag.Get("pri") == "1" { - pri = strings.ToLower(s.reflectType.Field(i).Name) + for i := 0; i < s.reflectValue.Type().NumField(); i++ { + if s.reflectValue.Type().Field(i).Tag.Get("pri") == "1" { + pri = strings.ToLower(s.reflectValue.Type().Field(i).Name) break } } diff --git a/pb/game.pb.go b/pb/game.pb.go index 330ce9d..22f09af 100644 --- a/pb/game.pb.go +++ b/pb/game.pb.go @@ -288,6 +288,61 @@ func (x *RoleRsp) GetTeam() []*Team { return nil } +type UpdateRolePropertyRsp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id []int32 `protobuf:"varint,1,rep,packed,name=id,proto3" json:"id,omitempty"` + Role *Role `protobuf:"bytes,2,opt,name=role,proto3" json:"role,omitempty"` +} + +func (x *UpdateRolePropertyRsp) Reset() { + *x = UpdateRolePropertyRsp{} + if protoimpl.UnsafeEnabled { + mi := &file_game_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UpdateRolePropertyRsp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UpdateRolePropertyRsp) ProtoMessage() {} + +func (x *UpdateRolePropertyRsp) ProtoReflect() protoreflect.Message { + mi := &file_game_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use UpdateRolePropertyRsp.ProtoReflect.Descriptor instead. +func (*UpdateRolePropertyRsp) Descriptor() ([]byte, []int) { + return file_game_proto_rawDescGZIP(), []int{5} +} + +func (x *UpdateRolePropertyRsp) GetId() []int32 { + if x != nil { + return x.Id + } + return nil +} + +func (x *UpdateRolePropertyRsp) GetRole() *Role { + if x != nil { + return x.Role + } + return nil +} + var File_game_proto protoreflect.FileDescriptor var file_game_proto_rawDesc = []byte{ @@ -311,8 +366,13 @@ var file_game_proto_rawDesc = []byte{ 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x48, 0x65, 0x72, 0x6f, 0x52, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d, - 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, - 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x22, 0x49, 0x0a, 0x15, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, + 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x52, 0x73, 0x70, 0x12, + 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x03, 0x28, 0x05, 0x52, 0x02, 0x69, 0x64, 0x12, + 0x20, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, + 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f, 0x6c, + 0x65, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -327,26 +387,28 @@ func file_game_proto_rawDescGZIP() []byte { return file_game_proto_rawDescData } -var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_game_proto_goTypes = []interface{}{ - (*HeartReq)(nil), // 0: game.HeartReq - (*HeartRsp)(nil), // 1: game.HeartRsp - (*LoginReq)(nil), // 2: game.LoginReq - (*CreateReq)(nil), // 3: game.CreateReq - (*RoleRsp)(nil), // 4: game.RoleRsp - (*Role)(nil), // 5: models.Role - (*Hero)(nil), // 6: models.Hero - (*Team)(nil), // 7: models.Team + (*HeartReq)(nil), // 0: game.HeartReq + (*HeartRsp)(nil), // 1: game.HeartRsp + (*LoginReq)(nil), // 2: game.LoginReq + (*CreateReq)(nil), // 3: game.CreateReq + (*RoleRsp)(nil), // 4: game.RoleRsp + (*UpdateRolePropertyRsp)(nil), // 5: game.UpdateRolePropertyRsp + (*Role)(nil), // 6: models.Role + (*Hero)(nil), // 7: models.Hero + (*Team)(nil), // 8: models.Team } var file_game_proto_depIdxs = []int32{ - 5, // 0: game.RoleRsp.role:type_name -> models.Role - 6, // 1: game.RoleRsp.hero:type_name -> models.Hero - 7, // 2: game.RoleRsp.team:type_name -> models.Team - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 6, // 0: game.RoleRsp.role:type_name -> models.Role + 7, // 1: game.RoleRsp.hero:type_name -> models.Hero + 8, // 2: game.RoleRsp.team:type_name -> models.Team + 6, // 3: game.UpdateRolePropertyRsp.role:type_name -> models.Role + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name } func init() { file_game_proto_init() } @@ -416,6 +478,18 @@ func file_game_proto_init() { return nil } } + file_game_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UpdateRolePropertyRsp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -423,7 +497,7 @@ func file_game_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_game_proto_rawDesc, NumEnums: 0, - NumMessages: 5, + NumMessages: 6, NumExtensions: 0, NumServices: 0, }, diff --git a/pb/protocode.pb.go b/pb/protocode.pb.go index a7f1138..9d59502 100644 --- a/pb/protocode.pb.go +++ b/pb/protocode.pb.go @@ -23,12 +23,13 @@ const ( type ProtoCode int32 const ( - ProtoCode_UNKNOWN ProtoCode = 0 - ProtoCode_HeartReq ProtoCode = 1 - ProtoCode_HeartRsp ProtoCode = 2 - ProtoCode_LoginReq ProtoCode = 3 - ProtoCode_CreateReq ProtoCode = 4 - ProtoCode_RoleRsp ProtoCode = 5 + ProtoCode_UNKNOWN ProtoCode = 0 + ProtoCode_HeartReq ProtoCode = 1 + ProtoCode_HeartRsp ProtoCode = 2 + ProtoCode_LoginReq ProtoCode = 3 + ProtoCode_CreateReq ProtoCode = 4 + ProtoCode_RoleRsp ProtoCode = 5 + ProtoCode_UpdateRolePropertyRsp ProtoCode = 6 ) // Enum value maps for ProtoCode. @@ -40,14 +41,16 @@ var ( 3: "LoginReq", 4: "CreateReq", 5: "RoleRsp", + 6: "UpdateRolePropertyRsp", } ProtoCode_value = map[string]int32{ - "UNKNOWN": 0, - "HeartReq": 1, - "HeartRsp": 2, - "LoginReq": 3, - "CreateReq": 4, - "RoleRsp": 5, + "UNKNOWN": 0, + "HeartReq": 1, + "HeartRsp": 2, + "LoginReq": 3, + "CreateReq": 4, + "RoleRsp": 5, + "UpdateRolePropertyRsp": 6, } ) @@ -82,14 +85,16 @@ var File_protocode_proto protoreflect.FileDescriptor var file_protocode_proto_rawDesc = []byte{ 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x5e, 0x0a, 0x09, + 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x79, 0x0a, 0x09, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x65, 0x71, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x73, 0x70, 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x10, 0x04, 0x12, - 0x0b, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x10, 0x05, 0x42, 0x0a, 0x5a, 0x08, - 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x0b, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x10, 0x05, 0x12, 0x19, 0x0a, 0x15, + 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, + 0x74, 0x79, 0x52, 0x73, 0x70, 0x10, 0x06, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, + 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( -- libgit2 0.21.2