diff --git a/cmd/gameserver/action/GmAction.go b/cmd/gameserver/action/GmAction.go index 19104df..6b4fbf4 100644 --- a/cmd/gameserver/action/GmAction.go +++ b/cmd/gameserver/action/GmAction.go @@ -1,7 +1,6 @@ package action import ( - "pro2d/common" "pro2d/common/components" "pro2d/common/logger" "pro2d/models" @@ -11,23 +10,9 @@ import ( type GmAction struct { } -func (gm *GmAction) AddExp(properties map[string]interface{}) int { - r := properties[common.Role_] - c := properties[common.Conn_] - - role := r.(*models.RoleModel) - var conn components.IConnection - if c == nil { - conn = nil - } else { - conn = c.(components.IConnection) - } - +func (gm *GmAction) AddExp(conn components.IConnection, role *models.RoleModel, properties map[string]interface{}) int { logger.Debug(properties) exp, _ := strconv.Atoi(properties["exp"].(string)) - logger.Debug("update before id: %s, exp: %d", role.Role.Id, role.Role.Exp) - role.Role.Exp += int64(exp) - role.UpdateProperty(conn, "exp", role.Role.Exp, true) - logger.Debug("update after id: %s, exp: %d", role.Role.Id, role.Role.Exp) + role.IncrPropertyChan(conn, "exp", int64(exp)) return 0 } diff --git a/cmd/gameserver/service/gm.go b/cmd/gameserver/service/gm.go index af8ce9f..00c671a 100644 --- a/cmd/gameserver/service/gm.go +++ b/cmd/gameserver/service/gm.go @@ -65,13 +65,9 @@ func (s *GmServer) HandlerFuncObj(tvl, obj reflect.Value) gin.HandlerFunc { role.LoadAll() } - properties["_conn"] = conn - properties["_role"] = role - //func start - v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(properties)}) - - role.SaveRoleData(0) + v := tvl.Call([]reflect.Value{obj, reflect.ValueOf(conn), reflect.ValueOf(role), reflect.ValueOf(properties)}) + role.SaveRoleDataChan(conn, 0) if len(v) != 1 { c.JSON(http.StatusNotFound, gin.H{"code": -100, "message": "request param len is error"}) diff --git a/common/components/conn.go b/common/components/conn.go index 973e49f..b80d140 100644 --- a/common/components/conn.go +++ b/common/components/conn.go @@ -17,12 +17,13 @@ type Connection struct { splitter ISplitter Id uint32 - scanner *bufio.Scanner - writer *bufio.Writer - WBuffer chan []byte - Quit chan *Connection - readFunc chan func() - timerFunc chan func() + scanner *bufio.Scanner + writer *bufio.Writer + WBuffer chan []byte + Quit chan *Connection + readFunc chan func() + timerFunc chan func() + customizeFunc chan func() messageCallback MessageCallback connectionCallback ConnectionCallback @@ -66,6 +67,9 @@ func (c *Connection) reset() { if c.timerFunc == nil { c.timerFunc = make(chan func(), 10) } + if c.customizeFunc == nil { + c.customizeFunc = make(chan func(), 10) + } //c.connectionCallback = c.defaultConnectionCallback //c.messageCallback = c.defaultMessageCallback @@ -136,6 +140,10 @@ func (c *Connection) Send(errCode int32, cmd uint32, data []byte) error { } } +func (c *Connection) CustomChan() chan<- func() { + return c.customizeFunc +} + func (c *Connection) defaultConnectionCallback(conn IConnection) { } @@ -206,6 +214,8 @@ func (c *Connection) listen() { timerFunc() case readFunc := <-c.readFunc: readFunc() + case customizeFunc := <-c.customizeFunc: + customizeFunc() case <-c.Quit: return } diff --git a/common/components/icompontents.go b/common/components/icompontents.go index d929717..afb1304 100644 --- a/common/components/icompontents.go +++ b/common/components/icompontents.go @@ -51,6 +51,7 @@ type ( Start() Stop() Send(errCode int32, cmd uint32, b []byte) error + CustomChan() chan<- func() SetConnectionCallback(ConnectionCallback) SetMessageCallback(MessageCallback) @@ -149,6 +150,7 @@ type ( SetProperty(key string, val interface{}) SetProperties(properties map[string]interface{}) + IncrProperty(key string, val int64) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 } ) diff --git a/models/role.go b/models/role.go index 75e9319..555ae09 100644 --- a/models/role.go +++ b/models/role.go @@ -23,7 +23,7 @@ type RoleModel struct { func RoleExistByUid(uid string) *RoleModel { data := &pb.Role{Uid: uid} - if err := mongoproxy.FindOne(mongoproxy.GetBsonM("uid", uid), data); err != nil { + if err := mongoproxy.FindOne(mongoproxy.GetCollName(data), mongoproxy.GetBsonM("uid", uid), data); err != nil { logger.Error("Role not exist err: %v", err) return nil } @@ -231,3 +231,35 @@ func (m *RoleModel) SaveRoleData(now int64) { } } } + +func (m *RoleModel) IncrPropertyChan(conn components.IConnection, key string, val int64) { + if conn != nil { + conn.CustomChan() <- func() { + m.IncrProperty(key, val) + } + } else { + m.IncrProperty(key, val) + } + +} + +func (m *RoleModel) UpdatePropertyChan(conn components.IConnection, key string, val interface{}, notify bool) { + if conn != nil { + conn.CustomChan() <- func() { + m.UpdateProperties(conn, map[string]interface{}{key: val}, notify) + } + } else { + m.UpdateProperties(conn, map[string]interface{}{key: val}, notify) + } +} + +func (m *RoleModel) SaveRoleDataChan(conn components.IConnection, now int64) { + if conn != nil { + conn.CustomChan() <- func() { + m.SaveRoleData(now) + } + } else { + m.SaveRoleData(now) + } + +} diff --git a/models/schema.go b/models/schema.go index f3ae9fb..1600667 100644 --- a/models/schema.go +++ b/models/schema.go @@ -153,6 +153,22 @@ func (s *Schema) SetProperties(properties map[string]interface{}) { } } +func (s *Schema) IncrProperty(key string, val int64) { + idx, ok := s.reflectIndex[strings.ToLower(key)] + if !ok { + return + } + field := s.reflectValue.Field(idx) + var v int64 + switch field.Kind() { + case reflect.Int64: + v = field.Int() + val + case reflect.Int: + v = field.Int() + val + } + s.SetProperty(key, v) +} + func (s *Schema) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 { ids := make([]int32, 0, len(properties)) for k, v := range properties { -- libgit2 0.21.2