Commit 4255fd8ecca81bea17c410cb63cc78ec632a4e42

Authored by zhangqijia
1 parent 8f83d322

feat: 更新字段

common/components/icompontents.go
1 1 package components
2 2  
  3 +import "google.golang.org/protobuf/reflect/protoreflect"
  4 +
3 5 //-----------------
4 6 //----net start----
5 7 //-----------------
... ... @@ -38,7 +40,7 @@ type (
38 40 GetID() uint32
39 41 Start()
40 42 Stop()
41   - Send(code int32, cmd uint32, b []byte) error
  43 + Send(errCode int32, cmd uint32, b []byte) error
42 44  
43 45 SetConnectionCallback(ConnectionCallback)
44 46 SetMessageCallback(MessageCallback)
... ... @@ -120,10 +122,12 @@ type (
120 122  
121 123 Load() error
122 124 Create() error
  125 + Save() error
123 126 Update()
124 127  
125 128 SetProperty(key string, val interface{})
126 129 SetProperties(properties map[string]interface{})
  130 + ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32
127 131 }
128 132 )
129 133  
... ...
common/utils.go
... ... @@ -3,6 +3,7 @@ package common
3 3 import (
4 4 "crypto/md5"
5 5 "encoding/hex"
  6 + "google.golang.org/protobuf/reflect/protoreflect"
6 7 "math/rand"
7 8 "strings"
8 9 "time"
... ... @@ -47,3 +48,27 @@ func RandomName(name [][]string) string {
47 48 return builder.String()
48 49 }
49 50  
  51 +//英文字符串,第一个字符大写
  52 +func FirstCharToUpper(key string) string {
  53 + first := strings.ToUpper(key[0:1])
  54 + str := strings.Builder{}
  55 + str.WriteString(first)
  56 + str.WriteString(key[1:])
  57 + return str.String()
  58 +}
  59 +
  60 +
  61 +func ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 {
  62 + ids := make([]int32,0)
  63 + for k, v := range properties {
  64 + name := protoreflect.Name(strings.ToLower(k))
  65 + field := message.Descriptor().Fields().ByName(name)
  66 + if field == nil {
  67 + continue
  68 + }
  69 +
  70 + ids = append(ids, int32(field.Index()))
  71 + message.Set(field, protoreflect.ValueOf(v))
  72 + }
  73 + return ids
  74 +}
50 75 \ No newline at end of file
... ...
common/utils_test.go
... ... @@ -3,8 +3,6 @@ package common
3 3 import (
4 4 "fmt"
5 5 "math/rand"
6   - "pro2d/common/db/redisproxy"
7   - "pro2d/common/logger"
8 6 "testing"
9 7 )
10 8  
... ... @@ -14,8 +12,9 @@ func TestRandomString(t *testing.T) {
14 12 }
15 13  
16 14 func TestRandomName(t *testing.T) {
17   - if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil {
18   - logger.Error(err)
19   - return
20   - }
  15 + //if err := redisproxy.ConnectRedis(GlobalConf.GameConf.RedisConf.DB, GlobalConf.GameConf.RedisConf.Auth, GlobalConf.GameConf.RedisConf.Address); err != nil {
  16 + // logger.Error(err)
  17 + // return
  18 + //}
  19 + fmt.Println(FirstCharToUpper("name"))
21 20 }
... ...
models/role.go
1 1 package models
2 2  
3 3 import (
  4 + "github.com/golang/protobuf/proto"
4 5 "pro2d/common"
5 6 "pro2d/common/components"
6 7 "pro2d/common/db/mongoproxy"
... ... @@ -129,7 +130,32 @@ func (m *RoleModel) LoadAll() {
129 130 m.LoadTeams()
130 131 }
131 132  
132   -func (m *RoleModel) updateProperty(property map[string]interface{}) {
  133 +func (m *RoleModel) UpdateProperty(conn components.IConnection, key string, val interface{}, notify bool) {
  134 + m.UpdateProperties(conn, map[string]interface{}{key:val}, notify)
  135 +}
  136 +
  137 +func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[string]interface{}, notify bool) {
  138 + if len(property) < 1 {
  139 + return
  140 + }
  141 +
  142 + role := &pb.Role{}
  143 + ids := m.ParseFields(role.ProtoReflect(), property)
  144 + if len(ids) == 0 {
  145 + logger.Error("ParseFields err, len is 0")
  146 + return
  147 + }
  148 +
  149 + update := &pb.UpdateRolePropertyRsp{
  150 + Id: ids,
  151 + Role: role,
  152 + }
  153 + if rsp, err := proto.Marshal(update); err != nil {
  154 + logger.Error("id %s, err:", m.Role.Id, err)
  155 + return
  156 + }else {
  157 + conn.Send(0, uint32(pb.ProtoCode_UpdateRolePropertyRsp), rsp)
  158 + }
133 159 }
134 160  
135 161 func (m *RoleModel) GetAllHero() []*pb.Hero {
... ...
models/role_test.go
... ... @@ -65,4 +65,25 @@ func TestRoleModel_AddHero(t *testing.T) {
65 65 }
66 66  
67 67 role.InitRole()
  68 +}
  69 +
  70 +func TestRoleModel_ProtoReflect(t *testing.T) {
  71 + err := mongoproxy.ConnectMongo(common.GlobalConf.GameConf.MongoConf)
  72 + if err != nil {
  73 + logger.Error(err)
  74 + return
  75 + }
  76 +
  77 + role := &pb.Role{}
  78 + sch := NewSchema("", role)
  79 + mp := map[string]interface{}{
  80 + "Id": "1",
  81 + "Device": "12312312312",
  82 + }
  83 + ids := sch.ParseFields(role.ProtoReflect(), mp)
  84 + if len(ids) == 0 {
  85 + return
  86 + }
  87 +
  88 + fmt.Println(role)
68 89 }
69 90 \ No newline at end of file
... ...
models/schema.go
1 1 package models
2 2  
3 3 import (
  4 + "google.golang.org/protobuf/reflect/protoreflect"
  5 + "pro2d/common"
4 6 "pro2d/common/components"
5 7 "pro2d/common/db/mongoproxy"
6 8 "pro2d/common/logger"
... ... @@ -21,7 +23,6 @@ type SchemaMap map[string]components.ISchema
21 23 type Schema struct {
22 24 db components.IDB
23 25 reflectValue *reflect.Value
24   - reflectType reflect.Type
25 26  
26 27 cacheFields map[string]interface{}
27 28  
... ... @@ -36,7 +37,6 @@ func NewSchema(key string, schema interface{}) *Schema {
36 37 }
37 38 sch := &Schema{
38 39 reflectValue: &s,
39   - reflectType: s.Type(),
40 40 cacheFields: make(map[string]interface{}),
41 41 schema: schema,
42 42 }
... ... @@ -48,16 +48,16 @@ func NewSchema(key string, schema interface{}) *Schema {
48 48  
49 49 func (s *Schema) FindIndex() (string, []string) {
50 50 var index []string
51   - for i := 0; i < s.reflectType.NumField(); i++ {
52   - if s.reflectType.Field(i).Tag.Get("index") != "" {
53   - js := strings.Split(s.reflectType.Field(i).Tag.Get("json"), ",")
  51 + for i := 0; i < s.reflectValue.Type().NumField(); i++ {
  52 + if s.reflectValue.Type().Field(i).Tag.Get("index") != "" {
  53 + js := strings.Split(s.reflectValue.Type().Field(i).Tag.Get("json"), ",")
54 54 if len(js) == 0 {
55 55 continue
56 56 }
57 57 index = append(index, js[0])
58 58 }
59 59 }
60   - return strings.ToLower(s.reflectType.Name()), index
  60 + return strings.ToLower(s.reflectValue.Type().Name()), index
61 61 }
62 62  
63 63 func (s *Schema) Init() {
... ... @@ -87,7 +87,7 @@ func (s *Schema) GetSchema() interface{} {
87 87 }
88 88  
89 89 func (s *Schema) GetSchemaName() string {
90   - return strings.ToLower(s.reflectType.Name())
  90 + return strings.ToLower(s.reflectValue.Type().Name())
91 91 }
92 92  
93 93 func (s *Schema) Load() error {
... ... @@ -111,22 +111,40 @@ func (s *Schema) Update() {
111 111 }
112 112  
113 113 func (s *Schema) SetProperty(key string, val interface{}) {
114   - s.reflectValue.FieldByName(key).Set(reflect.ValueOf(val))
  114 + s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val))
115 115 s.cacheFields[strings.ToLower(key)] = val
116 116 }
117 117  
118 118 func (s *Schema) SetProperties(properties map[string]interface{}) {
119 119 for key, val := range properties {
120   - s.reflectValue.FieldByName(key).Set(reflect.ValueOf(val))
  120 + s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val))
121 121 s.cacheFields[strings.ToLower(key)] = val
122 122 }
123 123 }
124 124  
  125 +func (s *Schema) ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 {
  126 + ids := make([]int32, 0, len(properties))
  127 + for k, v := range properties {
  128 + name := protoreflect.Name(strings.ToLower(k))
  129 + field := message.Descriptor().Fields().ByName(name)
  130 + if field == nil {
  131 + continue
  132 + }
  133 +
  134 + ids = append(ids, int32(field.Index()))
  135 + message.Set(field, protoreflect.ValueOf(v))
  136 +
  137 + s.SetProperty(k, v)
  138 + }
  139 +
  140 + return ids
  141 +}
  142 +
125 143 func (s *Schema) getPriTag() string {
126 144 var pri string
127   - for i := 0; i < s.reflectType.NumField(); i++ {
128   - if s.reflectType.Field(i).Tag.Get("pri") == "1" {
129   - pri = strings.ToLower(s.reflectType.Field(i).Name)
  145 + for i := 0; i < s.reflectValue.Type().NumField(); i++ {
  146 + if s.reflectValue.Type().Field(i).Tag.Get("pri") == "1" {
  147 + pri = strings.ToLower(s.reflectValue.Type().Field(i).Name)
130 148 break
131 149 }
132 150 }
... ...
pb/game.pb.go
... ... @@ -288,6 +288,61 @@ func (x *RoleRsp) GetTeam() []*Team {
288 288 return nil
289 289 }
290 290  
  291 +type UpdateRolePropertyRsp struct {
  292 + state protoimpl.MessageState
  293 + sizeCache protoimpl.SizeCache
  294 + unknownFields protoimpl.UnknownFields
  295 +
  296 + Id []int32 `protobuf:"varint,1,rep,packed,name=id,proto3" json:"id,omitempty"`
  297 + Role *Role `protobuf:"bytes,2,opt,name=role,proto3" json:"role,omitempty"`
  298 +}
  299 +
  300 +func (x *UpdateRolePropertyRsp) Reset() {
  301 + *x = UpdateRolePropertyRsp{}
  302 + if protoimpl.UnsafeEnabled {
  303 + mi := &file_game_proto_msgTypes[5]
  304 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  305 + ms.StoreMessageInfo(mi)
  306 + }
  307 +}
  308 +
  309 +func (x *UpdateRolePropertyRsp) String() string {
  310 + return protoimpl.X.MessageStringOf(x)
  311 +}
  312 +
  313 +func (*UpdateRolePropertyRsp) ProtoMessage() {}
  314 +
  315 +func (x *UpdateRolePropertyRsp) ProtoReflect() protoreflect.Message {
  316 + mi := &file_game_proto_msgTypes[5]
  317 + if protoimpl.UnsafeEnabled && x != nil {
  318 + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
  319 + if ms.LoadMessageInfo() == nil {
  320 + ms.StoreMessageInfo(mi)
  321 + }
  322 + return ms
  323 + }
  324 + return mi.MessageOf(x)
  325 +}
  326 +
  327 +// Deprecated: Use UpdateRolePropertyRsp.ProtoReflect.Descriptor instead.
  328 +func (*UpdateRolePropertyRsp) Descriptor() ([]byte, []int) {
  329 + return file_game_proto_rawDescGZIP(), []int{5}
  330 +}
  331 +
  332 +func (x *UpdateRolePropertyRsp) GetId() []int32 {
  333 + if x != nil {
  334 + return x.Id
  335 + }
  336 + return nil
  337 +}
  338 +
  339 +func (x *UpdateRolePropertyRsp) GetRole() *Role {
  340 + if x != nil {
  341 + return x.Role
  342 + }
  343 + return nil
  344 +}
  345 +
291 346 var File_game_proto protoreflect.FileDescriptor
292 347  
293 348 var file_game_proto_rawDesc = []byte{
... ... @@ -311,8 +366,13 @@ var file_game_proto_rawDesc = []byte{
311 366 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x48, 0x65, 0x72, 0x6f, 0x52, 0x04,
312 367 0x68, 0x65, 0x72, 0x6f, 0x12, 0x20, 0x0a, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x18, 0x04, 0x20, 0x03,
313 368 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x54, 0x65, 0x61, 0x6d,
314   - 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b,
315   - 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  369 + 0x52, 0x04, 0x74, 0x65, 0x61, 0x6d, 0x22, 0x49, 0x0a, 0x15, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65,
  370 + 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x52, 0x73, 0x70, 0x12,
  371 + 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x03, 0x28, 0x05, 0x52, 0x02, 0x69, 0x64, 0x12,
  372 + 0x20, 0x0a, 0x04, 0x72, 0x6f, 0x6c, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e,
  373 + 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x73, 0x2e, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x04, 0x72, 0x6f, 0x6c,
  374 + 0x65, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70,
  375 + 0x72, 0x6f, 0x74, 0x6f, 0x33,
316 376 }
317 377  
318 378 var (
... ... @@ -327,26 +387,28 @@ func file_game_proto_rawDescGZIP() []byte {
327 387 return file_game_proto_rawDescData
328 388 }
329 389  
330   -var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
  390 +var file_game_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
331 391 var file_game_proto_goTypes = []interface{}{
332   - (*HeartReq)(nil), // 0: game.HeartReq
333   - (*HeartRsp)(nil), // 1: game.HeartRsp
334   - (*LoginReq)(nil), // 2: game.LoginReq
335   - (*CreateReq)(nil), // 3: game.CreateReq
336   - (*RoleRsp)(nil), // 4: game.RoleRsp
337   - (*Role)(nil), // 5: models.Role
338   - (*Hero)(nil), // 6: models.Hero
339   - (*Team)(nil), // 7: models.Team
  392 + (*HeartReq)(nil), // 0: game.HeartReq
  393 + (*HeartRsp)(nil), // 1: game.HeartRsp
  394 + (*LoginReq)(nil), // 2: game.LoginReq
  395 + (*CreateReq)(nil), // 3: game.CreateReq
  396 + (*RoleRsp)(nil), // 4: game.RoleRsp
  397 + (*UpdateRolePropertyRsp)(nil), // 5: game.UpdateRolePropertyRsp
  398 + (*Role)(nil), // 6: models.Role
  399 + (*Hero)(nil), // 7: models.Hero
  400 + (*Team)(nil), // 8: models.Team
340 401 }
341 402 var file_game_proto_depIdxs = []int32{
342   - 5, // 0: game.RoleRsp.role:type_name -> models.Role
343   - 6, // 1: game.RoleRsp.hero:type_name -> models.Hero
344   - 7, // 2: game.RoleRsp.team:type_name -> models.Team
345   - 3, // [3:3] is the sub-list for method output_type
346   - 3, // [3:3] is the sub-list for method input_type
347   - 3, // [3:3] is the sub-list for extension type_name
348   - 3, // [3:3] is the sub-list for extension extendee
349   - 0, // [0:3] is the sub-list for field type_name
  403 + 6, // 0: game.RoleRsp.role:type_name -> models.Role
  404 + 7, // 1: game.RoleRsp.hero:type_name -> models.Hero
  405 + 8, // 2: game.RoleRsp.team:type_name -> models.Team
  406 + 6, // 3: game.UpdateRolePropertyRsp.role:type_name -> models.Role
  407 + 4, // [4:4] is the sub-list for method output_type
  408 + 4, // [4:4] is the sub-list for method input_type
  409 + 4, // [4:4] is the sub-list for extension type_name
  410 + 4, // [4:4] is the sub-list for extension extendee
  411 + 0, // [0:4] is the sub-list for field type_name
350 412 }
351 413  
352 414 func init() { file_game_proto_init() }
... ... @@ -416,6 +478,18 @@ func file_game_proto_init() {
416 478 return nil
417 479 }
418 480 }
  481 + file_game_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
  482 + switch v := v.(*UpdateRolePropertyRsp); i {
  483 + case 0:
  484 + return &v.state
  485 + case 1:
  486 + return &v.sizeCache
  487 + case 2:
  488 + return &v.unknownFields
  489 + default:
  490 + return nil
  491 + }
  492 + }
419 493 }
420 494 type x struct{}
421 495 out := protoimpl.TypeBuilder{
... ... @@ -423,7 +497,7 @@ func file_game_proto_init() {
423 497 GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
424 498 RawDescriptor: file_game_proto_rawDesc,
425 499 NumEnums: 0,
426   - NumMessages: 5,
  500 + NumMessages: 6,
427 501 NumExtensions: 0,
428 502 NumServices: 0,
429 503 },
... ...
pb/protocode.pb.go
... ... @@ -23,12 +23,13 @@ const (
23 23 type ProtoCode int32
24 24  
25 25 const (
26   - ProtoCode_UNKNOWN ProtoCode = 0
27   - ProtoCode_HeartReq ProtoCode = 1
28   - ProtoCode_HeartRsp ProtoCode = 2
29   - ProtoCode_LoginReq ProtoCode = 3
30   - ProtoCode_CreateReq ProtoCode = 4
31   - ProtoCode_RoleRsp ProtoCode = 5
  26 + ProtoCode_UNKNOWN ProtoCode = 0
  27 + ProtoCode_HeartReq ProtoCode = 1
  28 + ProtoCode_HeartRsp ProtoCode = 2
  29 + ProtoCode_LoginReq ProtoCode = 3
  30 + ProtoCode_CreateReq ProtoCode = 4
  31 + ProtoCode_RoleRsp ProtoCode = 5
  32 + ProtoCode_UpdateRolePropertyRsp ProtoCode = 6
32 33 )
33 34  
34 35 // Enum value maps for ProtoCode.
... ... @@ -40,14 +41,16 @@ var (
40 41 3: "LoginReq",
41 42 4: "CreateReq",
42 43 5: "RoleRsp",
  44 + 6: "UpdateRolePropertyRsp",
43 45 }
44 46 ProtoCode_value = map[string]int32{
45   - "UNKNOWN": 0,
46   - "HeartReq": 1,
47   - "HeartRsp": 2,
48   - "LoginReq": 3,
49   - "CreateReq": 4,
50   - "RoleRsp": 5,
  47 + "UNKNOWN": 0,
  48 + "HeartReq": 1,
  49 + "HeartRsp": 2,
  50 + "LoginReq": 3,
  51 + "CreateReq": 4,
  52 + "RoleRsp": 5,
  53 + "UpdateRolePropertyRsp": 6,
51 54 }
52 55 )
53 56  
... ... @@ -82,14 +85,16 @@ var File_protocode_proto protoreflect.FileDescriptor
82 85  
83 86 var file_protocode_proto_rawDesc = []byte{
84 87 0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,
85   - 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x5e, 0x0a, 0x09,
  88 + 0x6f, 0x12, 0x09, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x79, 0x0a, 0x09,
86 89 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b,
87 90 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52,
88 91 0x65, 0x71, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x48, 0x65, 0x61, 0x72, 0x74, 0x52, 0x73, 0x70,
89 92 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x10, 0x03,
90 93 0x12, 0x0d, 0x0a, 0x09, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x10, 0x04, 0x12,
91   - 0x0b, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x10, 0x05, 0x42, 0x0a, 0x5a, 0x08,
92   - 0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
  94 + 0x0b, 0x0a, 0x07, 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x73, 0x70, 0x10, 0x05, 0x12, 0x19, 0x0a, 0x15,
  95 + 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72,
  96 + 0x74, 0x79, 0x52, 0x73, 0x70, 0x10, 0x06, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x70, 0x62,
  97 + 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
93 98 }
94 99  
95 100 var (
... ...