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 | 59 | update := make(map[string]interface{}, 3) | 
| 60 | 60 | c, ok := params["clotheslimit"] | 
| 61 | 61 | if ok { | 
| 62 | - update["clotheslimit"], _ = strconv.Atoi(c) | |
| 62 | + l, _ := strconv.Atoi(c) | |
| 63 | + update["clotheslimit"] = uint32(l) | |
| 63 | 64 | } | 
| 64 | 65 | w := params["weaponslimit"] | 
| 65 | 66 | if ok { | 
| 66 | - update["weaponslimit"], _ = strconv.Atoi(w) | |
| 67 | + l, _ := strconv.Atoi(w) | |
| 68 | + update["weaponslimit"] = uint32(l) | |
| 67 | 69 | } | 
| 68 | 70 | o := params["otherlimit"] | 
| 69 | 71 | if ok { | 
| 70 | - update["otherlimit"], _ = strconv.Atoi(o) | |
| 72 | + l, _ := strconv.Atoi(o) | |
| 73 | + update["otherlimit"] = uint32(l) | |
| 71 | 74 | } | 
| 72 | 75 | role.UpdateProperties(update, true) | 
| 73 | 76 | } | ... | ... | 
cmd/gameserver/service/gm.go
| ... | ... | @@ -3,7 +3,7 @@ package service | 
| 3 | 3 | import ( | 
| 4 | 4 | "github.com/gin-gonic/gin" | 
| 5 | 5 | "net/http" | 
| 6 | - "pro2d/cmd/gameserver/action" | |
| 6 | + "pro2d/cmd/gameserver/gmaction" | |
| 7 | 7 | "pro2d/common/components" | 
| 8 | 8 | "pro2d/common/logger" | 
| 9 | 9 | "pro2d/models" | 
| ... | ... | @@ -79,7 +79,7 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { | 
| 79 | 79 | |
| 80 | 80 | func (s *GmServer) Start() error { | 
| 81 | 81 | s.SetHandlerFuncCallback(s.HandlerFuncObj) | 
| 82 | - s.BindHandler(&action.GmAction{}) | |
| 82 | + s.BindHandler(&gmaction.GmAction{}) | |
| 83 | 83 | //gin.SetMode(gin.ReleaseMode) | 
| 84 | 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 | 196 | } | 
| 197 | 197 | |
| 198 | 198 | role := &pb.Role{} | 
| 199 | + | |
| 199 | 200 | ids := m.ParseFields(role.ProtoReflect(), property) | 
| 200 | 201 | if len(ids) == 0 { | 
| 201 | 202 | logger.Error("ParseFields err, len is 0") | ... | ... | 
models/schema.go
| 1 | 1 | package models | 
| 2 | 2 | |
| 3 | 3 | import ( | 
| 4 | + "github.com/golang/protobuf/proto" | |
| 4 | 5 | "google.golang.org/protobuf/reflect/protoreflect" | 
| 5 | 6 | "pro2d/common/components" | 
| 6 | 7 | "pro2d/common/db/mongoproxy" | 
| ... | ... | @@ -24,6 +25,7 @@ type Schema struct { | 
| 24 | 25 | db components.IDB | 
| 25 | 26 | reflectValue *reflect.Value | 
| 26 | 27 | reflectIndex map[string]int | 
| 28 | + protoIndex map[string]int | |
| 27 | 29 | |
| 28 | 30 | cacheFields map[string]interface{} | 
| 29 | 31 | |
| ... | ... | @@ -41,14 +43,20 @@ func NewSchema(key string, schema interface{}) *Schema { | 
| 41 | 43 | cacheFields: make(map[string]interface{}), | 
| 42 | 44 | schema: schema, | 
| 43 | 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 | 54 | for i := 0; i < s.Type().NumField(); i++ { | 
| 47 | 55 | name := s.Type().Field(i).Name | 
| 48 | 56 | if strings.Compare(name[0:1], strings.ToLower(name[0:1])) == 0 { | 
| 49 | 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 | 62 | sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) | 
| ... | ... | @@ -195,13 +203,21 @@ func (s *Schema) IncrProperty(key string, val int64) int64 { | 
| 195 | 203 | |
| 196 | 204 | func (s *Schema) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 { | 
| 197 | 205 | ids := make([]int32, 0, len(properties)) | 
| 206 | + | |
| 198 | 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 | 213 | if field == nil { | 
| 201 | 214 | continue | 
| 202 | 215 | } | 
| 203 | 216 | |
| 204 | 217 | ids = append(ids, int32(field.Index())) | 
| 218 | + | |
| 219 | + field.Kind() | |
| 220 | + | |
| 205 | 221 | message.Set(field, protoreflect.ValueOf(v)) | 
| 206 | 222 | |
| 207 | 223 | s.SetProperty(k, v) | ... | ... |