Commit d35f4f817840a3b22981b969d144d9c146ea7a45
1 parent
97bec184
fix: 优化proto to index,fix updatepacklimit
Showing
4 changed files
with
27 additions
and
7 deletions
Show diff stats
cmd/gameserver/gmaction/GmAction.go
@@ -59,15 +59,18 @@ func (gm *GmAction) UpdatePackLimit(role *models.RoleModel, params GMParams) { | @@ -59,15 +59,18 @@ func (gm *GmAction) UpdatePackLimit(role *models.RoleModel, params GMParams) { | ||
59 | update := make(map[string]interface{}, 3) | 59 | update := make(map[string]interface{}, 3) |
60 | c, ok := params["clotheslimit"] | 60 | c, ok := params["clotheslimit"] |
61 | if ok { | 61 | if ok { |
62 | - update["clotheslimit"], _ = strconv.Atoi(c) | 62 | + l, _ := strconv.Atoi(c) |
63 | + update["clotheslimit"] = uint32(l) | ||
63 | } | 64 | } |
64 | w := params["weaponslimit"] | 65 | w := params["weaponslimit"] |
65 | if ok { | 66 | if ok { |
66 | - update["weaponslimit"], _ = strconv.Atoi(w) | 67 | + l, _ := strconv.Atoi(w) |
68 | + update["weaponslimit"] = uint32(l) | ||
67 | } | 69 | } |
68 | o := params["otherlimit"] | 70 | o := params["otherlimit"] |
69 | if ok { | 71 | if ok { |
70 | - update["otherlimit"], _ = strconv.Atoi(o) | 72 | + l, _ := strconv.Atoi(o) |
73 | + update["otherlimit"] = uint32(l) | ||
71 | } | 74 | } |
72 | role.UpdateProperties(update, true) | 75 | role.UpdateProperties(update, true) |
73 | } | 76 | } |
cmd/gameserver/service/gm.go
@@ -3,7 +3,7 @@ package service | @@ -3,7 +3,7 @@ package service | ||
3 | import ( | 3 | import ( |
4 | "github.com/gin-gonic/gin" | 4 | "github.com/gin-gonic/gin" |
5 | "net/http" | 5 | "net/http" |
6 | - "pro2d/cmd/gameserver/action" | 6 | + "pro2d/cmd/gameserver/gmaction" |
7 | "pro2d/common/components" | 7 | "pro2d/common/components" |
8 | "pro2d/common/logger" | 8 | "pro2d/common/logger" |
9 | "pro2d/models" | 9 | "pro2d/models" |
@@ -79,7 +79,7 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { | @@ -79,7 +79,7 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { | ||
79 | 79 | ||
80 | func (s *GmServer) Start() error { | 80 | func (s *GmServer) Start() error { |
81 | s.SetHandlerFuncCallback(s.HandlerFuncObj) | 81 | s.SetHandlerFuncCallback(s.HandlerFuncObj) |
82 | - s.BindHandler(&action.GmAction{}) | 82 | + s.BindHandler(&gmaction.GmAction{}) |
83 | //gin.SetMode(gin.ReleaseMode) | 83 | //gin.SetMode(gin.ReleaseMode) |
84 | return s.IHttp.Start() | 84 | return s.IHttp.Start() |
85 | } | 85 | } |
models/role.go
@@ -196,6 +196,7 @@ func (m *RoleModel) UpdateProperties(property map[string]interface{}, notify boo | @@ -196,6 +196,7 @@ func (m *RoleModel) UpdateProperties(property map[string]interface{}, notify boo | ||
196 | } | 196 | } |
197 | 197 | ||
198 | role := &pb.Role{} | 198 | role := &pb.Role{} |
199 | + | ||
199 | ids := m.ParseFields(role.ProtoReflect(), property) | 200 | ids := m.ParseFields(role.ProtoReflect(), property) |
200 | if len(ids) == 0 { | 201 | if len(ids) == 0 { |
201 | logger.Error("ParseFields err, len is 0") | 202 | logger.Error("ParseFields err, len is 0") |
models/schema.go
1 | package models | 1 | package models |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "github.com/golang/protobuf/proto" | ||
4 | "google.golang.org/protobuf/reflect/protoreflect" | 5 | "google.golang.org/protobuf/reflect/protoreflect" |
5 | "pro2d/common/components" | 6 | "pro2d/common/components" |
6 | "pro2d/common/db/mongoproxy" | 7 | "pro2d/common/db/mongoproxy" |
@@ -24,6 +25,7 @@ type Schema struct { | @@ -24,6 +25,7 @@ type Schema struct { | ||
24 | db components.IDB | 25 | db components.IDB |
25 | reflectValue *reflect.Value | 26 | reflectValue *reflect.Value |
26 | reflectIndex map[string]int | 27 | reflectIndex map[string]int |
28 | + protoIndex map[string]int | ||
27 | 29 | ||
28 | cacheFields map[string]interface{} | 30 | cacheFields map[string]interface{} |
29 | 31 | ||
@@ -41,14 +43,20 @@ func NewSchema(key string, schema interface{}) *Schema { | @@ -41,14 +43,20 @@ func NewSchema(key string, schema interface{}) *Schema { | ||
41 | cacheFields: make(map[string]interface{}), | 43 | cacheFields: make(map[string]interface{}), |
42 | schema: schema, | 44 | schema: schema, |
43 | reflectIndex: make(map[string]int), | 45 | reflectIndex: make(map[string]int), |
46 | + protoIndex: make(map[string]int), | ||
44 | } | 47 | } |
45 | 48 | ||
49 | + proto.MessageReflect(schema.(proto.Message)).Range(func(des protoreflect.FieldDescriptor, value protoreflect.Value) bool { | ||
50 | + sch.protoIndex[strings.ToLower(des.JSONName())] = des.Index() | ||
51 | + return true | ||
52 | + }) | ||
53 | + | ||
46 | for i := 0; i < s.Type().NumField(); i++ { | 54 | for i := 0; i < s.Type().NumField(); i++ { |
47 | name := s.Type().Field(i).Name | 55 | name := s.Type().Field(i).Name |
48 | if strings.Compare(name[0:1], strings.ToLower(name[0:1])) == 0 { | 56 | if strings.Compare(name[0:1], strings.ToLower(name[0:1])) == 0 { |
49 | continue | 57 | continue |
50 | } | 58 | } |
51 | - sch.reflectIndex[strings.ToLower(strings.ToLower(name))] = i | 59 | + sch.reflectIndex[strings.ToLower(name)] = i |
52 | } | 60 | } |
53 | 61 | ||
54 | sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) | 62 | sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) |
@@ -195,13 +203,21 @@ func (s *Schema) IncrProperty(key string, val int64) int64 { | @@ -195,13 +203,21 @@ func (s *Schema) IncrProperty(key string, val int64) int64 { | ||
195 | 203 | ||
196 | func (s *Schema) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 { | 204 | func (s *Schema) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 { |
197 | ids := make([]int32, 0, len(properties)) | 205 | ids := make([]int32, 0, len(properties)) |
206 | + | ||
198 | for k, v := range properties { | 207 | for k, v := range properties { |
199 | - field := message.Descriptor().Fields().ByName(protoreflect.Name(strings.ToLower(k))) | 208 | + idx, ok := s.protoIndex[strings.ToLower(k)] |
209 | + if !ok { | ||
210 | + continue | ||
211 | + } | ||
212 | + field := message.Descriptor().Fields().Get(idx) | ||
200 | if field == nil { | 213 | if field == nil { |
201 | continue | 214 | continue |
202 | } | 215 | } |
203 | 216 | ||
204 | ids = append(ids, int32(field.Index())) | 217 | ids = append(ids, int32(field.Index())) |
218 | + | ||
219 | + field.Kind() | ||
220 | + | ||
205 | message.Set(field, protoreflect.ValueOf(v)) | 221 | message.Set(field, protoreflect.ValueOf(v)) |
206 | 222 | ||
207 | s.SetProperty(k, v) | 223 | s.SetProperty(k, v) |